Skip to content

Commit 85ab784

Browse files
fix(css): settle a css chunk load without a DOM where the stylesheet cannot be read (#21970)
The no-DOM branch of the css chunk loader chained the fs and url imports into a `then` with no rejection handler. Where the import rejects, or `readFile` throws on sight because the public path is not a file url, nothing called `loadingEnded`: the chunk promise never settled, and on Node 15 and later the unhandled rejection ended the process. A rejection now settles the load the way a missing file already does, with nothing collected.
1 parent c85f246 commit 85ab784

10 files changed

Lines changed: 90 additions & 0 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+
Settle a css chunk load without a DOM where its stylesheet cannot be read.

‎lib/css/CssLoadingRuntimeModule.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ class CssLoadingRuntimeModule extends RuntimeModule {
385385
]
386386
)});`
387387
]
388+
// No fs to import, or a url `readFile` rejects on sight (a CDN public
389+
// path): nothing to collect, which is what a missing file already means.
390+
)}).catch(${runtimeTemplate.expressionFunction(
391+
"loadingEnded({ type: 'load' })"
388392
)});`
389393
]),
390394
"}"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
it("should settle a css chunk load where the stylesheet is not on disk", () =>
2+
import("./lazy.css").then(() => {
3+
expect(__webpack_css_server_styles__).not.toContain("rebeccapurple");
4+
}));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.lazy {
2+
color: rebeccapurple;
3+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
5+
const CDN = "https://cdn.example/assets/";
6+
7+
module.exports = {
8+
// No DOM: the server side of the universal bundle.
9+
moduleScope(scope) {
10+
delete scope.window;
11+
delete scope.document;
12+
delete scope.self;
13+
},
14+
// The chunk's script is read from disk in its place; its stylesheet url is not.
15+
resolveModule(module) {
16+
return module.startsWith(CDN) ? `./${path.basename(module)}` : module;
17+
}
18+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
devtool: false,
6+
target: ["web", "node"],
7+
mode: "development",
8+
experiments: {
9+
css: true,
10+
outputModule: true
11+
},
12+
output: {
13+
module: true,
14+
uniqueName: "universal-cdn-public-path",
15+
// Not a file url, so the stylesheet is not on disk to be read.
16+
publicPath: "https://cdn.example/assets/"
17+
}
18+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
it("should settle a css chunk load where the stylesheet cannot be read", () =>
2+
import("./lazy.css").then(() => {
3+
expect(__webpack_css_server_styles__).not.toContain("rebeccapurple");
4+
}));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.lazy {
2+
color: rebeccapurple;
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
3+
module.exports = {
4+
// No DOM and no file system: a worker running the universal bundle.
5+
moduleScope(scope) {
6+
delete scope.window;
7+
delete scope.document;
8+
delete scope.self;
9+
},
10+
modules: {
11+
get fs() {
12+
throw new Error("Cannot find module 'fs'");
13+
}
14+
}
15+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
devtool: false,
6+
target: ["web", "node"],
7+
mode: "development",
8+
experiments: {
9+
css: true,
10+
outputModule: true
11+
},
12+
output: {
13+
module: true,
14+
uniqueName: "universal-no-fs"
15+
}
16+
};

0 commit comments

Comments
 (0)