Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(module-federation): normalize kebab-cased names to snake_cased #28237

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, '_');
}