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