Skip to content

Commit cc222c6

Browse files
committed
fix: allow multiple runtime instances to receive HMR updates
1 parent 513a407 commit cc222c6

File tree

6 files changed

+63
-1
lines changed

6 files changed

+63
-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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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').textContent()).toBe("1");
9+
await expect(await page.getByTestId('1').textContent()).toBe("1");
10+
11+
fileAction.updateFile("src/value.js", content => {
12+
return content.replace("1", "2");
13+
}
14+
);
15+
16+
await expect(await page.getByTestId('0')).toHaveText("2");
17+
await expect(await page.getByTestId('1')).toHaveText("2");
18+
});
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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);
7+
8+
module.hot.accept('./value.js', () => {
9+
const newValue = require('./value.js').value;
10+
div.innerHTML = newValue;
11+
})
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)