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 alias merging #6497

Merged
merged 2 commits into from
Jan 13, 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
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 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order here isn't important?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

B should override A, which is the behavior of object merging, but not array previously

}
// 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