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 3 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
4 changes: 2 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 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
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 | string
}

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

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

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']
}
})
}))