Skip to content

Commit 3449e64

Browse files
authored
feat(plugin-react): enhance splitChunks option to support boolean type (#6515)
1 parent 5b2c2d7 commit 3449e64

File tree

5 files changed

+62
-40
lines changed

5 files changed

+62
-40
lines changed

e2e/cases/plugin-react/split-chunk/index.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ test('should not split react chunks when strategy is `all-in-one`', async ({
3434
expect(filesNames.find((file) => file.includes('lib-router'))).toBeFalsy();
3535
});
3636

37+
test('should not split react chunks when splitChunks is disabled', async ({
38+
build,
39+
}) => {
40+
const rsbuild = await build({
41+
config: {
42+
plugins: [
43+
pluginReact({
44+
splitChunks: false,
45+
}),
46+
],
47+
},
48+
});
49+
50+
const files = rsbuild.getDistFiles();
51+
const filesNames = Object.keys(files);
52+
expect(filesNames.find((file) => file.includes('lib-react'))).toBeFalsy();
53+
expect(filesNames.find((file) => file.includes('lib-router'))).toBeFalsy();
54+
});
55+
3756
test('should not override user defined cache groups', async ({ build }) => {
3857
const rsbuild = await build({
3958
config: {

packages/plugin-react/src/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ export type PluginReactOptions = {
2525
*/
2626
swcReactOptions?: Rspack.SwcLoaderTransformConfig['react'];
2727
/**
28-
* Configuration for chunk splitting of React-related dependencies.
28+
* Configuration for chunk splitting of React-related dependencies when `chunkSplit.strategy`
29+
* is set to `split-by-experience`.
30+
* @default true
2931
*/
30-
splitChunks?: SplitReactChunkOptions;
32+
splitChunks?: boolean | SplitReactChunkOptions;
3133
/**
3234
* When set to `true`, enables the React Profiler for performance analysis in production builds.
3335
* @default false
@@ -59,10 +61,11 @@ export const pluginReact = (
5961
name: PLUGIN_REACT_NAME,
6062

6163
setup(api) {
62-
const defaultOptions: PluginReactOptions = {
64+
const defaultOptions = {
6365
fastRefresh: true,
66+
splitChunks: true,
6467
enableProfiler: false,
65-
};
68+
} satisfies PluginReactOptions;
6669
const finalOptions = {
6770
...defaultOptions,
6871
...options,
@@ -76,6 +79,6 @@ export const pluginReact = (
7679
}
7780
}
7881

79-
applySplitChunksRule(api, finalOptions?.splitChunks);
82+
applySplitChunksRule(api, finalOptions.splitChunks);
8083
},
8184
});

packages/plugin-react/src/splitChunks.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
import type { RsbuildPluginAPI, Rspack } from '@rsbuild/core';
22
import type { SplitReactChunkOptions } from './index.js';
33

4-
const isPlainObject = (obj: unknown): obj is Record<string, unknown> =>
5-
obj !== null &&
6-
typeof obj === 'object' &&
7-
Object.prototype.toString.call(obj) === '[object Object]';
8-
94
export const applySplitChunksRule = (
105
api: RsbuildPluginAPI,
11-
options: SplitReactChunkOptions = {
12-
react: true,
13-
router: true,
14-
},
6+
options: SplitReactChunkOptions | boolean,
157
): void => {
168
api.modifyBundlerChain((chain, { environment, isProd }) => {
179
const { config } = environment;
18-
if (config.performance.chunkSplit.strategy !== 'split-by-experience') {
10+
if (
11+
config.performance.chunkSplit.strategy !== 'split-by-experience' ||
12+
options === false
13+
) {
1914
return;
2015
}
2116

17+
const normalizedOptions =
18+
options === true ? { react: true, router: true } : options;
19+
2220
const currentConfig =
2321
chain.optimization.splitChunks.values() as Rspack.Optimization['splitChunks'];
24-
if (!isPlainObject(currentConfig)) {
22+
if (typeof currentConfig !== 'object') {
2523
return;
2624
}
2725

@@ -30,7 +28,7 @@ export const applySplitChunksRule = (
3028
Rspack.OptimizationSplitChunksCacheGroup
3129
> = {};
3230

33-
if (options.react) {
31+
if (normalizedOptions.react) {
3432
extraGroups.react = {
3533
name: 'lib-react',
3634
test: isProd
@@ -40,7 +38,7 @@ export const applySplitChunksRule = (
4038
};
4139
}
4240

43-
if (options.router) {
41+
if (normalizedOptions.router) {
4442
extraGroups.router = {
4543
name: 'lib-router',
4644
test: /node_modules[\\/](?:react-router|react-router-dom|history|@remix-run[\\/]router)[\\/]/,

website/docs/en/plugins/list/plugin-react.mdx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,27 +158,28 @@ This option is used to control this behavior and determine whether the `react` a
158158
- **Type:**
159159

160160
```ts
161-
type SplitReactChunkOptions = {
162-
react?: boolean;
163-
router?: boolean;
164-
};
161+
type SplitChunks =
162+
| boolean
163+
| {
164+
react?: boolean;
165+
router?: boolean;
166+
};
165167
```
166168

167-
- **Default:**
169+
- **Default:** `true` (equivalent to `{ react: true, router: true }`)
170+
171+
For example, to disable all chunk splitting:
168172

169173
```ts
170-
const defaultOptions = {
171-
react: true,
172-
router: true,
173-
};
174+
pluginReact({ splitChunks: false });
174175
```
175176

176-
- **Example:**
177+
Or to disable only the `router` chunk splitting:
177178

178179
```ts
179180
pluginReact({
180181
splitChunks: {
181-
react: false,
182+
react: true,
182183
router: false,
183184
},
184185
});

website/docs/zh/plugins/list/plugin-react.mdx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,27 +158,28 @@ pluginReact({
158158
- **类型:**
159159

160160
```ts
161-
type SplitReactChunkOptions = {
162-
react?: boolean;
163-
router?: boolean;
164-
};
161+
type SplitChunks =
162+
| boolean
163+
| {
164+
react?: boolean;
165+
router?: boolean;
166+
};
165167
```
166168

167-
- **默认值:**
169+
- **默认值:** `true`(等价于 `{ react: true, router: true }`
170+
171+
例如,禁用所有 chunks 拆分:
168172

169173
```ts
170-
const defaultOptions = {
171-
react: true,
172-
router: true,
173-
};
174+
pluginReact({ splitChunks: false });
174175
```
175176

176-
- **示例:**
177+
或是仅禁用 `router` chunk 拆分:
177178

178179
```ts
179180
pluginReact({
180181
splitChunks: {
181-
react: false,
182+
react: true,
182183
router: false,
183184
},
184185
});

0 commit comments

Comments
 (0)