Skip to content

Commit

Permalink
fix: improve array config merging (#6344)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored and antfu committed Jan 13, 2022
1 parent dd4c111 commit 631bf45
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 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 @@ -46,6 +46,22 @@ describe('mergeConfig', () => {
expect(mergeConfig(baseConfig, newConfig)).toEqual(mergedConfig)
})

test('handles arrays', () => {
const baseConfig: UserConfigExport = {
envPrefix: 'string1'
}

const newConfig: UserConfigExport = {
envPrefix: ['string2', 'string3']
}

const mergedConfig: UserConfigExport = {
envPrefix: ['string1', 'string2', 'string3']
}

expect(mergeConfig(baseConfig, newConfig)).toEqual(mergedConfig)
})

test('handles assetsInclude', () => {
const baseConfig: UserConfigExport = {
assetsInclude: 'some-string'
Expand Down
25 changes: 13 additions & 12 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,18 +669,6 @@ function mergeConfigRecursively(
}

const existing = merged[key]
if (Array.isArray(existing) && Array.isArray(value)) {
merged[key] = [...existing, ...value]
continue
}
if (isObject(existing) && isObject(value)) {
merged[key] = mergeConfigRecursively(
existing,
value,
rootPath ? `${rootPath}.${key}` : key
)
continue
}

// fields that require special handling
if (existing != null) {
Expand All @@ -695,6 +683,19 @@ function mergeConfigRecursively(
}
}

if (Array.isArray(existing) || Array.isArray(value)) {
merged[key] = [...arraify(existing), ...arraify(value)]
continue
}
if (isObject(existing) && isObject(value)) {
merged[key] = mergeConfigRecursively(
existing,
value,
rootPath ? `${rootPath}.${key}` : key
)
continue
}

merged[key] = value
}
return merged
Expand Down

0 comments on commit 631bf45

Please sign in to comment.