npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@browserify/uglifyify

v6.0.0

Published

A browserify transform which minifies your code using Terser

Downloads

12,088

Readme

@browserify/uglifyify

A Browserify transform which minifies your code using terser.

Installation

npm install @browserify/uglifyify

Motivation/Usage

Ordinarily you'd be fine doing this:

browserify index.js | terser -c > bundle.js

But uglifyify is able to yield smaller output by processing files individually instead of just the entire bundle. When using uglifyify you should generally also use Uglify, to achieve the smallest output. Uglifyify provides an additional optimization when used with Uglify, but does not provide all of the optimization that using Uglify on its own does, so it's not a replacement.

Uglifyify gives you the benefit of applying Uglify's "squeeze" transform on each file before it's included in the bundle, meaning you can remove dead code paths for conditional requires. Here's a contrived example:

if (true) {
  module.exports = require('./browser')
} else {
  module.exports = require('./node')
}

module.exports = require('./node') will be excluded by Uglify, meaning that only ./browser will be bundled and required.

If you combine uglifyify with envify, you can make this a little more accessible. Take this code:

if (process.env.NODE_ENV === 'development') {
  module.exports = require('./development')
} else {
  module.exports = require('./production')
}

And use this to compile:

NODE_ENV=development browserify -t envify -t @browserify/uglifyify index.js -o dev.js &&
NODE_ENV=production browserify -t envify -t @browserify/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 --standalone tag.

File Extensions

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 as uglify expects JavaScript and will throw if it can't parse what it's given.

This is done using the -x or --exts transform options, e.g. from the command-line:

browserify     \
  -t coffeeify \
  -t [ @browserify/uglifyify -x .js -x .coffee ]

The above example will only minify .js and .coffee files, ignoring the rest.

Global Transforms

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 @browserify/uglifyify ./index.js > bundle.js

Or programatically:

var browserify = require('browserify')
var fs = require('fs')

var bundler = browserify(__dirname + '/index.js')

bundler.transform('@browserify/uglifyify', { global: true  })

bundler.bundle()
  .pipe(fs.createWriteStream(__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.

Ignoring Files

Sometimes uglifyjs will break specific files under specific settings – it's rare, but does happen – and to work around that, you can use the ignore option. Given one or more glob patterns, you can filter out specific files this way:

browserify -g [ @browserify/uglifyify --ignore '**/node_modules/weakmap/*' ] ./index.js
var bundler = browserify('index.js')

bundler.transform('@browserify/uglifyify', {
  global: true,
  ignore: [
      '**/node_modules/weakmap/*'
    , '**/node_modules/async/*'
  ]
})

bundler.bundle().pipe(process.stdout)

Source Maps

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 --debug flag (or debug option) to your browserify bundle.

Enabling --debug with browserify is easy:

browserify -t @browserify/uglifyify --debug index.js
var bundler = browserify({ debug: true })

bundler
  .add('index.js')
  .transform('@browserify/uglifyify')
  .bundle()
  .pipe(process.stdout)

If you'd prefer them not to be included regardless, you can opt out using the sourcemap option:

browserify -t [ @browserify/uglifyify --no-sourcemap ] app.js
var bundler = browserify('index.js')

bundler.transform('@browserify/uglifyify', { sourceMap: false })
  .bundle()
  .pipe(process.stdout)