Skip to content

Commit b6d655a

Browse files
patak-devbluwy
andauthored
feat: ssrBuild flag in config env (#8863)
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
1 parent 8970f16 commit b6d655a

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

docs/config/index.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ Vite also directly supports TS config files. You can use `vite.config.ts` with t
6161

6262
## Conditional Config
6363

64-
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:
64+
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:
6565

6666
```js
67-
export default defineConfig(({ command, mode }) => {
67+
export default defineConfig(({ command, mode, ssrBuild }) => {
6868
if (command === 'serve') {
6969
return {
7070
// dev specific config
@@ -80,6 +80,8 @@ export default defineConfig(({ command, mode }) => {
8080

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

83+
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.
84+
8385
## Async Config
8486

8587
If the config needs to call async function, it can export a async function instead:

packages/vite/src/node/config.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ export type { RenderBuiltAssetUrl } from './build'
6262

6363
// NOTE: every export in this file is re-exported from ./index.ts so it will
6464
// be part of the public API.
65+
6566
export interface ConfigEnv {
6667
command: 'build' | 'serve'
6768
mode: string
69+
ssrBuild: boolean
6870
}
6971

7072
/**
@@ -375,7 +377,8 @@ export async function resolveConfig(
375377

376378
const configEnv = {
377379
mode,
378-
command
380+
command,
381+
ssrBuild: !!config.build?.ssr
379382
}
380383

381384
let { configFile } = config

playground/ssr-vue/vite.config.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const nestedVirtualId = '\0' + nestedVirtualFile
1010

1111
const base = '/test/'
1212

13-
export default defineConfig({
13+
export default defineConfig(({ command, ssrBuild }) => ({
1414
base,
1515
plugins: [
1616
vuePlugin(),
@@ -22,9 +22,15 @@ export default defineConfig({
2222
return id
2323
}
2424
},
25-
load(id) {
25+
load(id, options) {
26+
const ssrFromOptions = options?.ssr ?? false
2627
if (id === '@foo') {
27-
return `export default { msg: 'hi' }`
28+
// Force a mismatch error if ssrBuild is different from ssrFromOptions
29+
return `export default { msg: '${
30+
command === 'build' && !!ssrBuild !== ssrFromOptions
31+
? `defineConfig ssrBuild !== ssr from load options`
32+
: 'hi'
33+
}' }`
2834
}
2935
}
3036
},
@@ -111,4 +117,4 @@ export default defineConfig({
111117
optimizeDeps: {
112118
exclude: ['example-external-component']
113119
}
114-
})
120+
}))

0 commit comments

Comments
 (0)