Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vite 8 (Rolldown-powered) configuration, plugin API, SSR, library mode, and Environment API reference.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/core-config.md
1---2name: vite-config3description: Vite configuration patterns using vite.config.ts4---56# Vite Configuration78## Basic Setup910```ts11// vite.config.ts12import { defineConfig } from 'vite'1314export default defineConfig({15// config options16})17```1819Vite auto-resolves `vite.config.ts` from project root. Supports ES modules syntax regardless of `package.json` type.2021## Conditional Config2223Export a function to access command and mode:2425```ts26export default defineConfig(({ command, mode, isSsrBuild, isPreview }) => {27if (command === 'serve') {28return { /* dev config */ }29} else {30return { /* build config */ }31}32})33```3435- `command`: `'serve'` during dev, `'build'` for production36- `mode`: `'development'` or `'production'` (or custom via `--mode`)3738## Async Config3940```ts41export default defineConfig(async ({ command, mode }) => {42const data = await fetchSomething()43return { /* config */ }44})45```4647## Using Environment Variables in Config4849`.env` files are loaded **after** config resolution. Use `loadEnv` to access them in config:5051```ts52import { defineConfig, loadEnv } from 'vite'5354export default defineConfig(({ mode }) => {55// Load env files from cwd, include all vars (empty prefix)56const env = loadEnv(mode, process.cwd(), '')5758return {59define: {60__APP_ENV__: JSON.stringify(env.APP_ENV),61},62server: {63port: env.APP_PORT ? Number(env.APP_PORT) : 5173,64},65}66})67```6869## Key Config Options7071### resolve.alias7273```ts74export default defineConfig({75resolve: {76alias: {77'@': '/src',78'~': '/src',79},80},81})82```8384### define (Global Constants)8586```ts87export default defineConfig({88define: {89__APP_VERSION__: JSON.stringify('1.0.0'),90__API_URL__: 'window.__backend_api_url',91},92})93```9495Values must be JSON-serializable or single identifiers. Non-strings auto-wrapped with `JSON.stringify`.9697### plugins9899```ts100import vue from '@vitejs/plugin-vue'101102export default defineConfig({103plugins: [vue()],104})105```106107Plugins array is flattened; falsy values ignored.108109### server.proxy110111```ts112export default defineConfig({113server: {114proxy: {115'/api': {116target: 'http://localhost:3000',117changeOrigin: true,118rewrite: (path) => path.replace(/^\/api/, ''),119},120},121},122})123```124125### build.target126127Default: Baseline Widely Available browsers. Customize:128129```ts130export default defineConfig({131build: {132target: 'esnext', // or 'es2020', ['chrome90', 'firefox88']133},134})135```136137## TypeScript Intellisense138139For plain JS config files:140141```js142/** @type {import('vite').UserConfig} */143export default {144// ...145}146```147148Or use `satisfies`:149150```ts151import type { UserConfig } from 'vite'152153export default {154// ...155} satisfies UserConfig156```157158<!--159Source references:160- https://vite.dev/config/161- https://vite.dev/guide/162-->163