From e0ec27d3f548e96c58cb63378eefb471f613fbf6 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Fri, 29 Mar 2019 17:51:36 -0400 Subject: [PATCH 1/3] feat(ordering): add support for insertInto option This adds an new `insertInto` option that is optional and backards-compatible. Specifying a CSS selector allows the user to specify a DOM node into which the async-loaded tags should be inserted. Closes #370, related to #328,#331 --- src/index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 0451484d..517afebd 100644 --- a/src/index.js +++ b/src/index.js @@ -125,6 +125,7 @@ class MiniCssExtractPlugin { { filename: DEFAULT_FILENAME, moduleFilename: () => options.filename || DEFAULT_FILENAME, + insertInto: null, }, options ); @@ -418,8 +419,10 @@ class MiniCssExtractPlugin { '}', ]) : '', - 'var head = document.getElementsByTagName("head")[0];', - 'head.appendChild(linkTag);', + `var selector = "${this.options.insertInto}";`, + 'var parent = selector && document.querySelector && document.querySelector(selector);', + 'if (parent) { parent.appendChild(linkTag); }', + 'else { document.getElementsByTagName("head")[0].appendChild(linkTag); }', ]), '}).then(function() {', Template.indent(['installedCssChunks[chunkId] = 0;']), From 56dabe73babf3ec91d6ebc29f42abf096b003b45 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Thu, 27 Jun 2019 10:12:34 -0400 Subject: [PATCH 2/3] feat: convert insertInto option to be a function --- src/index.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 517afebd..547986af 100644 --- a/src/index.js +++ b/src/index.js @@ -129,7 +129,9 @@ class MiniCssExtractPlugin { }, options ); - + if (typeof this.options.insertInto !== 'function') { + throw new Error('insertInto option must be a function'); + } if (!this.options.chunkFilename) { const { filename } = this.options; @@ -363,7 +365,9 @@ class MiniCssExtractPlugin { contentHashType: MODULE_TYPE, } ); - + const insertInto = this.options.insertInto + ? this.options.insertInto.toString() + : 'null'; return Template.asString([ source, '', @@ -419,8 +423,8 @@ class MiniCssExtractPlugin { '}', ]) : '', - `var selector = "${this.options.insertInto}";`, - 'var parent = selector && document.querySelector && document.querySelector(selector);', + `var insertInto = ${insertInto};`, + 'var parent = insertInto ? insertInto(href) : null;', 'if (parent) { parent.appendChild(linkTag); }', 'else { document.getElementsByTagName("head")[0].appendChild(linkTag); }', ]), From 9a3328c3eaf17e08bb72ac907d3ce55c32865846 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Thu, 27 Jun 2019 10:12:59 -0400 Subject: [PATCH 3/3] docs: add docs for insertInto option --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 74d1d293..060b0309 100644 --- a/README.md +++ b/README.md @@ -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 ``` 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