Skip to content

Commit 12c0eeb

Browse files
committed
fix: fix used exports for global entry that inject to async entrypoints (#12883)
1 parent 1cb57fa commit 12c0eeb

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

crates/rspack_core/src/module_graph/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,10 @@ impl ModuleGraph {
533533
.expect("should insert block before get it")
534534
}
535535

536+
pub fn blocks(&self) -> &HashMap<AsyncDependenciesBlockIdentifier, Box<AsyncDependenciesBlock>> {
537+
&self.inner.blocks
538+
}
539+
536540
pub fn dependencies(&self) -> HashMap<DependencyId, &BoxDependency> {
537541
self
538542
.inner
@@ -550,7 +554,7 @@ impl ModuleGraph {
550554
///
551555
/// **PREFERRED METHOD**: Use this for ALL internal Rust code including:
552556
/// - Core compilation logic
553-
/// - All plugins (`rspack_plugin_*`)
557+
/// - All plugins (`rspack_plugin_*`)
554558
/// - Stats generation, code generation, runtime templates
555559
/// - Chunk graph building, export analysis
556560
/// - Module graph building operations

crates/rspack_plugin_javascript/src/plugin/flag_dependency_usage_plugin.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,21 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
6565
let mut q = Queue::new();
6666
let mg = &mut module_graph;
6767

68+
let mut global_runtime: Option<RuntimeSpec> = if self.global {
69+
None
70+
} else {
71+
let mut global_runtime = RuntimeSpec::default();
72+
for block in module_graph.blocks().values() {
73+
if let Some(GroupOptions::Entrypoint(options)) = block.get_group_options()
74+
&& let Some(runtime) = RuntimeSpec::from_entry_options(options)
75+
{
76+
global_runtime.extend(&runtime);
77+
}
78+
}
79+
Some(global_runtime)
80+
};
6881
// SAFETY: we can make sure that entries will not be used other place at the same time,
6982
// this take is aiming to avoid use self ref and mut ref at the same time;
70-
let mut global_runtime: Option<RuntimeSpec> = None;
7183
let entries = &self.compilation.entries;
7284
for (entry_name, entry) in entries.iter() {
7385
let runtime = if self.global {

tests/rspack-test/configCases/container-1-5/runtime-plugin-with-used-exports/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ it("should generate correct worker runtime code with tree shaking and MF runtime
33
expect(getMessage()).toBe('App rendered with [This is react 0.2.1]');
44

55
const plugins = __webpack_require__.federation.initOptions.plugins;
6-
expect(plugins.length).toBeGreaterThan(0);
7-
expect(plugins.some(p => p.name === 'my-runtime-plugin')).toBe(true);
6+
expect(plugins.length).toBe(2);
7+
expect(plugins.map(p => p.name)).toEqual(['my-runtime-plugin', 'my-runtime-plugin-esm']);
88

99
expect(await getWorkerMessage()).toBe('Echo: Hello, Rspack!');
1010
});

tests/rspack-test/configCases/container-1-5/runtime-plugin-with-used-exports/rspack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ module.exports = {
2222
}
2323
},
2424
runtimePlugins: [
25-
path.resolve(__dirname, "runtime-plugin.js")
25+
path.resolve(__dirname, "runtime-plugin.js"),
26+
path.resolve(__dirname, "runtime-plugin-esm.js")
2627
]
2728
})
2829
]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default function MyRuntimePlugin() {
2+
return {
3+
name: 'my-runtime-plugin-esm',
4+
resolveShare: function(args) {
5+
const { shareScopeMap, scope, pkgName, version, GlobalFederation } = args;
6+
args.resolver = function () {
7+
shareScopeMap[scope][pkgName][version] = {
8+
lib: ()=>()=> 'This is react 0.2.1'
9+
};
10+
return shareScopeMap[scope][pkgName][version];
11+
};
12+
return args;
13+
}
14+
};
15+
}

0 commit comments

Comments
 (0)