Skip to content

Commit

Permalink
fix(types): narrow down the return type of defineConfig (#13792)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodatea committed Jul 17, 2023
1 parent 975a631 commit c971f26
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
8 changes: 4 additions & 4 deletions packages/vite/src/node/__tests__/config.spec.ts
Expand Up @@ -7,7 +7,7 @@ import { mergeConfig } from '../publicUtils'

describe('mergeConfig', () => {
test('handles configs with different alias schemas', () => {
const baseConfig: UserConfigExport = {
const baseConfig = defineConfig({
resolve: {
alias: [
{
Expand All @@ -16,16 +16,16 @@ describe('mergeConfig', () => {
},
],
},
}
})

const newConfig: UserConfigExport = {
const newConfig = defineConfig({
resolve: {
alias: {
bar: 'bar-value',
baz: 'baz-value',
},
},
}
})

const mergedConfig: UserConfigExport = {
resolve: {
Expand Down
13 changes: 12 additions & 1 deletion packages/vite/src/node/config.ts
Expand Up @@ -100,15 +100,26 @@ export interface ConfigEnv {
*/
export type AppType = 'spa' | 'mpa' | 'custom'

export type UserConfigFnObject = (env: ConfigEnv) => UserConfig
export type UserConfigFnPromise = (env: ConfigEnv) => Promise<UserConfig>
export type UserConfigFn = (env: ConfigEnv) => UserConfig | Promise<UserConfig>
export type UserConfigExport = UserConfig | Promise<UserConfig> | UserConfigFn

export type UserConfigExport =
| UserConfig
| Promise<UserConfig>
| UserConfigFnObject
| UserConfigFnPromise
| UserConfigFn

/**
* Type helper to make it easier to use vite.config.ts
* accepts a direct {@link UserConfig} object, or a function that returns it.
* The function receives a {@link ConfigEnv} object that exposes two properties:
* `command` (either `'build'` or `'serve'`), and `mode`.
*/
export function defineConfig(config: UserConfig): UserConfig
export function defineConfig(config: Promise<UserConfig>): Promise<UserConfig>
export function defineConfig(config: UserConfigExport): UserConfigExport
export function defineConfig(config: UserConfigExport): UserConfigExport {
return config
}
Expand Down
2 changes: 0 additions & 2 deletions playground/assets/vite.config.js
@@ -1,8 +1,6 @@
import path from 'node:path'
import { defineConfig } from 'vite'

/** @type {import('vite').UserConfig} */
// @ts-expect-error typecast
export default defineConfig({
base: '/foo',
publicDir: 'static',
Expand Down
2 changes: 0 additions & 2 deletions playground/css/vite.config.js
Expand Up @@ -9,8 +9,6 @@ globalThis.window = {}
// @ts-expect-error refer to https://github.com/vitejs/vite/pull/11079
globalThis.location = new URL('http://localhost/')

/** @type {import('vite').UserConfig} */
// @ts-expect-error typecast
export default defineConfig({
build: {
cssTarget: 'chrome61',
Expand Down
2 changes: 0 additions & 2 deletions playground/define/vite.config.js
@@ -1,7 +1,5 @@
import { defineConfig } from 'vite'

/** @type {import('vite').UserConfig} */
// @ts-expect-error typecast
export default defineConfig({
define: {
__EXP__: 'false',
Expand Down
2 changes: 0 additions & 2 deletions playground/lib/vite.config.js
Expand Up @@ -2,8 +2,6 @@ import fs from 'node:fs'
import path from 'node:path'
import { defineConfig } from 'vite'

/** @type {import('vite').UserConfig} */
// @ts-expect-error typecast
export default defineConfig({
esbuild: {
supported: {
Expand Down

0 comments on commit c971f26

Please sign in to comment.