Course contentsShow
← React
Lesson 1415 of 2,39635. Code SplittingFree lesson

Setting Up rollup-plugin-visualizer for Vite

Add and configure rollup-plugin-visualizer to analyze Vite production bundles with interactive reports.

Setting Up rollup-plugin-visualizer for Vite

You'll learn how to install and configure rollup-plugin-visualizer to generate interactive bundle analysis reports for Vite projects.

The Vite Bundle Analysis Tool

While webpack has webpack-bundle-analyzer, Vite uses Rollup under the hood for production builds. The rollup-plugin-visualizer creates similar interactive visualizations showing which modules consume the most space in your bundles. Think of it as an X-ray machine for your built application—it reveals exactly what's inside each JavaScript file and how big each piece is.

Installation and Configuration

First, install the plugin as a dev dependency:

npm install --save-dev rollup-plugin-visualizer

Next, add it to your vite.config.js:

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

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

Configuration Options Explained

  • open: true: Automatically opens the report in your browser after build
  • filename: Where to save the HTML report (inside dist keeps it with build artifacts)
  • gzipSize: Shows compressed sizes (what users actually download)
  • brotliSize: Shows Brotli compression (modern alternative to gzip)

When you run npm run build, Vite generates your production bundle and creates an interactive treemap visualization. You can click blocks to drill down into what's inside each chunk.

Key Takeaway: The rollup-plugin-visualizer gives Vite projects the same bundle analysis capabilities as webpack-bundle-analyzer, revealing exactly where your bundle size is coming from with an interactive visual report.