Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Dec 12, 2024
1 parent fa5ea86 commit 1c7c718
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
9 changes: 8 additions & 1 deletion packages/core/src/cli/mf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
19 changes: 15 additions & 4 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type EnvironmentConfig,
type RsbuildConfig,
type RsbuildPlugin,
type RsbuildPlugins,
defineConfig as defineRsbuildConfig,
loadConfig as loadRsbuildConfig,
mergeRsbuildConfig,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1258,7 +1262,11 @@ export async function composeCreateRsbuildConfig(
rslibConfig: RslibConfig,
): Promise<RsbuildConfigWithLibInfo[]> {
const constantRsbuildConfig = await createConstantRsbuildConfig();
const { lib: libConfigsArray, plugins, ...sharedRsbuildConfig } = rslibConfig;
const {
lib: libConfigsArray,
plugins: sharedPlugins,
...sharedRsbuildConfig
} = rslibConfig;

if (!libConfigsArray) {
throw new Error(
Expand All @@ -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.
Expand Down
14 changes: 9 additions & 5 deletions packages/core/src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 1c7c718

Please sign in to comment.