Skip to content

Commit 145aca4

Browse files
authored
fix: mf should correctly hoist modules (#11743)
1 parent 4799b51 commit 145aca4

File tree

10 files changed

+104
-3
lines changed

10 files changed

+104
-3
lines changed

crates/rspack_plugin_mf/src/container/hoist_container_references_plugin.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::{
2020
use async_trait::async_trait;
2121
use rspack_core::{
2222
Compilation, CompilationOptimizeChunks, CompilerCompilation, Dependency, DependencyId,
23-
ModuleIdentifier, Plugin, incremental::Mutation,
23+
ModuleIdentifier, Plugin, RuntimeSpec, incremental::Mutation,
2424
};
2525
use rspack_error::Result;
2626
use rspack_hook::{plugin, plugin_hook};
@@ -206,7 +206,8 @@ async fn optimize_chunks(&self, compilation: &mut Compilation) -> Result<Option<
206206
let runtime_chunks = compilation
207207
.chunk_graph
208208
.get_module_runtimes_iter(*module, &compilation.chunk_by_ukey)
209-
.filter_map(|runtime_spec| entries.get(&runtime_spec).copied())
209+
.flat_map(|runtime| runtime.iter())
210+
.filter_map(|runtime| entries.get(&RuntimeSpec::from_iter([*runtime])).copied())
210211
.collect::<Vec<_>>();
211212
for runtime_chunk in runtime_chunks {
212213
if !compilation

tests/rspack-test/watchCases/async-modules/top-level-await/0/case1/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ it("should have correct value", async () => {
88
expect(globalThis.case1).toBe(parseInt(WATCH_STEP, 10))
99
const content = await fs.promises.readFile(__filename, "utf-8")
1010
const result = WATCH_STEP === "1";
11-
debugger
1211
expect(isAsyncModule(content, "./case1/index.js")).toBe(result)
1312
expect(isAsyncModule(content, "./case1/a.js")).toBe(result)
1413
expect(isAsyncModule(content, "./case1/b.js")).toBe(result)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function Test(count) {
2+
return <div>{count}</div>;
3+
}
4+
5+
it("should not throw error", async () => {
6+
expect(Test(0).props.children).toBe(0);
7+
await import("./main");
8+
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function Test(count) {
2+
return <div>{count}</div>;
3+
}
4+
5+
it("should not throw error", async () => {
6+
expect(Test(0).props.children).toBe(0);
7+
await import("./main");
8+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default () => {
2+
const worker = new Worker(new URL("./worker", import.meta.url));
3+
worker.testName = `test worker ${WATCH_STEP}`;
4+
return worker;
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import getWorker from "./getWorker";
2+
3+
it("should have correct value", () => {
4+
expect(getWorker().testName).toBe("test worker 0");
5+
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const x = {};
2+
export { x };
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import getWorker from "./getWorker";
2+
3+
it("should rebuild successfully", () => {
4+
expect(getWorker().testName).toBe("test worker 1");
5+
})
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const rspack = require("@rspack/core");
2+
const ReactRefreshPlugin = require("@rspack/plugin-react-refresh");
3+
4+
/** @type {import("webpack").Configuration} */
5+
module.exports = {
6+
entry: {
7+
a: "./a.js",
8+
b: "./b.js",
9+
},
10+
output: {
11+
filename: "[name].js",
12+
},
13+
target: "web",
14+
optimization: {
15+
splitChunks: {
16+
chunks: "all",
17+
cacheGroups: {
18+
defaultVendors: {
19+
name: "vendors", // add name for defaultVendors, need a specific name to run it at test.config.js findBundle
20+
reuseExistingChunk: true,
21+
test: /[\\/]node_modules[\\/]/i,
22+
priority: -10
23+
}
24+
},
25+
}
26+
},
27+
module: {
28+
rules: [
29+
{
30+
test: /\.js/,
31+
use: [{
32+
loader: 'builtin:swc-loader',
33+
options: {
34+
jsc: {
35+
parser: {
36+
jsx: true,
37+
syntax: 'ecmascript'
38+
},
39+
transform: {
40+
react: {
41+
development: true,
42+
refresh: true,
43+
runtime: 'automatic'
44+
}
45+
},
46+
target: "es2022"
47+
},
48+
}
49+
}],
50+
}
51+
]
52+
},
53+
plugins: [
54+
new rspack.container.ModuleFederationPlugin({
55+
name: "test",
56+
shareStrategy: "loaded-first"
57+
}),
58+
new ReactRefreshPlugin() // Need this to trigger hoisting (hoist_container_references_plugin.rs)
59+
]
60+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
findBundle(i, config, step) {
3+
return ["vendors.js", "a.js", "b.js"]
4+
},
5+
moduleScope(scope) {
6+
scope.window.document.defaultView = scope.window;
7+
}
8+
};

0 commit comments

Comments
 (0)