Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

rollup-plugin-analyzer

doesdev260.7kMIT4.0.0TypeScript support: included

Mad metrics for your rollup bundles, know all the things

rollup, plugin, config, import, bundle, file, size, treeshaking, metrics, analysis, analyze, analyzer, bloat

readme

rollup-plugin-analyzer

Mad metrics for your rollup bundles, know all the things

npm Travis (.com) Dependents (via libraries.io) npm

TOC

Install

$ npm install --save-dev rollup-plugin-analyzer

Usage

Importing or Requiring

Import as ES Module

import analyze from 'rollup-plugin-analyzer'

Requiring as CJS

const analyze = require('rollup-plugin-analyzer')

Usage from rollup config

export default {
  entry: 'module.js',
  dest: 'index.js',
  format: 'cjs',
  plugins: [analyze()]
}

Usage from build script

rollup({
  entry: 'main.js',
  plugins: [analyze()]
}).then(...)

CI usage example

const limitBytes = 1e6

const onAnalysis = ({ bundleSize }) => {
  if (bundleSize < limitBytes) return
  console.log(`Bundle size exceeds ${limitBytes} bytes: ${bundleSize} bytes`)
  return process.exit(1)
}

rollup({
  entry: 'main.js',
  plugins: [analyze({ onAnalysis, skipFormatted: true })]
}).then(...)

results

logged to console on rollup completion

-----------------------------
Rollup File Analysis
-----------------------------
bundle size:    2.809 KB
original size:  11.436 KB
code reduction: 75.44 %
module count:   5

█████████████████████████████████████████████░░░░░
file:            /virtual-insanity.js
bundle space:    90.64 %
rendered size:   2.546 KB
original size:   2.57 KB
code reduction:  0.93 %
dependents:      1
  - /jamiroquai.js

██░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
file:            /bundle-a.js
bundle space:    4.27 %
rendered size:   120 Bytes
original size:   309 Bytes
code reduction:  61.17 %
dependents:      0

█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
file:            /jamiroquai.js
bundle space:    2.95 %
rendered size:   83 Bytes
original size:   169 Bytes
code reduction:  50.89 %
dependents:      1
  - /the-alphabet-but-incomplete.js
...

results (with summaryOnly enabled)

-----------------------------
Rollup File Analysis
-----------------------------
bundle size:    2.809 KB
original size:  11.436 KB
code reduction: 75.44 %
module count:   5

/virtual-insanity.js
█████████████████████████████████████████████░░░░░ 90.64 % (2.546 KB)
/bundle-a.js
██░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 4.27 % (120 Bytes)
/jamiroquai.js
█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 2.95 % (83 Bytes)
/the-alphabet-but-incomplete.js
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 1.17 % (33 Bytes)
/the-declaration-of-independence.js
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0.96 % (27 Bytes)

Options

  • stdout - optional
    • type: Boolean
    • default: false
    • description: Print to stdout (console.log) instead of stderr (console.error)
  • limit - optional
    • type: Number
    • default: null
    • description: Limit number of files to output analysis of, sorted by DESC size
  • filter - optional
    • type: Array | String | Function
    • default: null
    • description: Filter module to output in analysis results.
      • If a string is passed, checks if module name contains the string
      • If array it checks the same for each string in the array
      • If function, return a boolean to indicate if a module should be in analysis results
      • This only suppresses the modules details, it does not affect the summary output
      • If you would like it to filter from summary info as well set filterSummary to true
    • notes: Function receives module object specified below, should return boolean
  • filterSummary - optional
    • type: Boolean
    • default: false
    • description: If true the filter and limit options will also remove any filtered out module data from the summary
  • root - optional
    • type: String
    • default: process.cwd()
    • description: Application directory, used to display file paths relatively
  • hideDeps - optional
    • type: Boolean
    • default: false
    • description: Don't itemize dependents in the formatted output
  • showExports - optional
    • type: Boolean
    • default: false
    • description: Show used and unused exports
  • summaryOnly - optional
    • type: Boolean
    • default: false
    • description: Only output bundle summary and module usage bar graphs
  • skipFormatted - optional
    • type: Boolean
    • default: false
    • description: Don't output formatted string
  • writeTo - optional
    • type: Function
    • default: null
    • description: Callback to be invoked with formatted string
    • function will be invoked with:
      • analysisString (String)
  • transformModuleId - optional
    • type: Function
    • default: null
    • description: Modify module ids
    • function will be invoked with:
      • id (String) - path of module / rollup module id
    • function should return:
      • id (String) - desired final module id to display in analysis results
    • example: (id) => id.replace(/^\0(?:commonjs-proxy:)?/, '')
  • onAnalysis - optional
    • type: Function
    • default: null
    • description: Callback to be invoked with analysis object
    • function will be invoked with:
      • analysisObject (Object)
        • bundleSize (Number) - rendered bundle size in bytes
        • bundleOrigSize (Number) - original bundle size in bytes
        • bundleReduction (Number) - percentage of rendered bundle size reduction
        • moduleCount (Number) - Count of all included modules
        • modules (Array) - array of module analysis objects
          • module (Object)
            • id (String) - path of module / rollup module id
            • size (Number) - size of rendered module code in bytes
            • origSize (Number) - size of module's original code in bytes
            • dependents (Array) - list of dependent module ids / paths
            • percent (Number) - percentage of module size relative to entire bundle
            • reduction (Number) - percentage of rendered size reduction
            • renderedExports (Array) - list of used named exports
            • removedExports (Array) - list of unused named exports

FAQ

Why is the reported size not the same as the file on disk?

This module is geared towards the details of the individual modules that make up the bundle and their relative impact to bundle size. That's a detailed way of saying, it doesn't really care about size on disk. There are other options which focus on size on disk as well as delivery size which can be used alongside this module (or in place of if your concern is not per module impact). In particular rollup-plugin-size-snapshot seems like a great option for that.

Getting a bit further into the details, rather than just intent, of why the reported size differs from that on disk. We get the module data from Rollup which reports it after chunk (module) resolution and tree-shaking, but before post-processing (such as minification and compression). We then add the sizes of each of those modules together, this is the bundle size that we report.

That means it won't account for post-processing from other plugins and also won't account for post-processing by Rollup itself, which includes boilerplate / shims depending on what the output format is (CJS, ESM, iife, etc...).

Why am I seeing multiple analysis outputs emitted?

Rollup allows you to output to multiple files. If you are outputting to multiple files you will get a distinct analysis for each output file. Each analysis will contain data on the files imported by the respective target.

License

MIT © Andrew Carpenter

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

4.0.0

19 December 2020

  • [BREAKING] Limit does not affect summary (unless filterSummary === true)
  • [DEV] Update dev dependencies (standard, husky)
  • [DEV] Fix standard style issue
  • [DOCS] Add FAQ to readme
  • [DOCS] Add TOC to readme

3.3.0

8 July 2020

  • Add filterSummary option (#19 && #20)
  • Fix origSize, bundleOrigSize, and bundleReduction (was apparently borked)
  • Remove Rollup 0.60.0 from tests
  • Fix incorrect key names in analysisObject docs
  • Clarify filter usage
  • Update dev deps

3.2.3

13 May 2020

3.2.2

1 November 2019

  • Hack: Coalesce bundleMods
  • Update dev deps

3.2.1

12 September 2019

  • Explicitly add .default property to plugin in CJS output
  • Because my previous unbreaking change broke things
  • Clarify import and require usage in README

3.2.0

12 September 2019

  • [SHOULD NOT BE BREAKING] Correct export signature in module.js
    • This, while an API change, should not be a breaking change
    • Previously the export signature when used as a module was a bit effed
    • This should fix that
    • To my knowledge and in my tests that should not break things
  • Update yarn lockfile using latest Rollup version
  • Correct path to latest Rollup version under scripts
  • Remove changelog script (manually maintain changelog, tis better this way)
  • Add engines to package.json, Node >= 8
  • Update dev dependencies (mvt, husky, standard)
  • Fix some standard style issues

3.1.2

14 July 2019

  • Update dependencies
  • Fix some standardjs issues
  • Use mvt for testing instead of AVA

3.1.1

7 July 2019

  • Don't combine bar graph with id in summary

3.1.0

6 July 2019

  • Add bar graphs to formatted output
  • Add summaryOnly option

3.0.1

1 June 2019

  • Update dependencies

3.0.0

5 January 2019

  • Add default export (fixes #6), warn on rollup < 1.0.0 #6
  • Remove support for rollup < 1.0.0, support 1.0.0 506d93f
  • Implement transformModuleId option a173a43
  • Update Node versions to test in CI, drop 6 ee60a38

2.1.0

11 July 2018

  • Let onAnalysis and formatted coexist, addresses #2 0bdc851
  • Don't swallows errors, as noted in #1 07f5c9a
  • Add test to ensure plugin isn't adding overhead 513bd8b
  • Add CONTRIBUTING.md 77ed765
  • Test that callback is only invoked once, failing 8c6ac14
  • Bump to 2.1.0 3fa544f

2.0.4

3 July 2018

  • Update multi-out tests, failing, skipped 8813ec7
  • Handle bundles with multiple outputs, 2.0.4 12512c9
  • Test with experimentalCodeSplitting enabled as well fc54b1b
  • Fix module name in examples dafa44e

2.0.3

9 June 2018

  • Add showExports option, expose exports in analysis 2be9c1c
  • Some cleanup and formatting tweaks e40789e
  • Add module count to formatted output e880a9d
  • Fix redundant imports in CI example a5328de
  • Include module count in example output 86b0d55

2.0.2

9 June 2018

  • Add ability to filter with callback, 2.0.2 4a5f535

2.0.1

8 June 2018

  • Use updated example output 9795af7

2.0.0

8 June 2018

  • 2.0.0 #5
  • Beef up test fixtures 8e5d30f
  • Epic commit, 97% moar awesomer ee6a7dd
  • Update from rollup-analyzer to rollup-plugin-analyzer 675dcdc
  • Rework analysis object API 6f98a0b
  • Add overall code reduction metrics 2023900
  • Formatting tweaks de2252a
  • Ensure we don't fallback if renderedLength === 0 7dc5b4e
  • Clamp percentages 9dbbc79
  • Set root default again e0262a1

2.0.0-beta.1

7 June 2018

  • Switch to yarn with multiple rollup versions to test 3cd30cc
  • Update deps, add failing test for tree shaking 9d26c38
  • Tests against many rollup versions >= 0.40.x b52c793
  • Start working on 2.0.0 46c03cb
  • Reflect treeshaking, only in plugin currently 1ddf5f7
  • Rework test file structure 2767bfc
  • Import from two files in test 73e524b
  • Start updating API in readme 6c78820
  • Integrate plugin, add related (failing) test 9b9c2ca
  • Bundle with each test 4f9f437
  • Test that it works after bundle generated eed1240
  • Update locked rollup to 0.60.0 96b1cde
  • Fix node 6 compatibility 286b8b3
  • Ensure formatted string is as expected in test 746db48
  • Add travis config 160859e
  • Tweak test title 0acd1e3
  • Fix travis urls 46b5bc5
  • Add travis badge cfda8b7

1.1.0

21 October 2017

  • Add tests, fix filter bugs, 1.1.0 db98ac3

v1.0.3

19 August 2017

  • Add info about plugin, config, v1.0.3 2a34c2d
  • Add deps badge 596c470
  • Add filter, init as default, bump to v1.0.2 e2c932b
  • Add percent, v1.0.1 f2a7c4d
  • Add index to commit from build script 6c3bf72
  • Make root path an init option 6804deb
  • Initial commit a6b3b9b