Skip to content

fix: allow multiple runtime instances to receive HMR updates #10426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ function loadUpdateChunk(chunkId, updatedModulesList) {
});
}

<%- _global_object %>[<%- _hot_update_global %>] = <%- basicFunction("chunkId, moreModules, runtime") %> {
var hmrGlobalJsonpLoader = <%- basicFunction("chunkId, moreModules, runtime") %> {
if (!currentUpdate) return;
for (var moduleId in moreModules) {
if (<%- HAS_OWN_PROPERTY %>(moreModules, moduleId)) {
currentUpdate[moduleId] = moreModules[moduleId];
Expand All @@ -45,3 +46,8 @@ function loadUpdateChunk(chunkId, updatedModulesList) {
waitingUpdateResolves[chunkId] = undefined;
}
};
var oldHmrGlobalJsonpLoader = <%- _global_object %>[<%- _hot_update_global %>] || (<%- basicFunction("") %> {});
<%- _global_object %>[<%- _hot_update_global %>] = <%- basicFunction("chunkId, moreModules, runtime") %> {
oldHmrGlobalJsonpLoader(chunkId, moreModules, runtime);
hmrGlobalJsonpLoader(chunkId, moreModules, runtime);
};
28 changes: 28 additions & 0 deletions tests/e2e/cases/hmr/multiple-runtime-instances/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { test, expect } from "@/fixtures";

test("hmr works for multiple instances of the same runtime", async ({
page,
fileAction,
rspack,
}) => {
await expect(await page.getByTestId('0')).toHaveText("1");
await expect(await page.getByTestId('1')).toHaveText("1");

fileAction.updateFile("src/value.js", content => {
return content.replace("1", "2");
});

await expect(await page.getByTestId('0')).toHaveText("2");
await expect(await page.getByTestId('1')).toHaveText("2");

fileAction.updateFile("src/value.js", content => {
return content.replace("'2'", "Math.random().toString()")
});

await expect(await page.getByTestId('0')).not.toHaveText("2");
await expect(await page.getByTestId('1')).not.toHaveText("2");

const text0 = await page.getByTestId('0').textContent();
const text1 = await page.getByTestId('1').textContent();
await expect(text0).not.toEqual(text1);
});
17 changes: 17 additions & 0 deletions tests/e2e/cases/hmr/multiple-runtime-instances/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const rspack = require('@rspack/core');

/** @type import('@rspack/core').Configuration */
module.exports ={
mode: "development",
entry: "./src/index.js",
output: {
filename: "bundle.js",
},
plugins: [
new rspack.HtmlRspackPlugin({
template: "./src/index.html",
// Skip default script injection
chunks: []
})
],
};
9 changes: 9 additions & 0 deletions tests/e2e/cases/hmr/multiple-runtime-instances/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<script src="./bundle.js"></script>
<script src="./bundle.js"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions tests/e2e/cases/hmr/multiple-runtime-instances/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { value } from './value.js';

const div = document.createElement('div');
div.innerHTML = value
div.setAttribute('data-testid', document.querySelectorAll('div').length.toString());
document.body.appendChild(div);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export let value = '1'