Skip to content

Commit

Permalink
fix(build): properly handle alias key in config merge (vitejs#2847)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <[email protected]>
  • Loading branch information
chrissantamaria and antfu authored Apr 14, 2021
1 parent 176e55d commit 41261c7
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 8 deletions.
94 changes: 94 additions & 0 deletions packages/vite/src/node/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { mergeConfig, UserConfigExport } from '../config'

describe('mergeConfig', () => {
test('handles configs with different alias schemas', () => {
const baseConfig: UserConfigExport = {
resolve: {
alias: [
{
find: 'foo',
replacement: 'foo-value'
}
]
}
}

const newConfig: UserConfigExport = {
resolve: {
alias: {
bar: 'bar-value',
baz: 'baz-value'
}
}
}

const mergedConfig: UserConfigExport = {
resolve: {
alias: [
{
find: 'foo',
replacement: 'foo-value'
},
{
find: 'bar',
replacement: 'bar-value'
},
{
find: 'baz',
replacement: 'baz-value'
}
]
}
}

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

test('handles assetsInclude', () => {
const baseConfig: UserConfigExport = {
assetsInclude: 'some-string'
}

const newConfig: UserConfigExport = {
assetsInclude: ['some-other-string', /regexp?/]
}

const mergedConfig: UserConfigExport = {
assetsInclude: ['some-string', 'some-other-string', /regexp?/]
}

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

test('not handles alias not under `resolve`', () => {
const baseConfig = {
custom: {
alias: {
bar: 'bar-value',
baz: 'baz-value'
}
}
}

const newConfig = {
custom: {
alias: {
bar: 'bar-value-2',
foo: 'foo-value'
}
}
}

const mergedConfig = {
custom: {
alias: {
bar: 'bar-value-2',
baz: 'baz-value',
foo: 'foo-value'
}
}
}

expect(mergeConfig(baseConfig, newConfig)).toEqual(mergedConfig)
})
})
28 changes: 20 additions & 8 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,11 @@ function resolveBaseUrl(
return base
}

export function mergeConfig(
function mergeConfigRecursively(
a: Record<string, any>,
b: Record<string, any>,
isRoot = true
): Record<string, any> {
rootPath: string
) {
const merged: Record<string, any> = { ...a }
for (const key in b) {
const value = b[key]
Expand All @@ -520,16 +520,20 @@ export function mergeConfig(
continue
}
if (isObject(existing) && isObject(value)) {
merged[key] = mergeConfig(existing, value, false)
merged[key] = mergeConfigRecursively(
existing,
value,
rootPath ? `${rootPath}.${key}` : key
)
continue
}

// root fields that require special handling
if (existing != null && isRoot) {
if (key === 'alias') {
// fields that require special handling
if (existing != null) {
if (key === 'alias' && (rootPath === 'resolve' || rootPath === '')) {
merged[key] = mergeAlias(existing, value)
continue
} else if (key === 'assetsInclude') {
} else if (key === 'assetsInclude' && rootPath === '') {
merged[key] = [].concat(existing, value)
continue
}
Expand All @@ -540,6 +544,14 @@ export function mergeConfig(
return merged
}

export function mergeConfig(
a: Record<string, any>,
b: Record<string, any>,
isRoot = true
): Record<string, any> {
return mergeConfigRecursively(a, b, isRoot ? '' : '.')
}

function mergeAlias(a: AliasOptions = [], b: AliasOptions = []): Alias[] {
return [...normalizeAlias(a), ...normalizeAlias(b)]
}
Expand Down

0 comments on commit 41261c7

Please sign in to comment.