A browserify transform which minifies your code using UglifyJS2
npm install uglifyify
Ordinarily you'd be fine doing this:
browserify index.js | uglifyjs -c > bundle.js
But uglifyify gives you the benefit applying Uglify's "squeeze" transform before it's processed by Browserify, meaning you can remove dead code paths for conditional requires. Here's a contrived example:
if truemoduleexports = require'./browser'elsemoduleexports = require'./node'
module.exports = require('./node') will be excluded by Uglify, meaning that
./browser will be bundled and required.
If you combine uglifyify with envify, you can make this a little more accessible. Take this code:
if processenvNODE_ENV === 'development'moduleexports = require'./development'elsemoduleexports = require'./production'
And use this to compile:
NODE_ENV=development browserify -t envify -t uglifyify index.js -o dev.js &&NODE_ENV=production browserify -t envify -t uglifyify index.js -o prod.js
It should go without saying that you should be hesitant using environment
variables in a Browserify module - this is best suited to your own
applications or modules built with Browserify's
Sometimes, you don't want uglifyify to minify all of your files – for example,
if you're using a transform to
require CSS or HTML, you might get an error
This is done using the
--exts transform options, e.g. from the
browserify \-t coffeeify \-t [ uglifyify -x .js -x .coffee ]
The above example will only minify
.coffee files, ignoring the rest.
You might also want to take advantage of uglifyify's pre-bundle minification to produce slightly leaner files across your entire browserify bundle. By default, transforms only alter your application code, but you can use global transforms to minify module code too. From your terminal:
browserify -g uglifyify ./index.js > bundle.js
var browserify = require'browserify'var fs = require'fs'var bundler = browserify__dirname + '/index.js'bundlertransformglobal: true'uglifyify'bundlerbundlepipefscreateWriteStream__dirname + '/bundle.js'
Note that this is fine for uglifyify as it shouldn't modify the behavior of your code unexpectedly, but transforms such as envify should almost always stay local – otherwise you'll run into unexpected side-effects within modules that weren't expecting to be modified as such.
Sometimes uglifyjs will break specific files under specific settings – it's
rare, but does happen – and to work around that, you can use the
option. Given one or more glob patterns, you can filter out specific files
browserify -g [ uglifyify --ignore '**/node_modules/weakmap/*' ] ./index.js
var bundler = browserify'index.js'bundlertransformglobal: trueignore:'**/node_modules/weakmap/*''**/node_modules/async/*''uglifyify'bundlerbundlepipeprocessstdout
Uglifyify supports source maps, so you can minify your code and still see the original source – this works especially well with a tool such as exorcist when creating production builds.
Source maps are enabled when:
- You're using another transform, such as coffeeify, that inlines source maps.
- You've passed the
debugoption) to your browserify bundle.
--debug with browserify is easy:
browserify -t uglifyify --debug index.js
var bundler = browserify debug: truebundleradd'index.js'transform sourcemap: false 'uglifyify'bundlepipeprocessstdout
If you'd prefer them not to be included regardless, you can opt out
browserify -t [ uglifyify --no-sourcemap ] app.js
var bundler = browserify'index.js'bundlertransform sourcemap: false 'uglifyify'bundlepipeprocessstdout