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(types): narrow down the return type of defineConfig #13792

Merged
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
8 changes: 4 additions & 4 deletions packages/vite/src/node/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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