Skip to content

Commit

Permalink
fix: use modifyEnvironmentConfig hook
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy committed Jun 26, 2024
1 parent 02afcfa commit 0dd7f7b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 62 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/mergeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type RsbuildConfig, castArray } from '@rsbuild/shared';
import { type RsbuildConfig, castArray, cloneDeep } from '@rsbuild/shared';
import { isFunction, isPlainObject } from './helpers';

const OVERRIDE_PATHS = [
Expand Down Expand Up @@ -32,10 +32,10 @@ const merge = (x: unknown, y: unknown, path = '') => {

// ignore undefined property
if (x === undefined) {
return y;
return isPlainObject(y) ? cloneDeep(y) : y;
}
if (y === undefined) {
return x;
return isPlainObject(x) ? cloneDeep(x) : x;
}

const pair = [x, y];
Expand Down
74 changes: 15 additions & 59 deletions packages/core/src/plugins/moduleFederation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,65 +88,21 @@ export function pluginModuleFederation(): RsbuildPlugin {
name: 'rsbuild:module-federation',

setup(api) {
api.modifyRsbuildConfig({
order: 'post',
handler: (config) => {
/**
* Currently, splitChunks will take precedence over module federation shared modules.
* So we need to disable the default split chunks rules to make shared modules to work properly.
* @see https://github.com/module-federation/module-federation-examples/issues/3161
*/
if (config.moduleFederation?.options) {
if (
config.performance?.chunkSplit?.strategy === 'split-by-experience'
) {
if (
config.environments &&
Object.keys(config.environments).length
) {
// override chunkSplit strategy in every environment
for (const envConfig of Object.values(config.environments)) {
if (
envConfig.performance?.chunkSplit?.strategy ===
'split-by-experience' ||
!envConfig.performance?.chunkSplit?.strategy
) {
envConfig.performance ??= {};
envConfig.performance.chunkSplit = {
...envConfig.performance.chunkSplit,
strategy: 'custom',
};
}
}
} else {
config.performance.chunkSplit = {
...config.performance.chunkSplit,
strategy: 'custom',
};
}
}
} else if (config.environments) {
for (const envConfig of Object.values(config.environments)) {
if (envConfig.moduleFederation?.options) {
if (
envConfig.performance?.chunkSplit?.strategy ===
'split-by-experience' ||
(!envConfig.performance?.chunkSplit?.strategy &&
config.performance?.chunkSplit?.strategy ===
'split-by-experience')
) {
envConfig.performance ??= {};
envConfig.performance.chunkSplit = {
...envConfig.performance.chunkSplit,
strategy: 'custom',
};
}
}
}
}

return config;
},
api.modifyEnvironmentConfig((config) => {
/**
* Currently, splitChunks will take precedence over module federation shared modules.
* So we need to disable the default split chunks rules to make shared modules to work properly.
* @see https://github.com/module-federation/module-federation-examples/issues/3161
*/
if (
config.moduleFederation?.options &&
config.performance?.chunkSplit?.strategy === 'split-by-experience'
) {
config.performance.chunkSplit = {
...config.performance.chunkSplit,
strategy: 'custom',
};
}
});

api.modifyBundlerChain(
Expand Down

0 comments on commit 0dd7f7b

Please sign in to comment.