From d7d8e7d3ca5ab9c5382f69f625701d4027849aba Mon Sep 17 00:00:00 2001 From: neverland Date: Wed, 26 Jun 2024 10:46:44 +0800 Subject: [PATCH 1/2] feat: support for merging environments config --- packages/core/src/mergeConfig.ts | 11 ++++- packages/core/tests/mergeConfig.test.ts | 59 +++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/packages/core/src/mergeConfig.ts b/packages/core/src/mergeConfig.ts index 20b54d6084..ac1a8264b4 100644 --- a/packages/core/src/mergeConfig.ts +++ b/packages/core/src/mergeConfig.ts @@ -1,7 +1,7 @@ import { type RsbuildConfig, castArray } from '@rsbuild/shared'; import { isFunction, isPlainObject } from './helpers'; -const OVERRIDE_PATH = [ +const OVERRIDE_PATHS = [ 'performance.removeConsole', 'output.inlineScripts', 'output.inlineStyles', @@ -15,7 +15,14 @@ const OVERRIDE_PATH = [ /** * When merging configs, some properties prefer `override` over `merge to array` */ -const isOverridePath = (key: string) => OVERRIDE_PATH.includes(key); +const isOverridePath = (key: string) => { + // ignore environments name prefix, such as `environments.web` + if (key.startsWith('environments.')) { + const realKey = key.split('.').slice(2).join('.'); + return OVERRIDE_PATHS.includes(realKey); + } + return OVERRIDE_PATHS.includes(key); +}; const merge = (x: unknown, y: unknown, path = '') => { // force some keys to override diff --git a/packages/core/tests/mergeConfig.test.ts b/packages/core/tests/mergeConfig.test.ts index 5f7eff55bb..1d4d2b06ee 100644 --- a/packages/core/tests/mergeConfig.test.ts +++ b/packages/core/tests/mergeConfig.test.ts @@ -222,4 +222,63 @@ describe('mergeRsbuildConfig', () => { }, }); }); + + test('should merge overrideBrowserslist in environments as expected', async () => { + expect( + mergeRsbuildConfig( + { + output: { + overrideBrowserslist: ['chrome 50'], + }, + environments: { + web: { + output: { + overrideBrowserslist: ['edge 10'], + }, + }, + node: { + output: { + overrideBrowserslist: ['node 14'], + }, + }, + }, + }, + { + output: { + overrideBrowserslist: ['chrome 100'], + }, + }, + { + environments: { + web: { + output: { + overrideBrowserslist: ['edge 11'], + }, + }, + node: { + output: { + overrideBrowserslist: ['node 16'], + }, + }, + }, + }, + ), + ).toEqual({ + output: { + overrideBrowserslist: ['chrome 100'], + }, + environments: { + web: { + output: { + overrideBrowserslist: ['edge 11'], + }, + }, + node: { + output: { + overrideBrowserslist: ['node 16'], + }, + }, + }, + }); + }); }); From 4877c8718a2ba61aa8c065662a968f68ac959e04 Mon Sep 17 00:00:00 2001 From: neverland Date: Wed, 26 Jun 2024 10:48:50 +0800 Subject: [PATCH 2/2] fix: rspack --- packages/core/tests/mergeConfig.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/core/tests/mergeConfig.test.ts b/packages/core/tests/mergeConfig.test.ts index 1d4d2b06ee..0cd72fc8fe 100644 --- a/packages/core/tests/mergeConfig.test.ts +++ b/packages/core/tests/mergeConfig.test.ts @@ -34,25 +34,25 @@ describe('mergeRsbuildConfig', () => { const config = mergeRsbuildConfig( { source: { alias: {} } }, { source: { alias: undefined } }, - { tools: { webpack: noop } }, - { tools: { webpack: undefined } }, + { tools: { rspack: noop } }, + { tools: { rspack: undefined } }, ); expect(config).toEqual({ source: { alias: {}, }, tools: { - webpack: noop, + rspack: noop, }, }); }); test('should keep single function value', () => { const config = mergeRsbuildConfig( - { tools: { webpack: undefined } }, - { tools: { webpack: () => ({}) } }, + { tools: { rspack: undefined } }, + { tools: { rspack: () => ({}) } }, ); - expect(typeof config.tools?.webpack).toEqual('function'); + expect(typeof config.tools?.rspack).toEqual('function'); }); test('should merge string and string[] correctly', async () => {