From fdad2b6aa5af6ff78c4e084580d523d92d3aea9a Mon Sep 17 00:00:00 2001 From: neverland Date: Wed, 26 Jun 2024 10:57:58 +0800 Subject: [PATCH] feat: support for merging environments config (#2700) --- packages/core/src/mergeConfig.ts | 11 +++- packages/core/tests/mergeConfig.test.ts | 71 ++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 8 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..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 () => { @@ -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'], + }, + }, + }, + }); + }); });