Skip to content

Commit

Permalink
fix(mergeConfig): don't accept callback config (#13135)
Browse files Browse the repository at this point in the history
  • Loading branch information
Minhir authored Jun 6, 2023
1 parent aa1776f commit 998512b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
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 @@ -1042,11 +1042,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

0 comments on commit 998512b

Please sign in to comment.