Skip to content

Commit b733032

Browse files
committed
fix: handle null module.layer in renderContentAsset
Webpack's Module base class defaults `layer` to `null` when `experiments.layers` is enabled and no explicit layer is assigned. The existing check `typeof module.layer !== "undefined"` incorrectly evaluates to `true` for `null` (since `typeof null` is `"object"`), causing a crash when accessing `module.layer.length`. Replace the check with `typeof module.layer === "string"` which correctly handles both `null` and `undefined` while being more precise about the expected type.
1 parent 144de04 commit b733032

5 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,11 +1537,11 @@ class MiniCssExtractPlugin {
15371537
if (
15381538
module.media ||
15391539
module.supports ||
1540-
typeof module.layer !== "undefined"
1540+
typeof module.layer === "string"
15411541
) {
15421542
let atImportExtra = "";
15431543

1544-
const needLayer = typeof module.layer !== "undefined";
1544+
const needLayer = typeof module.layer === "string";
15451545

15461546
if (needLayer) {
15471547
atImportExtra +=
@@ -1577,7 +1577,7 @@ class MiniCssExtractPlugin {
15771577
source.add(`@media ${module.media} {\n`);
15781578
}
15791579

1580-
const needLayer = typeof module.layer !== "undefined";
1580+
const needLayer = typeof module.layer === "string";
15811581

15821582
if (needLayer) {
15831583
source.add(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
background: red;
3+
}
4+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./style.css";
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Self from "../../../src";
2+
3+
module.exports = {
4+
entry: "./index.js",
5+
experiments: {
6+
layers: true,
7+
},
8+
module: {
9+
rules: [
10+
{
11+
test: /\.css$/,
12+
use: [Self.loader, "css-loader"],
13+
},
14+
],
15+
},
16+
plugins: [
17+
new Self({
18+
filename: "[name].css",
19+
}),
20+
],
21+
};

0 commit comments

Comments
 (0)