UNPKG

7.34 kBMarkdownView Raw
1# Rollup Plugin Visualizer
2
3[![NPM Version](https://img.shields.io/npm/v/rollup-plugin-visualizer.svg)](https://npmjs.org/package/rollup-plugin-visualizer) [![Node.js CI](https://github.com/btd/rollup-plugin-visualizer/actions/workflows/node.js.yml/badge.svg)](https://github.com/btd/rollup-plugin-visualizer/actions/workflows/node.js.yml)
4
5Visualize and analyze your Rollup bundle to see which modules are taking up space.
6
7## Screenshots
8
9![pic](https://github.com/btd/rollup-plugin-visualizer/blob/master/pics/collage.png?raw=true)
10
11## Installation
12
13```sh
14npm install --save-dev rollup-plugin-visualizer
15```
16
17or via yarn:
18
19```sh
20yarn add --dev rollup-plugin-visualizer
21```
22
23## Usage
24
25Import
26
27```javascript
28// es
29import { visualizer } from "rollup-plugin-visualizer";
30// or
31// cjs
32const { visualizer } = require("rollup-plugin-visualizer");
33```
34
35Usage with rollup (rollup.config.js)
36
37```js
38module.exports = {
39 plugins: [
40 // put it the last one
41 visualizer(),
42 ],
43};
44```
45
46Usage with vite (vite.config.js)
47
48```js
49module.exports = {
50 plugins: [visualizer()],
51};
52```
53
54Usage with vite TypeScript (vite.config.ts)
55
56```ts
57import { defineConfig, type PluginOption } from 'vite'
58export default defineConfig({
59 plugins: [visualizer() as PluginOption],
60})
61```
62
63Usage with SvelteKit (vite.config.js)
64
65```js
66const config = {
67 plugins: [
68 visualizer({
69 emitFile: true,
70 filename: "stats.html",
71 }),
72 ],
73};
74
75export default config;
76```
77
78You will find 2/3 files in .svelte-kit/output dir named stats.html (see vite logs for file locations) . You can use this snippet as a starting point and change props/path.
79You can also generate 3 json files and combine them to one with cli util.
80
81## How to use generated files
82
83Blue color used to to mark project-own files. This could be: written by you files or generated by build tool files.
84
85Green color used to mark your dependencies.
86
87Internally it just checks if file id prefix is `node_modules`.
88
89All charts refresh layout on window resize.
90
91### Sunburst
92
93This circular hierarchical diagram can help you find huge pieces of code (aka that one huge thing). If you click on some arc it will increase its and all nested arcs size for better inspection.
94
95### Treemap
96
97This rectangular hierarchical diagram can help you find huge pieces. Just look on biggest reclangle. But also it can help you find modules included several times, they will have the same topology and relative size. If you click on reclangle it will increase in size for further inspection.
98
99### Network
100
101This digram should help you answer for the question 'why it is included?'. After force layout stabilize all nodes, you can move it back and forth by dragging with your mouse. Gray circles are treeshakened out files.
102
103In real life scenarious, sometimes you will see terribly connected diagrams. There is no 100% working solution for everyone, it is expected you topology will look 'terrible' and not hold on screen. To make it still visually ispectable, first remove all highly connected nodes that you see (typical examples: commonjsHelpers, tslib, react etc, basically if tooltip for the node is not hold on the screen - exclude this node), after layout stabilization you will see, your layout is not that terrible anymore and most of dependencies cluster together. Move layout to find pieces you looked for.
104
105When you click on node it will hightlight nodes that are listed in tooltip (the files that imports current node).
106
107### Raw-data
108
109This template produce JSON output with raw data. Normally it should be used with CLI from this plugin.
110
111### List
112
113Output yml file with all the data, could be good idea to commit this file to track file changes.
114
115## Options
116
117`filename` (string, default `stats.{ext depending template}`) - name of the file with diagram to generate
118
119`title` (string, default `Rollup Visualizer`) - title tag value
120
121`open` (boolean, default `false`) - Open generated file in default user agent
122
123`template` (string, default `treemap`) - Which diagram type to use: `sunburst`, `treemap`, `network`, `raw-data`, `list`.
124
125`gzipSize` (boolean, default `false`) - Collect gzip size from source code and display it at chart.
126
127`brotliSize` (boolean, default `false`) - Collect brotli size from source code and display it at chart.
128
129### Advanced options (touch only if you really need it):
130
131`emitFile` (boolean, default `false`) - Use rollup's `emitFile` to generate file. Useful if you want to control all output in one place (via rollup output options). This also could be usefull with svelte as it calls vite several times.
132
133`sourcemap` (boolean, default `false`) - Use sourcemaps to calculate sizes (e.g. after UglifyJs or Terser). **Always add plugin as last option.**
134
135`projectRoot` (string | RegExp, default `process.cwd()`) - This is some common root(s) path to your files. This is used to cut absolute files paths out.
136
137`include` (Filter | Filter[], default `undefined`) - Filter for inclusion
138
139`exclude` (Filter | Filter[], default `undefined`) - Filter for exclusion
140
141`Filter` type is `{ bundle?: picomatchPattern, file?: picomatchPattern }`
142
143**Note about `include` and `exclude`** - If options.include is omitted or has zero length, filter will return true by default. Otherwise, an ID must match one or more of the picomatch patterns, and must not match any of the options.exclude patterns. This entries will not be included in stats at all.
144
145#### Include and Exclude
146
147Include and exclude filters uses glob matchers with picomatch. In UI you can do such combinations (both exclude and include):
148* Filter bundle and file in one string
149 * `translation-*.js:*/**/index.js` - this selects all bundles that matches `translation-*.js` and all the files by all paths that name is `index.js`. `:` is separator and required only when bundle search used.
150 * Format for this kind of filter is `BUNDLE_GLOB:FILE_GLOB`
151* Filter bundle in one string
152 * This is special case of bundle+file filter, you need to omit `FILE_GLOB` part (empty string)
153* Filter file in one string
154 * **This is DEFAULT search option**
155 * `*/**/index.js` - select all files that name is index.js
156
157## CLI
158
159This plugin provides cli util `rollup-plugin-visualizer`. Add `--help` to check actual options. It can be used like:
160
161```sh
162rollup-plugin-visualizer [OPTIONS] stat1.json stat2.json ../stat3.json
163```
164
165This can be usefull in case you have different config files in the same project and you want to display all of them in the same chart.
166
167## Build plugin
168
169For development if you need to build plugin, just exec:
170
171```js
172npm run build
173```
174
175## Disclaimer about generated files
176
177Generated html files do not and never will contain your source code (contents of files). They can contain only js/html/css code required to build chart (plugin code) and statistical information about your source code.
178
179This statistical information can contain:
180
181- size of files included in bundle
182- size of files included in source map
183- file's paths
184- files hierarchy (fs tree for your files)
185
186## Upgrades
187
188See CHANGELOG.md.
189
190## Versioning
191
192* Plugin backend (one appears in configs) are strictly follows SemVer
193* Plugin frontend (generated file):
194 * `network`, `treemap`, `sunburst` can change does not matter of version (colors, texts, visual structure etc)
195 * `raw-data` format follows own `version` property
196 * `list` follows semver