Vite Optimizing Bundles
Vite's defaults already produce a lean build. When your app grows, a few habits and tools keep it fast: measure, remove what you don't need, split what you don't need yet, and cache what rarely changes.
Step 1: measure
The build report is your first signal. Vite warns when a chunk is bigger than build.chunkSizeWarningLimit (500 kB by default):
npm run build> my-app@0.0.0 build
> vite build
vite v8.3.4 building client environment for production...
✓ 412 modules transformed.
dist/index.html 0.51 kB │ gzip: 0.32 kB
dist/assets/index-DiwrgTda.css 8.92 kB │ gzip: 2.41 kB
dist/assets/index-Ba7Uk2Lr.js 742.18 kB │ gzip: 231.40 kB
(!) Some chunks are larger than 500 kB after minification. Consider:
- Using dynamic import() to code-split the application
- Use build.rolldownOptions.output.codeSplitting to improve chunking: https://rolldown.rs/reference/OutputOptions.codeSplitting
- Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.
✓ built in 1.84s
To see what is inside, use a bundle visualizer such as rollup-plugin-visualizer (it works with Rolldown too):
// vite.config.js
import { defineConfig } from 'vite';
import { visualizer } from 'rollup-plugin-visualizer';
export default defineConfig({
plugins: [visualizer({ filename: 'stats.html', gzipSize: true })],
});After building, open stats.html to get a treemap of every module and its size.
Step 2: find the heavy hitters
A simple way to reason about the report is to rank modules by size. This playground does that with a sample of a build's module list:
const modules = [
{ id: 'node_modules/moment/moment.js', kb: 289 },
{ id: 'node_modules/moment/locale/*', kb: 172 },
{ id: 'node_modules/lodash/lodash.js', kb: 531 },
{ id: 'node_modules/react-dom/client.js', kb: 135 },
{ id: 'src/App.jsx', kb: 12 },
{ id: 'src/pages/Dashboard.jsx', kb: 9 },
];
const total = modules.reduce((sum, m) => sum + m.kb, 0);
const ranked = [...modules].sort((a, b) => b.kb - a.kb);
console.log(`total: ${total} kB`);
for (const m of ranked.slice(0, 3)) {
const pct = ((m.kb / total) * 100).toFixed(1);
console.log(`${pct.padStart(5)}% ${m.id}`);
}total: 1148 kB
46.3% node_modules/lodash/lodash.js
25.2% node_modules/moment/moment.js
15.0% node_modules/moment/locale/*
Step 3: fix the usual suspects
| Problem | Fix |
|---|---|
Whole utility library bundled (lodash) |
Use ESM version with named imports (lodash-es) or native methods |
Big date library with all locales (moment) |
Switch to date-fns, dayjs or Intl.DateTimeFormat |
| Icon pack imported as a whole | Import individual icons (import { Check } from 'lucide-react') |
| Rarely used feature in main bundle | Dynamic import() it |
| Large JSON imported statically | Put it in public/ and fetch() it, or lazy-import it |
| Huge images | Compress, use modern formats (WebP/AVIF), correct dimensions |
| Polyfills for old browsers everyone pays for | Use @vitejs/plugin-legacy, which only serves them to legacy browsers |
Native APIs often replace a whole dependency:
const amount = 1234567.891;
const date = new Date(Date.UTC(2026, 9, 16));
console.log(new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(amount));
console.log(new Intl.NumberFormat('de-DE').format(amount));
console.log(new Intl.DateTimeFormat('en-GB', { dateStyle: 'long', timeZone: 'UTC' }).format(date));
console.log(new Intl.RelativeTimeFormat('en', { numeric: 'auto' }).format(-1, 'day'));$1,234,567.89
1.234.567,891
16 October 2026
yesterday
Zero kilobytes added to your bundle.
Step 4: help tree-shaking
- Use packages that ship ES modules and declare
"sideEffects": falsein theirpackage.json. - Avoid
import * as _ from 'lib'followed by dynamic property access (_[name]) — the bundler can't know what you use. - Mark your own pure modules with
"sideEffects": false(or a list like["*.css"]) inpackage.json. - Don't import barrel files (
index.jsre-exporting everything) from huge packages if the package isn't side-effect free.
Step 5: caching and loading
export default defineConfig({
build: {
target: 'es2022', // less syntax lowering if you only support modern browsers
sourcemap: 'hidden', // keep maps for error tracking, but don't link them publicly
rolldownOptions: {
output: {
codeSplitting: {
groups: [{ name: 'vendor', test: /node_modules/ }],
},
},
},
},
});Then configure the host to serve /assets/* with Cache-Control: immutable, enable gzip or Brotli compression, and use HTTP/2 or HTTP/3.
Dev-server performance
Optimization isn't only for builds. If the dev server feels slow on a large app:
| Tip | Why |
|---|---|
| Write explicit file extensions in imports | Fewer file-system checks during resolution |
| Avoid deep barrel files in your own code | Importing one function shouldn't load 300 modules |
server.warmup.clientFiles |
Pre-transform files you know the first page needs |
| Keep plugins lean | Every transform hook runs on every module |
optimizeDeps.include |
Pre-bundle deps that are discovered late (avoids reloads) |
Tip: Measure before and after every change. A smaller number in the gzip column (or a faster Lighthouse score) is the only proof that an optimization worked.
Test yourself
What is the default chunkSizeWarningLimit?
- 100 kB
- 250 kB
- 500 kB
- 1 MB
Which change most likely reduces bundle size?
import _ from 'lodash'→import { debounce } from 'lodash-es'- Adding more plugins
- Turning off minification
- Moving code from src/ to a barrel file
What does sourcemap: 'hidden' do?
- Disables source maps
- Generates maps but omits the sourceMappingURL comment
- Inlines maps into JS
- Encrypts maps