Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve array config merging #6344

Merged
merged 2 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -677,18 +677,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 @@ -703,6 +691,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