diff --git a/src/index.js b/src/index.js index 154f5fea..069ac721 100644 --- a/src/index.js +++ b/src/index.js @@ -826,7 +826,7 @@ class MiniCssExtractPlugin { return Template.asString([ `var createStylesheet = ${runtimeTemplate.basicFunction( - "chunkId, fullhref, resolve, reject", + "chunkId, fullhref, oldTag, resolve, reject", [ 'var linkTag = document.createElement("link");', this.runtimeOptions.attributes @@ -886,7 +886,13 @@ class MiniCssExtractPlugin { `var target = document.querySelector("${this.runtimeOptions.insert}");`, `target.parentNode.insertBefore(linkTag, target.nextSibling);`, ]) - : Template.asString(["document.head.appendChild(linkTag);"]), + : Template.asString([ + "if (oldTag) {", + Template.indent(["oldTag.after(linkTag);"]), + "} else {", + Template.indent(["document.head.appendChild(linkTag);"]), + "}", + ]), "return linkTag;", ] )};`, @@ -919,7 +925,7 @@ class MiniCssExtractPlugin { `var href = ${RuntimeGlobals.require}.miniCssF(chunkId);`, `var fullhref = ${RuntimeGlobals.publicPath} + href;`, "if(findStylesheet(href, fullhref)) return resolve();", - "createStylesheet(chunkId, fullhref, resolve, reject);", + "createStylesheet(chunkId, fullhref, null, resolve, reject);", ] )});` )}`, @@ -995,7 +1001,7 @@ class MiniCssExtractPlugin { `promises.push(new Promise(${runtimeTemplate.basicFunction( "resolve, reject", [ - `var tag = createStylesheet(chunkId, fullhref, ${runtimeTemplate.basicFunction( + `var tag = createStylesheet(chunkId, fullhref, oldTag, ${runtimeTemplate.basicFunction( "", [ 'tag.as = "style";', diff --git a/test/manual/index.html b/test/manual/index.html index 0a438be4..610b7268 100644 --- a/test/manual/index.html +++ b/test/manual/index.html @@ -49,6 +49,10 @@ . Additional clicks have no effect.

+

+ (HMR) Now modify the color in lazy.css and save it: This should remain + orange. +

Refresh and press buttons in reverse order: This should turn green instead. diff --git a/test/manual/webpack.config.js b/test/manual/webpack.config.js index 4587f51f..3892d04a 100644 --- a/test/manual/webpack.config.js +++ b/test/manual/webpack.config.js @@ -69,7 +69,13 @@ module.exports = { ], devServer: { hot: ENABLE_HMR, - static: __dirname, + static: { + directory: __dirname, + watch: { + // prevent page reload on source change so that we can test HMR + ignored: /src/, + }, + }, headers: { "Access-Control-Allow-Origin": "*", },