Skip to content

Commit 253d14b

Browse files
whydidoodannyhw
andauthored
fix(repack): support Module Federation 2.x runtime plugins (#1484)
Co-authored-by: Daniel Williams <me@dannyhw.com> Co-authored-by: Daniel Williams <dannyhyunsoowilliams@gmail.com>
1 parent 93ba2dc commit 253d14b

9 files changed

Lines changed: 76 additions & 258 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@callstack/repack": patch
3+
---
4+
5+
Keep the options of Module Federation 2.x runtime plugins passed to `ModuleFederationPluginV2` as `[path, options]` in `runtimePlugins`. Such entries were silently dropped before.
6+
7+
Fix the typings of the `@callstack/repack/mf/*` runtime plugins with `@module-federation/enhanced` 0.17 and newer, where `FederationRuntimePlugin` and `FederationHost` were renamed to `ModuleFederationRuntimePlugin` and `ModuleFederation`. The types are now derived from the installed runtime, so they work with every supported version.

‎packages/repack/package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@
113113
"@babel/core": "^7.29.7",
114114
"@babel/plugin-transform-export-namespace-from": "^7.29.7",
115115
"@babel/plugin-transform-modules-commonjs": "^7.29.7",
116-
"@module-federation/enhanced": "0.8.9",
117-
"@module-federation/sdk": "0.6.10",
116+
"@module-federation/enhanced": "2.9.2",
117+
"@module-federation/sdk": "2.9.2",
118118
"@rspack/core": "catalog:",
119119
"@swc/helpers": "catalog:",
120120
"@types/babel__core": "^7.20.5",

‎packages/repack/src/modules/FederationRuntimePlugins/CorePlugin.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
21
import type * as RepackClient from '../ScriptManager/index.js';
2+
import type { MFRuntimePlugin } from './types.js';
33

4-
const RepackCorePlugin: () => FederationRuntimePlugin = () => ({
4+
const RepackCorePlugin: () => MFRuntimePlugin = () => ({
55
name: 'repack-core-plugin',
66
loadEntry: async ({ remoteInfo }) => {
77
const client = require('../ScriptManager/index.js') as typeof RepackClient;

‎packages/repack/src/modules/FederationRuntimePlugins/PrefetchPlugin.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
21
import type * as RepackClient from '../ScriptManager/index.js';
2+
import type { MFRuntimePlugin } from './types.js';
33

44
interface PrefetchAsset {
55
name: string;
@@ -32,7 +32,7 @@ function prefetchAsset(asset: PrefetchAsset) {
3232
);
3333
}
3434

35-
const RepackPrefetchPlugin: () => FederationRuntimePlugin = () => ({
35+
const RepackPrefetchPlugin: () => MFRuntimePlugin = () => ({
3636
name: 'repack-prefetch-plugin',
3737
generatePreloadAssets: async (args) => {
3838
const preloadConfig = args.preloadOptions.preloadConfig;

‎packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import type {
2-
FederationHost,
3-
FederationRuntimePlugin,
4-
} from '@module-federation/enhanced/runtime';
51
import type { ScriptLocator } from '../ScriptManager/index.js';
2+
import type { MFInstance, MFRuntimePlugin } from './types.js';
63

7-
type MFRemote = Parameters<FederationHost['registerRemotes']>[0][0];
4+
type MFRemote = Parameters<MFInstance['registerRemotes']>[0][0];
85

96
export type RepackResolverPluginConfiguration =
107
| Omit<ScriptLocator, 'url'>
@@ -87,7 +84,7 @@ const registerResolver = async (
8784

8885
const RepackResolverPlugin: (
8986
config?: RepackResolverPluginConfiguration
90-
) => FederationRuntimePlugin = (config) => ({
87+
) => MFRuntimePlugin = (config) => ({
9188
name: 'repack-resolver-plugin',
9289
registerRemote: (args) => {
9390
registerResolver(args.remote, config);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { init } from '@module-federation/enhanced/runtime';
2+
3+
// Module Federation 0.17 renamed FederationHost and FederationRuntimePlugin to
4+
// ModuleFederation and ModuleFederationRuntimePlugin. Deriving both from `init`
5+
// keeps the published typings working with every supported version.
6+
export type MFInstance = ReturnType<typeof init>;
7+
8+
export type MFRuntimePlugin = NonNullable<
9+
Parameters<typeof init>[0]['plugins']
10+
>[number];

‎packages/repack/src/plugins/ModuleFederationPluginV2.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ export interface ModuleFederationPluginV2Config
3232
reactNativeDeepImports?: boolean;
3333
}
3434

35+
// a runtime plugin path, or [path, options] (Module Federation 2.x)
36+
type RuntimePlugin = NonNullable<
37+
MF.ModuleFederationPluginOptions['runtimePlugins']
38+
>[number];
39+
40+
const runtimePluginPath = (plugin: RuntimePlugin) =>
41+
typeof plugin === 'string' ? plugin : plugin[0];
42+
3543
/**
3644
* Webpack plugin to configure Module Federation with platform differences
3745
* handled under the hood.
@@ -140,23 +148,28 @@ export class ModuleFederationPluginV2 {
140148

141149
private adaptRuntimePlugins(
142150
context: string,
143-
runtimePlugins: string[] | undefined = []
151+
runtimePlugins: RuntimePlugin[] | undefined = []
144152
) {
145153
const plugins = runtimePlugins
146-
.map((pluginPath) => {
154+
.map((plugin) => {
147155
try {
148156
// resolve the paths to compare against absolute paths
149-
return require.resolve(pluginPath, { paths: [context] });
157+
const pluginPath = require.resolve(runtimePluginPath(plugin), {
158+
paths: [context],
159+
});
160+
if (typeof plugin === 'string') return pluginPath;
161+
const entry: RuntimePlugin = [pluginPath, plugin[1]];
162+
return entry;
150163
} catch {
151164
// ignore invalid paths
152165
return undefined;
153166
}
154167
})
155-
.filter((pluginPath) => !!pluginPath) as string[];
168+
.filter((plugin): plugin is RuntimePlugin => !!plugin);
156169

157170
for (const plugin of this.defaultRuntimePlugins) {
158171
const pluginPath = require.resolve(plugin);
159-
if (!plugins.includes(pluginPath)) {
172+
if (!plugins.some((entry) => runtimePluginPath(entry) === pluginPath)) {
160173
plugins.unshift(pluginPath);
161174
}
162175
}

‎packages/repack/src/plugins/__tests__/ModuleFederationPluginV2.test.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ type CompilerWarning = Error & {
99
};
1010
};
1111

12-
jest.mock('@module-federation/enhanced/rspack');
12+
jest.mock('@module-federation/enhanced/rspack', () => ({
13+
// 2.x's export shape can't be automocked into a constructor
14+
ModuleFederationPlugin: jest.fn(() => ({ apply: jest.fn() })),
15+
}));
1316

1417
const mockCompiler = {
1518
context: __dirname,
@@ -212,6 +215,22 @@ describe('ModuleFederationPlugin', () => {
212215
expect(config.runtimePlugins).toHaveLength(3);
213216
});
214217

218+
it('should keep options of runtime plugins passed as [path, options]', () => {
219+
new ModuleFederationPluginV2({
220+
name: 'test',
221+
runtimePlugins: [['@callstack/repack/mf/resolver-plugin', { retry: 2 }]],
222+
}).apply(mockCompiler);
223+
224+
const config = mockPlugin.mock.calls[0][0];
225+
expect(config.runtimePlugins).toContainEqual([
226+
resolverPluginPath,
227+
{ retry: 2 },
228+
]);
229+
expect(config.runtimePlugins).toContain(corePluginPath);
230+
expect(config.runtimePlugins).toContain(prefetchPluginPath);
231+
expect(config.runtimePlugins).toHaveLength(3);
232+
});
233+
215234
it('should use loaded-first as default shareStrategy', () => {
216235
new ModuleFederationPluginV2({ name: 'test' }).apply(mockCompiler);
217236

0 commit comments

Comments
 (0)