Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve order of link tags on HMR #982

Merged
merged 6 commits into from Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/index.js
Expand Up @@ -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
Expand Down Expand Up @@ -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;",
]
)};`,
Expand Down Expand Up @@ -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);",
]
)});`
)}`,
Expand Down Expand Up @@ -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";',
Expand Down
4 changes: 4 additions & 0 deletions test/manual/index.html
Expand Up @@ -49,6 +49,10 @@
<button class="lazy-button2">pressing this button</button>. Additional
clicks have no effect.
</p>
<p>
(HMR) Now modify the color in lazy.css and save it: This should remain
orange.
</p>
<p>
Refresh and press buttons in reverse order: This should turn green
instead.
Expand Down
8 changes: 7 additions & 1 deletion test/manual/webpack.config.js
Expand Up @@ -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": "*",
},
Expand Down