diff --git a/crates/rspack_plugin_hmr/src/lib.rs b/crates/rspack_plugin_hmr/src/lib.rs index c0a2b35ade81..0ca7c4cafd3f 100644 --- a/crates/rspack_plugin_hmr/src/lib.rs +++ b/crates/rspack_plugin_hmr/src/lib.rs @@ -191,7 +191,11 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { if !new_modules.is_empty() || !new_runtime_modules.is_empty() { let mut hot_update_chunk = Chunk::new(None, ChunkKind::HotUpdate); hot_update_chunk.id = Some(chunk_id.to_string()); - hot_update_chunk.runtime = new_runtime.clone(); + hot_update_chunk.runtime = if let Some(current_chunk) = current_chunk { + current_chunk.runtime.clone() + } else { + new_runtime.clone() + }; let ukey = hot_update_chunk.ukey; if let Some(current_chunk) = current_chunk { diff --git a/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/index.js b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/index.js new file mode 100644 index 000000000000..f076bcf37629 --- /dev/null +++ b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/index.js @@ -0,0 +1,13 @@ +let value = require("./module.js"); +import {a} from "./lib/a.js"; + +it("should compile", (done) => { + expect(value).toBe(1); + expect(a).toBe(1); + module.hot.accept("./module.js", () => { + value = require("./module"); + expect(value).toBe(2); + done(); + }); + NEXT(require("../../update")(done)); +}); diff --git a/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/lib/a.js b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/lib/a.js new file mode 100644 index 000000000000..41715495f45f --- /dev/null +++ b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/lib/a.js @@ -0,0 +1 @@ +export const a = 1 diff --git a/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/lib/b.js b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/lib/b.js new file mode 100644 index 000000000000..9439af423079 --- /dev/null +++ b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/lib/b.js @@ -0,0 +1 @@ +export const b = 1 diff --git a/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/lib/package.json b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/lib/package.json new file mode 100644 index 000000000000..3802144dedbd --- /dev/null +++ b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/lib/package.json @@ -0,0 +1,3 @@ +{ + "sideEffects": true +} diff --git a/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/module.js b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/module.js new file mode 100644 index 000000000000..25f40da13307 --- /dev/null +++ b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/module.js @@ -0,0 +1,4 @@ +module.exports = 1; +--- +new Worker(new URL('./worker.js', import.meta.url)) +module.exports = 2; diff --git a/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/rspack.config.js b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/rspack.config.js new file mode 100644 index 000000000000..d0d0854b6da2 --- /dev/null +++ b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/rspack.config.js @@ -0,0 +1,17 @@ +module.exports = { + optimization: { + usedExports: true, + // make 'lib' chunk runtime to be worker + entry + splitChunks: { + minSize: 0, + chunks: "all", + cacheGroups: { + lib: { + test: /[/\\]lib[/\\](a|b|index).js$/, + name: "lib", + filename: "bundle-lib.js" + } + } + } + } +}; diff --git a/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/test.filter.js b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/test.filter.js new file mode 100644 index 000000000000..7701a43cf16c --- /dev/null +++ b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/test.filter.js @@ -0,0 +1,5 @@ +module.exports = function (config) { + if (config.target !== "web") { + return false; + } +}; diff --git a/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/worker.js b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/worker.js new file mode 100644 index 000000000000..087c8371c2e3 --- /dev/null +++ b/packages/rspack-test-tools/tests/hotCases/runtime/add-runtime/worker.js @@ -0,0 +1,2 @@ +import {b} from "./lib/b.js"; +b;