Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 38 ยท Build and Optimization

Bundle Analysis and Performance

Find out what is inside your Vite bundle with a visualizer, shrink it with tree shaking, lazy imports and lighter dependencies, and speed up dev start and builds.

"The bundle is too big" is a feeling until you measure it. This lesson shows how to see what your Vite bundle contains, a repeatable way to shrink it, and the knobs that make dev start-up and builds faster. The workflow is always the same: measure, find the biggest item, fix it, measure again.

Vite itself is not available in our runners. Real config is read-only; the runnable blocks are stand-ins that compute the same kind of numbers (sizes, shares, budgets) you would read from a real report.

Step 1: read the build output

Every vite build prints a table:

Text
dist/assets/index-DgK2xQ1a.css    4.12 kB | gzip:  1.32 kB
dist/assets/index-B8k3Qp1x.js   412.77 kB | gzip: 130.11 kB

Pay attention to the gzip column; it approximates what travels over the network. Warnings about chunks over 500 kB (raw) are your first hint. Raw size still matters because the browser must parse and run the code, which is slow on cheap phones.

Step 2: look inside with a visualizer

rollup-plugin-visualizer writes an interactive treemap where each rectangle is a module and its area is its size:

JavaScript
// vite.config.js (real Vite, read-only)
import { defineConfig } from 'vite';
import { visualizer } from 'rollup-plugin-visualizer';

export default defineConfig({
  plugins: [
    visualizer({ filename: 'stats.html', gzipSize: true, brotliSize: true }),
  ],
});

Open stats.html and look for: one dependency taking a large share, the same library in two versions, locale or icon packs you do not use, and code in the entry chunk that only one page needs. Alternatives are vite-bundle-visualizer (a one-off CLI) and the rollup-plugin-analyzer.

A stand-in analysis

Given a list of modules with sizes, a visualizer groups by package and computes shares. You can do the same on any JSON stats file.

JS Stand-in: who is taking the space?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In this fake report moment (with its locales) is the largest item, so replacing it with a smaller date library such as date-fns or the built-in Intl and Temporal-style APIs is the highest-value change.

Step 3: shrink what you find

1. Replace heavy dependencies. Check sizes on a site like bundlephobia before adding a package. Prefer libraries that are ESM and tree-shakable.

2. Import precisely. With ES modules and a tree-shakable library, import { debounce } from 'lodash-es' includes only debounce; import _ from 'lodash' pulls in everything. Barrel files (index.js re-exporting everything) can defeat tree shaking if modules have side effects.

3. Lazy-load. Move big, rarely used features (rich text editors, charts, PDF viewers) behind dynamic import(). See Code Splitting and Chunks.

4. Remove dead branches. Guard dev tooling with import.meta.env.DEV; Vite removes the branch from production builds.

5. Optimize assets. Images are usually the largest bytes of a page. Use modern formats (WebP, AVIF), right-sized dimensions and loading="lazy". Plugins like vite-imagetools can resize at build time.

6. Compress. Serve Brotli or gzip. Vite reports gzip size but does not write compressed files; use a plugin or your server/CDN.

Step 4: set a budget

A performance budget turns "keep it small" into a test that fails. A small script that reads dist/ and checks sizes is easy to add to CI.

JS Stand-in: a size-budget check

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Dev start-up and build speed

Dependency pre-bundling. On first start Vite uses esbuild to convert your node_modules dependencies into single ESM files, cached in node_modules/.vite. If a rarely used dependency is discovered late, the page reloads. Add it up front:

JavaScript
// real Vite
export default defineConfig({
  optimizeDeps: {
    include: ['some-cjs-package', 'linked-pkg > nested-dep'],
    exclude: ['my-local-esm-package'],
  },
});

Warm up. server.warmup.clientFiles pre-transforms your entry files at start so the first page load is faster.

Avoid barrel imports in dev. A barrel file that re-exports 500 icons makes the dev server transform all of them. Import from the specific file or use the library's per-icon path.

Build speed. Disable build.sourcemap if you do not need it, skip reportCompressedSize in big projects (build.reportCompressedSize: false), and keep the TypeScript check in a separate step. Rolldown-based builds in newer Vite versions speed this up further without config changes.

A routine you can repeat

  1. Build, note the entry gzip size.
  2. Open the visualizer, find the top three items.
  3. Fix the biggest one (replace, lazy-load or trim).
  4. Build again, compare, commit the improvement.
  5. Add a budget so it cannot silently grow back.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which number best approximates what travels over the network?

    Compressed size is what is transferred; raw size still affects parse and execution time.
  2. 2. What does a bundle visualizer show?

    A treemap shows modules by size so you can spot large dependencies and duplicates.
  3. 3. Which import style is best for tree shaking?

    Named imports from an ESM build let the bundler include only what you use.
  4. 4. What is optimizeDeps.include for?

    Listing a dependency makes Vite pre-bundle it at start-up instead of discovering it mid-session.

Summary

  • Measure first: read the build table (gzip column) and open a visualizer.
  • Fix the biggest items: replace heavy libraries, import precisely, lazy-load and remove dead code.
  • Optimize images and serve compressed files.
  • Put budgets in CI so size regressions fail loudly.
  • Tune dev speed with optimizeDeps and warm-up, and avoid barrel imports.

Next: Framework Templates and Setup