Skip to content

Commit cb28b1f

Browse files
authored
feat!: add isPreview to ConfigEnv and resolveConfig (#14855)
1 parent 28e69ed commit cb28b1f

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

config/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ Vite also directly supports TS config files. You can use `vite.config.ts` with t
4848

4949
## Conditional Config
5050

51-
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:
51+
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:
5252

5353
```js
54-
export default defineConfig(({ command, mode, ssrBuild }) => {
54+
export default defineConfig(({ command, mode, isSsrBuild, isPreview }) => {
5555
if (command === 'serve') {
5656
return {
5757
// dev specific config
@@ -67,7 +67,7 @@ export default defineConfig(({ command, mode, ssrBuild }) => {
6767

6868
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`).
6969

70-
`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`.
70+
`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`.
7171

7272
## Async Config
7373

guide/api-javascript.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,12 @@ async function resolveConfig(
251251
inlineConfig: InlineConfig,
252252
command: 'build' | 'serve',
253253
defaultMode = 'development',
254+
defaultNodeEnv = 'development',
255+
isPreview = false,
254256
): Promise<ResolvedConfig>
255257
```
256258

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

259261
## `mergeConfig`
260262

guide/migration.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ Also there are other breaking changes which only affect few users.
221221
- 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.
222222
- [[#14733] feat(resolve)!: remove `resolve.browserField`](https://github.com/vitejs/vite/pull/14733)
223223
- `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).
224+
- [[#14855] feat!: add isPreview to ConfigEnv and resolveConfig](https://github.com/vitejs/vite/pull/14855)
225+
- Renamed `ssrBuild` to `isSsrBuild` in the `ConfigEnv` object.
224226

225227
## Migration from v3
226228

0 commit comments

Comments
 (0)