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

feat: support apply environment moduleFederation config #2692

Merged
merged 3 commits into from
Jun 26, 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
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
77 changes: 37 additions & 40 deletions packages/core/src/plugins/moduleFederation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,54 +88,51 @@ 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 &&
config.performance?.chunkSplit?.strategy === 'split-by-experience'
) {
config.performance.chunkSplit = {
...config.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(async (chain, { CHAIN_ID, target }) => {
const config = api.getNormalizedConfig();
api.modifyBundlerChain(
async (chain, { CHAIN_ID, target, environment }) => {
const config = api.getNormalizedConfig({ environment });

if (!config.moduleFederation?.options || target !== 'web') {
return;
}

const { options } = config.moduleFederation;
if (!config.moduleFederation?.options || target !== 'web') {
return;
}

chain
.plugin(CHAIN_ID.PLUGIN.MODULE_FEDERATION)
.use(rspack.container.ModuleFederationPlugin, [options]);
const { options } = config.moduleFederation;

if (options.name) {
chain
.plugin('mf-patch-split-chunks')
.use(PatchSplitChunksPlugin, [options.name]);
}
.plugin(CHAIN_ID.PLUGIN.MODULE_FEDERATION)
.use(rspack.container.ModuleFederationPlugin, [options]);

const publicPath = chain.output.get('publicPath');
if (options.name) {
chain
.plugin('mf-patch-split-chunks')
.use(PatchSplitChunksPlugin, [options.name]);
}

// set the default publicPath to 'auto' to make MF work
if (publicPath === DEFAULT_ASSET_PREFIX) {
chain.output.set('publicPath', 'auto');
}
});
const publicPath = chain.output.get('publicPath');

// set the default publicPath to 'auto' to make MF work
if (publicPath === DEFAULT_ASSET_PREFIX) {
chain.output.set('publicPath', 'auto');
}
},
);
},
};
}
170 changes: 170 additions & 0 deletions packages/core/tests/__snapshots__/moduleFederation.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`plugin-module-federation > should set environment module federation config correctly 1`] = `
[
{
"optimization": {
"splitChunks": {
"cacheGroups": {},
"chunks": "all",
"enforceSizeThreshold": 50000,
},
},
"plugins": [
ModuleFederationPlugin {
"_options": {
"exposes": {
"Button": "src/Button",
},
"filename": "remoteEntry.js",
"name": "remote",
"shared": {
"react": {
"requiredVersion": "^18.2.0",
"singleton": true,
},
"react-dom": {
"requiredVersion": "^18.2.0",
"singleton": true,
},
},
},
},
PatchSplitChunksPlugin {
"name": "remote",
},
],
},
{
"optimization": {
"splitChunks": {
"cacheGroups": {
"lib-axios": {
"name": "lib-axios",
"priority": 0,
"reuseExistingChunk": true,
"test": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]\\(axios\\|axios-\\.\\+\\)\\[\\\\\\\\/\\]/,
},
"lib-lodash": {
"name": "lib-lodash",
"priority": 0,
"reuseExistingChunk": true,
"test": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]\\(lodash\\|lodash-es\\)\\[\\\\\\\\/\\]/,
},
"lib-polyfill": {
"name": "lib-polyfill",
"priority": 0,
"reuseExistingChunk": true,
"test": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]\\(tslib\\|core-js\\|@babel\\\\/runtime\\|@swc\\\\/helpers\\)\\[\\\\\\\\/\\]/,
},
},
"chunks": "all",
"enforceSizeThreshold": 50000,
},
},
},
]
`;

exports[`plugin-module-federation > should set module federation and environment chunkSplit config correctly 1`] = `
[
{
"optimization": {
"splitChunks": {
"cacheGroups": {},
"chunks": "all",
"enforceSizeThreshold": 50000,
},
},
"plugins": [
ModuleFederationPlugin {
"_options": {
"exposes": {
"Button": "src/Button",
},
"filename": "remoteEntry.js",
"name": "remote",
"shared": {
"react": {
"requiredVersion": "^18.2.0",
"singleton": true,
},
"react-dom": {
"requiredVersion": "^18.2.0",
"singleton": true,
},
},
},
},
PatchSplitChunksPlugin {
"name": "remote",
},
],
},
{
"optimization": {
"splitChunks": false,
},
"plugins": [
ModuleFederationPlugin {
"_options": {
"exposes": {
"Button": "src/Button",
},
"filename": "remoteEntry.js",
"name": "remote",
"shared": {
"react": {
"requiredVersion": "^18.2.0",
"singleton": true,
},
"react-dom": {
"requiredVersion": "^18.2.0",
"singleton": true,
},
},
},
},
PatchSplitChunksPlugin {
"name": "remote",
},
],
},
]
`;

exports[`plugin-module-federation > should set module federation config 1`] = `
{
"optimization": {
"splitChunks": {
"cacheGroups": {},
"chunks": "all",
"enforceSizeThreshold": 50000,
},
},
"plugins": [
ModuleFederationPlugin {
"_options": {
"exposes": {
"Button": "src/Button",
},
"filename": "remoteEntry.js",
"name": "remote",
"shared": {
"react": {
"requiredVersion": "^18.2.0",
"singleton": true,
},
"react-dom": {
"requiredVersion": "^18.2.0",
"singleton": true,
},
},
},
},
PatchSplitChunksPlugin {
"name": "remote",
},
],
}
`;
Loading
Loading