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 23ec29d commit 8863fa2
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 8863fa2

Please sign in to comment.