Skip to content

Commit 9f26ec5

Browse files
committed
fix: preserve order of link tags on HMR
1 parent 331031a commit 9f26ec5

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

Diff for: src/index.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ class MiniCssExtractPlugin {
826826

827827
return Template.asString([
828828
`var createStylesheet = ${runtimeTemplate.basicFunction(
829-
"chunkId, fullhref, resolve, reject",
829+
"chunkId, fullhref, oldTag, resolve, reject",
830830
[
831831
'var linkTag = document.createElement("link");',
832832
this.runtimeOptions.attributes
@@ -886,7 +886,13 @@ class MiniCssExtractPlugin {
886886
`var target = document.querySelector("${this.runtimeOptions.insert}");`,
887887
`target.parentNode.insertBefore(linkTag, target.nextSibling);`,
888888
])
889-
: Template.asString(["document.head.appendChild(linkTag);"]),
889+
: Template.asString([
890+
"if (oldTag) {",
891+
Template.indent(["oldTag.after(linkTag);"]),
892+
"} else {",
893+
Template.indent(["document.head.appendChild(linkTag);"]),
894+
"}",
895+
]),
890896
"return linkTag;",
891897
]
892898
)};`,
@@ -919,7 +925,7 @@ class MiniCssExtractPlugin {
919925
`var href = ${RuntimeGlobals.require}.miniCssF(chunkId);`,
920926
`var fullhref = ${RuntimeGlobals.publicPath} + href;`,
921927
"if(findStylesheet(href, fullhref)) return resolve();",
922-
"createStylesheet(chunkId, fullhref, resolve, reject);",
928+
"createStylesheet(chunkId, fullhref, null, resolve, reject);",
923929
]
924930
)});`
925931
)}`,
@@ -995,7 +1001,7 @@ class MiniCssExtractPlugin {
9951001
`promises.push(new Promise(${runtimeTemplate.basicFunction(
9961002
"resolve, reject",
9971003
[
998-
`var tag = createStylesheet(chunkId, fullhref, ${runtimeTemplate.basicFunction(
1004+
`var tag = createStylesheet(chunkId, fullhref, oldTag, ${runtimeTemplate.basicFunction(
9991005
"",
10001006
[
10011007
'tag.as = "style";',

Diff for: test/manual/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
<button class="lazy-button2">pressing this button</button>. Additional
5050
clicks have no effect.
5151
</p>
52+
<p>
53+
(HMR) Now modify the color in lazy.css and save it: This should remain
54+
orange.
55+
</p>
5256
<p>
5357
Refresh and press buttons in reverse order: This should turn green
5458
instead.

Diff for: test/manual/webpack.config.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ module.exports = {
6969
],
7070
devServer: {
7171
hot: ENABLE_HMR,
72-
static: __dirname,
72+
static: {
73+
directory: __dirname,
74+
watch: {
75+
// prevent page reload on source change so that we can test HMR
76+
ignored: /src/,
77+
},
78+
},
7379
headers: {
7480
"Access-Control-Allow-Origin": "*",
7581
},

0 commit comments

Comments
 (0)