UNPKG

4.24 kBMarkdownView Raw
1# Webpack Bundle Analyzer
2Webpack 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?
7Just 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
11This module will help you:
12
131. Realize what's *really* inside your bundle
142. Find out what modules make up the most of it's size
153. Find modules that got there by mistake
164. Optimize it!
17
18And the best thing is it supports minified bundles! It parses them to get real size of bundled modules.
19And it also shows their gzipped sizes!
20
21## Installation and usage
22There are two ways to use this module:
23
24### As plugin
25```sh
26npm install --save-dev webpack-bundle-analyzer
27```
28
29In `webpack.config.js`:
30```js
31var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
32
33// ...
34plugins: [new BundleAnalyzerPlugin()]
35// ...
36```
37
38`BundleAnalyzerPlugin` constructor can take an optional configuration object that defaults to this:
39
40```js
41new 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
75You can also analyze already existing bundles if you have Webpack Stats JSON file.
76
77You can generate it using `BundleAnalyzerPlugin` with `generateStatsFile` option set to `true` or with this simple
78command:
79
80```
81webpack --profile --json > stats.json
82```
83
84If 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```
87webpack --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
94webpack-bundle-analyzer reports three values:
95
96### Stat size
97
98This is the "input" size of your files, before any transformations like
99minification.
100
101It 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
106This is the "output" size of your files. If you're using a Webpack plugin such
107as Uglify, then this value will reflect the minified size of your code.
108
109### Gzip size
110
111This is the size of running the file(s) through gzip compression.
112
113## Contributing
114
115Check 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