CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-webpack-bundle-analyzer

Webpack plugin and CLI utility that represents bundle content as convenient interactive zoomable treemap

Overall
score

98%

Overview
Eval results
Files

webpack-plugin.mddocs/

Webpack Plugin

The BundleAnalyzerPlugin is the primary webpack plugin class that integrates bundle analysis into your webpack build process. It provides multiple analysis modes, flexible configuration options, and seamless integration with webpack's plugin system.

Capabilities

BundleAnalyzerPlugin Constructor

Creates a new instance of the webpack bundle analyzer plugin with optional configuration.

/**
 * Creates a new BundleAnalyzerPlugin instance
 * @param options - Plugin configuration options
 */
class BundleAnalyzerPlugin {
  constructor(options?: BundleAnalyzerOptions);
  apply(compiler: object): void;
}

interface BundleAnalyzerOptions {
  /** Analysis mode: server starts HTTP server, static generates HTML file, json creates JSON report, disabled only generates stats */
  analyzerMode?: 'server' | 'static' | 'json' | 'disabled';
  /** Host for HTTP server in server mode */
  analyzerHost?: string;
  /** Port for HTTP server in server mode, 'auto' for automatic assignment */
  analyzerPort?: number | 'auto';
  /** Custom URL generation function for server mode */
  analyzerUrl?: (options: { listenHost: string; listenPort: number; boundAddress: object }) => string;
  /** Output filename for static and JSON modes, relative to webpack output directory */
  reportFilename?: string;
  /** Report title, can be string or function returning string */
  reportTitle?: string | (() => string);
  /** Default size type to display in visualization */
  defaultSizes?: 'stat' | 'parsed' | 'gzip';
  /** Whether to automatically open report in browser */
  openAnalyzer?: boolean;
  /** Whether to generate webpack stats JSON file */
  generateStatsFile?: boolean;
  /** Filename for webpack stats JSON file */
  statsFilename?: string;
  /** Options passed to stats.toJson() method */
  statsOptions?: object;
  /** Patterns to exclude assets from analysis */
  excludeAssets?: string | RegExp | ((assetName: string) => boolean) | Array<string | RegExp | ((assetName: string) => boolean)>;
  /** Logging level for plugin output */
  logLevel?: 'info' | 'warn' | 'error' | 'silent';
  /** Whether to start analyzer server (deprecated, use analyzerMode instead) */
  startAnalyzer?: boolean;
}

Usage Examples:

const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');

// Basic usage with default options
module.exports = {
  plugins: [
    new BundleAnalyzerPlugin()
  ]
};

// Server mode with custom port
module.exports = {
  plugins: [
    new BundleAnalyzerPlugin({
      analyzerMode: 'server',
      analyzerHost: 'localhost',
      analyzerPort: 9999,
      openAnalyzer: true
    })
  ]
};

// Static report generation
module.exports = {
  plugins: [
    new BundleAnalyzerPlugin({
      analyzerMode: 'static',
      reportFilename: 'bundle-report.html',
      openAnalyzer: false
    })
  ]
};

// JSON report with stats file
module.exports = {
  plugins: [
    new BundleAnalyzerPlugin({
      analyzerMode: 'json',
      reportFilename: 'bundle-report.json',
      generateStatsFile: true,
      statsFilename: 'webpack-stats.json'
    })
  ]
};

Plugin Application

Applies the plugin to a webpack compiler instance, integrating with webpack's compilation lifecycle.

/**
 * Applies the plugin to webpack compiler
 * @param compiler - Webpack compiler instance
 */
apply(compiler: object): void;

Asset Exclusion Patterns

Configure which assets to exclude from bundle analysis using various pattern types.

// Asset filter pattern types
type AssetFilterPattern = 
  | string              // Converted to RegExp
  | RegExp              // Used directly for matching
  | ((assetName: string) => boolean);  // Custom filter function

// Multiple patterns
type ExcludeAssets = AssetFilterPattern | AssetFilterPattern[];

Usage Examples:

// Exclude by string pattern (converted to RegExp)
new BundleAnalyzerPlugin({
  excludeAssets: 'vendor'
});

// Exclude by RegExp
new BundleAnalyzerPlugin({
  excludeAssets: /\.(css|png|jpg|gif)$/
});

// Exclude by custom function
new BundleAnalyzerPlugin({
  excludeAssets: (assetName) => assetName.includes('legacy')
});

// Multiple exclusion patterns
new BundleAnalyzerPlugin({
  excludeAssets: [
    'vendor',
    /\.css$/,
    (assetName) => assetName.startsWith('runtime.')
  ]
});

Analysis Modes

Different modes for bundle analysis output and behavior.

type AnalyzerMode = 'server' | 'static' | 'json' | 'disabled';
  • server: Starts HTTP server with interactive visualization (default)
  • static: Generates single HTML file with embedded visualization
  • json: Creates JSON report with bundle data
  • disabled: Only generates stats file if generateStatsFile is true

Size Types

Different size metrics available for bundle analysis.

type SizeType = 'stat' | 'parsed' | 'gzip';
  • stat: Original file sizes from webpack stats (before minification)
  • parsed: Actual parsed bundle sizes (after minification)
  • gzip: Gzipped sizes of the parsed bundles

Report Title Configuration

Flexible report title configuration supporting static strings and dynamic generation.

type ReportTitle = string | (() => string);

Usage Examples:

// Static title
new BundleAnalyzerPlugin({
  reportTitle: 'My App Bundle Analysis'
});

// Dynamic title with current date
new BundleAnalyzerPlugin({
  reportTitle: () => `Bundle Analysis - ${new Date().toLocaleDateString()}`
});

// Using built-in default title function
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const utils = require('webpack-bundle-analyzer/lib/utils');

new BundleAnalyzerPlugin({
  reportTitle: utils.defaultTitle
});

Install with Tessl CLI

npx tessl i tessl/npm-webpack-bundle-analyzer

docs

analysis-engine.md

cli-tool.md

index.md

report-generation.md

webpack-plugin.md

tile.json