Skip to content

Commit

Permalink
fix: improve alias merging (vitejs#6497)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 13, 2022
1 parent 6ecf9b0 commit e57d8c6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
40 changes: 36 additions & 4 deletions packages/vite/src/node/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ describe('mergeConfig', () => {
const mergedConfig: UserConfigExport = {
resolve: {
alias: [
{
find: 'foo',
replacement: 'foo-value'
},
{
find: 'bar',
replacement: 'bar-value'
},
{
find: 'baz',
replacement: 'baz-value'
},
{
find: 'foo',
replacement: 'foo-value'
}
]
}
Expand All @@ -46,6 +46,38 @@ describe('mergeConfig', () => {
expect(mergeConfig(baseConfig, newConfig)).toEqual(mergedConfig)
})

test('keep object alias schema', () => {
const baseConfig = {
resolve: {
alias: {
bar: 'bar-value',
baz: 'baz-value'
}
}
}

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

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

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

test('handles arrays', () => {
const baseConfig: UserConfigExport = {
envPrefix: 'string1'
Expand Down
28 changes: 20 additions & 8 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,13 @@ export async function resolveConfig(
]

// resolve alias with internal client alias
const resolvedAlias = mergeAlias(
// @ts-ignore because @rollup/plugin-alias' type doesn't allow function
// replacement, but its implementation does work with function values.
clientAlias,
config.resolve?.alias || config.alias || []
const resolvedAlias = normalizeAlias(
mergeAlias(
// @ts-ignore because @rollup/plugin-alias' type doesn't allow function
// replacement, but its implementation does work with function values.
clientAlias,
config.resolve?.alias || config.alias || []
)
)

const resolveOptions: ResolvedConfig['resolve'] = {
Expand Down Expand Up @@ -768,11 +770,21 @@ export function mergeConfig(
return mergeConfigRecursively(defaults, overrides, isRoot ? '' : '.')
}

function mergeAlias(a: AliasOptions = [], b: AliasOptions = []): Alias[] {
return [...normalizeAlias(a), ...normalizeAlias(b)]
function mergeAlias(
a?: AliasOptions,
b?: AliasOptions
): AliasOptions | undefined {
if (!a) return b
if (!b) return a
if (isObject(a) && isObject(b)) {
return { ...a, ...b }
}
// the order is flipped because the alias is resolved from top-down,
// where the later should have higher priority
return [...normalizeAlias(b), ...normalizeAlias(a)]
}

function normalizeAlias(o: AliasOptions): Alias[] {
function normalizeAlias(o: AliasOptions = []): Alias[] {
return Array.isArray(o)
? o.map(normalizeSingleAlias)
: Object.keys(o).map((find) =>
Expand Down

0 comments on commit e57d8c6

Please sign in to comment.