diff --git a/.changeset/happy-chefs-pay.md b/.changeset/happy-chefs-pay.md new file mode 100644 index 0000000000..bfe665d12c --- /dev/null +++ b/.changeset/happy-chefs-pay.md @@ -0,0 +1,5 @@ +--- +'@module-federation/rsbuild-plugin': patch +--- + +fix: configure the default assetPrefix for MF apps correctly diff --git a/packages/rsbuild-plugin/src/cli/index.ts b/packages/rsbuild-plugin/src/cli/index.ts index e00834eb62..af9fbf2714 100644 --- a/packages/rsbuild-plugin/src/cli/index.ts +++ b/packages/rsbuild-plugin/src/cli/index.ts @@ -2,7 +2,7 @@ import { parseOptions } from '@module-federation/enhanced'; import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack'; import { isRequiredVersion } from '@module-federation/sdk'; -import { isRegExp } from '../utils/index'; +import { isRegExp, DEFAULT_ASSET_PREFIX } from '../utils/index'; import pkgJson from '../../package.json'; import type { @@ -44,16 +44,26 @@ export const pluginModuleFederation = ( ); api.modifyRsbuildConfig((config) => { - // If this is a provider app, Rsbuild should send the ws request to the provider's dev server. - // This allows the provider to do HMR when the provider module is loaded in the consumer's page. - if ( - moduleFederationOptions.exposes && - config.server?.port && - !config.dev?.client?.port - ) { + // Change some default configs for remote modules + if (moduleFederationOptions.exposes) { config.dev ||= {}; - config.dev.client ||= {}; - config.dev.client.port = config.server.port; + + // For remote modules, Rsbuild should send the ws request to the provider's dev server. + // This allows the provider to do HMR when the provider module is loaded in the consumer's page. + if (config.server?.port && !config.dev.client?.port) { + config.dev.client ||= {}; + config.dev.client.port = config.server.port; + } + + // Change the default assetPrefix to `true` for remote modules. + // This ensures that the remote module's assets can be requested by consumer apps with the correct URL. + const originalConfig = api.getRsbuildConfig('original'); + if ( + originalConfig.dev?.assetPrefix === undefined && + config.dev.assetPrefix === DEFAULT_ASSET_PREFIX + ) { + config.dev.assetPrefix = true; + } } }); @@ -147,12 +157,6 @@ export const pluginModuleFederation = ( if (!chain.output.get('uniqueName')) { chain.output.set('uniqueName', moduleFederationOptions.name); } - - const publicPath = chain.output.get('publicPath'); - // set the default publicPath to 'auto' to make MF work - if (publicPath === '/') { - chain.output.set('publicPath', 'auto'); - } }); }, }); diff --git a/packages/rsbuild-plugin/src/utils/constant.ts b/packages/rsbuild-plugin/src/utils/constant.ts new file mode 100644 index 0000000000..b1755235b2 --- /dev/null +++ b/packages/rsbuild-plugin/src/utils/constant.ts @@ -0,0 +1 @@ +export const DEFAULT_ASSET_PREFIX = '/'; diff --git a/packages/rsbuild-plugin/src/utils/index.ts b/packages/rsbuild-plugin/src/utils/index.ts index c28eb2d3aa..fcf940ac8e 100644 --- a/packages/rsbuild-plugin/src/utils/index.ts +++ b/packages/rsbuild-plugin/src/utils/index.ts @@ -3,3 +3,5 @@ import util from 'util'; export function isRegExp(target: any) { return util.types.isRegExp(target); } + +export * from './constant';