Skip to content

@tank/vite-mastery

1.0.0
Skill

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-mastery

Vite Mastery

Configure, optimize, and extend Vite builds for any frontend project.

Core Philosophy

  1. Unbundled dev, bundled prod. Dev uses native ESM for speed; prod uses Rollup for optimization. Understand both pipelines.
  2. Convention over configuration. Vite works out of the box. Only configure what you measured needs changing.
  3. Measure before splitting. Never add manual chunks, change minifiers, or adjust thresholds without profiling first.
  4. Plugins are Rollup hooks + Vite extras. Master Rollup's plugin interface and Vite's extensions on top of it.
  5. Environment isolation is non-negotiable. Env variables must never leak between modes or between client and server.

Quick-Start: Common Problems

"Bundle too large"

  1. Run npx vite-bundle-visualizer or add rollup-plugin-visualizer to see what's in the bundle.
  2. Check for accidentally bundled server-only code or dev dependencies.
  3. Apply manual chunks for large vendor libraries. -> See references/build-optimization.md for chunk splitting strategies.

"HMR not working"

  1. Verify the file is within the project root (files outside root bypass HMR).
  2. Check for circular imports — Vite's HMR propagation fails on cycles.
  3. Ensure the framework plugin is loaded (@vitejs/plugin-react, @vitejs/plugin-vue).
  4. Check server.hmr config if behind a reverse proxy. -> See references/configuration-and-dev-server.md for proxy and HMR config.

"Module not found / resolve errors"

  1. Check resolve.alias paths — use path.resolve(__dirname, ...) or fileURLToPath.
  2. Verify optimizeDeps.include for CJS dependencies that fail pre-bundling.
  3. Check ssr.noExternal for SSR builds that need to bundle specific packages. -> See references/configuration-and-dev-server.md for resolve configuration.

"Env variables undefined"

  1. Confirm the variable is prefixed with VITE_ (only VITE_* vars are exposed to client).
  2. Use import.meta.env.VITE_* — not process.env.
  3. Verify the .env file matches the current mode (.env.production, .env.staging). -> See references/environment-pwa-tooling.md for env and mode configuration.

Configuration Decision Trees

SignalRecommendation
Need path shortcutsresolve.alias with absolute paths
CJS dependency fails in devAdd to optimizeDeps.include
Need compile-time constantsdefine with JSON.stringify
Need different config per modeExport a function: export default defineConfig(({ mode }) => ...)
API calls hit CORS in devserver.proxy with changeOrigin: true
Bundle > 500kB gzippedProfile with visualizer, apply manual chunks
Building a libraryUse build.lib with external dependencies
Need SSRConfigure ssr.entry and ssr.noExternal
Need offline supportAdd vite-plugin-pwa with workbox strategies

Build Optimization Decision Tree

SymptomDiagnosticFix
Large single chunkNo code splittingAdd dynamic imports at route boundaries
Vendor chunk > 250kBSingle vendor bundleSplit with manualChunks by domain
Duplicate modules across chunksShared deps not extractedRollup handles this automatically; verify with visualizer
Slow minificationUsing terserSwitch to esbuild (default) unless terser features needed
Large CSS bundleAll CSS in one fileEnable build.cssCodeSplit (default true)
Small assets bloating requestsToo many HTTP requests for tiny filesIncrease build.assetsInlineLimit (default 4096 bytes)

Plugin Selection Guide

NeedPluginNotes
React Fast Refresh@vitejs/plugin-reactUses Babel. Use @vitejs/plugin-react-swc for speed.
Vue SFC support@vitejs/plugin-vueRequired for .vue files
Svelte support@sveltejs/vite-plugin-svelteRequired for .svelte files
Legacy browser support@vitejs/plugin-legacyGenerates polyfilled chunks
PWA / service workervite-plugin-pwaWorkbox integration
Bundle analysisrollup-plugin-visualizerAdd in build only
SVG as componentsvite-plugin-svgr (React)Framework-specific
Environment validation@t3-oss/env-coreValidates env at build time

Anti-Patterns

Anti-PatternWhy It FailsFix
Importing process.env in client codeUndefined in browser; Vite uses import.meta.envReplace with import.meta.env.VITE_*
Using __dirname in vite.config.tsFails with ESM config formatUse fileURLToPath(new URL('.', import.meta.url))
Splitting every dependency into its own chunkCreates waterfall of HTTP requestsGroup by domain (e.g., react-vendor, ui-vendor)
Disabling pre-bundling globallyBreaks dev server performanceOnly exclude specific packages with optimizeDeps.exclude
Putting secrets in VITE_* env varsExposed in client bundleOnly prefix public values with VITE_
Running terser in devDestroys dev server speedTerser is prod-only by default; never change this
Giant vite.config.ts with all logic inlineUnmaintainableExtract plugin factories and helper functions
Ignoring build.rollupOptions.output.manualChunks return valueOrphaned modules in wrong chunksAlways return a string or undefined, never skip

Reference Index

FileContents
references/configuration-and-dev-server.mdvite.config.ts structure, resolve aliases, define, conditional config, dev server, HMR, proxy, HTTPS, middleware
references/build-optimization.mdRollup options, manual chunks, code splitting strategies, minification, CSS code splitting, asset inlining, tree-shaking
references/plugin-development.mdPlugin hooks, virtual modules, transform pipeline, enforce ordering, HMR API, plugin testing
references/ssr-and-library-mode.mdSSR 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

Command Palette

Search packages, docs, and navigate Tank