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 isPreview to ConfigEnv and resolveConfig #14855

Merged
merged 5 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 2 additions & 4 deletions docs/config/index.md
Original file line number Diff line number Diff line change
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,8 +67,6 @@ 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`.

## Async Config

If the config needs to call async functions, it can export an async function instead. And this async function can also be passed through `defineConfig` for improved intellisense support:
Expand Down
4 changes: 3 additions & 1 deletion docs/guide/api-javascript.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,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
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ const promisifiedRealpath = promisify(fs.realpath)
export interface ConfigEnv {
command: 'build' | 'serve'
mode: string
/**
* @experimental
*/
ssrBuild?: boolean
isSsrBuild: boolean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the plan for avoiding #8912? (https://discord.com/channels/804011606160703521/831456449632534538/993429210867707914)
For example, are we going to say "call loadConfigFromFile for each build separately"?
Otherwise, I guess vitepress won't be able to pass the correct value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea I'm thinking is that, if you don't know or can't be sure what it is, you can simply set false (we do the same for vitestSetup.ts in this PR). Maybe it makes more sense to allow undefined to convey the "i don't know part"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a commit that makes them undefined-able

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
3 changes: 3 additions & 0 deletions playground/vitestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ export async function startDefaultServe(): Promise<void> {
const configEnv: ConfigEnv = {
command: isBuild ? 'build' : 'serve',
mode: isBuild ? 'production' : 'development',
// we're unable to differentiate these properties so just set false
isSsrBuild: false,
isPreview: false,
}

// config file named by convention as the *.spec.ts folder
Expand Down