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