Vite Patterns
Build tool and dev server patterns for Vite 8+ projects. Covers configuration, environment variables, proxy setup, library mode, dependency pre-bundling, and common production pitfalls.
When to Use
- Configuring
vite.config.ts or vite.config.js
- Setting up environment variables or
.env files
- Configuring dev server proxy for API backends
- Optimizing build output (chunks, minification, assets)
- Publishing libraries with
build.lib
- Troubleshooting dependency pre-bundling or CJS/ESM interop
- Debugging HMR, dev server, or build errors
- Choosing or ordering Vite plugins
How It Works
- Dev mode serves source files as native ESM — no bundling. Transforms happen on-demand per module request, which is why cold starts are fast and HMR is precise.
- Build mode uses Rolldown (v7+) or Rollup (v5–v6) to bundle the app for production with tree-shaking, code-splitting, and Oxc-based minification.
- Dependency pre-bundling converts CJS/UMD deps to ESM once via esbuild and caches the result under
node_modules/.vite, so subsequent starts skip the work.
- Plugins share a unified interface across dev and build — the same plugin object works for both the dev server's on-demand transforms and the production pipeline.
- Environment variables are statically inlined at build time.
VITE_-prefixed vars become public constants in the bundle; everything unprefixed is invisible to client code.
Examples
Config Structure
Basic Config
// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
resolve: {
alias: { '@': new URL('./src', import.meta.url).pathname },
},
})
Conditional Config
// vite.config.ts
import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd()) // VITE_ prefixed only (safe)
return {
plugins: [react()],
server: command === 'serve' ? { port: 3000 } : undefined,
define: {
__API_URL__: JSON.stringify(env.VITE_API_URL),
},
1---2name: vite-patterns3description: "Use when vite build tool patterns including config, plugins, HMR, env variables, proxy setup, SSR, library mode, dependency pre-bundling, and build optimization. Activate when working with vite.config.ts, Vite plugins, or Vite-based projects. Triggers on \"vite-patterns\", \"vite patterns\", \"patterns\"."4metadata:5 source: https://skillmd.com/skills/majinmagros/vite-patterns6 origin: ECC7---89# Vite Patterns1011Build tool and dev server patterns for Vite 8+ projects. Covers configuration, environment variables, proxy setup, library mode, dependency pre-bundling, and common production pitfalls.1213## When to Use1415- Configuring `vite.config.ts` or `vite.config.js`16- Setting up environment variables or `.env` files17- Configuring dev server proxy for API backends18- Optimizing build output (chunks, minification, assets)19- Publishing libraries with `build.lib`20- Troubleshooting dependency pre-bundling or CJS/ESM interop21- Debugging HMR, dev server, or build errors22- Choosing or ordering Vite plugins2324## How It Works2526- **Dev mode** serves source files as native ESM — no bundling. Transforms happen on-demand per module request, which is why cold starts are fast and HMR is precise.27- **Build mode** uses Rolldown (v7+) or Rollup (v5–v6) to bundle the app for production with tree-shaking, code-splitting, and Oxc-based minification.28- **Dependency pre-bundling** converts CJS/UMD deps to ESM once via esbuild and caches the result under `node_modules/.vite`, so subsequent starts skip the work.29- **Plugins** share a unified interface across dev and build — the same plugin object works for both the dev server's on-demand transforms and the production pipeline.30- **Environment variables** are statically inlined at build time. `VITE_`-prefixed vars become public constants in the bundle; everything unprefixed is invisible to client code.3132## Examples3334### Config Structure3536#### Basic Config3738```typescript39// vite.config.ts40import { defineConfig } from 'vite'41import react from '@vitejs/plugin-react'4243export default defineConfig({44 plugins: [react()],45 resolve: {46 alias: { '@': new URL('./src', import.meta.url).pathname },47 },48})49```5051#### Conditional Config5253```typescript54// vite.config.ts55import { defineConfig, loadEnv } from 'vite'56import react from '@vitejs/plugin-react'5758export default defineConfig(({ command, mode }) => {59 const env = loadEnv(mode, process.cwd()) // VITE_ prefixed only (safe)6061 return {62 plugins: [react()],63 server: command === 'serve' ? { port: 3000 } : undefined,64 define: {65 __API_URL__: JSON.stringify(env.VITE_API_URL),66 },