From b951f42c6bf4e31f9e08f1af3feff66bf9cbd8da Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 14 Jan 2022 03:26:17 +0800 Subject: [PATCH 1/2] fix(config): merge array correctly --- .../vite/src/node/__tests__/config.spec.ts | 16 ++++++++++++ packages/vite/src/node/config.ts | 25 +++++++++++-------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/packages/vite/src/node/__tests__/config.spec.ts b/packages/vite/src/node/__tests__/config.spec.ts index 6d4102830badda..dd427c19433475 100644 --- a/packages/vite/src/node/__tests__/config.spec.ts +++ b/packages/vite/src/node/__tests__/config.spec.ts @@ -141,6 +141,22 @@ describe('mergeConfig', () => { expect(mergeConfig(baseConfig, newConfig)).toEqual(mergedConfig) }) + + test('merge array correctly', () => { + const baseConfig = { + foo: null + } + + const newConfig = { + foo: ['bar'] + } + + const mergedConfig = { + foo: ['bar'] + } + + expect(mergeConfig(baseConfig, newConfig)).toEqual(mergedConfig) + }) }) describe('resolveConfig', () => { diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 1313a16c01df9a..017863048a9bd9 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -731,21 +731,24 @@ function mergeConfigRecursively( const existing = merged[key] + if (existing == null) { + merged[key] = value + continue + } + // fields that require special handling - if (existing != null) { - if (key === 'alias' && (rootPath === 'resolve' || rootPath === '')) { - merged[key] = mergeAlias(existing, value) - continue - } else if (key === 'assetsInclude' && rootPath === '') { - merged[key] = [].concat(existing, value) - continue - } else if (key === 'noExternal' && existing === true) { - continue - } + if (key === 'alias' && (rootPath === 'resolve' || rootPath === '')) { + merged[key] = mergeAlias(existing, value) + continue + } else if (key === 'assetsInclude' && rootPath === '') { + merged[key] = [].concat(existing, value) + continue + } else if (key === 'noExternal' && existing === true) { + continue } if (Array.isArray(existing) || Array.isArray(value)) { - merged[key] = [...arraify(existing), ...arraify(value)] + merged[key] = [...arraify(existing || []), ...arraify(value || [])] continue } if (isObject(existing) && isObject(value)) { From f9b88b0b8e6cc239e0bc840f166feafb3b2d93d2 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 14 Jan 2022 03:27:12 +0800 Subject: [PATCH 2/2] chore: update --- packages/vite/src/node/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 017863048a9bd9..2b55f7355a6ca2 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -748,7 +748,7 @@ function mergeConfigRecursively( } if (Array.isArray(existing) || Array.isArray(value)) { - merged[key] = [...arraify(existing || []), ...arraify(value || [])] + merged[key] = [...arraify(existing ?? []), ...arraify(value ?? [])] continue } if (isObject(existing) && isObject(value)) {