diff --git a/packages/native-federation-typescript/src/configurations/hostPlugin.test.ts b/packages/native-federation-typescript/src/configurations/hostPlugin.test.ts index 8fd1161dcdb..2b8bc72e6d0 100644 --- a/packages/native-federation-typescript/src/configurations/hostPlugin.test.ts +++ b/packages/native-federation-typescript/src/configurations/hostPlugin.test.ts @@ -21,78 +21,97 @@ describe('hostPlugin', () => { const invokeRetrieve = () => retrieveHostConfig({}); expect(invokeRetrieve).toThrowError('moduleFederationConfig is required'); }); + }); - describe('correctly intersect with default options', () => { - it('only moduleFederationConfig provided', () => { - const { hostOptions, mapRemotesToDownload } = retrieveHostConfig({ - moduleFederationConfig, - }); - - expect(hostOptions).toStrictEqual({ - moduleFederationConfig, - typesFolder: '@mf-types', - deleteTypesFolder: true, - maxRetries: 3, - }); - - expect(mapRemotesToDownload).toStrictEqual({ - moduleFederationTypescript: 'http://localhost:3000/@mf-types.zip', - }); + describe('correctly intersect with default options', () => { + it('only moduleFederationConfig provided', () => { + const { hostOptions, mapRemotesToDownload } = retrieveHostConfig({ + moduleFederationConfig, }); - it('all options provided', () => { - const options = { - moduleFederationConfig, - typesFolder: 'custom-types', - deleteTypesFolder: false, - maxRetries: 1, - }; - - const { hostOptions, mapRemotesToDownload } = - retrieveHostConfig(options); - - expect(hostOptions).toStrictEqual(options); + expect(hostOptions).toStrictEqual({ + moduleFederationConfig, + typesFolder: '@mf-types', + deleteTypesFolder: true, + maxRetries: 3, + }); - expect(mapRemotesToDownload).toStrictEqual({ - moduleFederationTypescript: 'http://localhost:3000/custom-types.zip', - }); + expect(mapRemotesToDownload).toStrictEqual({ + moduleFederationTypescript: 'http://localhost:3000/@mf-types.zip', }); }); - it('correctly resolve subpath remotes', () => { - const subpathModuleFederationConfig = { - ...moduleFederationConfig, - remotes: { - moduleFederationTypescript: - 'http://localhost:3000/subpatha/subpathb/remoteEntry.js', - }, + it('all options provided', () => { + const options = { + moduleFederationConfig, + typesFolder: 'custom-types', + deleteTypesFolder: false, + maxRetries: 1, }; - const { mapRemotesToDownload } = retrieveHostConfig({ - moduleFederationConfig: subpathModuleFederationConfig, - }); + const { hostOptions, mapRemotesToDownload } = retrieveHostConfig(options); + + expect(hostOptions).toStrictEqual(options); expect(mapRemotesToDownload).toStrictEqual({ - moduleFederationTypescript: - 'http://localhost:3000/subpatha/subpathb/@mf-types.zip', + moduleFederationTypescript: 'http://localhost:3000/custom-types.zip', }); }); + }); + + it('correctly resolve subpath remotes', () => { + const subpathModuleFederationConfig = { + ...moduleFederationConfig, + remotes: { + moduleFederationTypescript: + 'http://localhost:3000/subpatha/subpathb/remoteEntry.js', + }, + }; + + const { mapRemotesToDownload } = retrieveHostConfig({ + moduleFederationConfig: subpathModuleFederationConfig, + }); - it('correctly resolve remotes with relative reference in place of absolute url', () => { - const subpathModuleFederationConfig = { - ...moduleFederationConfig, - remotes: { - moduleFederationTypescript: '/subpatha/remoteEntry.js', + expect(mapRemotesToDownload).toStrictEqual({ + moduleFederationTypescript: + 'http://localhost:3000/subpatha/subpathb/@mf-types.zip', + }); + }); + + it('correctly resolve remotes with relative reference in place of absolute url', () => { + const subpathModuleFederationConfig = { + ...moduleFederationConfig, + remotes: { + moduleFederationTypescript: '/subpatha/remoteEntry.js', + }, + }; + + const { mapRemotesToDownload } = retrieveHostConfig({ + moduleFederationConfig: subpathModuleFederationConfig, + }); + + expect(mapRemotesToDownload).toStrictEqual({ + moduleFederationTypescript: '/subpatha/@mf-types.zip', + }); + }); + + it('correctly resolve remotes with object entry', () => { + const subpathModuleFederationConfig = { + ...moduleFederationConfig, + remotes: { + moduleFederationTypescript: { + entry: 'http://localhost:3000/subpatha/subpathb/remoteEntry.js', }, - }; + }, + }; - const { mapRemotesToDownload } = retrieveHostConfig({ - moduleFederationConfig: subpathModuleFederationConfig, - }); + const { mapRemotesToDownload } = retrieveHostConfig({ + moduleFederationConfig: subpathModuleFederationConfig, + }); - expect(mapRemotesToDownload).toStrictEqual({ - moduleFederationTypescript: '/subpatha/@mf-types.zip', - }); + expect(mapRemotesToDownload).toStrictEqual({ + moduleFederationTypescript: + 'http://localhost:3000/subpatha/subpathb/@mf-types.zip', }); }); }); diff --git a/packages/native-federation-typescript/src/configurations/hostPlugin.ts b/packages/native-federation-typescript/src/configurations/hostPlugin.ts index 8ebe70acc04..584c13d4253 100644 --- a/packages/native-federation-typescript/src/configurations/hostPlugin.ts +++ b/packages/native-federation-typescript/src/configurations/hostPlugin.ts @@ -6,9 +6,18 @@ const defaultOptions = { maxRetries: 3, } satisfies Partial; -const retrieveRemoteStringUrl = (remote: string) => { - const splittedRemote = remote.split('@'); - return splittedRemote[splittedRemote.length - 1]; +const retrieveRemoteStringUrl = (remote: string | Record) => { + if (typeof remote === 'string') { + const splittedRemote = remote.split('@'); + return splittedRemote[splittedRemote.length - 1]; + } + + if (typeof remote === 'object' && 'entry' in remote) { + const splittedRemote = remote.entry.split('@'); + return splittedRemote[splittedRemote.length - 1]; + } + + throw new Error(`Invalid type of remote: ${typeof remote}`); }; const FILE_PROTOCOL = 'file:';