|
| 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