Skip to content

Commit 3edda4d

Browse files
committed
fix css hmr edge cases
insert css into old location only look for rel="stylesheet" tags
1 parent b3871e8 commit 3edda4d

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

examples/css/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ module.exports = __webpack_require__.p + "89a353e9c515885abd8e.png";
208208
/******/ var links = document.getElementsByTagName("link");
209209
/******/ for(var i = 0; i < links.length; i++) {
210210
/******/ var l = links[i];
211-
/******/ if(l.href == url || l.getAttribute("href") == url || l.getAttribute("data-webpack") == uniqueName + ":" + key) { link = l; break; }
211+
/******/ if(l.rel == "stylesheet" && (l.href == url || l.getAttribute("href") == url || l.getAttribute("data-webpack") == uniqueName + ":" + key)) { link = l; break; }
212212
/******/ }
213213
/******/ if(!done) return link;
214214
/******/
@@ -233,6 +233,7 @@ module.exports = __webpack_require__.p + "89a353e9c515885abd8e.png";
233233
/******/ link.onerror = onLinkComplete.bind(null, link.onerror);
234234
/******/ link.onload = onLinkComplete.bind(null, link.onload);
235235
/******/ } else onLinkComplete(undefined, { type: 'load', target: link });
236+
/******/
236237
/******/ needAttach && document.head.appendChild(link);
237238
/******/ return link;
238239
/******/ };
@@ -486,12 +487,12 @@ head{--webpack-app-1:_7;}
486487
## Unoptimized
487488

488489
```
489-
assets by chunk 16.8 KiB (name: main)
490-
asset output.js 16.4 KiB [emitted] (name: main)
490+
assets by chunk 16.9 KiB (name: main)
491+
asset output.js 16.5 KiB [emitted] (name: main)
491492
asset output.css 385 bytes [emitted] (name: main)
492493
asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: images/file.png] (auxiliary name: main)
493494
asset 1.output.css 49 bytes [emitted]
494-
Entrypoint main 16.8 KiB (14.6 KiB) = output.js 16.4 KiB output.css 385 bytes 1 auxiliary asset
495+
Entrypoint main 16.9 KiB (14.6 KiB) = output.js 16.5 KiB output.css 385 bytes 1 auxiliary asset
495496
chunk (runtime: main) output.js, output.css (main) 218 bytes (javascript) 335 bytes (css) 14.6 KiB (asset) 42 bytes (css-import) 10 KiB (runtime) [entry] [rendered]
496497
> ./example.js main
497498
runtime modules 10 KiB 9 modules
@@ -512,12 +513,12 @@ webpack 5.66.0 compiled successfully
512513
## Production mode
513514

514515
```
515-
assets by chunk 4.23 KiB (name: main)
516-
asset output.js 3.85 KiB [emitted] [minimized] (name: main)
516+
assets by chunk 4.25 KiB (name: main)
517+
asset output.js 3.87 KiB [emitted] [minimized] (name: main)
517518
asset output.css 385 bytes [emitted] (name: main)
518519
asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: images/file.png] (auxiliary name: main)
519520
asset 159.output.css 53 bytes [emitted]
520-
Entrypoint main 4.23 KiB (14.6 KiB) = output.js 3.85 KiB output.css 385 bytes 1 auxiliary asset
521+
Entrypoint main 4.25 KiB (14.6 KiB) = output.js 3.87 KiB output.css 385 bytes 1 auxiliary asset
521522
chunk (runtime: main) 159.output.css 23 bytes
522523
> ./lazy-style.css ./example.js 4:0-26
523524
./lazy-style.css 23 bytes [built] [code generated]

lib/css/CssLoadingRuntimeModule.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,15 @@ class CssLoadingRuntimeModule extends RuntimeModule {
216216
"for(var i = 0; i < links.length; i++) {",
217217
Template.indent([
218218
"var l = links[i];",
219-
`if(${
219+
`if(l.rel == "stylesheet" && (${
220220
withHmr
221221
? 'l.href.startsWith(url) || l.getAttribute("href").startsWith(url)'
222222
: 'l.href == url || l.getAttribute("href") == url'
223223
}${
224224
uniqueName
225225
? ' || l.getAttribute("data-webpack") == uniqueName + ":" + key'
226226
: ""
227-
}) { link = l; break; }`
227+
})) { link = l; break; }`
228228
]),
229229
"}",
230230
"if(!done) return link;",
@@ -253,6 +253,7 @@ class CssLoadingRuntimeModule extends RuntimeModule {
253253
"link.onload = onLinkComplete.bind(null, link.onload);"
254254
]),
255255
"} else onLinkComplete(undefined, { type: 'load', target: link });", // We assume any existing stylesheet is render blocking
256+
withHmr ? "hmr ? document.head.insertBefore(link, hmr) :" : "",
256257
"needAttach && document.head.appendChild(link);",
257258
"return link;"
258259
]
@@ -433,7 +434,7 @@ class CssLoadingRuntimeModule extends RuntimeModule {
433434
]),
434435
"}"
435436
]
436-
)}, true);`
437+
)}, oldTag);`
437438
]
438439
)}));`
439440
])});`

0 commit comments

Comments
 (0)