Skip to content

Commit

Permalink
feat: add async support for vite config file (#2758)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiangmoe committed Mar 31, 2021
1 parent b08e973 commit aee8b37
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
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

0 comments on commit aee8b37

Please sign in to comment.