Skip to content

Commit 17a359e

Browse files
authored
refactor: use length as id instead of module id for lazy compilation (#10855)
1 parent 0733e4c commit 17a359e

File tree

9 files changed

+65
-21
lines changed

9 files changed

+65
-21
lines changed

packages/rspack-test-tools/etc/test-tools.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,7 @@ export type TTestConfig<T extends ECompilerType> = {
16461646
modules?: Record<string, Object>;
16471647
timeout?: number;
16481648
concurrent?: boolean;
1649+
snapshotContent?(content: string): string;
16491650
checkSteps?: boolean;
16501651
};
16511652

packages/rspack-test-tools/src/processor/hot-step.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,12 @@ export class HotSnapshotProcessor<
226226
}
227227
}
228228

229-
const replaceContent = (str: string) => {
229+
const replaceContent = (rawStr: string) => {
230+
let str = rawStr;
231+
const replaceContentConfig = context.getTestConfig().snapshotContent;
232+
if (replaceContentConfig) {
233+
str = replaceContentConfig(str);
234+
}
230235
return normalizePlaceholder(
231236
Object.entries(hashes)
232237
.reduce((str, [raw, replacement]) => {

packages/rspack-test-tools/src/processor/snapshot.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,16 @@ export class SnapshotProcessor<
6868
const snapshotFileFilter =
6969
this._snapshotOptions.snapshotFileFilter ||
7070
((file: string) => file.endsWith(".js") && !file.includes("runtime.js"));
71+
7172
const fileContents = Object.entries(compilation.assets)
7273
.filter(([file]) => snapshotFileFilter(file))
7374
.map(([file, source]) => {
7475
const tag = path.extname(file).slice(1) || "txt";
75-
const content = this.serializeEachFile(source.source().toString());
76+
let content = this.serializeEachFile(source.source().toString());
77+
const testConfig = context.getTestConfig();
78+
if (testConfig.snapshotContent) {
79+
content = testConfig.snapshotContent(content);
80+
}
7681

7782
return `\`\`\`${tag} title=${file}\n${content}\n\`\`\``;
7883
});

packages/rspack-test-tools/src/type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ export type TTestConfig<T extends ECompilerType> = {
220220
modules?: Record<string, Object>;
221221
timeout?: number;
222222
concurrent?: boolean;
223+
snapshotContent?(content: string): string;
223224

224225
// Only valid for Hot tests
225226
checkSteps?: boolean;

packages/rspack-test-tools/tests/hotCases/lazy-compilation/context/__snapshots__/web/1.snap.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- Bundle: modules_module_js_lazy-compilation-proxy.chunk.CURRENT_HASH.js
1111
- Manifest: main.LAST_HASH.hot-update.json, size: 69
1212
- Update: main.LAST_HASH.hot-update.js, size: 582
13-
- Update: modules_demo_js_lazy-compilation-proxy.LAST_HASH.hot-update.js, size: 1590
13+
- Update: modules_demo_js_lazy-compilation-proxy.LAST_HASH.hot-update.js, size: 1430
1414

1515
## Manifest
1616

@@ -79,7 +79,7 @@ self["webpackHotUpdate"]("modules_demo_js_lazy-compilation-proxy", {
7979
\*********************************************************************************************************************/
8080
(function (module, __unused_webpack_exports, __webpack_require__) {
8181
var client = __webpack_require__("../../../../../rspack/hot/lazy-compilation-web.js?http%3A%2F%2Flocalhost%3APORT%2Flazy-compilation-using-");
82-
var data = "<TEST_TOOLS_ROOT>/dist/helper/loaders/hot-update.js%3F%3FruleSet%5B1%5D.rules%5B0%5D.use%5B0%5D!<TEST_TOOLS_ROOT>/tests/hotCases/lazy-compilation/context/modules/demo.js"
82+
var data = __LAZY_ID__
8383
module.exports = __webpack_require__.e(/*! import() */ "modules_demo_js").then(__webpack_require__.bind(__webpack_require__, /*! ./modules/demo.js */ "./modules/demo.js"));
8484
if (module.hot) {
8585
module.hot.accept();

packages/rspack-test-tools/tests/hotCases/lazy-compilation/context/__snapshots__/web/2.snap.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
- Bundle: modules_module_js_lazy-compilation-proxy.chunk.CURRENT_HASH.js
1212
- Manifest: main.LAST_HASH.hot-update.json, size: 71
1313
- Update: main.LAST_HASH.hot-update.js, size: 582
14-
- Update: modules_module_js_lazy-compilation-proxy.LAST_HASH.hot-update.js, size: 1610
14+
- Update: modules_module_js_lazy-compilation-proxy.LAST_HASH.hot-update.js, size: 1448
1515

1616
## Manifest
1717

@@ -80,7 +80,7 @@ self["webpackHotUpdate"]("modules_module_js_lazy-compilation-proxy", {
8080
\***********************************************************************************************************************/
8181
(function (module, __unused_webpack_exports, __webpack_require__) {
8282
var client = __webpack_require__("../../../../../rspack/hot/lazy-compilation-web.js?http%3A%2F%2Flocalhost%3APORT%2Flazy-compilation-using-");
83-
var data = "<TEST_TOOLS_ROOT>/dist/helper/loaders/hot-update.js%3F%3FruleSet%5B1%5D.rules%5B0%5D.use%5B0%5D!<TEST_TOOLS_ROOT>/tests/hotCases/lazy-compilation/context/modules/module.js"
83+
var data = __LAZY_ID__
8484
module.exports = __webpack_require__.e(/*! import() */ "modules_module_js").then(__webpack_require__.bind(__webpack_require__, /*! ./modules/module.js */ "./modules/module.js"));
8585
if (module.hot) {
8686
module.hot.accept();
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
const RE = /var data = "\d*"/g;
2+
13
module.exports = {
24
documentType: "fake",
3-
checkSteps: false
5+
checkSteps: false,
6+
snapshotContent(
7+
/**@type {string} */
8+
content
9+
) {
10+
return content.replaceAll(RE, "var data = __LAZY_ID__");
11+
}
412
};

packages/rspack-test-tools/tests/hotCases/lazy-compilation/module-test/__snapshots__/web/1.snap.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- Bundle: moduleB_js.chunk.CURRENT_HASH.js
1111
- Manifest: main.LAST_HASH.hot-update.json, size: 64
1212
- Update: main.LAST_HASH.hot-update.js, size: 182
13-
- Update: moduleA_js_lazy-compilation-proxy.LAST_HASH.hot-update.js, size: 1559
13+
- Update: moduleA_js_lazy-compilation-proxy.LAST_HASH.hot-update.js, size: 1392
1414

1515
## Manifest
1616

@@ -64,7 +64,7 @@ self["webpackHotUpdate"]("moduleA_js_lazy-compilation-proxy", {
6464
\*********************************************************************************************************************/
6565
(function (module, __unused_webpack_exports, __webpack_require__) {
6666
var client = __webpack_require__("../../../../../rspack/hot/lazy-compilation-web.js?http%3A%2F%2Flocalhost%3APORT%2Flazy-compilation-using-");
67-
var data = "<TEST_TOOLS_ROOT>/dist/helper/loaders/hot-update.js%3F%3FruleSet%5B1%5D.rules%5B0%5D.use%5B0%5D!<TEST_TOOLS_ROOT>/tests/hotCases/lazy-compilation/module-test/moduleA.js"
67+
var data = "0"
6868
module.exports = __webpack_require__.e(/*! import() */ "moduleA_js").then(__webpack_require__.bind(__webpack_require__, /*! ./moduleA.js */ "./moduleA.js"));
6969
if (module.hot) {
7070
module.hot.accept();

packages/rspack/src/builtin-plugin/lazy-compilation/middleware.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,28 @@ export const lazyCompilationMiddleware = (
5656
options.prefix = `${prefix}__${i++}`;
5757
const activeModules = new Map<string, boolean>();
5858
const filesByKey = new Map<string, string>();
59+
const indexToModule = new Map();
60+
const moduleToIndex = new Map();
5961

6062
middlewareByCompiler.set(
6163
options.prefix,
6264
lazyCompilationMiddlewareInternal(
6365
compiler,
66+
indexToModule,
6467
activeModules,
6568
filesByKey,
6669
options.prefix
6770
)
6871
);
6972

70-
applyPlugin(c, options, activeModules, filesByKey);
73+
applyPlugin(
74+
c,
75+
moduleToIndex,
76+
indexToModule,
77+
options,
78+
activeModules,
79+
filesByKey
80+
);
7181
}
7282

7383
const keys = [...middlewareByCompiler.keys()];
@@ -89,15 +99,25 @@ export const lazyCompilationMiddleware = (
8999

90100
const activeModules: Map<string, boolean> = new Map();
91101
const filesByKey: Map<string, string> = new Map();
102+
const indexToMap = new Map();
103+
const moduleToIndex = new Map();
92104

93105
const options = {
94106
...compiler.options.experiments.lazyCompilation
95107
};
96-
applyPlugin(compiler, options, activeModules, filesByKey);
108+
applyPlugin(
109+
compiler,
110+
moduleToIndex,
111+
indexToMap,
112+
options,
113+
activeModules,
114+
filesByKey
115+
);
97116

98117
const lazyCompilationPrefix = options.prefix || LAZY_COMPILATION_PREFIX;
99118
return lazyCompilationMiddlewareInternal(
100119
compiler,
120+
indexToMap,
101121
activeModules,
102122
filesByKey,
103123
lazyCompilationPrefix
@@ -106,24 +126,26 @@ export const lazyCompilationMiddleware = (
106126

107127
function applyPlugin(
108128
compiler: Compiler,
129+
moduleToIndex: Map<string, string>,
130+
indexToModule: Map<string, string>,
109131
options: LazyCompilationOptions,
110132
activeModules: Map<string, boolean>,
111133
filesByKey: Map<string, string>
112134
) {
113135
const plugin = new BuiltinLazyCompilationPlugin(
114136
({ module, path }) => {
115-
const key = encodeURIComponent(
116-
module.replace(/\\/g, "/").replace(/@/g, "_")
117-
)
118-
// module identifier may contain query, bang(!) or split(|),
119-
// should do our best to ensure it's the same with which comes
120-
// from server url
121-
.replace(/%(2F|3A|24|26|2B|2C|3B|3D)/g, decodeURIComponent);
137+
let index = moduleToIndex.get(module);
138+
if (index === undefined) {
139+
index = moduleToIndex.size.toString();
140+
moduleToIndex.set(module, index);
141+
indexToModule.set(index, module);
142+
}
143+
const key = indexToModule.get(index)!;
122144
filesByKey.set(key, path);
123145
const active = activeModules.get(key) === true;
124146
return {
125147
client: `${options.client || getDefaultClient(compiler)}?${encodeURIComponent(getFullServerUrl(options))}`,
126-
data: key,
148+
data: index,
127149
active
128150
};
129151
},
@@ -139,6 +161,7 @@ function applyPlugin(
139161
// used for reuse code, do not export this
140162
const lazyCompilationMiddlewareInternal = (
141163
compiler: Compiler | MultiCompiler,
164+
indexToModule: Map<string, string>,
142165
activeModules: Map<string, boolean>,
143166
filesByKey: Map<string, string>,
144167
lazyCompilationPrefix: string
@@ -151,15 +174,16 @@ const lazyCompilationMiddlewareInternal = (
151174
return next?.();
152175
}
153176

154-
const keys = req.url.slice(lazyCompilationPrefix.length).split("@");
177+
const indices = req.url.slice(lazyCompilationPrefix.length).split("@");
155178
req.socket.setNoDelay(true);
156179

157180
res.setHeader("content-type", "text/event-stream");
158181
res.writeHead(200);
159182
res.write("\n");
160183

161184
const moduleActivated = [];
162-
for (const key of keys) {
185+
for (const index of indices) {
186+
const key = indexToModule.get(index)!;
163187
const oldValue = activeModules.get(key) ?? false;
164188
activeModules.set(key, true);
165189
if (!oldValue) {

0 commit comments

Comments
 (0)