@tank/vite-mastery
1.0.0Skill
Description
Comprehensive Vite build tool mastery. Covers vite.config.ts, dev server (HMR, proxy), build optimization (Rollup, manual chunks, code splitting, minification), plugin development (hooks, virtual modules), SSR, library mode, PWA, environment config, and performance profiling. Synthesizes Vite docs, Rollup docs, esbuild docs, Evan You.
Triggered by
vitevite configvite buildHMRvite pluginSSR
Download
Verified
tank install @tank/vite-masteryVite Mastery
Configure, optimize, and extend Vite builds for any frontend project.
Core Philosophy
- Unbundled dev, bundled prod. Dev uses native ESM for speed; prod uses Rollup for optimization. Understand both pipelines.
- Convention over configuration. Vite works out of the box. Only configure what you measured needs changing.
- Measure before splitting. Never add manual chunks, change minifiers, or adjust thresholds without profiling first.
- Plugins are Rollup hooks + Vite extras. Master Rollup's plugin interface and Vite's extensions on top of it.
- Environment isolation is non-negotiable. Env variables must never leak between modes or between client and server.
Quick-Start: Common Problems
"Bundle too large"
- Run
npx vite-bundle-visualizeror addrollup-plugin-visualizerto see what's in the bundle. - Check for accidentally bundled server-only code or dev dependencies.
- Apply manual chunks for large vendor libraries.
-> See
references/build-optimization.mdfor chunk splitting strategies.
"HMR not working"
- Verify the file is within the project root (files outside root bypass HMR).
- Check for circular imports — Vite's HMR propagation fails on cycles.
- Ensure the framework plugin is loaded (
@vitejs/plugin-react,@vitejs/plugin-vue). - Check
server.hmrconfig if behind a reverse proxy. -> Seereferences/configuration-and-dev-server.mdfor proxy and HMR config.
"Module not found / resolve errors"
- Check
resolve.aliaspaths — usepath.resolve(__dirname, ...)orfileURLToPath. - Verify
optimizeDeps.includefor CJS dependencies that fail pre-bundling. - Check
ssr.noExternalfor SSR builds that need to bundle specific packages. -> Seereferences/configuration-and-dev-server.mdfor resolve configuration.
"Env variables undefined"
- Confirm the variable is prefixed with
VITE_(onlyVITE_*vars are exposed to client). - Use
import.meta.env.VITE_*— notprocess.env. - Verify the
.envfile matches the current mode (.env.production,.env.staging). -> Seereferences/environment-pwa-tooling.mdfor env and mode configuration.
Configuration Decision Trees
| Signal | Recommendation |
|---|---|
| Need path shortcuts | resolve.alias with absolute paths |
| CJS dependency fails in dev | Add to optimizeDeps.include |
| Need compile-time constants | define with JSON.stringify |
| Need different config per mode | Export a function: export default defineConfig(({ mode }) => ...) |
| API calls hit CORS in dev | server.proxy with changeOrigin: true |
| Bundle > 500kB gzipped | Profile with visualizer, apply manual chunks |
| Building a library | Use build.lib with external dependencies |
| Need SSR | Configure ssr.entry and ssr.noExternal |
| Need offline support | Add vite-plugin-pwa with workbox strategies |
Build Optimization Decision Tree
| Symptom | Diagnostic | Fix |
|---|---|---|
| Large single chunk | No code splitting | Add dynamic imports at route boundaries |
| Vendor chunk > 250kB | Single vendor bundle | Split with manualChunks by domain |
| Duplicate modules across chunks | Shared deps not extracted | Rollup handles this automatically; verify with visualizer |
| Slow minification | Using terser | Switch to esbuild (default) unless terser features needed |
| Large CSS bundle | All CSS in one file | Enable build.cssCodeSplit (default true) |
| Small assets bloating requests | Too many HTTP requests for tiny files | Increase build.assetsInlineLimit (default 4096 bytes) |
Plugin Selection Guide
| Need | Plugin | Notes |
|---|---|---|
| React Fast Refresh | @vitejs/plugin-react | Uses Babel. Use @vitejs/plugin-react-swc for speed. |
| Vue SFC support | @vitejs/plugin-vue | Required for .vue files |
| Svelte support | @sveltejs/vite-plugin-svelte | Required for .svelte files |
| Legacy browser support | @vitejs/plugin-legacy | Generates polyfilled chunks |
| PWA / service worker | vite-plugin-pwa | Workbox integration |
| Bundle analysis | rollup-plugin-visualizer | Add in build only |
| SVG as components | vite-plugin-svgr (React) | Framework-specific |
| Environment validation | @t3-oss/env-core | Validates env at build time |
Anti-Patterns
| Anti-Pattern | Why It Fails | Fix |
|---|---|---|
Importing process.env in client code | Undefined in browser; Vite uses import.meta.env | Replace with import.meta.env.VITE_* |
Using __dirname in vite.config.ts | Fails with ESM config format | Use fileURLToPath(new URL('.', import.meta.url)) |
| Splitting every dependency into its own chunk | Creates waterfall of HTTP requests | Group by domain (e.g., react-vendor, ui-vendor) |
| Disabling pre-bundling globally | Breaks dev server performance | Only exclude specific packages with optimizeDeps.exclude |
Putting secrets in VITE_* env vars | Exposed in client bundle | Only prefix public values with VITE_ |
| Running terser in dev | Destroys dev server speed | Terser is prod-only by default; never change this |
Giant vite.config.ts with all logic inline | Unmaintainable | Extract plugin factories and helper functions |
Ignoring build.rollupOptions.output.manualChunks return value | Orphaned modules in wrong chunks | Always return a string or undefined, never skip |
Reference Index
| File | Contents |
|---|---|
references/configuration-and-dev-server.md | vite.config.ts structure, resolve aliases, define, conditional config, dev server, HMR, proxy, HTTPS, middleware |
references/build-optimization.md | Rollup options, manual chunks, code splitting strategies, minification, CSS code splitting, asset inlining, tree-shaking |
references/plugin-development.md | Plugin hooks, virtual modules, transform pipeline, enforce ordering, HMR API, plugin testing |
references/ssr-and-library-mode.md | SSR entry, externalization, streaming, client hydration, build.lib, external deps, multiple formats, CSS extraction |
references/environment-pwa-tooling.md | .env files, import.meta.env, mode config, vite-plugin-pwa, workbox strategies, rollup-plugin-visualizer, optimizeDeps |