Skip to content

Commit c5fb111

Browse files
fix(css): skip the loading runtime when every css chunk is initial (#21862)
* fix(css): skip the loading runtime when every css chunk is initial An initial chunk is seeded into installedChunks as loaded, so the css chunk loading handler can never reach its loading path for one. When no other chunk has css the whole runtime is dead code and still shipped. * test(css): propagate assertion failures from the async callback Passing done as the rejection handler does not catch a throw from the fulfillment handler, so a failed expectation timed out with no message.
1 parent 17fa705 commit c5fb111

7 files changed

Lines changed: 83 additions & 8 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+
Stop emitting the CSS chunk loading runtime when every CSS chunk is initial.

‎lib/css/CssLoadingRuntimeModule.js‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,26 @@ class CssLoadingRuntimeModule extends RuntimeModule {
7676
} = compilation;
7777
const dedupePrefetch = Boolean(resourceHints && resourceHints.dedupe);
7878
const fn = RuntimeGlobals.ensureChunkHandlers;
79+
/** @type {Set<ChunkId>} */
80+
const initialChunkIds = new Set();
81+
for (const c of chunk.getAllInitialChunks()) {
82+
if (chunkHasCss(c, chunkGraph)) {
83+
initialChunkIds.add(/** @type {ChunkId} */ (c.id));
84+
}
85+
}
7986
// The predicate the stylesheet is emitted from: an `@import` external gives a
8087
// chunk a css asset without a `CSS_TYPE` module, and it still has to load.
8188
const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasCss);
82-
const hasCssMatcher = compileBooleanMatcher(conditionMap);
89+
// An initial chunk is installed before the handler can run, so it never
90+
// reaches the loading path — with no other one there is nothing to load.
91+
const loadableConditionMap = { ...conditionMap };
92+
for (const id of initialChunkIds) {
93+
loadableConditionMap[id] = false;
94+
}
95+
const hasCssMatcher =
96+
compileBooleanMatcher(loadableConditionMap) === false
97+
? false
98+
: compileBooleanMatcher(conditionMap);
8399

84100
const withLoading =
85101
_runtimeRequirements.has(RuntimeGlobals.ensureChunkHandlers) &&
@@ -88,13 +104,6 @@ class CssLoadingRuntimeModule extends RuntimeModule {
88104
const withHmr = _runtimeRequirements.has(
89105
RuntimeGlobals.hmrDownloadUpdateHandlers
90106
);
91-
/** @type {Set<ChunkId>} */
92-
const initialChunkIds = new Set();
93-
for (const c of chunk.getAllInitialChunks()) {
94-
if (chunkHasCss(c, chunkGraph)) {
95-
initialChunkIds.add(/** @type {ChunkId} */ (c.id));
96-
}
97-
}
98107

99108
if (!withLoading && !withHmr) {
100109
return null;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import "./style.css";
2+
3+
it("should apply the initial stylesheet", () => {
4+
const style = getComputedStyle(document.body);
5+
expect(style.getPropertyValue("background")).toBe(" red");
6+
});
7+
8+
it("should load a javascript-only chunk", done => {
9+
import("./lazy.js").then(({ default: value }) => {
10+
try {
11+
expect(value).toBe(42);
12+
done();
13+
} catch (err) {
14+
done(err);
15+
}
16+
}, done);
17+
});
18+
19+
it("should not emit the css chunk loading runtime", () => {
20+
// Every chunk with css is initial here, so the loading runtime could never
21+
// run — only the javascript one is needed.
22+
const fs = __non_webpack_require__("fs");
23+
const path = __non_webpack_require__("path");
24+
const source = fs.readFileSync(path.join(__dirname, "bundle0.js"), "utf-8");
25+
// Split so this module's own source, which the bundle embeds, is not a match.
26+
const handler = suffix => `__webpack_require__.f.${suffix}`;
27+
expect(source).toContain(handler("j"));
28+
expect(source).not.toContain(handler("css"));
29+
expect(source).not.toContain(`data-webpack-${"loading"}`);
30+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 42;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: red;
3+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
module.exports = {
4+
findBundle() {
5+
return ["lazy_js.bundle0.js", "bundle0.js"];
6+
},
7+
moduleScope(scope) {
8+
const link = scope.window.document.createElement("link");
9+
link.rel = "stylesheet";
10+
link.href = "bundle0.css";
11+
scope.window.document.head.appendChild(link);
12+
}
13+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
target: "web",
6+
mode: "development",
7+
externalsPresets: { web: false, webAsync: true },
8+
experiments: {
9+
css: true
10+
},
11+
node: {
12+
__dirname: false
13+
}
14+
};

0 commit comments

Comments
 (0)