Skip to content

Commit

Permalink
perf(mf): set splitChunks.chunks to async by default if using modul…
Browse files Browse the repository at this point in the history
…e federation (#3104)
  • Loading branch information
chenjiahan authored Aug 1, 2024
1 parent f946b7d commit cc38dec
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
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

0 comments on commit cc38dec

Please sign in to comment.