diff --git a/packages/core/src/cli/mf.ts b/packages/core/src/cli/mf.ts index be87ae68d..d2bfcc46c 100644 --- a/packages/core/src/cli/mf.ts +++ b/packages/core/src/cli/mf.ts @@ -25,8 +25,15 @@ async function initMFRsbuild( } mfRsbuildConfig.config = changeEnvToDev(mfRsbuildConfig.config); + const rsbuildInstance = await createRsbuild({ - rsbuildConfig: mfRsbuildConfig.config, + rsbuildConfig: { + ...mfRsbuildConfig.config, + plugins: [ + ...(rslibConfig.plugins || []), + ...(mfRsbuildConfig.config.plugins || []), + ], + }, }); const devServer = await rsbuildInstance.startDevServer(); diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 33b8d2c1d..48df97cbe 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -4,6 +4,7 @@ import { type EnvironmentConfig, type RsbuildConfig, type RsbuildPlugin, + type RsbuildPlugins, defineConfig as defineRsbuildConfig, loadConfig as loadRsbuildConfig, mergeRsbuildConfig, @@ -1143,8 +1144,11 @@ const composeExternalHelpersConfig = ( return defaultConfig; }; -async function composeLibRsbuildConfig(config: LibConfig) { - checkMFPlugin(config); +async function composeLibRsbuildConfig( + config: LibConfig, + sharedPlugins?: RsbuildPlugins, +) { + checkMFPlugin(config, sharedPlugins); // Get the absolute path of the root directory to align with Rsbuild's default behavior const rootPath = config.root @@ -1258,7 +1262,11 @@ export async function composeCreateRsbuildConfig( rslibConfig: RslibConfig, ): Promise { const constantRsbuildConfig = await createConstantRsbuildConfig(); - const { lib: libConfigsArray, plugins, ...sharedRsbuildConfig } = rslibConfig; + const { + lib: libConfigsArray, + plugins: sharedPlugins, + ...sharedRsbuildConfig + } = rslibConfig; if (!libConfigsArray) { throw new Error( @@ -1274,7 +1282,10 @@ export async function composeCreateRsbuildConfig( // Merge the configuration of each environment based on the shared Rsbuild // configuration and Lib configuration in the settings. - const libRsbuildConfig = await composeLibRsbuildConfig(userConfig); + const libRsbuildConfig = await composeLibRsbuildConfig( + userConfig, + sharedPlugins, + ); // Reset certain fields because they will be completely overridden by the upcoming merge. // We don't want to retain them in the final configuration. diff --git a/packages/core/src/utils/helper.ts b/packages/core/src/utils/helper.ts index e5c3805e8..0fefa76a1 100644 --- a/packages/core/src/utils/helper.ts +++ b/packages/core/src/utils/helper.ts @@ -184,16 +184,20 @@ export function isPluginIncluded( ); } -export function checkMFPlugin(config: LibConfig): boolean { +export function checkMFPlugin( + config: LibConfig, + sharedPlugins?: RsbuildPlugins, +): boolean { if (config.format !== 'mf') { return true; } // https://github.com/module-federation/core/blob/4e5c4b96ee45899f3ba5904b8927768980d5ad0e/packages/rsbuild-plugin/src/cli/index.ts#L17 - const added = isPluginIncluded( - 'rsbuild:module-federation-enhanced', - config.plugins, - ); + const added = isPluginIncluded('rsbuild:module-federation-enhanced', [ + ...(sharedPlugins || []), + ...(config.plugins || []), + ]); + if (!added) { logger.warn( `${color.green('format: "mf"')} should be used with ${color.blue(