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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ssrBuild flag in config env #8863

Merged
merged 7 commits into from Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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']
}
})
}))