Skip to content

Commit

Permalink
fix(config): merge array correctly (#6499)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 13, 2022
1 parent e57d8c6 commit b2d972e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
16 changes: 16 additions & 0 deletions packages/vite/src/node/__tests__/config.spec.ts
Expand Up @@ -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', () => {
Expand Down
25 changes: 14 additions & 11 deletions packages/vite/src/node/config.ts
Expand Up @@ -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)) {
Expand Down

0 comments on commit b2d972e

Please sign in to comment.