Skip to content

Commit 2c305a1

Browse files
committed
fix(css): load async stylesheets with a shared runtime
1 parent 52996d7 commit 2c305a1

7 files changed

Lines changed: 92 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Fix missing async stylesheets with a shared runtime.

‎lib/css/CssLoadingRuntimeModule.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,20 @@ class CssLoadingRuntimeModule extends RuntimeModule {
6565
_getLoadableChunks() {
6666
const chunk = /** @type {Chunk} */ (this.chunk);
6767
const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
68+
const asyncChunks = chunk.getAllAsyncChunks();
6869
/** @type {Set<ChunkId>} */
6970
const initialChunkIds = new Set();
71+
// A shared runtime can reach a stylesheet initially through one entry and
72+
// asynchronously through another, where it still needs to be loaded.
7073
for (const c of chunk.getAllInitialChunks()) {
71-
if (chunkHasCss(c, chunkGraph)) {
74+
if (chunkHasCss(c, chunkGraph) && !asyncChunks.has(c)) {
7275
initialChunkIds.add(/** @type {ChunkId} */ (c.id));
7376
}
7477
}
7578
// The predicate the stylesheet is emitted from: an `@import` external gives a
7679
// chunk a css asset without a `CSS_TYPE` module, and it still has to load.
7780
const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasCss);
78-
// An initial chunk is installed before the handler can run, so it never
79-
// reaches the loading path — with no other one there is nothing to load.
81+
// With no stylesheets left to request asynchronously, the handler is unused.
8082
const loadableConditionMap = { ...conditionMap };
8183
for (const id of initialChunkIds) {
8284
loadableConditionMap[id] = false;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
color: red;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import "./feature.css";
2+
3+
export const value = 42;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
it("should load an async stylesheet that is initial for another entry", async () => {
2+
if (typeof document !== "undefined") {
3+
expect(document.getElementsByTagName("link")).toHaveLength(0);
4+
}
5+
6+
const promise = import("./feature.js");
7+
let stylesheetLoaded = false;
8+
if (typeof document !== "undefined") {
9+
const links = document.getElementsByTagName("link");
10+
expect(links).toHaveLength(1);
11+
expect(links[0].rel).toBe("stylesheet");
12+
expect(links[0].href).toContain("shared-css.");
13+
links[0].addEventListener("load", () => {
14+
stylesheetLoaded = true;
15+
});
16+
}
17+
18+
expect((await promise).value).toBe(42);
19+
if (typeof document !== "undefined") {
20+
expect(stylesheetLoaded).toBe(true);
21+
expect(getComputedStyle(document.body).getPropertyValue("color")).toBe(
22+
" red"
23+
);
24+
await import("./feature.js");
25+
expect(document.getElementsByTagName("link")).toHaveLength(1);
26+
}
27+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
module.exports = {
4+
evaluateScriptOnAttached: true,
5+
findBundle(index, options) {
6+
const extension = options.output.module ? "mjs" : "js";
7+
return [`runtime.${index}.${extension}`, `main.${index}.${extension}`];
8+
}
9+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration[]} */
4+
module.exports = [
5+
{ target: "web", output: { module: false } },
6+
{ target: "web", output: { module: true } },
7+
{ target: ["web", "node"], output: { module: true } }
8+
].map((options, index) => ({
9+
...options,
10+
mode: "development",
11+
devtool: false,
12+
entry: {
13+
main: "./index.js",
14+
other: "./feature.js"
15+
},
16+
experiments: { css: true },
17+
output: {
18+
...options.output,
19+
filename: `[name].${index}.${options.output.module ? "mjs" : "js"}`,
20+
chunkFilename: `[name].${index}.${options.output.module ? "mjs" : "js"}`,
21+
cssFilename: `[name].${index}.css`,
22+
cssChunkFilename: `[name].${index}.css`,
23+
publicPath: ""
24+
},
25+
optimization: {
26+
runtimeChunk: "single",
27+
splitChunks: {
28+
cacheGroups: {
29+
default: false,
30+
defaultVendors: false,
31+
styles: {
32+
test: /\.css$/,
33+
name: "shared-css",
34+
chunks: "all",
35+
enforce: true
36+
}
37+
}
38+
}
39+
}
40+
}));

0 commit comments

Comments
 (0)