UNPKG

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