diff --git a/packages/module-federation/src/utils/remotes.spec.ts b/packages/module-federation/src/utils/remotes.spec.ts new file mode 100644 index 0000000000000..f00acc0f537bd --- /dev/null +++ b/packages/module-federation/src/utils/remotes.spec.ts @@ -0,0 +1,58 @@ +import { mapRemotes } from './remotes'; + +describe('mapRemotes', () => { + it('should map string remotes', () => { + expect( + mapRemotes( + ['remote1', 'remote2', 'remote3'], + 'js', + (remote) => { + return `http://localhost:300${remote.at(-1)}/remoteEntry.js`; + }, + true + ) + ).toEqual({ + remote1: 'remote1@http://localhost:3001/remoteEntry.js', + remote2: 'remote2@http://localhost:3002/remoteEntry.js', + remote3: 'remote3@http://localhost:3003/remoteEntry.js', + }); + }); + + it('should map array remotes', () => { + expect( + mapRemotes( + [ + ['remote1', 'http://localhost:4201'], + ['remote2', 'http://localhost:4202'], + ['remote3', 'http://localhost:4203'], + ], + 'js', + (remote) => remote, + true + ) + ).toEqual({ + remote1: 'remote1@http://localhost:4201/remoteEntry.js', + remote2: 'remote2@http://localhost:4202/remoteEntry.js', + remote3: 'remote3@http://localhost:4203/remoteEntry.js', + }); + }); + + it('should map array remotes with path to remoteEntry', () => { + expect( + mapRemotes( + [ + ['remote1', 'http://localhost:4201/remoteEntry.js'], + ['remote2', 'http://localhost:4202/remoteEntry.mjs'], + ['remote3', 'http://localhost:4203/mf-manifest.json'], + ], + 'js', + (remote) => remote, + true + ) + ).toEqual({ + remote1: 'remote1@http://localhost:4201/remoteEntry.js', + remote2: 'remote2@http://localhost:4202/remoteEntry.mjs', + remote3: 'remote3@http://localhost:4203/mf-manifest.json', + }); + }); +}); diff --git a/packages/module-federation/src/utils/remotes.ts b/packages/module-federation/src/utils/remotes.ts index 8372a9fe054cf..fd77f92e5526e 100644 --- a/packages/module-federation/src/utils/remotes.ts +++ b/packages/module-federation/src/utils/remotes.ts @@ -51,6 +51,9 @@ function handleArrayRemote( // If remote location already has .js or .mjs extension if (['.js', '.mjs', '.json'].includes(remoteLocationExt)) { + if (isRemoteGlobal && !remoteLocation.startsWith(`${mfRemoteName}@`)) { + return `${mfRemoteName}@${remoteLocation}`; + } return remoteLocation; }