Skip to content

Commit 419edec

Browse files
authored
fix(mf): use dynamic exports type for MF modules (#12841)
1 parent d9c06e1 commit 419edec

File tree

8 files changed

+65
-12
lines changed

8 files changed

+65
-12
lines changed

crates/rspack_plugin_mf/src/container/remote_module.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use rspack_collections::{Identifiable, Identifier};
66
use rspack_core::{
77
AsyncDependenciesBlockIdentifier, BoxDependency, BuildContext, BuildInfo, BuildMeta, BuildResult,
88
ChunkGraph, CodeGenerationResult, Compilation, ConcatenationScope, Context, DependenciesBlock,
9-
Dependency, DependencyId, FactoryMeta, LibIdentOptions, Module, ModuleGraph, ModuleIdentifier,
10-
ModuleType, RuntimeSpec, SourceType, impl_module_meta_info, impl_source_map_config,
11-
module_update_hash,
9+
Dependency, DependencyId, ExportsType, FactoryMeta, LibIdentOptions, Module, ModuleGraph,
10+
ModuleIdentifier, ModuleType, RuntimeSpec, SourceType, impl_module_meta_info,
11+
impl_source_map_config, module_update_hash,
1212
rspack_sources::{BoxSource, RawStringSource, SourceExt},
1313
};
1414
use rspack_error::{Result, impl_empty_diagnosable_trait};
@@ -140,6 +140,15 @@ impl Module for RemoteModule {
140140
Some(self.request.as_str().into())
141141
}
142142

143+
fn get_exports_type(
144+
&self,
145+
_module_graph: &ModuleGraph,
146+
_module_graph_cache: &rspack_core::ModuleGraphCacheArtifact,
147+
_strict: bool,
148+
) -> ExportsType {
149+
ExportsType::Dynamic
150+
}
151+
143152
async fn build(
144153
&mut self,
145154
build_context: BuildContext,

crates/rspack_plugin_mf/src/sharing/consume_shared_module.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rspack_collections::{Identifiable, Identifier};
66
use rspack_core::{
77
AsyncDependenciesBlock, AsyncDependenciesBlockIdentifier, BoxDependency, BuildContext, BuildInfo,
88
BuildMeta, BuildResult, CodeGenerationResult, Compilation, ConcatenationScope, Context,
9-
DependenciesBlock, DependencyId, FactoryMeta, LibIdentOptions, Module, ModuleGraph,
9+
DependenciesBlock, DependencyId, ExportsType, FactoryMeta, LibIdentOptions, Module, ModuleGraph,
1010
ModuleIdentifier, ModuleType, RuntimeGlobals, RuntimeSpec, SourceType, impl_module_meta_info,
1111
impl_source_map_config, module_update_hash, rspack_sources::BoxSource,
1212
};
@@ -155,6 +155,15 @@ impl Module for ConsumeSharedModule {
155155
Some(Box::new(self.context.clone()))
156156
}
157157

158+
fn get_exports_type(
159+
&self,
160+
_module_graph: &ModuleGraph,
161+
_module_graph_cache: &rspack_core::ModuleGraphCacheArtifact,
162+
_strict: bool,
163+
) -> ExportsType {
164+
ExportsType::Dynamic
165+
}
166+
158167
async fn build(
159168
&mut self,
160169
_build_context: BuildContext,

tests/e2e/cases/module-federation/async-startup-self-remote-runtimechunk-single/rspack.config.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ module.exports = {
1313
filename: 'static/js/[name].js',
1414
chunkFilename: 'static/js/[name].js',
1515
},
16-
module: {
17-
rules: [
18-
{
19-
test: /\.js$/,
20-
type: 'javascript/auto',
21-
},
22-
],
23-
},
2416
optimization: {
2517
runtimeChunk: 'single',
2618
chunkIds: 'named',
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
it("should correctly handle default imports in .mjs files from shared modules", async () => {
2+
await __webpack_init_sharing__("default");
3+
4+
const { testDefaultImport } = await import("./pure-esm-consumer.mjs");
5+
const result = testDefaultImport();
6+
7+
expect(result.defaultType).toBe("function");
8+
expect(result.defaultValue).toBe("shared default export");
9+
expect(result.namedExportValue).toBe("shared named export");
10+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import something from "./shared-esm-pkg/index.js";
2+
import { namedExport } from "./shared-esm-pkg/index.js";
3+
4+
export function testDefaultImport() {
5+
return {
6+
defaultType: typeof something,
7+
defaultValue: typeof something === "function" ? something() : something,
8+
namedExportValue: namedExport
9+
};
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
const { rspack } = require("@rspack/core");
4+
5+
/** @type {import("@rspack/core").Configuration} */
6+
module.exports = {
7+
mode: "development",
8+
plugins: [
9+
new rspack.container.ModuleFederationPluginV1({
10+
shared: ["./shared-esm-pkg/index.js"]
11+
}),
12+
]
13+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function sharedFunction() {
2+
return "shared default export";
3+
}
4+
5+
export const namedExport = "shared named export";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "shared-esm-pkg",
3+
"version": "1.0.0",
4+
"type": "module"
5+
}

0 commit comments

Comments
 (0)