• Stars
    star
    1,400
  • Rank 32,497 (Top 0.7 %)
  • Language
    TypeScript
  • License
    ISC License
  • Created about 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tool for finding out what contributes to the size of Webpack bundles

Webpack Bundle Size Analyzer

Build Status

A small utility to help you find out what is contributing to the size of your Webpack bundles.

Webpack has a JSON output mode which produces detailed machine-readable information about everything that has been included in a generated bundle.

This output is produced by running webpack --json. This tool analyzes the resulting JSON output and displays a tree of packages that were included in the bundle, ordered by the size of all the used modules.

For further reading on reducing the size of Webpack bundles, see their optimization guide

Usage

There are 2 ways to use this tool:

  1. As a command line application
  2. As a webpack plugin

CLI Usage

Assuming you have the webpack CLI command installed locally in your project:

npm install -g webpack-bundle-size-analyzer
node_modules/.bin/webpack --json | webpack-bundle-size-analyzer

If you have webpack installed globally then the first part of this command will be just webpack --json instead.

When run on react-testing for example, it produces this output, where <self> refers to the size of the bundle's own code.

react: 641.95 kB (55.3%)
  <self>: 641.95 kB (100%)
chai: 125.8 kB (10.8%)
  deep-eql: 7.51 kB (5.97%)
    type-detect: 2.72 kB (36.2%)
      <self>: 2.72 kB (100%)
    <self>: 4.79 kB (63.8%)
  assertion-error: 2.29 kB (1.82%)
    <self>: 2.29 kB (100%)
  <self>: 116 kB (92.2%)
flummox: 73.46 kB (6.33%)
  flux: 9.01 kB (12.3%)
    <self>: 9.01 kB (100%)
  eventemitter3: 5.94 kB (8.08%)
    <self>: 5.94 kB (100%)
  uniqueid: 947 B (1.26%)
    <self>: 947 B (100%)
  object-assign: 484 B (0.643%)
    <self>: 484 B (100%)
  <self>: 57.12 kB (77.8%)
q: 58.84 kB (5.07%)
  <self>: 58.84 kB (100%)
...
<self>: 195.57 kB (16.9%)

Plugin Usage

Description

When using the plugin version of this library, a file will be written to a location of your choice (when you run your webpack build). The file contents will be a list of the packages in your bundle (sorted in descending order). Each line will contain the name of a package, along with it's file size, and the percentage of space it takes up in your bundle.

Plugin Installation

Install this library as a development dependency to your webpack project:

npm install --save-dev webpack-bundle-size-analyzer

Import your plugin at the top of your webpack.config.js file:

Old School:

var WebpackBundleSizeAnalyzerPlugin = require('webpack-bundle-size-analyzer').WebpackBundleSizeAnalyzerPlugin;

New School:

import { WebpackBundleSizeAnalyzerPlugin } from 'webpack-bundle-size-analyzer';

Now in your webpack's plugin section, start using this plugin:

{
  {...topOfConfig}
  plugins: [
    new WebpackBundleSizeAnalyzerPlugin('./reports/plain-report.txt')
  ]
  {...bottomOfConfig}
}

Example Plugin Output

Using the above plugin configuration, the following file will be written:

<webpack-output-path>/reports/plain-report.txt:

marked: 27.53 KB (14.9%)
lru-cache: 6.29 KB (3.40%)
style-loader: 717 B (0.379%)
<self>: 150.33 KB (81.3%)

Important Note About Minified Code

If you minify/compress your code using the approach recommended in the Webpack documentation, which is to use the UglifyJS plugin, be aware that this tool will report the sizes of modules before they are minified. This is because the statistics generated by webpack --json do not take account of plugins that operate on the bundle as a whole.

Knowing the sizes of dependencies before they are minified can still be useful to get an idea of how much different dependencies are responsible for size of your minified bundle, but be aware that it can be misleading. This is because some libraries compress better than others.

If instead you minify modules in your bundle individually using a loader (eg. the UglifyJS loader), the stats output by webpack --json will show minified sizes. If you want to get a better idea of the sizes of different dependencies after they are minified, you can temporarily remove the UglifyJS plugin and replace it with the UglifyJS loader instead before running webpack --json | webpack-bundle-size-analyzer.

The reason that using a plugin, rather than a loader, is the recommended approach in Webpack is that compression is better if applied to the whole bundle rather than to individual source files one at a time.

More Repositories

1

Qt-Inspector

Utility to browse the Qt object tree of a running Qt application and edit object properties on the fly.
C++
375
star
2

react-testing

Slides, notes and sample project from a talk on testing React applications given at the London React meetup group
JavaScript
188
star
3

passcards

A 1Password-compatible command-line and web-based password manager
TypeScript
137
star
4

tex-linebreak

JavaScript implementation of the Knuth-Plass linebreaking algorithm
TypeScript
110
star
5

tesseract-wasm

JS/WebAssembly build of the Tesseract OCR engine for use in browsers and Node
TypeScript
110
star
6

qt-mustache

Mustache templating library for C++ using Qt
C++
85
star
7

1pass

1Password command-line client
Go
66
star
8

konsole

Terminal for KDE
C++
60
star
9

approx-string-match-js

Approximate string matching library for JavaScript
TypeScript
35
star
10

ts-style

A small library for defining CSS classes using JavaScript or TypeScript
TypeScript
33
star
11

qt-maybe

Implementation of sum/option types using QVariant
C++
29
star
12

qt-signal-tools

Utility classes related to Qt signal and slot handling
C++
24
star
13

rollup-cache

Disk caching to speed up Rollup builds
JavaScript
15
star
14

mandrawer

A miscellaneous collection of scripts I use across various devices.
Python
13
star
15

sass-unused

Utility for finding unused variables in a collection of SASS files
JavaScript
12
star
16

extension-tools

A collection of scripts for automated publishing of browser extensions
JavaScript
11
star
17

kdevelop

KDevelop IDE
C++
10
star
18

babel-plugin-mockable-imports

Babel plugin that enables mocking of ES and CommonJS imports in tests
JavaScript
10
star
19

idb-mutex

Mutual exclusion (locks) between tabs in browsers using IndexedDB
TypeScript
9
star
20

xar-js

A JavaScript library for generating xar archives
TypeScript
8
star
21

dbpro-bluegui

BlueGUI v2 GUI controls plugin for DarkBASIC Professional
C++
7
star
22

rust-optparse

Command-line option parsing lib for Rust
Rust
3
star
23

duckface

Go-like interface generator for C++
Ruby
2
star
24

ureact

A small implementation of the modern React API for educational purposes.
JavaScript
2
star
25

unroll-it

Helper for creating data-driven / parametrized tests with Mocha
JavaScript
2
star
26

textgen

Random text generator built with PyTorch
Python
2
star
27

hot-reloading-talk

A talk for LondonJS on architecting web applications to enable hot reloading
JavaScript
2
star
28

elm-arch-tutorial

Notes made whilst working through the Elm Architecture Tutorial
Elm
1
star
29

talks

Slides and resources for talks I've given or will give
JavaScript
1
star
30

find-unused-css-classes

Tool for finding CSS classes that are styled by selectors in a set of CSS files but not used
JavaScript
1
star
31

rd

A tool for quick access to recently used directories
Go
1
star
32

elm-april-hack-night

Elm
1
star
33

qt-webitemview

Qt item view delegate which uses WebKit to render item content
C++
1
star
34

rust-cmacros

Rust lib for parsing macros from C header files and assisting with translation to Rust code
Rust
1
star
35

labnotes

A river-of-news feed aggregator. Created as a Mendeley hackday project.
TypeScript
1
star
36

used-css-classes

Tool that prints the list of unique CSS classes used in a set of HTML files or templates
Python
1
star
37

gomendeley

A basic demo showing authentication and use of the Mendeley API with Go
Go
1
star
38

prune-merged-branches

Tool to clean up your local Git branches
Python
1
star
39

schema

CSL Schema
JavaScript
1
star