Skip to content

Commit

Permalink
fix(module-federation): normalize kebab-cased names to snake_cased
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Oct 2, 2024
1 parent b82ca22 commit 639498a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/utils/mf/with-module-federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 12 additions & 4 deletions packages/rspack/src/utils/module-federation/remotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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, '_');
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 15 additions & 6 deletions packages/webpack/src/utils/module-federation/remotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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, '_');
}

0 comments on commit 639498a

Please sign in to comment.