Skip to main content

Lesson 7 of 10

Bundle Analysis & Tree Shaking

You can't optimize a bundle you can't see. Bundle analysis tools create visual treemaps of every module in your bundle - you'll immediately see which third-party libraries dominate your size and which are easily replaceable with lighter alternatives.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Analyzing & Reducing Bundle Size

Analyzing & Reducing Bundle Size
// Vite bundle visualizer
npm install -D rollup-plugin-visualizer

// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { visualizer } from 'rollup-plugin-visualizer';

export default defineConfig({
  plugins: [
    react(),
    visualizer({
      open: true,         // open in browser after build
      filename: 'dist/stats.html',
      gzipSize: true,     // show gzipped sizes (closer to actual transfer size)
      brotliSize: true,
      template: 'treemap', // 'treemap' | 'sunburst' | 'network'
    }),
  ],
  build: {
    rollupOptions: {
      output: {
        // Manual chunk splitting for better caching
        manualChunks: {
          'react-vendor': ['react', 'react-dom'],
          'router': ['react-router-dom'],
          'query': ['@tanstack/react-query'],
          'forms': ['react-hook-form', 'zod', '@hookform/resolvers'],
          'state': ['zustand'],
        },
      },
    },
  },
});

// npm run build → opens browser with interactive treemap

// ---- Tree shaking best practices ----

// 1. Named imports (tree-shakeable)
import { format, parseISO } from 'date-fns'; // ✅ only imports what you use
import _ from 'lodash';                        // ❌ imports ENTIRE lodash (75KB)
import { debounce } from 'lodash';             // ✅ still imports all of lodash
import debounce from 'lodash/debounce';        // ✅ imports only debounce module

// 2. Package alternatives with smaller footprints
// date-fns (13KB ES) instead of moment.js (67KB)
// zustand (< 1KB) instead of redux + react-redux (~20KB)
// immer (14KB) - only use if you actually need deep immutable updates
// react-hot-toast instead of react-toastify (much smaller)

// 3. Dynamic imports for large optional features
// Don't include charting library in initial bundle
const ChartComponent = lazy(() => import('./ChartComponent'));
// Users who never see charts never download the chart library

// 4. Check for duplicate dependencies
npx duplicate-package-checker-webpack-plugin
// Common: multiple versions of react, date-fns, lodash pulled by different deps

Tip

Tip

Practice Bundle Analysis Tree Shaking in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and flow for Bundle Analysis & Tree Shaking

Practice Task

Note

Practice Task - (1) Write a working example of Bundle Analysis Tree Shaking from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Bundle Analysis Tree Shaking is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/10 done