From 8863fa2cc455641c297ff6742b9b757959b7a811 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 14 Jan 2022 03:33:36 +0800 Subject: [PATCH] fix(config): merge array correctly (#6499) --- .../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 443fd7013ec4f0..94b5fb5dc12b1c 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 a69779a629b0d6..1fa8f582502b4a 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -672,21 +672,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)) {