Setting the file. One moment.
Subchapter 11.2
references/core-config.mdMarkdown3 KBView on GitHub
// vite.config.ts
import { defineConfig } from 'vite'
export default defineConfig({
// config options
})Vite auto-resolves vite.config.ts from project root. Supports ES modules syntax regardless of package.json type.
Export a function to access command and mode:
export default defineConfig(({ command, mode, isSsrBuild, isPreview }) => {
if (command === 'serve') {
return { /* dev config */ }
} else {
return { /* build config */ }
}
})command: 'serve' during dev, 'build' for productionmode: 'development' or 'production' (or custom via --mode)export default defineConfig(async ({ command, mode }) => {
const data = await fetchSomething()
return { /* config */ }
}).env files are loaded after config resolution. Use loadEnv to access them in config:
import { defineConfig, loadEnv } from 'vite'
export default defineConfig(({ mode }) => {
// Load env files from cwd, include all vars (empty prefix)
const env = loadEnv(mode, process.cwd(), '')
return {
define: {
__APP_ENV__: JSON.stringify(env.APP_ENV),
},
server: {
port: env.APP_PORT ? Number(env.APP_PORT) : 5173,
},
}
})export default defineConfig({
resolve: {
alias: {
'@': '/src',
'~': '/src',
},
},
})export default defineConfig({
define: {
__APP_VERSION__: JSON.stringify('1.0.0'),
__API_URL__: 'window.__backend_api_url',
},
})Values must be JSON-serializable or single identifiers. Non-strings auto-wrapped with JSON.stringify.
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
})Plugins array is flattened; falsy values ignored.
export default defineConfig({
server: {
proxy: {
'/api': {
target: 'http://localhost:3000',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
},
},
},
})Default: Baseline Widely Available browsers. Customize:
export default defineConfig({
build: {
target: 'esnext', // or 'es2020', ['chrome90', 'firefox88']
},
})For plain JS config files:
/** @type {import('vite').UserConfig} */
export default {
// ...
}Or use satisfies:
import type { UserConfig } from 'vite'
export default {
// ...
} satisfies UserConfig