Skip to content

Commit c0feed1

Browse files
authored
fix: forceSplitting not work with single-vendor (#4708)
1 parent 0dec4a3 commit c0feed1

File tree

6 files changed

+67
-7
lines changed

6 files changed

+67
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { basename } from 'node:path';
2+
import { build } from '@e2e/helper';
3+
import { expect, test } from '@playwright/test';
4+
5+
test('should allow to use `forceSplitting` when chunkSplit is "single-vendor"', async () => {
6+
const rsbuild = await build({
7+
cwd: __dirname,
8+
});
9+
10+
const files = await rsbuild.unwrapOutputJSON();
11+
12+
const jsFiles = Object.keys(files)
13+
.filter((name) => name.endsWith('.js'))
14+
.map((name) => basename(name));
15+
16+
expect(jsFiles.length).toEqual(3);
17+
expect(jsFiles).toContain('index.js');
18+
expect(jsFiles).toContain('vendor.js');
19+
expect(jsFiles).toContain('my-react.js');
20+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { defineConfig } from '@rsbuild/core';
2+
import { pluginReact } from '@rsbuild/plugin-react';
3+
4+
export default defineConfig({
5+
plugins: [
6+
pluginReact({
7+
splitChunks: {
8+
react: false,
9+
},
10+
}),
11+
],
12+
output: {
13+
filenameHash: false,
14+
},
15+
performance: {
16+
chunkSplit: {
17+
strategy: 'single-vendor',
18+
forceSplitting: {
19+
'my-react': /node_modules[\\/]react[\\/]/,
20+
},
21+
},
22+
},
23+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const App = () => <div id="test">Hello Rsbuild!</div>;
2+
3+
export default App;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import { createRoot } from 'react-dom/client';
3+
import App from './App';
4+
5+
const container = document.getElementById('root');
6+
if (container) {
7+
const root = createRoot(container);
8+
root.render(React.createElement(App));
9+
}

packages/core/src/plugins/splitChunks.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import assert from 'node:assert';
22
import { NODE_MODULES_REGEX } from '../constants';
33
import type {
4+
ChunkSplit,
45
ForceSplitting,
56
Polyfill,
6-
RsbuildChunkSplit,
77
RsbuildPlugin,
88
Rspack,
99
SplitChunks,
@@ -32,7 +32,7 @@ interface SplitChunksContext {
3232
/**
3333
* User Rsbuild `chunkSplit` config
3434
*/
35-
userConfig: RsbuildChunkSplit;
35+
userConfig: ChunkSplit;
3636
/**
3737
* The root path of current project
3838
*/
@@ -43,7 +43,10 @@ interface SplitChunksContext {
4343
polyfill: Polyfill;
4444
}
4545

46-
function getForceSplittingGroups(forceSplitting: ForceSplitting): CacheGroups {
46+
function getForceSplittingGroups(
47+
forceSplitting: ForceSplitting,
48+
strategy: ChunkSplit['strategy'],
49+
): CacheGroups {
4750
const cacheGroups: CacheGroups = {};
4851
const pairs = Array.isArray(forceSplitting)
4952
? forceSplitting.map(
@@ -56,7 +59,8 @@ function getForceSplittingGroups(forceSplitting: ForceSplitting): CacheGroups {
5659
test: regexp,
5760
name: key,
5861
chunks: 'all',
59-
priority: 0,
62+
// ensure force splitting chunks have higher priority when chunkSplit is 'single-vendor'
63+
priority: strategy === 'single-vendor' ? 1 : 0,
6064
// Ignore minimum size, minimum chunks and maximum requests and always create chunks.
6165
enforce: true,
6266
};
@@ -263,6 +267,7 @@ export const pluginSplitChunks = (): RsbuildPlugin => ({
263267
if (chunkSplit.forceSplitting) {
264268
forceSplittingGroups = getForceSplittingGroups(
265269
chunkSplit.forceSplitting,
270+
chunkSplit.strategy,
266271
);
267272
}
268273

packages/core/src/types/config.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ export interface PerformanceConfig {
608608
/**
609609
* Configure the chunk splitting strategy.
610610
*/
611-
chunkSplit?: RsbuildChunkSplit;
611+
chunkSplit?: ChunkSplit;
612612

613613
/**
614614
* Analyze the size of output files.
@@ -655,7 +655,7 @@ export interface PerformanceConfig {
655655

656656
export interface NormalizedPerformanceConfig extends PerformanceConfig {
657657
printFileSize: PrintFileSizeOptions | boolean;
658-
chunkSplit: RsbuildChunkSplit;
658+
chunkSplit: ChunkSplit;
659659
}
660660

661661
export type SplitChunks = Configuration extends {
@@ -693,7 +693,7 @@ export interface SplitCustom extends BaseSplitRules {
693693
splitChunks?: SplitChunks;
694694
}
695695

696-
export type RsbuildChunkSplit = BaseChunkSplit | SplitBySize | SplitCustom;
696+
export type ChunkSplit = BaseChunkSplit | SplitBySize | SplitCustom;
697697

698698
export type DistPathConfig = {
699699
/**

0 commit comments

Comments
 (0)