Skip to content

Commit d2d9f75

Browse files
authored
fix: clear compiled modules from active modules (#10914)
* fix: clear compiled modules from active modules * fix: fix persistentCache test
1 parent 4fc871e commit d2d9f75

File tree

7 files changed

+19
-41
lines changed

7 files changed

+19
-41
lines changed

crates/rspack_plugin_lazy_compilation/src/module.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl Module for LazyCompilationProxyModule {
209209
);
210210

211211
let keep_active = format!(
212-
"var dispose = client.keepAlive({{ data: data, active: {}, module: module, onError: onError }})",
212+
"var dispose = client.activate({{ data: data, active: {}, module: module, onError: onError }})",
213213
block.is_some()
214214
);
215215

@@ -242,12 +242,10 @@ impl Module for LazyCompilationProxyModule {
242242
if (module.hot) {{
243243
module.hot.accept();
244244
module.hot.accept({}, function() {{ module.hot.invalidate(); }});
245-
module.hot.dispose(function(data) {{ delete data.resolveSelf; dispose(data); }});
245+
module.hot.dispose(function(data) {{ delete data.resolveSelf; }});
246246
if (module.hot.data && module.hot.data.resolveSelf)
247247
module.hot.data.resolveSelf(module.exports);
248248
}}
249-
function onError() {{ /* ignore */ }}
250-
{}
251249
",
252250
module_namespace_promise(
253251
&mut template_ctx,
@@ -261,7 +259,6 @@ impl Module for LazyCompilationProxyModule {
261259
ChunkGraph::get_module_id(&compilation.module_ids_artifact, *module)
262260
.expect("should have module id")
263261
),
264-
keep_active,
265262
))
266263
} else {
267264
RawStringSource::from(format!(

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

Lines changed: 2 additions & 4 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: 1430
13+
- Update: modules_demo_js_lazy-compilation-proxy.LAST_HASH.hot-update.js, size: 1268
1414

1515
## Manifest
1616

@@ -84,12 +84,10 @@ var data = __LAZY_ID__
8484
if (module.hot) {
8585
module.hot.accept();
8686
module.hot.accept("./modules/demo.js", function() { module.hot.invalidate(); });
87-
module.hot.dispose(function(data) { delete data.resolveSelf; dispose(data); });
87+
module.hot.dispose(function(data) { delete data.resolveSelf; });
8888
if (module.hot.data && module.hot.data.resolveSelf)
8989
module.hot.data.resolveSelf(module.exports);
9090
}
91-
function onError() { /* ignore */ }
92-
var dispose = client.keepAlive({ data: data, active: true, module: module, onError: onError })
9391

9492

9593
}),

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

Lines changed: 2 additions & 4 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: 1448
14+
- Update: modules_module_js_lazy-compilation-proxy.LAST_HASH.hot-update.js, size: 1286
1515

1616
## Manifest
1717

@@ -85,12 +85,10 @@ var data = __LAZY_ID__
8585
if (module.hot) {
8686
module.hot.accept();
8787
module.hot.accept("./modules/module.js", function() { module.hot.invalidate(); });
88-
module.hot.dispose(function(data) { delete data.resolveSelf; dispose(data); });
88+
module.hot.dispose(function(data) { delete data.resolveSelf; });
8989
if (module.hot.data && module.hot.data.resolveSelf)
9090
module.hot.data.resolveSelf(module.exports);
9191
}
92-
function onError() { /* ignore */ }
93-
var dispose = client.keepAlive({ data: data, active: true, module: module, onError: onError })
9492

9593

9694
}),

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

Lines changed: 2 additions & 4 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: 1392
13+
- Update: moduleA_js_lazy-compilation-proxy.LAST_HASH.hot-update.js, size: 1230
1414

1515
## Manifest
1616

@@ -69,12 +69,10 @@ var data = "0"
6969
if (module.hot) {
7070
module.hot.accept();
7171
module.hot.accept("./moduleA.js", function() { module.hot.invalidate(); });
72-
module.hot.dispose(function(data) { delete data.resolveSelf; dispose(data); });
72+
module.hot.dispose(function(data) { delete data.resolveSelf; });
7373
if (module.hot.data && module.hot.data.resolveSelf)
7474
module.hot.data.resolveSelf(module.exports);
7575
}
76-
function onError() { /* ignore */ }
77-
var dispose = client.keepAlive({ data: data, active: true, module: module, onError: onError })
7876

7977

8078
}),

packages/rspack/hot/lazy-compilation-node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var urlBase = decodeURIComponent(__resourceQuery.slice(1));
88
* @param {{ data: string, onError: (err: Error) => void, active: boolean, module: module }} options options
99
* @returns {() => void} function to destroy response
1010
*/
11-
exports.keepAlive = function (options) {
11+
exports.activate = function (options) {
1212
var data = options.data;
1313
var onError = options.onError;
1414
var active = options.active;

packages/rspack/hot/lazy-compilation-web.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ if (typeof EventSource !== "function") {
1111
var urlBase = decodeURIComponent(__resourceQuery.slice(1));
1212
/** @type {EventSource | undefined} */
1313
var activeEventSource;
14-
var activeKeys = new Map();
14+
var compiling = new Set();
1515
var errorHandlers = new Set();
1616

1717
var updateEventSource = function updateEventSource() {
1818
if (activeEventSource) activeEventSource.close();
19-
if (activeKeys.size) {
19+
if (compiling.size) {
2020
activeEventSource = new EventSource(
21-
urlBase + Array.from(activeKeys.keys()).join("@")
21+
urlBase + Array.from(compiling).join("@")
2222
);
2323
/**
2424
* @this {EventSource}
@@ -47,17 +47,18 @@ var updateEventSource = function updateEventSource() {
4747
* @param {{ data: string, onError: (err: Error) => void, active: boolean, module: module }} options options
4848
* @returns {() => void} function to destroy response
4949
*/
50-
exports.keepAlive = function (options) {
50+
exports.activate = function (options) {
5151
var data = options.data;
5252
var onError = options.onError;
5353
var active = options.active;
5454
var module = options.module;
5555
errorHandlers.add(onError);
56-
var value = activeKeys.get(data) || 0;
57-
activeKeys.set(data, value + 1);
58-
if (value === 0) {
56+
57+
if (!compiling.has(data)) {
58+
compiling.add(data);
5959
updateEventSource();
6060
}
61+
6162
if (!active && !module.hot) {
6263
console.log(
6364
"Hot Module Replacement is not enabled. Waiting for process restart..."
@@ -66,14 +67,7 @@ exports.keepAlive = function (options) {
6667

6768
return function () {
6869
errorHandlers.delete(onError);
69-
setTimeout(function () {
70-
var value = activeKeys.get(data);
71-
if (value === 1) {
72-
activeKeys.delete(data);
73-
updateEventSource();
74-
} else {
75-
activeKeys.set(data, value - 1);
76-
}
77-
}, 1000);
70+
compiling.delete(data);
71+
updateEventSource();
7872
};
7973
};

tests/e2e/cases/lazy-compilation/persistent-cache/index.test.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,8 @@ test("should load success", async ({ page, rspack }) => {
55
let component_count = await page.getByText("Component").count();
66
expect(component_count).toBe(1);
77

8-
const responsePromise = page.waitForResponse(
9-
response =>
10-
response.url().includes("lazy-compilation-using") &&
11-
response.request().method() === "GET",
12-
{ timeout: 5000 }
13-
);
148
await rspack.reboot();
159
await page.reload();
16-
await responsePromise;
1710

1811
await page.getByText("Click me").click();
1912
component_count = await page.getByText("Component").count();

0 commit comments

Comments
 (0)