diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index b8c929741..a302a7ff3 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -660,7 +660,17 @@ export async function composeCreateRsbuildConfig( return { format: libConfig.format!, + // The merge order represents the priority of the configuration + // The priorities from high to low are as follows: + // 1 - userConfig: users can configure any Rsbuild and Rspack config + // 2 - libRsbuildConfig: the configuration that we compose from Rslib unique config and userConfig from 1 + // 3 - internalRsbuildConfig: the built-in best practice Rsbuild configuration we provide in Rslib + // We should state in the document that the built-in configuration should not be changed optionally + // In compose process of 2, we may read some config from 1, and reassemble the related config, + // so before final mergeRsbuildConfig, we reset some specified fields config: mergeRsbuildConfig( + internalRsbuildConfig, + libRsbuildConfig, omitDeep(userConfig, [ 'bundle', 'format', @@ -669,10 +679,6 @@ export async function composeCreateRsbuildConfig( 'syntax', 'dts', ]), - libRsbuildConfig, - // Merge order matters, keep `internalRsbuildConfig` at the last position - // to ensure that the internal config is not overridden by user's config. - internalRsbuildConfig, ), }; }); diff --git a/packages/core/tests/__snapshots__/config.test.ts.snap b/packages/core/tests/__snapshots__/config.test.ts.snap index f975d2197..8955e14f1 100644 --- a/packages/core/tests/__snapshots__/config.test.ts.snap +++ b/packages/core/tests/__snapshots__/config.test.ts.snap @@ -59,6 +59,40 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1 "tools": { "htmlPlugin": false, "rspack": [ + { + "experiments": { + "rspackFuture": { + "bundlerInfo": { + "force": false, + }, + }, + }, + "optimization": { + "moduleIds": "named", + }, + "resolve": { + "extensionAlias": { + ".cjs": [ + ".cts", + ".cjs", + ], + ".js": [ + ".ts", + ".tsx", + ".js", + ".jsx", + ], + ".jsx": [ + ".tsx", + ".jsx", + ], + ".mjs": [ + ".mts", + ".mjs", + ], + }, + }, + }, { "experiments": { "outputModule": true, @@ -89,35 +123,11 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1 ], }, { - "experiments": { - "rspackFuture": { - "bundlerInfo": { - "force": false, - }, - }, - }, - "optimization": { - "moduleIds": "named", - }, "resolve": { "extensionAlias": { - ".cjs": [ - ".cts", - ".cjs", - ], ".js": [ ".ts", ".tsx", - ".js", - ".jsx", - ], - ".jsx": [ - ".tsx", - ".jsx", - ], - ".mjs": [ - ".mts", - ".mjs", ], }, }, @@ -185,22 +195,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1 "tools": { "htmlPlugin": false, "rspack": [ - { - "externalsType": "commonjs", - "output": { - "chunkFormat": "commonjs", - "iife": false, - "library": { - "type": "commonjs", - }, - }, - }, - [Function], - { - "target": [ - "web", - ], - }, { "experiments": { "rspackFuture": { @@ -235,6 +229,32 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1 }, }, }, + { + "externalsType": "commonjs", + "output": { + "chunkFormat": "commonjs", + "iife": false, + "library": { + "type": "commonjs", + }, + }, + }, + [Function], + { + "target": [ + "web", + ], + }, + { + "resolve": { + "extensionAlias": { + ".js": [ + ".ts", + ".tsx", + ], + }, + }, + }, ], }, }, @@ -294,20 +314,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1 "tools": { "htmlPlugin": false, "rspack": [ - { - "externalsType": "umd", - "output": { - "library": { - "type": "umd", - }, - }, - }, - [Function], - { - "target": [ - "web", - ], - }, { "experiments": { "rspackFuture": { @@ -342,6 +348,30 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1 }, }, }, + { + "externalsType": "umd", + "output": { + "library": { + "type": "umd", + }, + }, + }, + [Function], + { + "target": [ + "web", + ], + }, + { + "resolve": { + "extensionAlias": { + ".js": [ + ".ts", + ".tsx", + ], + }, + }, + }, ], }, }, diff --git a/packages/core/tests/config.test.ts b/packages/core/tests/config.test.ts index a0cd842ac..f099c72ed 100644 --- a/packages/core/tests/config.test.ts +++ b/packages/core/tests/config.test.ts @@ -178,7 +178,15 @@ describe('Should compose create Rsbuild config correctly', () => { }, output: { filenameHash: false, - minify: true, + }, + tools: { + rspack: { + resolve: { + extensionAlias: { + '.js': ['.ts', '.tsx'], + }, + }, + }, }, }; const composedRsbuildConfig = await composeCreateRsbuildConfig(