1 | # Webpack Bundle Analyzer
|
2 | Webpack plugin and CLI utility that represents bundle content as convenient interactive zoomable treemap
|
3 |
|
4 | [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url]
|
5 |
|
6 | ## What is this for?
|
7 | Just take a look at this demo:
|
8 |
|
9 | ![webpack bundle analyzer zoomable treemap](https://cloud.githubusercontent.com/assets/302213/20628702/93f72404-b338-11e6-92d4-9a365550a701.gif)
|
10 |
|
11 | This module will help you:
|
12 |
|
13 | 1. Realize what's *really* inside your bundle
|
14 | 2. Find out what modules make up the most of it's size
|
15 | 3. Find modules that got there by mistake
|
16 | 4. Optimize it!
|
17 |
|
18 | And the best thing is it supports minified bundles! It parses them to get real size of bundled modules.
|
19 | And it also shows their gzipped sizes!
|
20 |
|
21 | ## Installation and usage
|
22 | There are two ways to use this module:
|
23 |
|
24 | ### As plugin
|
25 | ```sh
|
26 | npm install --save-dev webpack-bundle-analyzer
|
27 | ```
|
28 |
|
29 | In `webpack.config.js`:
|
30 | ```js
|
31 | var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
|
32 |
|
33 | // ...
|
34 | plugins: [new BundleAnalyzerPlugin()]
|
35 | // ...
|
36 | ```
|
37 |
|
38 | `BundleAnalyzerPlugin` constructor can take an optional configuration object that defaults to this:
|
39 |
|
40 | ```js
|
41 | new BundleAnalyzerPlugin({
|
42 | // Can be `server`, `static` or `disabled`.
|
43 | // In `server` mode analyzer will start HTTP server to show bundle report.
|
44 | // In `static` mode single HTML file with bundle report will be generated.
|
45 | // In `disabled` mode you can use this plugin to just generate Webpack Stats JSON file by setting `generateStatsFile` to `true`.
|
46 | analyzerMode: 'server',
|
47 | // Host that will be used in `server` mode to start HTTP server.
|
48 | analyzerHost: '127.0.0.1',
|
49 | // Port that will be used in `server` mode to start HTTP server.
|
50 | analyzerPort: 8888,
|
51 | // Path to bundle report file that will be generated in `static` mode.
|
52 | // Relative to bundles output directory.
|
53 | reportFilename: 'report.html',
|
54 | // Module sizes to show in report by default.
|
55 | // Should be one of `stat`, `parsed` or `gzip`.
|
56 | // See "Definitions" section for more information.
|
57 | defaultSizes: 'parsed',
|
58 | // Automatically open report in default browser
|
59 | openAnalyzer: true,
|
60 | // If `true`, Webpack Stats JSON file will be generated in bundles output directory
|
61 | generateStatsFile: false,
|
62 | // Name of Webpack Stats JSON file that will be generated if `generateStatsFile` is `true`.
|
63 | // Relative to bundles output directory.
|
64 | statsFilename: 'stats.json',
|
65 | // Options for `stats.toJson()` method.
|
66 | // For example you can exclude sources of your modules from stats file with `source: false` option.
|
67 | // See more options here: https://github.com/webpack/webpack/blob/webpack-1/lib/Stats.js#L21
|
68 | statsOptions: null,
|
69 | // Log level. Can be 'info', 'warn', 'error' or 'silent'.
|
70 | logLevel: 'info'
|
71 | })
|
72 | ```
|
73 |
|
74 | ### As CLI utility
|
75 | You can also analyze already existing bundles if you have Webpack Stats JSON file.
|
76 |
|
77 | You can generate it using `BundleAnalyzerPlugin` with `generateStatsFile` option set to `true` or with this simple
|
78 | command:
|
79 |
|
80 | ```
|
81 | webpack --profile --json > stats.json
|
82 | ```
|
83 |
|
84 | If you're on Windows and using PowerShell, you can generate the stats file with this command to [avoid BOM issues](https://github.com/th0r/webpack-bundle-analyzer/issues/47):
|
85 |
|
86 | ```
|
87 | webpack --profile --json | Out-file 'stats.json' -Encoding OEM
|
88 | ```
|
89 |
|
90 | `webpack-bundle-analyzer --help` will show you all usage information.
|
91 |
|
92 | ## Definitions
|
93 |
|
94 | webpack-bundle-analyzer reports three values:
|
95 |
|
96 | ### Stat size
|
97 |
|
98 | This is the "input" size of your files, before any transformations like
|
99 | minification.
|
100 |
|
101 | It is called "stat size" because it's obtained from Webpack's
|
102 | [stats object](https://webpack.js.org/configuration/stats/).
|
103 |
|
104 | ### Parsed size
|
105 |
|
106 | This is the "output" size of your files. If you're using a Webpack plugin such
|
107 | as Uglify, then this value will reflect the minified size of your code.
|
108 |
|
109 | ### Gzip size
|
110 |
|
111 | This is the size of running the file(s) through gzip compression.
|
112 |
|
113 | ## Contributing
|
114 |
|
115 | Check out [CONTRIBUTING.md](./CONTRIBUTING.md) for instructions on contributing :tada:
|
116 |
|
117 | ## License
|
118 |
|
119 | [MIT](LICENSE)
|
120 |
|
121 | [downloads-image]: https://img.shields.io/npm/dt/webpack-bundle-analyzer.svg
|
122 | [npm-url]: https://www.npmjs.com/package/webpack-bundle-analyzer
|
123 | [npm-image]: https://img.shields.io/npm/v/webpack-bundle-analyzer.svg
|