Vite Build Optimizer Guide
Vite uses Rollup for production builds — blazing fast in dev, highly optimizable in prod. Learn how to analyze bundle composition, configure code splitting, and squeeze maximum performance from your Vite output.
Bundle Visualization with rollup-plugin-visualizer
npm install --save-dev rollup-plugin-visualizer
// vite.config.ts
import { defineConfig } from 'vite';
import { visualizer } from 'rollup-plugin-visualizer';
export default defineConfig({
plugins: [
visualizer({
open: true, // auto-open browser after build
filename: 'dist/stats.html',
gzipSize: true, // show gzip sizes
brotliSize: true, // show brotli sizes (most accurate)
template: 'treemap', // 'treemap' | 'sunburst' | 'network'
}),
],
}); Production Build Configuration
// vite.config.ts
import { defineConfig, splitVendorChunkPlugin } from 'vite';
export default defineConfig({
plugins: [splitVendorChunkPlugin()], // auto vendor splitting
build: {
target: 'es2020', // modern browsers only
minify: 'esbuild', // fastest; use 'terser' for max compression
cssCodeSplit: true, // separate CSS per chunk
sourcemap: false, // disable in prod for smaller output
chunkSizeWarningLimit: 500, // warn if chunk > 500KB
rollupOptions: {
output: {
// Manual vendor chunk splitting
manualChunks: {
'react-vendor': ['react', 'react-dom'],
'router': ['react-router-dom'],
'query': ['@tanstack/react-query'],
'ui': ['@radix-ui/react-dialog', '@radix-ui/react-dropdown-menu'],
},
// Asset file naming
chunkFileNames: 'assets/js/[name]-[hash].js',
entryFileNames: 'assets/js/[name]-[hash].js',
assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
},
},
},
}); Dynamic Imports for Code Splitting
// React Router v6 with lazy loading
import { lazy, Suspense } from 'react';
import { createBrowserRouter } from 'react-router-dom';
const Dashboard = lazy(() => import('./pages/Dashboard'));
const Settings = lazy(() => import('./pages/Settings'));
const Admin = lazy(() => import('./pages/Admin'));
const router = createBrowserRouter([
{
path: '/dashboard',
element: (
<Suspense fallback={<div>Loading...</div>}>
<Dashboard />
</Suspense>
),
},
]);
// Vue Router lazy loading
const routes = [
{
path: '/dashboard',
component: () => import('./pages/Dashboard.vue'),
},
]; Pre-Compression at Build Time
npm install --save-dev vite-plugin-compression
// vite.config.ts
import viteCompression from 'vite-plugin-compression';
export default defineConfig({
plugins: [
// Generate .gz files
viteCompression({ algorithm: 'gzip', ext: '.gz' }),
// Generate .br files (Brotli — 10–15% smaller than gzip)
viteCompression({ algorithm: 'brotliCompress', ext: '.br' }),
],
});
// Configure nginx to serve pre-compressed files:
// gzip_static on;
// brotli_static on; (requires ngx_brotli module) Build Performance Benchmarks
| Optimization | Impact | Effort |
|---|---|---|
| Route-level code splitting | High — 50–80% initial JS reduction | Low |
| Manual vendor chunks | High — better caching | Low |
| Brotli pre-compression | Medium — 10–15% smaller | Low |
| Terser vs esbuild minify | Low — 1–3% smaller | Low |
| Tree-shake heavy deps | High — depends on deps | Medium |
Frequently Asked Questions
How do I analyze my Vite bundle size?▼
Install `rollup-plugin-visualizer`: `npm install --save-dev rollup-plugin-visualizer`. Add it to vite.config.ts plugins array: `visualizer({ open: true, gzipSize: true, brotliSize: true })`. Run `vite build` and a stats.html file opens in your browser showing a treemap of bundle composition by gzip and brotli size.
What is the difference between Vite's dev server and production build?▼
In dev mode, Vite serves files as native ES modules — no bundling, instant HMR. In production, Vite uses Rollup under the hood to bundle, tree-shake, and minify. Dev performance does not predict production bundle size. Always run `vite build` to check production output. Use `vite preview` to serve the production build locally and verify behavior.
How do I set up code splitting in Vite?▼
Vite handles code splitting automatically for dynamic imports. Use `() => import('./Component')` for lazy-loaded routes. Configure `manualChunks` in `build.rollupOptions` to force specific packages into their own chunks. For React apps, use `React.lazy()` + `<Suspense>` for component-level splitting. Vite also automatically splits CSS imported by JS into separate chunk files.
How do I configure Vite for maximum production performance?▼
Key optimizations: (1) `build.minify: 'esbuild'` (default, fastest) or `'terser'` for maximum compression. (2) `build.cssCodeSplit: true` (default) — separate CSS per chunk. (3) `build.rollupOptions.output.manualChunks` — vendor splitting. (4) Enable Brotli/gzip at the server level (Vite doesn't compress, your web server or CDN does). (5) Use `vite-plugin-compression` to pre-compress assets at build time.
Related Products
on GumroadTypeScript Quick Reference Toolkit
5 practical reference files for TypeScript developers: cheatsheet, TS vs JS comparison, 20 utility types with real examples, tsconfig best practices template, and 15 common mistakes guide. Print it. Keep it on your desk.
Buy on Gumroad →Developer Productivity Bundle
Stop rebuilding the same setup every project. 51 VSCode snippets, 40 Git aliases, 5 Docker Compose stacks, 5 GitHub Actions workflows, 5 README templates, and a dotfiles kit. Set up a professional dev environment in under an hour.
Buy on Gumroad →AI Prompt Engineering Toolkit
58 structured prompt templates for Claude, ChatGPT, and Gemini — organized by use case. Code Review, Debugging, Documentation, Architecture, and 10 model-specific tips. Stop writing prompts from scratch.
Buy on Gumroad →You Might Also Need
Vitest Configuration Generator
Generate a complete vitest.config.ts visually. Configure test environment (node/jsdom/happy-dom), coverage provider, reporters, thresholds, and path aliases.
Open free tool →
Vitest Guide — Vite-native Testing
Comprehensive Vitest guide for Vite projects. Covers setup, vi.mock, in-source testing, coverage with V8/Istanbul, HMR test running, and migration from Jest.
Open free tool →
Jest to Vitest Migrator
Convert Jest configuration and test syntax to Vitest automatically. Handles jest.config.js, moduleNameMapper, jest.fn(), jest.mock() and more.
Open free tool →
More Free Tools
Related Articles
Vite 6 Deep Dive: Everything You Need to Know
A complete deep dive into Vite 6. Covers the new Environment API, Rolldown migration, improved HMR, plugin authoring changes, configuration upgrades, and what every developer should do before upgrading.
Vite 6 vs Webpack 5 深度對決:2026 大型專案遷移實戰指南
2026 年最完整的 Vite 6 vs Webpack 5 深度比較與遷移指南。涵蓋效能基準測試、分塊策略、CSS Modules、環境變數轉換、常見陷阱與真實大型專案遷移清單。
Performance Optimization 2026: Complete Guide to Frontend, Backend, and Infrastructure
Comprehensive performance optimization guide for 2026 covering Core Web Vitals, JavaScript bundle optimization, server-side rendering, caching strategies, database query optimization, CDN configuration, and real-world benchmarks.
Vite 6 完整深度指南:新功能、重大變更與升級策略 2026
Vite 6 完整深度解析。涵蓋全新 Environment API、Rolldown 實驗性整合、改進的 HMR 機制、外掛 API 變更、設定更新,以及升級前每位開發者必須了解的重點。
Vite 6 vs webpack vs esbuild vs Turbopack: Build Tool Comparison 2026
In-depth 2026 comparison of JavaScript build tools: Vite 6, webpack 5, esbuild, and Turbopack. Build speeds, HMR, plugin ecosystems, migration paths, and when to use each.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
Unlock AI-Powered Dev Tools
- ⚡ AI Code Review, Doc Generator & SQL Builder
- ⚡ All premium templates & early access
- ⚡ Member discounts on Gumroad products
$9/mo after trial · Cancel anytime
Want the full toolkit?
Get DevPlaybook Pro
Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.
MIT licensed · Instant download · No subscription
See what's included → Browse all deals →