Skip to content

Commit 5bf7f8f

Browse files
committed
fix(enhanced): resolve runtime plugin package names
1 parent 45b01e1 commit 5bf7f8f

3 files changed

Lines changed: 60 additions & 4 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@module-federation/enhanced": patch
3+
---
4+
5+
Preserve package-name entries in `runtimePlugins` so the bundler can resolve
6+
their package exports with the appropriate conditions.

packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,39 @@ type RuntimeEntrySpec = {
3939
cjs: string;
4040
};
4141

42+
function isLocalRuntimePluginPath(
43+
runtimePluginEntry: string,
44+
cwd = process.cwd(),
45+
) {
46+
const isRelativePath =
47+
runtimePluginEntry.startsWith('./') ||
48+
runtimePluginEntry.startsWith('../') ||
49+
runtimePluginEntry.startsWith('.\\') ||
50+
runtimePluginEntry.startsWith('..\\');
51+
52+
return (
53+
path.isAbsolute(runtimePluginEntry) ||
54+
isRelativePath ||
55+
fs.existsSync(path.resolve(cwd, runtimePluginEntry))
56+
);
57+
}
58+
59+
function resolveRuntimePluginPath(
60+
runtimePluginEntry: string,
61+
cwd = process.cwd(),
62+
resolve: ResolveFn = require.resolve,
63+
) {
64+
if (path.isAbsolute(runtimePluginEntry)) {
65+
return runtimePluginEntry;
66+
}
67+
68+
try {
69+
return resolve(runtimePluginEntry, { paths: [cwd] });
70+
} catch {
71+
return path.resolve(cwd, runtimePluginEntry);
72+
}
73+
}
74+
4275
function resolveRuntimeEntry(
4376
spec: RuntimeEntrySpec,
4477
implementation: string | undefined,
@@ -169,9 +202,9 @@ class FederationRuntimePlugin {
169202
? runtimePlugin[0]
170203
: runtimePlugin;
171204
const runtimePluginPath = normalizeToPosixPath(
172-
path.isAbsolute(runtimePluginEntry)
173-
? runtimePluginEntry
174-
: path.join(process.cwd(), runtimePluginEntry),
205+
isLocalRuntimePluginPath(runtimePluginEntry)
206+
? resolveRuntimePluginPath(runtimePluginEntry)
207+
: runtimePluginEntry,
175208
);
176209
const paramsStr =
177210
Array.isArray(runtimePlugin) && runtimePlugin.length > 1

packages/enhanced/test/unit/container/FederationRuntimePlugin.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ describe('FederationRuntimePlugin runtimePluginCalls', () => {
140140
it('should handle relative paths in runtimePlugins', () => {
141141
const optionsWithRelativePlugins = {
142142
...mockOptions,
143-
runtimePlugins: ['relative/path/plugin1.js'],
143+
runtimePlugins: ['./relative/path/plugin1.js'],
144144
};
145145

146146
const template = FederationRuntimePlugin.getTemplate(
@@ -156,6 +156,23 @@ describe('FederationRuntimePlugin runtimePluginCalls', () => {
156156
);
157157
});
158158

159+
it('should preserve package names for Webpack to resolve exports conditions', () => {
160+
const template = FederationRuntimePlugin.getTemplate(
161+
compiler as Compiler,
162+
{
163+
...mockOptions,
164+
runtimePlugins: [
165+
'@module-federation/inject-external-runtime-core-plugin',
166+
],
167+
},
168+
'bundler-runtime.js',
169+
);
170+
171+
expect(template).toContain(
172+
"from '@module-federation/inject-external-runtime-core-plugin'",
173+
);
174+
});
175+
159176
it('should filter out false plugins in runtimePluginCalls', () => {
160177
const optionsWithFalsyPlugins = {
161178
...mockOptions,

0 commit comments

Comments
 (0)