Skip to content

Commit 3c7a3d8

Browse files
committed
fix: allow multiple runtime instances to receive HMR updates
1 parent 20dce0d commit 3c7a3d8

File tree

6 files changed

+68
-1
lines changed

6 files changed

+68
-1
lines changed

crates/rspack_plugin_runtime/src/runtime_module/runtime/jsonp_chunk_loading_with_hmr.ejs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function loadUpdateChunk(chunkId, updatedModulesList) {
3232
});
3333
}
3434

35-
<%- _global_object %>[<%- _hot_update_global %>] = <%- basicFunction("chunkId, moreModules, runtime") %> {
35+
var hmrGlobalJsonpLoader = <%- basicFunction("chunkId, moreModules, runtime") %> {
36+
if (!currentUpdate) return;
3637
for (var moduleId in moreModules) {
3738
if (<%- HAS_OWN_PROPERTY %>(moreModules, moduleId)) {
3839
currentUpdate[moduleId] = moreModules[moduleId];
@@ -45,3 +46,8 @@ function loadUpdateChunk(chunkId, updatedModulesList) {
4546
waitingUpdateResolves[chunkId] = undefined;
4647
}
4748
};
49+
var oldHmrGlobalJsonpLoader = <%- _global_object %>[<%- _hot_update_global %>] || (<%- basicFunction("") %> {});
50+
<%- _global_object %>[<%- _hot_update_global %>] = <%- basicFunction("chunkId, moreModules, runtime") %> {
51+
oldHmrGlobalJsonpLoader(chunkId, moreModules, runtime);
52+
hmrGlobalJsonpLoader(chunkId, moreModules, runtime);
53+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { test, expect } from "@/fixtures";
2+
3+
test("hmr works for multiple instances of the same runtime", async ({
4+
page,
5+
fileAction,
6+
rspack,
7+
}) => {
8+
await expect(await page.getByTestId('0')).toHaveText("1");
9+
await expect(await page.getByTestId('1')).toHaveText("1");
10+
11+
fileAction.updateFile("src/value.js", content => {
12+
return content.replace("1", "2");
13+
});
14+
15+
await expect(await page.getByTestId('0')).toHaveText("2");
16+
await expect(await page.getByTestId('1')).toHaveText("2");
17+
18+
fileAction.updateFile("src/value.js", content => {
19+
return content.replace("'2'", "Math.random().toString()")
20+
});
21+
22+
await expect(await page.getByTestId('0')).not.toHaveText("2");
23+
await expect(await page.getByTestId('1')).not.toHaveText("2");
24+
25+
const text0 = await page.getByTestId('0').textContent();
26+
const text1 = await page.getByTestId('1').textContent();
27+
await expect(text0).not.toEqual(text1);
28+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const rspack = require('@rspack/core');
2+
3+
/** @type import('@rspack/core').Configuration */
4+
module.exports ={
5+
mode: "development",
6+
entry: "./src/index.js",
7+
output: {
8+
filename: "bundle.js",
9+
},
10+
plugins: [
11+
new rspack.HtmlRspackPlugin({
12+
template: "./src/index.html",
13+
// Skip default script injection
14+
chunks: []
15+
})
16+
],
17+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
</head>
5+
<body>
6+
<script src="./bundle.js"></script>
7+
<script src="./bundle.js"></script>
8+
</body>
9+
</html>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { value } from './value.js';
2+
3+
const div = document.createElement('div');
4+
div.innerHTML = value
5+
div.setAttribute('data-testid', document.querySelectorAll('div').length.toString());
6+
document.body.appendChild(div);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export let value = '1'

0 commit comments

Comments
 (0)