Skip to content

Commit

Permalink
feat!: add isPreview to ConfigEnv and resolveConfig (#14855)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Nov 3, 2023
1 parent bbd7461 commit d195860
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
6 changes: 3 additions & 3 deletions docs/config/index.md
Expand Up @@ -48,10 +48,10 @@ Vite also directly supports TS config files. You can use `vite.config.ts` with t

## Conditional Config

If the config needs to conditionally determine options based on the command (`dev`/`serve` or `build`), the [mode](/guide/env-and-mode) being used, or if it is an SSR build (`ssrBuild`), it can export a function instead:
If the config needs to conditionally determine options based on the command (`serve` or `build`), the [mode](/guide/env-and-mode) being used, if it's an SSR build (`isSsrBuild`), or is previewing the build (`isPreview`), it can export a function instead:

```js
export default defineConfig(({ command, mode, ssrBuild }) => {
export default defineConfig(({ command, mode, isSsrBuild, isPreview }) => {
if (command === 'serve') {
return {
// dev specific config
Expand All @@ -67,7 +67,7 @@ export default defineConfig(({ command, mode, ssrBuild }) => {

It is important to note that in Vite's API the `command` value is `serve` during dev (in the cli `vite`, `vite dev`, and `vite serve` are aliases), and `build` when building for production (`vite build`).

`ssrBuild` is experimental. It is only available during build instead of a more general `ssr` flag because, during dev, the config is shared by the single server handling SSR and non-SSR requests. The value could be `undefined` for tools that don't have separate commands for the browser and SSR build, so use explicit comparison against `true` and `false`.
`isSsrBuild` and `isPreview` are additional optional flags to differentiate the kind of `build` and `serve` commands respectively. Some tools that load the Vite config may not support these flags and will pass `undefined` instead. Hence, it's recommended to use explicit comparison against `true` and `false`.

## Async Config

Expand Down
4 changes: 3 additions & 1 deletion docs/guide/api-javascript.md
Expand Up @@ -251,10 +251,12 @@ async function resolveConfig(
inlineConfig: InlineConfig,
command: 'build' | 'serve',
defaultMode = 'development',
defaultNodeEnv = 'development',
isPreview = false,
): Promise<ResolvedConfig>
```

The `command` value is `serve` in dev (in the cli `vite`, `vite dev`, and `vite serve` are aliases).
The `command` value is `serve` in dev and preview, and `build` in build.

## `mergeConfig`

Expand Down
2 changes: 2 additions & 0 deletions docs/guide/migration.md
Expand Up @@ -221,6 +221,8 @@ Also there are other breaking changes which only affect few users.
- In the past, when a library `"exports"` field maps to an `.mjs` file, Vite will still try to match the `"browser"` and `"module"` fields to fix compatibility with certain libraries. This behavior is now removed to align with the exports resolution algorithm.
- [[#14733] feat(resolve)!: remove `resolve.browserField`](https://github.com/vitejs/vite/pull/14733)
- `resolve.browserField` has been deprecated since Vite 3 in favour of an updated default of `['browser', 'module', 'jsnext:main', 'jsnext']` for [`resolve.mainFields`](/config/shared-options.md#resolve-mainfields).
- [[#14855] feat!: add isPreview to ConfigEnv and resolveConfig](https://github.com/vitejs/vite/pull/14855)
- Renamed `ssrBuild` to `isSsrBuild` in the `ConfigEnv` object.

## Migration from v3

Expand Down
12 changes: 6 additions & 6 deletions packages/vite/src/node/config.ts
Expand Up @@ -77,10 +77,8 @@ const promisifiedRealpath = promisify(fs.realpath)
export interface ConfigEnv {
command: 'build' | 'serve'
mode: string
/**
* @experimental
*/
ssrBuild?: boolean
isSsrBuild?: boolean
isPreview?: boolean
}

/**
Expand Down Expand Up @@ -404,6 +402,7 @@ export async function resolveConfig(
command: 'build' | 'serve',
defaultMode = 'development',
defaultNodeEnv = 'development',
isPreview = false,
): Promise<ResolvedConfig> {
let config = inlineConfig
let configFileDependencies: string[] = []
Expand All @@ -417,10 +416,11 @@ export async function resolveConfig(
process.env.NODE_ENV = defaultNodeEnv
}

const configEnv = {
const configEnv: ConfigEnv = {
mode,
command,
ssrBuild: !!config.build?.ssr,
isSsrBuild: !!config.build?.ssr,
isPreview,
}

let { configFile } = config
Expand Down

0 comments on commit d195860

Please sign in to comment.