From b6d655ac73631bfdcc41c4534652c864dcc00b76 Mon Sep 17 00:00:00 2001 From: patak Date: Sun, 3 Jul 2022 08:37:05 +0200 Subject: [PATCH] feat: ssrBuild flag in config env (#8863) Co-authored-by: Bjorn Lu --- docs/config/index.md | 6 ++++-- packages/vite/src/node/config.ts | 5 ++++- playground/ssr-vue/vite.config.js | 14 ++++++++++---- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index 73a20082295304..cab69067574a3b 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -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 @@ -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: diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index b7c85723d5e71b..7325a736644b00 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -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 } /** @@ -375,7 +377,8 @@ export async function resolveConfig( const configEnv = { mode, - command + command, + ssrBuild: !!config.build?.ssr } let { configFile } = config diff --git a/playground/ssr-vue/vite.config.js b/playground/ssr-vue/vite.config.js index 50368bb46b8500..6d8073ad6755d4 100644 --- a/playground/ssr-vue/vite.config.js +++ b/playground/ssr-vue/vite.config.js @@ -10,7 +10,7 @@ const nestedVirtualId = '\0' + nestedVirtualFile const base = '/test/' -export default defineConfig({ +export default defineConfig(({ command, ssrBuild }) => ({ base, plugins: [ vuePlugin(), @@ -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' + }' }` } } }, @@ -111,4 +117,4 @@ export default defineConfig({ optimizeDeps: { exclude: ['example-external-component'] } -}) +}))