From d19586061639295257c7ea13b9ed68745aa90224 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Fri, 3 Nov 2023 23:53:34 +0800 Subject: [PATCH] feat!: add isPreview to ConfigEnv and resolveConfig (#14855) --- docs/config/index.md | 6 +++--- docs/guide/api-javascript.md | 4 +++- docs/guide/migration.md | 2 ++ packages/vite/src/node/config.ts | 12 ++++++------ 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index b002c6212224c1..6932c7f29dc595 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -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 @@ -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 diff --git a/docs/guide/api-javascript.md b/docs/guide/api-javascript.md index b51f5f4a4bcb51..1ff419058f2706 100644 --- a/docs/guide/api-javascript.md +++ b/docs/guide/api-javascript.md @@ -251,10 +251,12 @@ async function resolveConfig( inlineConfig: InlineConfig, command: 'build' | 'serve', defaultMode = 'development', + defaultNodeEnv = 'development', + isPreview = false, ): Promise ``` -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` diff --git a/docs/guide/migration.md b/docs/guide/migration.md index 44726ac0050310..ca121e1096f3f7 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -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 diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 18d7ab430672fb..a78b026c6ecf44 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -77,10 +77,8 @@ const promisifiedRealpath = promisify(fs.realpath) export interface ConfigEnv { command: 'build' | 'serve' mode: string - /** - * @experimental - */ - ssrBuild?: boolean + isSsrBuild?: boolean + isPreview?: boolean } /** @@ -404,6 +402,7 @@ export async function resolveConfig( command: 'build' | 'serve', defaultMode = 'development', defaultNodeEnv = 'development', + isPreview = false, ): Promise { let config = inlineConfig let configFileDependencies: string[] = [] @@ -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