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

feat: add async support for vite config file #2758

Merged
merged 1 commit into from
Mar 31, 2021
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
13 changes: 13 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ export default ({ command, mode }) => {
}
```

### Async Config

If the config needs to call async function, it can export a async function instead:

```js
export default async ({ command, mode }) => {
const data = await asyncFunction();
return {
// build specific config
}
}
```

## Shared Options

### root
Expand Down
9 changes: 5 additions & 4 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export interface ConfigEnv {
mode: string
}

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

/**
* Type helper to make it easier to use vite.config.ts
Expand Down Expand Up @@ -707,8 +707,9 @@ export async function loadConfigFromFile(
debug(`bundled config file loaded in ${Date.now() - start}ms`)
}

const config =
typeof userConfig === 'function' ? userConfig(configEnv) : userConfig
const config = await (typeof userConfig === 'function'
? userConfig(configEnv)
: userConfig)
if (!isObject(config)) {
throw new Error(`config must export or return an object.`)
}
Expand Down