CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-rollup-plugin-sizes

Show info about files/packages included with your rollup bundle

94

1.09x
Overview
Eval results
Files

task.mdevals/scenario-2/

Bundle Module Analyzer

A Rollup plugin that analyzes and categorizes modules in a bundle to help developers understand the composition of their build output.

Objective

Build a Rollup plugin that processes bundle information and categorizes each module into one of three types: npm packages, application code, or Rollup helpers. The plugin should provide a breakdown showing how many modules fall into each category and their total sizes.

Requirements

Module Categorization

The plugin must categorize each module in the bundle according to these rules:

  1. NPM Packages: Modules that originate from third-party libraries installed via npm (typically found in node_modules)
  2. Application Code: Modules that are part of the local application source code
  3. Rollup Helpers: Internal helper modules injected by Rollup during the bundling process (these have a null byte \u0000 prefix in their module ID)

Plugin Structure

Create a Rollup plugin that:

  • Exports a factory function that returns a plugin object
  • Has the plugin name set to "bundle-module-analyzer"
  • Implements the generateBundle hook to analyze bundle contents after generation
  • Processes each chunk in the bundle to extract module information

Output Format

For each bundle, output to the console:

  1. The input file path or bundle name
  2. A breakdown showing:
    • Number of modules in each category (npm packages, app, rollup helpers)
    • Total size in bytes for each category
    • List of unique package names found (for npm packages category only)

Example output format:

Bundle: src/index.js
Categories:
  - npm packages: 5 modules, 125000 bytes
    Packages: lodash, react, express
  - app: 3 modules, 45000 bytes
  - rollup helpers: 2 modules, 5000 bytes

Module Information

Access module data from Rollup's bundle chunks:

  • Each chunk has a modules property containing module information
  • Each module has an id property (the module's file path or identifier)
  • Each module has an originalLength property (the original size in bytes)

Implementation

@generates

API

/**
 * Creates a bundle module analyzer plugin
 * @returns {object} Rollup plugin object
 */
function bundleModuleAnalyzer() {
  // Returns a plugin object with:
  // - name: "bundle-module-analyzer"
  // - generateBundle hook that analyzes and categorizes modules
}

module.exports = bundleModuleAnalyzer;

Test Cases

  • When a bundle contains only application modules (no dependencies), it correctly categorizes them as "app" @test
  • When a bundle contains npm packages from node_modules, it correctly identifies them as separate packages by name @test
  • When a bundle contains Rollup helper modules (with \u0000 prefix), it correctly categorizes them as "rollup helpers" @test
  • The plugin correctly calculates total sizes for each category by summing originalLength values @test

Dependencies { .dependencies }

module-details-from-path { .dependency }

Parses module paths to identify npm packages and extract package information.

Install with Tessl CLI

npx tessl i tessl/npm-rollup-plugin-sizes

tile.json