Skip to content

Commit a5f17c4

Browse files
feat: added the insert option
1 parent 3d017a2 commit a5f17c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2646
-1168
lines changed

README.md

+57-5
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ module.exports = {
7575

7676
### Plugin Options
7777

78-
| Name | Type | Default | Description |
79-
| :-----------------------------------: | :------------------: | :-----------------: | :------------------------------------------------------- |
80-
| **[`filename`](#filename)** | `{String\|Function}` | `[name].css` | This option determines the name of each output CSS file |
81-
| **[`chunkFilename`](#chunkFilename)** | `{String\|Function}` | `based on filename` | This option determines the name of non-entry chunk files |
82-
| **[`ignoreOrder`](#ignoreOrder)** | `{Boolean}` | `false` | Remove Order Warnings |
78+
| Name | Type | Default | Description |
79+
| :-----------------------------------: | :------------------: | :------------------------------------------------------------------------------: | :------------------------------------------------------- |
80+
| **[`filename`](#filename)** | `{String\|Function}` | `[name].css` | This option determines the name of each output CSS file |
81+
| **[`chunkFilename`](#chunkFilename)** | `{String\|Function}` | `based on filename` | This option determines the name of non-entry chunk files |
82+
| **[`ignoreOrder`](#ignoreOrder)** | `{Boolean}` | `false` | Remove Order Warnings |
83+
| **[`insert`](#insert)** | `{String\|Function}` | `var head = document.getElementsByTagName("head")[0];head.appendChild(linkTag);` | Inserts `<link>` at the given position |
8384

8485
#### `filename`
8586

@@ -109,6 +110,57 @@ Default: `false`
109110
Remove Order Warnings.
110111
See [examples](#remove-order-warnings) below for details.
111112

113+
#### `insert`
114+
115+
Type: `String|Function`
116+
Default: `var head = document.getElementsByTagName("head")[0];head.appendChild(linkTag);`
117+
118+
By default, the `extract-css-chunks-plugin` appends styles (`<link>` elements) to `document.head` of the current `window`.
119+
120+
However in some circumstances it might be necessary to have finer control over the append target or even delay `link` elements instertion.
121+
For example this is the case when you asynchronously load styles for an application that runs inside of an iframe.
122+
In such cases `insert` can be configured to be a function or a custom selector.
123+
124+
If you target an [iframe](https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement) make sure that the parent document has sufficient access rights to reach into the frame document and append elements to it.
125+
126+
##### `String`
127+
128+
Allows to setup custom [query selector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector).
129+
A new `<link>` element will be inserted after the found item.
130+
131+
**webpack.config.js**
132+
133+
```js
134+
new MiniCssExtractPlugin({
135+
insert: '#some-element',
136+
});
137+
```
138+
139+
A new `<link>` element will be inserted after the element with id `some-element`.
140+
141+
##### `Function`
142+
143+
Allows to override default behavior and insert styles at any position.
144+
145+
> ⚠ Do not forget that this code will run in the browser alongside your application. Since not all browsers support latest ECMA features like `let`, `const`, `arrow function expression` and etc we recommend you to use only ECMA 5 features and syntax.
146+
147+
> > ⚠ The `insert` function is serialized to string and passed to the plugin. This means that it won't have access to the scope of the webpack configuration module.
148+
149+
**webpack.config.js**
150+
151+
```js
152+
new MiniCssExtractPlugin({
153+
insert: function (linkTag) {
154+
const reference = document.querySelector('#some-element');
155+
if (reference) {
156+
reference.parentNode.insertBefore(linkTag, reference);
157+
}
158+
},
159+
});
160+
```
161+
162+
A new `<link>` element will be inserted before the element with id `some-element`.
163+
112164
### Loader Options
113165

114166
| Name | Type | Default | Description |

0 commit comments

Comments
 (0)