Skip to content

Commit

Permalink
fix(mergeConfig) Don't accept callback form config
Browse files Browse the repository at this point in the history
mergeConfig function doesn't work with config in
callback form. To make the error more transparent
added both a runtime and type checks.
  • Loading branch information
Minhir committed May 9, 2023
1 parent a8c7eb2 commit 9209a11
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 @@ -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

0 comments on commit 9209a11

Please sign in to comment.