Vite Build Tool Cheat Sheet
Quick reference guide for Vite: vite.config.ts, plugin configuration, environment variables, and dev server.
Build Tools
vite
build-tool
frontend
Vite is a modern frontend build tool that provides a fast development server using native ES Modules (ESM) and bundles production code with Rollup.
Configuration File Example (`vite.config.ts`)
typescript
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import path from "node:path";
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src")
}
},
server: {
port: 3000,
open: true,
proxy: {
"/api": {
target: "http://localhost:8080",
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, "")
}
}
},
build: {
outDir: "dist",
sourcemap: true
}
});
Environment Variables (`import.meta.env`)
Vite exposes environment variables on the special import.meta.env object.
Table
| Variable Name | Purpose / Output |
|---|---|
import.meta.env.VITE_APP_TITLE | Custom env variable (MUST start with VITE_) |
import.meta.env.MODE | Current running app mode (development or production) |
import.meta.env.BASE_URL | Base URL path configured in vite.config.ts |
import.meta.env.PROD | Boolean flag (true during production build) |
import.meta.env.DEV | Boolean flag (true during local dev server) |
Essential Vite CLI Commands
Table
| Command | Action / Purpose |
|---|---|
npx create-vite app-name --template react-ts | Scaffold new project template |
npx vite | Start local HMR development server |
npx vite build | Compile optimized production bundle to dist/ |
npx vite preview | Boot local HTTP server to preview dist/ production bundle |
Common Pitfalls & Tips
[!WARNING] Environment variables must be prefixed with
VITE_(e.g.VITE_API_URL) to be exposed to client-side frontend code bundle!
[!TIP] Use
resolve.aliasinvite.config.tsalong withcompilerOptions.pathsintsconfig.jsonfor clean path imports like@/components/Button.