Skip to content

Commit

Permalink
feat: ssrBuild flag in config env (#8863)
Browse files Browse the repository at this point in the history
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
  • Loading branch information
patak-dev and bluwy committed Jul 3, 2022
1 parent 8970f16 commit b6d655a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
6 changes: 4 additions & 2 deletions docs/config/index.md
Expand Up @@ -61,10 +61,10 @@ Vite also directly supports TS config files. You can use `vite.config.ts` with t

## Conditional Config

If the config needs to conditional determine options based on the command (`dev`/`serve` or `build`) or the [mode](/guide/env-and-mode) being used, it can export a function instead:
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:

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

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

Only `ssrBuild` is included instead of a more general `ssr` flag because, during dev, the config is shared by the single server handling SSR and non-SSR requests.

## Async Config

If the config needs to call async function, it can export a async function instead:
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/config.ts
Expand Up @@ -62,9 +62,11 @@ export type { RenderBuiltAssetUrl } from './build'

// NOTE: every export in this file is re-exported from ./index.ts so it will
// be part of the public API.

export interface ConfigEnv {
command: 'build' | 'serve'
mode: string
ssrBuild: boolean
}

/**
Expand Down Expand Up @@ -375,7 +377,8 @@ export async function resolveConfig(

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

let { configFile } = config
Expand Down
14 changes: 10 additions & 4 deletions playground/ssr-vue/vite.config.js
Expand Up @@ -10,7 +10,7 @@ const nestedVirtualId = '\0' + nestedVirtualFile

const base = '/test/'

export default defineConfig({
export default defineConfig(({ command, ssrBuild }) => ({
base,
plugins: [
vuePlugin(),
Expand All @@ -22,9 +22,15 @@ export default defineConfig({
return id
}
},
load(id) {
load(id, options) {
const ssrFromOptions = options?.ssr ?? false
if (id === '@foo') {
return `export default { msg: 'hi' }`
// Force a mismatch error if ssrBuild is different from ssrFromOptions
return `export default { msg: '${
command === 'build' && !!ssrBuild !== ssrFromOptions
? `defineConfig ssrBuild !== ssr from load options`
: 'hi'
}' }`
}
}
},
Expand Down Expand Up @@ -111,4 +117,4 @@ export default defineConfig({
optimizeDeps: {
exclude: ['example-external-component']
}
})
}))

0 comments on commit b6d655a

Please sign in to comment.