Skip to content

Commit 86fac45

Browse files
committed
fix: CSS @import HMR for non-link exportType
1 parent e01b562 commit 86fac45

File tree

7 files changed

+61
-0
lines changed

7 files changed

+61
-0
lines changed
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+
Fixed HMR failure for CSS modules with @import when exportType !== "link". When exportType is not "link", CSS modules now behave like JavaScript modules and don't require special HMR handling, allowing @import CSS to work correctly during hot module replacement.

lib/css/CssModulesPlugin.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,13 @@ class CssModulesPlugin {
386386
compilation
387387
).renderModuleContent.tap(PLUGIN_NAME, (source, module) => {
388388
if (module instanceof CssModule && module.hot) {
389+
const exportType = /** @type {BuildMeta} */ (module.buildMeta)
390+
.exportType;
391+
// When exportType !== "link", modules behave like JavaScript modules
392+
if (exportType && exportType !== "link") {
393+
return source;
394+
}
395+
// For exportType === "link", we can optimize with self-acceptance
389396
const cssData = /** @type {BuildInfo} */ (module.buildInfo).cssData;
390397
if (!cssData) {
391398
return source;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.bar-v1 {
2+
color: bar;
3+
}
4+
---
5+
.bar-v2 {
6+
color: bar-updated;
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import "./bar.css";
2+
3+
.foo {
4+
color: foo;
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import fooStyle from "./foo.css";
2+
3+
it("should handle HMR for exportType", function (done) {
4+
expect(fooStyle).toContain("bar-v1");
5+
module.hot.accept(["./foo.css"], () => {
6+
expect(fooStyle).toContain("bar-v2");
7+
done();
8+
});
9+
NEXT(require("../../update")(done))
10+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
module.exports = {
4+
env: "jsdom"
5+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../types").Configuration} */
4+
module.exports = {
5+
target: "web",
6+
mode: "development",
7+
devtool: false,
8+
module: {
9+
rules: [
10+
{
11+
test: /.css$/,
12+
type: "css/module",
13+
parser: {
14+
exportType: "text"
15+
}
16+
}
17+
]
18+
},
19+
experiments: {
20+
css: true
21+
}
22+
};

0 commit comments

Comments
 (0)