Skip to content

feat(ordering): add support for insertInto option #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,23 @@ const miniCssExtractPlugin = new MiniCssExtractPlugin({

For long term caching use `filename: "[contenthash].css"`. Optionally add `[name]`.

#### Determining Insertion Point for Async CSS Chunks

If you are extracting CSS into separate files and using Webpack Dynamic Imports, it is possible you will need to be able to configure the insertion point of your async CSS chunks in the document to preserve ordering for the CSS cascade. This is possible using the `insertInto` option, which takes a function that will be called to determine the parent node into which to insert the `<link>`` tag for the chunk CSs file

```javascript
module.exports = {
...
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css",
insertInto: href => document.querySeletor('.async-styles-container'),
})
],
...
}
```

### Remove Order Warnings

If the terminal is getting bloated with chunk order warnings. You can filter by configuring [warningsFilter](https://webpack.js.org/configuration/stats/) withing the webpack stats option
Expand Down
15 changes: 11 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,13 @@ class MiniCssExtractPlugin {
{
filename: DEFAULT_FILENAME,
moduleFilename: () => options.filename || DEFAULT_FILENAME,
insertInto: null,
},
options
);

if (typeof this.options.insertInto !== 'function') {
throw new Error('insertInto option must be a function');
}
if (!this.options.chunkFilename) {
const { filename } = this.options;

Expand Down Expand Up @@ -362,7 +365,9 @@ class MiniCssExtractPlugin {
contentHashType: MODULE_TYPE,
}
);

const insertInto = this.options.insertInto
? this.options.insertInto.toString()
: 'null';
return Template.asString([
source,
'',
Expand Down Expand Up @@ -418,8 +423,10 @@ class MiniCssExtractPlugin {
'}',
])
: '',
'var head = document.getElementsByTagName("head")[0];',
'head.appendChild(linkTag);',
`var insertInto = ${insertInto};`,
'var parent = insertInto ? insertInto(href) : null;',
'if (parent) { parent.appendChild(linkTag); }',
'else { document.getElementsByTagName("head")[0].appendChild(linkTag); }',
]),
'}).then(function() {',
Template.indent(['installedCssChunks[chunkId] = 0;']),
Expand Down