diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 864ffe8a9..8cdbe6a15 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -1891,6 +1891,16 @@ export async function composeCreateRsbuildConfig( userConfig.output ??= {}; delete userConfig.output.externals; + // Convert output.distPath to an object to ensure it merges correctly with the built-in constant config + if ( + userConfig.output.distPath && + typeof userConfig.output.distPath === 'string' + ) { + userConfig.output.distPath = { + root: userConfig.output.distPath, + }; + } + const config: RsbuildConfigWithLibInfo = { format: libConfig.format ?? 'esm', // The merge order represents the priority of the configuration diff --git a/packages/core/tests/config.test.ts b/packages/core/tests/config.test.ts index cd72a17e7..61ea17397 100644 --- a/packages/core/tests/config.test.ts +++ b/packages/core/tests/config.test.ts @@ -219,6 +219,51 @@ describe('Should compose create Rsbuild config correctly', () => { expect(rsbuildConfig).toMatchSnapshot('inspected Rsbuild configs'); expect(bundlerConfigs).toMatchSnapshot('inspected Rspack configs'); }); + + test('Merge output.distPath correctly', async () => { + const rslibConfig: RslibConfig = { + lib: [ + { + format: 'esm', + output: { + distPath: 'dist/esm', + }, + }, + { + format: 'cjs', + output: { + distPath: { + root: 'dist/cjs', + }, + }, + }, + ], + }; + + const composedRsbuildConfig = await composeCreateRsbuildConfig(rslibConfig); + expect( + composedRsbuildConfig[0]?.config.output?.distPath, + ).toMatchInlineSnapshot(` + { + "css": "./", + "cssAsync": "./", + "js": "./", + "jsAsync": "./", + "root": "dist/esm", + } + `); + expect( + composedRsbuildConfig[1]?.config.output?.distPath, + ).toMatchInlineSnapshot(` + { + "css": "./", + "cssAsync": "./", + "js": "./", + "jsAsync": "./", + "root": "dist/cjs", + } + `); + }); }); describe('syntax', () => {