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

perf(mf): set splitChunks.chunks to async by default if using module federation #3104

Merged
merged 1 commit into from
Aug 1, 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: 4 additions & 2 deletions packages/core/src/plugins/moduleFederation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isRegExp } from 'node:util/types';
import { rspack } from '@rspack/core';
import type { RspackPluginInstance } from '@rspack/core';
import { DEFAULT_ASSET_PREFIX } from '../constants';
Expand All @@ -23,14 +24,14 @@ class PatchSplitChunksPlugin implements RspackPluginInstance {
}

const applyPatch = (cacheGroup: CacheGroup) => {
if (typeof cacheGroup !== 'object' || cacheGroup instanceof RegExp) {
if (typeof cacheGroup !== 'object' || isRegExp(cacheGroup)) {
return;
}

// cacheGroup.chunks will inherit splitChunks.chunks
// so we only need to modify the chunks that are set separately.
const { chunks } = cacheGroup;
if (!chunks) {
if (!chunks || chunks === 'async') {
return;
}

Expand Down Expand Up @@ -110,6 +111,7 @@ export function pluginModuleFederation(): RsbuildPlugin {
if (!config.moduleFederation?.options) {
return;
}

/**
* 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.
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/plugins/splitChunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,14 @@ export const pluginSplitChunks = (): RsbuildPlugin => ({
}

const { config } = environment;

const defaultConfig: Exclude<SplitChunks, false> = {
// Optimize both `initial` and `async` chunks
chunks: 'all',
chunks: config.moduleFederation?.options?.exposes
? // split only `async` chunks for module federation provider app
// this ensures that remote entries are not affected by chunk splitting
'async'
: // split both `initial` and `async` chunks for normal app
'all',
// When chunk size >= 50000 bytes, split it into separate chunk
// @ts-expect-error Rspack type missing
enforceSizeThreshold: 50000,
Expand All @@ -259,7 +264,7 @@ export const pluginSplitChunks = (): RsbuildPlugin => ({
// Patch the override config difference between the `custom` strategy and other strategy.
const override =
chunkSplit.strategy === 'custom'
? chunkSplit.splitChunks ?? chunkSplit.override
? (chunkSplit.splitChunks ?? chunkSplit.override)
: chunkSplit.override;

// Apply different strategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`plugin-module-federation > should set environment module federation con
"optimization": {
"splitChunks": {
"cacheGroups": {},
"chunks": "all",
"chunks": "async",
"enforceSizeThreshold": 50000,
},
},
Expand Down Expand Up @@ -71,7 +71,7 @@ exports[`plugin-module-federation > should set module federation and environment
"optimization": {
"splitChunks": {
"cacheGroups": {},
"chunks": "all",
"chunks": "async",
"enforceSizeThreshold": 50000,
},
},
Expand Down Expand Up @@ -145,7 +145,7 @@ exports[`plugin-module-federation > should set module federation config 1`] = `
"optimization": {
"splitChunks": {
"cacheGroups": {},
"chunks": "all",
"chunks": "async",
"enforceSizeThreshold": 50000,
},
},
Expand Down
Loading