Skip to content

Commit c9dab5d

Browse files
giaBaoJSdannyhw
andauthored
fix: stop DevelopmentPlugin crashing on official MF plugins (#1471)
Co-authored-by: Daniel Williams <dannyhyunsoowilliams@gmail.com>
1 parent a87b592 commit c9dab5d

3 files changed

Lines changed: 85 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@callstack/repack": patch
3+
---
4+
5+
Fix `DevelopmentPlugin` crashing with `Cannot read properties of undefined (reading 'exposes')` when an official Module Federation plugin (from `@module-federation/enhanced`, or the built-in `container.ModuleFederationPlugin` of Rspack or webpack) is used with the dev server. The check for official plugins read `exposes` from `plugin.config`, which only Re.Pack's own Module Federation plugins have, instead of `plugin._options`.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class DevelopmentPlugin {
6565
}
6666

6767
// official MF plugins expose _options property
68-
if ('_options' in plugin && plugin.config.exposes) {
68+
if ('_options' in plugin && plugin._options.exposes) {
6969
return plugin._options.name;
7070
}
7171

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { ModuleFederationPlugin as MFPluginRspack } from '@module-federation/enhanced/rspack';
2+
import type { Compiler, EntryNormalized } from '@rspack/core';
3+
import { DevelopmentPlugin } from '../DevelopmentPlugin.js';
4+
import { ModuleFederationPluginV2 } from '../ModuleFederationPluginV2.js';
5+
6+
const createMockCompiler = (plugins: unknown[]) => {
7+
const EntryPlugin = jest.fn(() => ({ apply: jest.fn() }));
8+
let onEntryOption: (context: string, entry: EntryNormalized) => void =
9+
() => {};
10+
11+
const compiler = {
12+
context: __dirname,
13+
options: {
14+
devServer: { hot: true, host: 'localhost', port: 8081 },
15+
output: { uniqueName: 'test' },
16+
resolve: { alias: {} },
17+
module: { rules: [] },
18+
plugins,
19+
},
20+
hooks: {
21+
entryOption: {
22+
tap: (_: unknown, callback: typeof onEntryOption) => {
23+
onEntryOption = callback;
24+
},
25+
},
26+
},
27+
webpack: {
28+
rspackVersion: '1.0.0',
29+
DefinePlugin: jest.fn(() => ({ apply: jest.fn() })),
30+
HotModuleReplacementPlugin: jest.fn(() => ({ apply: jest.fn() })),
31+
ProvidePlugin: jest.fn(() => ({ apply: jest.fn() })),
32+
Template: { toIdentifier: (name: string) => name },
33+
EntryPlugin,
34+
},
35+
} as unknown as Compiler;
36+
37+
return {
38+
compiler,
39+
getEntryNames: () => {
40+
onEntryOption(__dirname, { main: { import: ['./index.js'] } });
41+
const calls = EntryPlugin.mock.calls as unknown as Array<
42+
[string, string, { name: string }]
43+
>;
44+
return [...new Set(calls.map(([, , options]) => options.name))];
45+
},
46+
};
47+
};
48+
49+
describe('DevelopmentPlugin', () => {
50+
it('should add development entries to the container of an official MF plugin with exposes', () => {
51+
const { compiler, getEntryNames } = createMockCompiler([
52+
new MFPluginRspack({ name: 'remote', exposes: { './App': './App.js' } }),
53+
]);
54+
new DevelopmentPlugin({ platform: 'ios' }).apply(compiler);
55+
56+
expect(getEntryNames()).toEqual(['main', 'remote']);
57+
});
58+
59+
it('should skip an official MF plugin without exposes', () => {
60+
const { compiler, getEntryNames } = createMockCompiler([
61+
new MFPluginRspack({ name: 'host' }),
62+
]);
63+
new DevelopmentPlugin({ platform: 'ios' }).apply(compiler);
64+
65+
expect(getEntryNames()).toEqual(['main']);
66+
});
67+
68+
it('should add development entries to the container of a Re.Pack MF plugin with exposes', () => {
69+
const { compiler, getEntryNames } = createMockCompiler([
70+
new ModuleFederationPluginV2({
71+
name: 'remote',
72+
exposes: { './App': './App.js' },
73+
}),
74+
]);
75+
new DevelopmentPlugin({ platform: 'ios' }).apply(compiler);
76+
77+
expect(getEntryNames()).toEqual(['main', 'remote']);
78+
});
79+
});

0 commit comments

Comments
 (0)