diff --git a/packages/angular/src/utils/mf/with-module-federation-ssr.ts b/packages/angular/src/utils/mf/with-module-federation-ssr.ts index e017d13458834..e5918a9d01934 100644 --- a/packages/angular/src/utils/mf/with-module-federation-ssr.ts +++ b/packages/angular/src/utils/mf/with-module-federation-ssr.ts @@ -40,7 +40,7 @@ export async function withModuleFederationForSSR( ...(config.plugins ?? []), new (require('@module-federation/enhanced').ModuleFederationPlugin)( { - name: options.name, + name: options.name.replace(/-/g, '_'), filename: 'remoteEntry.js', exposes: options.exposes, remotes: mappedRemotes, diff --git a/packages/angular/src/utils/mf/with-module-federation.ts b/packages/angular/src/utils/mf/with-module-federation.ts index 8bdb757a92008..1845d6ac8893f 100644 --- a/packages/angular/src/utils/mf/with-module-federation.ts +++ b/packages/angular/src/utils/mf/with-module-federation.ts @@ -42,7 +42,7 @@ export async function withModuleFederation( plugins: [ ...(config.plugins ?? []), new ModuleFederationPlugin({ - name: options.name, + name: options.name.replace(/-/g, '_'), filename: 'remoteEntry.mjs', exposes: options.exposes, remotes: mappedRemotes, diff --git a/packages/react/src/module-federation/with-module-federation-ssr.ts b/packages/react/src/module-federation/with-module-federation-ssr.ts index a396f28954e14..a852b93b36843 100644 --- a/packages/react/src/module-federation/with-module-federation-ssr.ts +++ b/packages/react/src/module-federation/with-module-federation-ssr.ts @@ -28,7 +28,7 @@ export async function withModuleFederationForSSR( config.plugins.push( new (require('@module-federation/enhanced').ModuleFederationPlugin)( { - name: options.name, + name: options.name.replace(/-/g, '_'), filename: 'remoteEntry.js', exposes: options.exposes, remotes: mappedRemotes, diff --git a/packages/react/src/module-federation/with-module-federation.ts b/packages/react/src/module-federation/with-module-federation.ts index 6ca1a841009b1..885da9165eb1f 100644 --- a/packages/react/src/module-federation/with-module-federation.ts +++ b/packages/react/src/module-federation/with-module-federation.ts @@ -41,7 +41,7 @@ export async function withModuleFederation( config.plugins.push( new ModuleFederationPlugin({ - name: options.name, + name: options.name.replace(/-/g, '_'), filename: 'remoteEntry.js', exposes: options.exposes, remotes: mappedRemotes, diff --git a/packages/rspack/src/utils/module-federation/remotes.ts b/packages/rspack/src/utils/module-federation/remotes.ts index de98778fb9754..8c000da93f0be 100644 --- a/packages/rspack/src/utils/module-federation/remotes.ts +++ b/packages/rspack/src/utils/module-federation/remotes.ts @@ -18,7 +18,8 @@ export function mapRemotes( for (const remote of remotes) { if (Array.isArray(remote)) { - mappedRemotes[remote[0]] = handleArrayRemote(remote, remoteEntryExt); + const remoteName = normalizeRemoteName(remote[0]); + mappedRemotes[remoteName] = handleArrayRemote(remote, remoteEntryExt); } else if (typeof remote === 'string') { mappedRemotes[remote] = handleStringRemote(remote, determineRemoteUrl); } @@ -32,7 +33,8 @@ function handleArrayRemote( remote: [string, string], remoteEntryExt: 'js' | 'mjs' ): string { - const [remoteName, remoteLocation] = remote; + let [remoteName, remoteLocation] = remote; + remoteName = normalizeRemoteName(remoteName); const remoteLocationExt = extname(remoteLocation); // If remote location already has .js or .mjs extension @@ -82,7 +84,8 @@ export function mapRemotesForSSR( for (const remote of remotes) { if (Array.isArray(remote)) { - const [remoteName, remoteLocation] = remote; + let [remoteName, remoteLocation] = remote; + remoteName = normalizeRemoteName(remoteName); const remoteLocationExt = extname(remoteLocation); mappedRemotes[remoteName] = `${remoteName}@${ ['.js', '.mjs'].includes(remoteLocationExt) @@ -94,9 +97,14 @@ export function mapRemotesForSSR( }/remoteEntry.${remoteEntryExt}` }`; } else if (typeof remote === 'string') { - mappedRemotes[remote] = `${remote}@${determineRemoteUrl(remote)}`; + const remoteName = normalizeRemoteName(remote); + mappedRemotes[remoteName] = `${remoteName}@${determineRemoteUrl(remote)}`; } } return mappedRemotes; } + +function normalizeRemoteName(remote: string): string { + return remote.replace(/-/g, '_'); +} diff --git a/packages/rspack/src/utils/module-federation/with-module-federation/with-module-federation-ssr.ts b/packages/rspack/src/utils/module-federation/with-module-federation/with-module-federation-ssr.ts index b6fa214ae4fce..492aa612a3305 100644 --- a/packages/rspack/src/utils/module-federation/with-module-federation/with-module-federation-ssr.ts +++ b/packages/rspack/src/utils/module-federation/with-module-federation/with-module-federation-ssr.ts @@ -31,7 +31,7 @@ export async function withModuleFederationForSSR( // eslint-disable-next-line @typescript-eslint/no-var-requires new (require('@module-federation/enhanced/rspack').ModuleFederationPlugin)( { - name: options.name, + name: options.name.replace(/-/g, '_'), filename: 'remoteEntry.js', exposes: options.exposes, remotes: mappedRemotes, diff --git a/packages/rspack/src/utils/module-federation/with-module-federation/with-module-federation.ts b/packages/rspack/src/utils/module-federation/with-module-federation/with-module-federation.ts index 7e3245a76652b..7f43b02c63cd4 100644 --- a/packages/rspack/src/utils/module-federation/with-module-federation/with-module-federation.ts +++ b/packages/rspack/src/utils/module-federation/with-module-federation/with-module-federation.ts @@ -55,7 +55,7 @@ export async function withModuleFederation( config.plugins.push( new ModuleFederationPlugin({ - name: options.name, + name: options.name.replace(/-/g, '_'), filename: 'remoteEntry.js', exposes: options.exposes, remotes: mappedRemotes, diff --git a/packages/webpack/src/utils/module-federation/remotes.ts b/packages/webpack/src/utils/module-federation/remotes.ts index ad29d5d3bc587..1651d37cd1245 100644 --- a/packages/webpack/src/utils/module-federation/remotes.ts +++ b/packages/webpack/src/utils/module-federation/remotes.ts @@ -19,14 +19,16 @@ export function mapRemotes( for (const remote of remotes) { if (Array.isArray(remote)) { - mappedRemotes[remote[0]] = handleArrayRemote( + const remoteName = normalizeRemoteName(remote[0]); + mappedRemotes[remoteName] = handleArrayRemote( remote, remoteEntryExt, isRemoteGlobal ); } else if (typeof remote === 'string') { - mappedRemotes[remote] = handleStringRemote( - remote, + const remoteName = normalizeRemoteName(remote); + mappedRemotes[remoteName] = handleStringRemote( + remoteName, determineRemoteUrl, isRemoteGlobal ); @@ -42,7 +44,8 @@ function handleArrayRemote( remoteEntryExt: 'js' | 'mjs', isRemoteGlobal: boolean ): string { - const [remoteName, remoteLocation] = remote; + let [remoteName, remoteLocation] = remote; + remoteName = normalizeRemoteName(remoteName); const remoteLocationExt = extname(remoteLocation); // If remote location already has .js or .mjs extension @@ -95,7 +98,8 @@ export function mapRemotesForSSR( for (const remote of remotes) { if (Array.isArray(remote)) { - const [remoteName, remoteLocation] = remote; + let [remoteName, remoteLocation] = remote; + remoteName = normalizeRemoteName(remoteName); const remoteLocationExt = extname(remoteLocation); mappedRemotes[remoteName] = `${remoteName}@${ ['.js', '.mjs'].includes(remoteLocationExt) @@ -107,9 +111,14 @@ export function mapRemotesForSSR( }/remoteEntry.${remoteEntryExt}` }`; } else if (typeof remote === 'string') { - mappedRemotes[remote] = `${remote}@${determineRemoteUrl(remote)}`; + const remoteName = normalizeRemoteName(remote); + mappedRemotes[remoteName] = `${remoteName}@${determineRemoteUrl(remote)}`; } } return mappedRemotes; } + +function normalizeRemoteName(remote: string) { + return remote.replace(/-/g, '_'); +}