UNPKG

1.47 kBMarkdownView Raw
1# uglifyify
2
3A [Browserify](http://browserify.org) v2 transform which minifies your code
4using [UglifyJS2](https://github.com/mishoo/UglifyJS).
5
6## Installation
7
8``` bash
9npm install uglifyify
10```
11
12## Motivation/Usage
13
14Ordinarily you'd be fine doing this:
15
16``` bash
17browserify index.js | uglifyjs -c > bundle.js
18```
19
20But uglifyify gives you the benefit applying Uglify's "squeeze" transform
21*before* it's processed by Browserify, meaning you can remove dead code paths
22for conditional requires. Here's a contrived example:
23
24``` javascript
25if (true) {
26 module.exports = require('./browser')
27} else {
28 module.exports = require('./node')
29}
30```
31
32`module.exports = require('./node')` will be excluded by Uglify, meaning that
33only `./browser` will be bundled and required.
34
35If you combine uglifyify with [envify](http://github.com/hughsk/envify), you
36can make this a little more accessible. Take this code:
37
38``` javascript
39if (process.env.NODE_ENV === 'development') {
40 module.exports = require('./development')
41} else {
42 module.exports = require('./production')
43}
44```
45
46And use this to compile:
47
48``` bash
49NODE_ENV=development browserify -t envify -t uglifyify index.js -o dev.js &&
50NODE_ENV=production browserify -t envify -t uglifyify index.js -o prod.js
51```
52
53It should go without saying that you should be hesitant using environment
54variables in a Browserify module - this is best suited to your own
55applications or modules built with Browserify's `--standalone` tag.