Skip to content

Commit d649715

Browse files
committed
feat: port css-extract latest features
1 parent 376e333 commit d649715

File tree

8 files changed

+1586
-11
lines changed

8 files changed

+1586
-11
lines changed

packages/rspack/src/builtin-plugin/css-extract/loader.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import path from "node:path";
22

3-
import schema from "./loader-options.json";
3+
import type { Filename, LoaderContext, LoaderDefinition } from "../..";
44
import { CssExtractRspackPlugin } from "./index";
5+
import schema from "./loader-options.json";
56
import { stringifyLocal, stringifyRequest } from "./utils";
6-
import type { Filename, LoaderContext, LoaderDefinition } from "../..";
77

88
export const BASE_URI = "webpack://";
99
export const MODULE_TYPE = "css/mini-extract";
@@ -36,12 +36,12 @@ export interface CssExtractRspackLoaderOptions {
3636
defaultExport?: boolean;
3737
}
3838

39-
function hotLoader(
39+
export function hotLoader(
4040
content: string,
4141
context: {
4242
loaderContext: LoaderContext;
43-
options: CssExtractRspackLoaderOptions;
44-
locals: Record<string, string>;
43+
options?: CssExtractRspackLoaderOptions;
44+
locals?: Record<string, string>;
4545
}
4646
): string {
4747
const localsJsonString = JSON.stringify(JSON.stringify(context.locals));
@@ -53,7 +53,7 @@ function hotLoader(
5353
var cssReload = require(${stringifyRequest(
5454
context.loaderContext,
5555
path.join(__dirname, "hmr/hotModuleReplacement.js")
56-
)})(module.id, ${JSON.stringify(context.options)});
56+
)}).cssReload(module.id, ${JSON.stringify(context.options ?? {})});
5757
// only invalidate when locals change
5858
if (
5959
module.hot.data &&

tests/plugin-test/css-extract/HMR.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/* eslint-disable no-console */
66

77
const hotModuleReplacement = require("../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement").cssReload;
8+
const hotLoader = require("../../../packages/rspack/dist/builtin-plugin/css-extract/loader").hotLoader;
89

910
function getLoadEvent() {
1011
const event = document.createEvent("Event");
@@ -361,4 +362,29 @@ describe("HMR", () => {
361362
done();
362363
}, 100);
363364
});
365+
366+
it("hotLoader works for non-locals", () => {
367+
const o = Date.now;
368+
Date.now = () => 1;
369+
const code = hotLoader("//content;", {
370+
loaderContext: {
371+
context: __dirname,
372+
},
373+
});
374+
Date.now = o;
375+
expect(code).toMatchSnapshot();
376+
});
377+
378+
it("hotLoader works for locals", () => {
379+
const o = Date.now;
380+
Date.now = () => 1;
381+
const code = hotLoader("//content;", {
382+
loaderContext: {
383+
context: __dirname,
384+
},
385+
locals: { foo: "bar" },
386+
});
387+
Date.now = o;
388+
expect(code).toMatchSnapshot();
389+
});
364390
});

tests/plugin-test/css-extract/__snapshots__/HMR.test.js.snap

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,57 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`HMR hotLoader works for locals 1`] = `
4+
"//content;
5+
if(module.hot) {
6+
(function() {
7+
var localsJsonString = \\"{\\\\\\"foo\\\\\\":\\\\\\"bar\\\\\\"}\\";
8+
// 1
9+
var cssReload = require(\\"../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js\\").cssReload(module.id, {});
10+
// only invalidate when locals change
11+
if (
12+
module.hot.data &&
13+
module.hot.data.value &&
14+
module.hot.data.value !== localsJsonString
15+
) {
16+
module.hot.invalidate();
17+
} else {
18+
module.hot.accept();
19+
}
20+
module.hot.dispose(function(data) {
21+
data.value = localsJsonString;
22+
cssReload();
23+
});
24+
})();
25+
}
26+
"
27+
`;
28+
29+
exports[`HMR hotLoader works for non-locals 1`] = `
30+
"//content;
31+
if(module.hot) {
32+
(function() {
33+
var localsJsonString = undefined;
34+
// 1
35+
var cssReload = require(\\"../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js\\").cssReload(module.id, {});
36+
// only invalidate when locals change
37+
if (
38+
module.hot.data &&
39+
module.hot.data.value &&
40+
module.hot.data.value !== localsJsonString
41+
) {
42+
module.hot.invalidate();
43+
} else {
44+
module.hot.accept();
45+
}
46+
module.hot.dispose(function(data) {
47+
data.value = localsJsonString;
48+
cssReload();
49+
});
50+
})();
51+
}
52+
"
53+
`;
54+
355
exports[`HMR should handle error event 1`] = `"[HMR] css reload %s"`;
456

557
exports[`HMR should handle error event 2`] = `"<link rel=\\"stylesheet\\" href=\\"/dist/main.css\\"><link rel=\\"stylesheet\\" href=\\"http://localhost/dist/main.css?1479427200000\\">"`;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.VoofDB21D_QzDbRdwMiY {
2+
color: red;
3+
}

0 commit comments

Comments
 (0)