UNPKG

4.68 kBMarkdownView Raw
1# disc ![gittip: hughsk](http://img.shields.io/gittip/hughsk.svg) ![npm](http://img.shields.io/npm/dm/disc.svg) ![stable](http://img.shields.io/badge/stability-stable-green.svg) #
2
3Disc is a tool for analyzing the module tree of
4[browserify](http://browserify.org) project bundles. It's especially handy
5for catching large and/or duplicate modules which might be either bloating up
6your bundle or slowing down the build process.
7
8The demo included on disc's [github page](http://hughsk.github.io/disc)
9is the end result of running the tool on browserify's own code base.
10
11## Installation ##
12
13Disc lives on [npm](http://npmjs.org/package/npm), so if you haven't already
14make sure you have [node](http://nodejs.org/) installed on your machine first.
15
16Installing should then be as easy as:
17
18``` bash
19sudo npm install -g disc
20```
21
22## Command-Line Interface ##
23
24***Note:*** *you'll need to build your bundle with the `--full-paths` flag
25for disc to do its thing.*
26
27``` bash
28discify [bundle(s)...] {options}
29
30Options:
31 -h, --help Displays these instructions.
32 -o, --output Output path of the bundle. Defaults to stdout.
33 -O, --open Opens disc in a new browser window automatically
34 -m, --mode the default file scale mode to display: should be
35 either "count" or "size". Default: size
36```
37
38When you install disc globally, you the `discify` command-line tool is made
39available as the quickest means of checking out your bundle. As of disc v1.0.0,
40this tool takes any bundled browserify script as input and spits out a
41standalone HTML page as output.
42
43For example:
44
45``` bash
46browserify --full-paths index.js > bundle.js
47discify bundle.js > disc.html
48open disc.html
49```
50
51You can easily chain this file into another command, or use the `--open`
52flag to open disc in your browser automatically:
53
54``` bash
55browserify --full-paths index.js | discify --open
56```
57
58## Module API ##
59
60***Note:*** *you'll need to build your bundle with the `fullPaths` option
61for disc to do its thing.*
62
63### `require('disc')(opts)` ###
64
65Creates a through stream that you can pipe a bundle into, and get an HTML file
66in return – much like you would expect when working with the command-line tool.
67
68So to perform the above example with Node instead of Bash:
69
70``` javascript
71var browserify = require('browserify')
72var open = require('opener')
73var disc = require('disc')
74var fs = require('fs')
75
76var input = __dirname + '/index.js'
77var output = __dirname + '/disc.html'
78
79var bundler = browserify(input, {
80 fullPaths: true
81})
82
83bundler.bundle()
84 .pipe(disc())
85 .pipe(fs.createWriteStream(output))
86 .once('close', function() {
87 open(output)
88 })
89```
90
91This method takes the following options:
92
93* `header`: HTML to include above the visualisation. Used internally to render
94 the "Fork me on GitHub" ribbon.
95* `footer`: HTML to include beneath the visualisation. Used internally for the
96 description on the demo page.
97* `mode`: the default file scale mode to display: one of either `"count"` or
98 `"size"`, defaulting to `"size"`.
99
100### `disc.bundle(bundles, [opts], callback)` ###
101
102A callback-style interface for disc: takes an array of `bundles` (note: the
103file contents and not the file names), calling `callback(err, html)` with
104either an error or the resulting standalone HTML file as arguments.
105
106This currently mirrors how disc is currently implemented, but the stream API is
107a little more convenient to work with.
108
109### `disc.json(bundles, callback)` ###
110
111Takes an array of bundle contents (as strings, or Buffers), and gathers the
112required data - calling `callback(err, json)` with either an error or the
113results.
114
115## Palettes ##
116
117You can switch between multiple color palettes, most of which serve to highlight
118specific features of your bundle:
119
120### Structure Highlights ###
121
122![Structure Highlights](http://i.imgur.com/LO6Gio3.png)
123
124Highlights `node_modules` directories as green and `lib` directories as orange.
125This makes it easier to scan for "kitchen sink" modules or modules with lots of
126dependencies.
127
128### File Types ###
129
130![File Types](http://i.imgur.com/A8zDrbN.png)
131
132Highlights each file type (e.g. `.js`, `.css`, etc.) a different color. Helpful
133for tracking down code generated from a transform that's bloating up your bundle
134more than expected.
135
136### Browserify Core ###
137
138![Browserify Core](http://i.imgur.com/AtiKgwR.png)
139
140Highlights the automatically included and/or inserted modules that come courtesy
141of browserify in red. Makes it easy to quantify just how much space in your
142bundle is the result of shimming node's core functionality.
143
144### Original/Pastel ###
145
146Nothing particularly special about these palettes – colored for legibility and
147aesthetics respectively.