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(mergeConfig): don't accept callback config #13135

Merged
merged 1 commit into from
Jun 6, 2023
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
4 changes: 4 additions & 0 deletions docs/guide/api-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ function mergeConfig(

Deeply merge two Vite configs. `isRoot` represents the level within the Vite config which is being merged. For example, set `false` if you're merging two `build` options.

::: tip NOTE
`mergeConfig` accepts only config in object form. If you have a config in callback form, you should call it before passing into `mergeConfig`.
:::

## `searchForWorkspaceRoot`

**Type Signature:**
Expand Down
23 changes: 22 additions & 1 deletion packages/vite/src/node/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, test } from 'vitest'
import type { InlineConfig } from '..'
import type { PluginOption, UserConfig, UserConfigExport } from '../config'
import { resolveConfig } from '../config'
import { defineConfig, resolveConfig } from '../config'
import { resolveEnvPrefix } from '../env'
import { mergeConfig } from '../publicUtils'

Expand Down Expand Up @@ -184,6 +184,27 @@ describe('mergeConfig', () => {
expect(mergeConfig(baseConfig, newConfig)).toEqual(mergedConfig)
expect(mergeConfig(newConfig, baseConfig)).toEqual(mergedConfig)
})

test('throws error with functions', () => {
const baseConfig = defineConfig(() => ({ base: 'base' }))
const newConfig = defineConfig(() => ({ base: 'new' }))

expect(() =>
mergeConfig(
// @ts-expect-error TypeScript shouldn't give you to pass a function as argument
baseConfig,
newConfig,
),
).toThrowError('Cannot merge config in form of callback')

expect(() =>
mergeConfig(
{},
// @ts-expect-error TypeScript shouldn't give you to pass a function as argument
newConfig,
),
).toThrowError('Cannot merge config in form of callback')
})
})

describe('resolveEnvPrefix', () => {
Expand Down
13 changes: 10 additions & 3 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1037,11 +1037,18 @@ function mergeConfigRecursively(
return merged
}

export function mergeConfig(
defaults: Record<string, any>,
overrides: Record<string, any>,
export function mergeConfig<
D extends Record<string, any>,
O extends Record<string, any>,
>(
defaults: D extends Function ? never : D,
overrides: O extends Function ? never : O,
isRoot = true,
): Record<string, any> {
if (typeof defaults === 'function' || typeof overrides === 'function') {
throw new Error(`Cannot merge config in form of callback`)
}

return mergeConfigRecursively(defaults, overrides, isRoot ? '' : '.')
}

Expand Down