From b97cf91e38f842d336ab5a851633592554e66c14 Mon Sep 17 00:00:00 2001 From: patak-dev Date: Thu, 30 Jun 2022 08:07:11 +0200 Subject: [PATCH 1/5] feat: ssrBuild flag in config env --- docs/config/index.md | 4 ++-- packages/vite/src/node/config.ts | 5 ++++- playground/ssr-vue/vite.config.js | 14 ++++++++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index 73a20082295304..35991ae0da5d75 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 conditional determine options based on the command (`dev`/`serve` or `build`), the [mode](/guide/env-and-mode) being used, or if the build is for a SSR entry, 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 diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 38da3cf5503768..80419b3b88fdd8 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.ssr || !!config.build?.ssr } let { configFile } = config diff --git a/playground/ssr-vue/vite.config.js b/playground/ssr-vue/vite.config.js index df509f63268a47..8c8a489261c4ce 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' + }' }` } } }, @@ -128,4 +134,4 @@ export default defineConfig({ optimizeDeps: { exclude: ['example-external-component'] } -}) +})) From aed31c7c63091de7e8975c988a33b326139410a2 Mon Sep 17 00:00:00 2001 From: patak-dev Date: Thu, 30 Jun 2022 09:22:30 +0200 Subject: [PATCH 2/5] chore: update --- packages/vite/src/node/config.ts | 4 ++-- playground/ssr-vue/vite.config.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index c81a4169c98d5d..9940c92c698f0a 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -66,7 +66,7 @@ export type { RenderBuiltAssetUrl } from './build' export interface ConfigEnv { command: 'build' | 'serve' mode: string - ssrBuild: boolean + ssrBuild: boolean | string } /** @@ -378,7 +378,7 @@ export async function resolveConfig( const configEnv = { mode, command, - ssrBuild: !!config.ssr || !!config.build?.ssr + ssrBuild: config.build?.ssr ?? false } let { configFile } = config diff --git a/playground/ssr-vue/vite.config.js b/playground/ssr-vue/vite.config.js index d21ede22a4bfe6..6d8073ad6755d4 100644 --- a/playground/ssr-vue/vite.config.js +++ b/playground/ssr-vue/vite.config.js @@ -27,7 +27,7 @@ export default defineConfig(({ command, ssrBuild }) => ({ if (id === '@foo') { // Force a mismatch error if ssrBuild is different from ssrFromOptions return `export default { msg: '${ - command === 'build' && ssrBuild !== ssrFromOptions + command === 'build' && !!ssrBuild !== ssrFromOptions ? `defineConfig ssrBuild !== ssr from load options` : 'hi' }' }` From 03f30dc1597570410bf203c99a040f226215aa6f Mon Sep 17 00:00:00 2001 From: patak-dev Date: Sat, 2 Jul 2022 18:12:56 +0200 Subject: [PATCH 3/5] refactor: ssrBuild is boolean only --- docs/config/index.md | 4 +++- packages/vite/src/node/config.ts | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index 35991ae0da5d75..d57c4b41727e55 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -61,7 +61,7 @@ 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`), the [mode](/guide/env-and-mode) being used, or if the build is for a SSR entry, it can export a function instead: +If the config needs to conditional determine options based on the command (`dev`/`serve` or `build`), the [mode](/guide/env-and-mode) being used, or if it is a SSR build (`ssrBuild`), it can export a function instead: ```js export default defineConfig(({ command, mode, ssrBuild }) => { @@ -78,6 +78,8 @@ export default defineConfig(({ command, mode, ssrBuild }) => { }) ``` +Only `ssrBuild` is included, as during dev, the config creates a single server shared between the SSR and non-SSR requests. + 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`). ## Async Config diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index dd1df4dab9b39d..7325a736644b00 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -66,7 +66,7 @@ export type { RenderBuiltAssetUrl } from './build' export interface ConfigEnv { command: 'build' | 'serve' mode: string - ssrBuild: boolean | string + ssrBuild: boolean } /** @@ -378,7 +378,7 @@ export async function resolveConfig( const configEnv = { mode, command, - ssrBuild: config.build?.ssr ?? false + ssrBuild: !!config.build?.ssr } let { configFile } = config From 48efde7171ad079f3ada694e0779ef0cbe34692c Mon Sep 17 00:00:00 2001 From: patak Date: Sun, 3 Jul 2022 07:11:34 +0200 Subject: [PATCH 4/5] chore: typos Co-authored-by: Bjorn Lu --- docs/config/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/config/index.md b/docs/config/index.md index d57c4b41727e55..b30628f07d523f 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -61,7 +61,7 @@ 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`), the [mode](/guide/env-and-mode) being used, or if it is a SSR build (`ssrBuild`), 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, ssrBuild }) => { From 15b264b8d0d1d6cea83a42eb2746309985972e5b Mon Sep 17 00:00:00 2001 From: patak Date: Sun, 3 Jul 2022 07:14:16 +0200 Subject: [PATCH 5/5] chore: better ordering --- docs/config/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index b30628f07d523f..cab69067574a3b 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -78,10 +78,10 @@ export default defineConfig(({ command, mode, ssrBuild }) => { }) ``` -Only `ssrBuild` is included, as during dev, the config creates a single server shared between the SSR and non-SSR requests. - 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: