Skip to content

Commit 67120f8

Browse files
iancwmichael-ciniawsky
authored andcommittedSep 8, 2017
feat: support 'before' insertions (options.insertAt) (#253)
1 parent a2ae3ac commit 67120f8

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed
 

‎README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Styles are not added on `import/require()`, but instead on call to `use`/`ref`.
137137
|**`base`** |`{Number}`|`true`|Set module ID base (DLLPlugin)|
138138
|**`attrs`**|`{Object}`|`{}`|Add custom attrs to `<style></style>`|
139139
|**`transform`** |`{Function}`|`false`|Transform/Conditionally load CSS by passing a transform/condition function|
140-
|**`insertAt`**|`{String}`|`bottom`|Inserts `<style></style>` at the given position|
140+
|**`insertAt`**|`{String\|Object}`|`bottom`|Inserts `<style></style>` at the given position|
141141
|**`insertInto`**|`{String}`|`<head>`|Inserts `<style></style>` into the given position|
142142
|**`sourceMap`**|`{Boolean}`|`false`|Enable/Disable Sourcemaps|
143143
|**`convertToAbsoluteUrls`**|`{Boolean}`|`false`|Converts relative URLs to absolute urls, when source maps are enabled|
@@ -285,6 +285,20 @@ By default, the style-loader appends `<style>` elements to the end of the style
285285
}
286286
```
287287

288+
A new `<style>` element can be inserted before a specific element by passing an object, e.g.
289+
290+
**webpack.config.js**
291+
```js
292+
{
293+
loader: 'style-loader'
294+
options: {
295+
insertAt: {
296+
before: '#id'
297+
}
298+
}
299+
}
300+
```
301+
288302
### `insertInto`
289303
By default, the style-loader inserts the `<style>` elements into the `<head>` tag of the page. If you want the tags to be inserted somewhere else you can specify a CSS selector for that element here. If you target an [IFrame](https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement) make sure you have sufficient access rights, the styles will be injected into the content document head.
290304
You can also insert the styles into a [ShadowRoot](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot), e.g

‎lib/addStyles.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,11 @@ function insertStyleElement (options, style) {
170170
stylesInsertedAtTop.push(style);
171171
} else if (options.insertAt === "bottom") {
172172
target.appendChild(style);
173+
} else if (typeof options.insertAt === "object" && options.insertAt.before) {
174+
var nextSibling = getElement(options.insertInto + " " + options.insertAt.before);
175+
target.insertBefore(style, nextSibling);
173176
} else {
174-
throw new Error("Invalid value for parameter 'insertAt'. Must be 'top' or 'bottom'.");
177+
throw new Error("[Style Loader]\n\n Invalid value for parameter 'insertAt' ('options.insertAt') found.\n Must be 'top', 'bottom', or Object.\n (https://github.com/webpack-contrib/style-loader#insertat)\n");
175178
}
176179
}
177180

‎options.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"type": "object"
1010
},
1111
"insertAt": {
12-
"type": "string"
12+
"type": ["string", "object"]
1313
},
1414
"insertInto": {
1515
"type": "string"

‎test/basicTest.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe("basic tests", function() {
1919
}
2020
`,
2121
requiredStyle = `<style type="text/css">${requiredCss}</style>`,
22-
existingStyle = "<style>.existing { color: yellow }</style>",
22+
existingStyle = `<style id="existing-style">.existing { color: yellow }</style>`,
2323
checkValue = '<div class="check">check</div>',
2424
rootDir = path.resolve(__dirname + "/../") + "/",
2525
jsdomHtml = [
@@ -95,6 +95,26 @@ describe("basic tests", function() {
9595
runCompilerTest(expected, done);
9696
}); // it insert at top
9797

98+
it("insert at before", function(done) {
99+
styleLoaderOptions.insertAt = {
100+
before: "#existing-style"
101+
};
102+
103+
let expected = [requiredStyle, existingStyle].join("");
104+
105+
runCompilerTest(expected, done);
106+
}); // it insert at before
107+
108+
it("insert at before invalid selector", function(done) {
109+
styleLoaderOptions.insertAt = {
110+
before: "#missing"
111+
};
112+
113+
let expected = [existingStyle, requiredStyle].join("\n");
114+
115+
runCompilerTest(expected, done);
116+
}); // it insert at before
117+
98118
it("insert into", function(done) {
99119
let selector = "div.target";
100120
styleLoaderOptions.insertInto = selector;

0 commit comments

Comments
 (0)
Please sign in to comment.