Skip to content

Commit 2535c8e

Browse files
committed
fix: avoid chunk conflication when muliple libs present
1 parent f6981ca commit 2535c8e

File tree

13 files changed

+143
-9
lines changed

13 files changed

+143
-9
lines changed

packages/core/src/config.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,26 @@ export const resolveEntryPath = (
10931093
): RsbuildEntry =>
10941094
traverseEntryQuery(entries, (item) => path.resolve(root, item));
10951095

1096+
const composeOutputConfig = (
1097+
multiCompilerIndex: number | null,
1098+
format: Format,
1099+
jsExtension: string,
1100+
): EnvironmentConfig => {
1101+
if (typeof multiCompilerIndex === 'number' && format !== 'mf') {
1102+
return {
1103+
tools: {
1104+
rspack: {
1105+
output: {
1106+
chunkFilename: `${multiCompilerIndex}~[name]${jsExtension}`,
1107+
},
1108+
},
1109+
},
1110+
};
1111+
}
1112+
1113+
return {};
1114+
};
1115+
10961116
const composeEntryConfig = async (
10971117
rawEntry: RsbuildConfigEntry,
10981118
bundle: LibConfig['bundle'],
@@ -1604,6 +1624,7 @@ const composeExternalHelpersConfig = (
16041624

16051625
async function composeLibRsbuildConfig(
16061626
config: LibConfig,
1627+
multiCompilerIndex: number | null, // null means there's non multi-compiler
16071628
root?: string,
16081629
sharedPlugins?: RsbuildPlugins,
16091630
) {
@@ -1660,6 +1681,11 @@ async function composeLibRsbuildConfig(
16601681
cssModulesAuto,
16611682
config.outBase,
16621683
);
1684+
const outputConfig = composeOutputConfig(
1685+
multiCompilerIndex,
1686+
format,
1687+
jsExtension,
1688+
);
16631689
const { config: bundlelessExternalConfig } = composeBundlelessExternalConfig(
16641690
jsExtension,
16651691
redirect,
@@ -1709,6 +1735,7 @@ async function composeLibRsbuildConfig(
17091735

17101736
return mergeRsbuildConfig(
17111737
formatConfig,
1738+
outputConfig,
17121739
shimsConfig,
17131740
syntaxConfig,
17141741
externalHelpersConfig,
@@ -1761,7 +1788,7 @@ export async function composeCreateRsbuildConfig(
17611788
);
17621789
}
17631790

1764-
const libConfigPromises = libConfigsArray.map(async (libConfig) => {
1791+
const libConfigPromises = libConfigsArray.map(async (libConfig, index) => {
17651792
const userConfig = mergeRsbuildConfig<LibConfig>(
17661793
sharedRsbuildConfig,
17671794
libConfig,
@@ -1771,6 +1798,7 @@ export async function composeCreateRsbuildConfig(
17711798
// configuration and Lib configuration in the settings.
17721799
const libRsbuildConfig = await composeLibRsbuildConfig(
17731800
userConfig,
1801+
libConfigsArray.length > 1 ? index : null,
17741802
root,
17751803
sharedPlugins,
17761804
);

packages/core/tests/__snapshots__/config.test.ts.snap

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
393393
output: {
394394
path: '<WORKSPACE>/dist',
395395
filename: '[name].js',
396-
chunkFilename: '[name].js',
396+
chunkFilename: '0~[name].js',
397397
publicPath: 'auto',
398398
pathinfo: false,
399399
hashFunction: 'xxhash64',
@@ -1091,7 +1091,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
10911091
output: {
10921092
path: '<WORKSPACE>/dist',
10931093
filename: '[name].cjs',
1094-
chunkFilename: '[name].cjs',
1094+
chunkFilename: '1~[name].cjs',
10951095
publicPath: 'auto',
10961096
pathinfo: false,
10971097
hashFunction: 'xxhash64',
@@ -1775,7 +1775,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
17751775
output: {
17761776
path: '<WORKSPACE>/dist',
17771777
filename: '[name].js',
1778-
chunkFilename: '[name].js',
1778+
chunkFilename: '2~[name].js',
17791779
publicPath: '/',
17801780
pathinfo: false,
17811781
hashFunction: 'xxhash64',
@@ -2375,7 +2375,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
23752375
output: {
23762376
path: '<WORKSPACE>/dist',
23772377
filename: '[name].js',
2378-
chunkFilename: '[name].js',
2378+
chunkFilename: '3~[name].js',
23792379
publicPath: '/',
23802380
pathinfo: false,
23812381
hashFunction: 'xxhash64',
@@ -3674,6 +3674,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
36743674
},
36753675
},
36763676
"output": {
3677+
"chunkFilename": "0~[name].js",
36773678
"chunkFormat": "module",
36783679
"chunkLoading": "import",
36793680
"library": {
@@ -3945,6 +3946,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
39453946
},
39463947
},
39473948
"output": {
3949+
"chunkFilename": "1~[name].cjs",
39483950
"chunkFormat": "commonjs",
39493951
"chunkLoading": "require",
39503952
"iife": false,
@@ -4182,6 +4184,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
41824184
},
41834185
"output": {
41844186
"asyncChunks": false,
4187+
"chunkFilename": "2~[name].js",
41854188
"library": {
41864189
"type": "umd",
41874190
},
@@ -4413,6 +4416,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
44134416
},
44144417
"output": {
44154418
"asyncChunks": false,
4419+
"chunkFilename": "3~[name].js",
44164420
"iife": true,
44174421
"library": {
44184422
"type": "modern-module",

pnpm-lock.yaml

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "minify-output-chunk-file-name-multi-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
source: {
8+
entry: {
9+
lib1: './src/lib1.js',
10+
},
11+
},
12+
}),
13+
generateBundleEsmConfig({
14+
source: {
15+
entry: {
16+
lib2: './src/lib2.js',
17+
},
18+
},
19+
}),
20+
],
21+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default async function main() {
2+
const { foo } = await import('./shared.js');
3+
return foo;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default async function main() {
2+
const { bar } = await import('./shared.js');
3+
return bar;
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const foo = 'foo';
2+
export const bar = 'bar';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "minify-output-chunk-file-name-single-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
source: {
8+
entry: {
9+
lib1: './src/lib1.js',
10+
},
11+
},
12+
}),
13+
],
14+
});

0 commit comments

Comments
 (0)