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..2b55f7355a6ca2 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)) {