rollup-plugin-visualizer

repository·master·Indexed 25 days ago

https://github.com/btd/rollup-plugin-visualizer

A tool for visualizing and analyzing JavaScript bundles to identify modules consuming the most space. It supports Rollup, Rolldown, and Vite, and provides various report layouts including treemap, sunburst, flamegraph, and network graphs. Version 7.0.1 includes a CLI for combining multiple JSON reports and options for gzip/Brotli size calculation and source map integration.

Tokens
2.1K
Snippets
6
Records
16
Agent score
81%

What's inside rollup-plugin-visualizer

  1. Filter files using include and exclude

    master

    The include and exclude options accept a Filter type: { bundle?: picomatchPattern, file?: picomatchPattern }.

    Filters use picomatch globs and support the format BUNDLE_GLOB:FILE_GLOB (the colon is required when matching the bundle).

    Supported forms:

    • Bundle + file: translation-*.js:*/**/index.js
    • Bundle only: translation-*.js:
    • File only (default): */**/index.js

    If include is omitted or empty, all files are included by default. Otherwise, an ID must match at least one include pattern and must not match any exclude pattern.

  2. Understand the visual report layouts

    master

    The visualizer provides several ways to inspect your bundle:

    • Sunburst: A circular view. Clicking an arc zooms into that section.
    • Flamegraph: A top-down hierarchical view.
    • Treemap: A rectangular hierarchical view for finding large modules quickly.
    • Network: A force-directed graph to see module relationships. Gray circles represent tree-shaken files. To reduce noise, try removing highly connected nodes like commonjsHelpers, tslib, or react.
    • Raw-data: JSON output (usually used with the CLI).
    • List: A YAML file useful for tracking bundle changes over time.
    • Markdown: A report with summary tables and configuration notes.
  3. Configure visualizer options

    master

    The visualizer() function accepts several options to customize the report generation:

    OptionTypeDefaultDescription
    filenamestringstats.{ext}Name of the generated report file.
    titlestringRollup VisualizerHTML <title> value.
    openbooleanfalseOpen the generated file in your default browser.
    templatestringtreemapReport type: sunburst, treemap, treemap-3d, network, raw-data, list, markdown, flamegraph.
    gzipSizebooleanfalseInclude gzip size in the report.
    brotliSizebooleanfalseInclude Brotli size in the report.
    emitFilebooleanfalseUse Rollup emitFile to generate output. Useful for SvelteKit.
    sourcemapbooleanfalseUse source maps for module size calculations. Keep this plugin last.
    projectRootstring | RegExpprocess.cwd()Common root path used to trim absolute file paths.
    includeFilter | Filter[]undefinedInclude filter.
    excludeFilter | Filter[]undefinedExclude filter.
  4. Configure rollup-plugin-visualizer in Rolldown

    master

    Import visualizer and cast it to RolldownPlugin within your defineConfig call.

    import { defineConfig, type RolldownPlugin } from "rolldown";
    import { visualizer } from "rollup-plugin-visualizer";
    
    export default defineConfig({
      plugins: [visualizer() as RolldownPlugin],
    });
  5. Configure rollup-plugin-visualizer in Vite

    master

    Add visualizer() to the plugins array in your vite.config.js or vite.config.ts.

    import { defineConfig, type PluginOption } from "vite";
    import { visualizer } from "rollup-plugin-visualizer";
    
    export default defineConfig({
      plugins: [visualizer() as PluginOption],
    });
  6. Configure rollup-plugin-visualizer in Rollup

    master

    Import visualizer from rollup-plugin-visualizer and add it to your plugins array. It is recommended to keep the visualizer as the last plugin in your configuration.

    import { visualizer } from "rollup-plugin-visualizer";
    
    export default {
      plugins: [
        // Keep it last.
        visualizer(),
      ],
    };
  7. Configure rollup-plugin-visualizer in SvelteKit

    master

    In SvelteKit, it is often necessary to set emitFile: true because SvelteKit may run Vite multiple times. When emitFile is true, the filename must be a file name rather than a path.

    import { visualizer } from "rollup-plugin-visualizer";
    
    export default {
      plugins: [
        visualizer({
          emitFile: true,
          filename: "stats.html",
        }),
      ],
    };
  8. Configure PluginVisualizerOptions

    master

    The PluginVisualizerOptions object allows you to customize the report generation.

    Key options include:

    • filename: The path to the generated file. Defaults to stats.html (or stats.json if using raw-data template).
    • title: The HTML <title> of the generated file. Defaults to "Rollup Visualizer".
    • open: If true, opens the generated file in the browser. Ignored if json or emitFile is true.
    • template: The type of diagram to generate. Options include 'treemap', 'sunburst', 'treemap-3d', 'network', 'flamegraph', 'markdown', and 'raw-data'. Defaults to 'treemap'.
    • gzipSize: If true, calculates gzipped file sizes.
    • brotliSize: If true, calculates brotli compressed file sizes.
    • sourcemap: If true, uses sourcemaps to calculate module sizes for higher accuracy. Note: gzipSize and brotliSize are not used when sourcemap is enabled.
    • projectRoot: Absolute path used to strip prefixes from file paths. Defaults to process.cwd().
    • emitFile: If true, uses Rollup's .emitFile API to output the report into the configured output directory. When this is enabled, filename must be a simple filename, not a path.
    • include / exclude: Picomatch patterns to filter which modules are included in the report.
  9. Configure rollup-plugin-visualizer CLI options

    master

    The following flags are available when using the CLI:

    OptionTypeDefaultDescription
    --filenamestring./stats.htmlOutput file name
    --titlestringRollup VisualizerOutput file title
    --templatestringtreemapTemplate type (must be one of the supported template types)
    --sourcemapbooleanfalseIndicates if the provided files are sourcemaps
    --openbooleanfalseOpen the generated template in the default user agent (browser)
  10. Combine multiple reports using the CLI

    master

    The rollup-plugin-visualizer CLI utility allows you to combine multiple JSON reports into a single chart. This is useful when you have multiple build configurations.

    rollup-plugin-visualizer [OPTIONS] stat1.json stat2.json ../stat3.json