@browserify/uglifyify

中文文档
6.0.0发布于 2022/10/16MITCommonJS近 30 天下载 6.6万

A browserify transform which minifies your code using Terser

原始使用文档
文档目录6 节
  1. Installation
  2. Motivation/Usage
  3. File Extensions
  4. Global Transforms
  5. Ignoring Files
  6. Source Maps

@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)

版本与发布记录

@browserify/uglifyify 共发布 1 个版本,首次发布于 2022/10/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
6.0.0latest2022/10/164 年前

关于 @browserify/uglifyify 的常见问题

@browserify/uglifyify 是什么?

A browserify transform which minifies your code using Terser

如何安装 @browserify/uglifyify?

在项目目录运行 npm install @browserify/uglifyify,或使用 pnpm add / yarn add / bun add。

@browserify/uglifyify 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/browserify__uglifyify 是否存在。

@browserify/uglifyify 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@browserify/uglifyify 可以商用吗?

@browserify/uglifyify 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@browserify/uglifyify 的下载量有多少?

根据 npm 官方下载统计,@browserify/uglifyify 在 2026/10/09 – 2026/11/07 这 30 天内被下载 66,186 次,日均约 2,206 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@browserify/uglifyify 还在维护吗?

最新版本 6.0.0 发布于 2022/10/16(4 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/10/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。