npx skills add ...
npx skills add affaan-m/ecc --skill vite-patterns
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.
npx skills add affaan-m/ecc --skill 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.
vite.config.ts or vite.config.js.env filesbuild.libnode_modules/.vite, so subsequent starts skip the work.VITE_-prefixed vars become public constants in the bundle; everything unprefixed is invisible to client code.| Key | Default | Description |
|---|---|---|
root | '.' | Project root (where index.html lives) |
base | '/' | Public base path for deployed assets |
envPrefix | 'VITE_' | Prefix for client-exposed env vars |
build.outDir | 'dist' | Output directory |
build.minify | 'oxc' | Minifier ('oxc', 'terser', or false) |
build.sourcemap | false | true, 'inline', or 'hidden' |
Most plugin needs are covered by a handful of well-maintained packages. Reach for these before writing your own.
| Plugin | Purpose | When to use |
|---|---|---|
@vitejs/plugin-react-swc | React HMR + Fast Refresh via SWC | Default for React apps (faster than Babel variant) |
@vitejs/plugin-react | React HMR + Fast Refresh via Babel | Only if you need Babel plugins (emotion, MobX decorators) |
@vitejs/plugin-vue | Vue 3 SFC support | Vue apps |
vite-plugin-checker | Runs tsc + ESLint in worker thread with HMR overlay | Any TypeScript app — Vite does NOT type-check during vite build |
vite-tsconfig-paths | Honors tsconfig.json paths aliases | Any time you already have aliases in tsconfig.json |
vite-plugin-dts | Emits .d.ts files in library mode | Publishing TypeScript libraries |
vite-plugin-svgr | Imports SVGs as React components | React apps using SVGs as components |
rollup-plugin-visualizer | Bundle treemap/sunburst report | Periodic bundle size audits (use enforce: 'post') |
vite-plugin-pwa | Zero-config PWA + Workbox | Offline-capable apps |
Critical callout: vite build transpiles but does NOT type-check. Type errors silently ship to production unless you add vite-plugin-checker or run tsc --noEmit in CI.
Authoring is rare — most needs are covered by existing plugins. When you do need one, start inline in vite.config.ts and only extract if reused.
Key hooks: transform (modify source), resolveId + load (virtual modules), transformIndexHtml (inject into HTML), configureServer (add dev middleware), hotUpdate (custom HMR — replaces deprecated handleHotUpdate in v7+).
Virtual modules use the \0 prefix convention — resolveId returns '\0virtual:my-id' so other plugins skip it. User code imports 'virtual:my-id'.
For full plugin API, see vite.dev/guide/api-plugin. Use vite-plugin-inspect during development to debug the transform pipeline.
Framework plugins (@vitejs/plugin-react, @vitejs/plugin-vue, etc.) handle HMR automatically. Reach for import.meta.hot directly only when building custom state stores, dev tools, or framework-agnostic utilities that need to persist state across updates.
All import.meta.hot code is tree-shaken out of production builds — no guard removal needed.
Vite loads .env, .env.local, .env.[mode], and .env.[mode].local in that order (later overrides earlier); *.local files are gitignored and meant for local secrets.
Only VITE_-prefixed vars are exposed to client code:
VITE_ Prefix is NOT a Security BoundaryAny variable prefixed with VITE_ is statically inlined into the client bundle at build time. Minification, base64 encoding, and disabling source maps do NOT hide it. A determined attacker can extract any VITE_ var from the shipped JavaScript.
Rule: Only public values (API URLs, feature flags, public keys) go in VITE_ vars. Secrets (API tokens, database URLs, private keys) MUST live server-side behind an API or serverless function.
loadEnv('') TrapProduction source maps leak your original source code. Disable them unless you upload to an error tracker (Sentry, Bugsnag) and delete locally afterward:
.gitignore Checklist.env.local, .env.*.local — local secret overridesdist/ — build outputnode_modules/.vite — pre-bundle cache (stale entries cause phantom errors)For WebSocket proxying, add ws: true to the route config.
Barrel files (index.ts re-exporting everything from a directory) force Vite to load every re-exported file even when you import a single symbol. This is the #1 dev-server slowdown flagged by the official docs.
Each implicit extension forces up to 6 filesystem checks via resolve.extensions. In large codebases, this adds up.
Narrow tsconfig.json allowImportingTsExtensions + resolve.extensions to only the extensions you actually use.
server.warmup.clientFiles pre-transforms known hot entries before the browser requests them — eliminating the cold-load request waterfall on large apps.
When vite dev feels slow, start with vite --profile, interact with the app, then press p+enter to save a .cpuprofile. Load it in Speedscope to find which plugins are eating time — usually buildStart, config, or configResolved hooks in community plugins.
When publishing an npm package, use build.lib. Two footguns matter more than config detail:
vite-plugin-dts or run tsc --emitDeclarationOnly separately.Bare createServer({ middlewareMode: true }) setups are framework-author territory. Most apps should use Nuxt, Remix, SvelteKit, Astro, or TanStack Start instead. What you will tweak as a framework user is the externals config when deps break in SSR:
Vite pre-bundles dependencies to convert CJS/UMD to ESM and reduce request count.
Dev uses esbuild/Rolldown for transforms; build uses Rolldown for bundling. CJS libraries can behave differently between the two. Always verify with vite build && vite preview before deploying.
New builds produce new chunk hashes. Users with active sessions request old filenames that no longer exist. Vite has no built-in solution. Mitigations:
dist/assets/ files live for a deployment windowVite binds to localhost by default, which is unreachable from outside a container:
Vite restricts file serving to the project root. Packages outside root are blocked:
Process anti-patterns:
vite preview is NOT a production server — it is a smoke test for the built bundle. Deploy dist/ to a real static host (NGINX, Cloudflare Pages, Vercel static) or use a multi-stage Dockerfile.vite build to type-check — it only transpiles. Type errors silently ship to production. Add vite-plugin-checker or run tsc --noEmit in CI.@vitejs/plugin-legacy by default — it bloats bundles ~40%, breaks source-map bundle analyzers, and is unnecessary for the 95%+ of users on modern browsers. Gate it on real analytics, not assumption.resolve.alias entries that duplicate tsconfig.json paths — use vite-tsconfig-paths instead. Observed in Excalidraw and PostHog; avoid in new projects.node_modules/.vite after dep changes — pre-bundle cache causes phantom errors. Clear it when switching branches or after patching deps.| Pattern | When to Use |
|---|---|
defineConfig | Always — provides type inference |
loadEnv(mode, root, ['VITE_']) | Access env vars in config (explicit prefix) |
vite-plugin-checker | Any TypeScript app (fills the type-check gap) |
vite-tsconfig-paths | Instead of hand-rolled resolve.alias |
optimizeDeps.include | CJS deps causing interop issues |
server.proxy | Route API requests to backend in dev |
server.host: true | Docker, containers, remote access |
server.warmup.clientFiles | Pre-transform hot-path routes |
build.lib + external | Publishing npm packages |
manualChunks (object) | Vendor bundle splitting |
vite --profile | Debug slow dev server |
vite build && vite preview | Smoke-test prod bundle locally (NOT a prod server) |
frontend-patterns — React component patternsdocker-patterns — containerized dev with Vitenextjs-turbopack — alternative bundler for Next.js