@bazel/terser

中文文档
5.8.1发布于 2023/02/17Apache-2.0零依赖近 30 天下载 8.9万

Run Terser JS optimizer under Bazel

原始使用文档
文档目录3 节
  1. Installation
  2. Installing with user-managed dependencies
  3. terser_minified

Terser rules for Bazel

The Terser rules run the Terser JS minifier with Bazel.

Wraps the Terser CLI documented at https://github.com/terser-js/terser#command-line-usage

Installation

Add the @bazel/terser npm package to your devDependencies in package.json.

Installing with user-managed dependencies

If you didn't use the yarn_install or npm_install rule, you'll have to declare a rule in your root BUILD.bazel file to execute terser:

# Create a terser rule to use in terser_minified#terser_bin
# attribute when using user-managed dependencies
nodejs_binary(
    name = "terser_bin",
    entry_point = "//:node_modules/terser/bin/uglifyjs",
    # Point bazel to your node_modules to find the entry point
    data = ["//:node_modules"],
)

terser_minified

USAGE

terser_minified(name, args, config_file, debug, sourcemap, src, terser_bin)

Run the terser minifier.

Typical example:

load("@npm//@bazel/terser:index.bzl", "terser_minified")

terser_minified(
    name = "out.min",
    src = "input.js",
    config_file = "terser_config.json",
)

Note that the name attribute determines what the resulting files will be called. So the example above will output out.min.js and out.min.js.map (since sourcemap defaults to true). If the input is a directory, then the output will also be a directory, named after the name attribute. Note that this rule is NOT recursive. It assumes a flat file structure. Passing in a folder with nested folder will result in an empty output directory.

ATTRIBUTES

name

(Name, mandatory): A unique name for this target.

args

(List of strings): Additional command line arguments to pass to terser.

Terser only parses minify() args from the config file so additional arguments such as --comments may be passed to the rule using this attribute. See https://github.com/terser/terser#command-line-usage for the full list of terser CLI options.

Defaults to []

config_file

(Label): A JSON file containing Terser minify() options.

This is the file you would pass to the --config-file argument in terser's CLI. https://github.com/terser-js/terser#minify-options documents the content of the file.

Bazel will make a copy of your config file, treating it as a template.

Run bazel with --subcommands to see the path to the copied file.

If you use the magic strings "bazel_debug" or "bazel_no_debug", these will be replaced with true and false respecting the value of the debug attribute or the --compilation_mode=dbg bazel flag.

For example

{
    "compress": {
        "arrows": "bazel_no_debug"
    }
}

Will disable the arrows compression setting when debugging.

If config_file isn't supplied, Bazel will use a default config file.

Defaults to @npm//@bazel/terser:terser_config.default.json

debug

(Boolean): Configure terser to produce more readable output.

Instead of setting this attribute, consider using debugging compilation mode instead bazel build --compilation_mode=dbg //my/terser:target so that it only affects the current build.

Defaults to False

sourcemap

(Boolean): Whether to produce a .js.map output

Defaults to True

src

(Label, mandatory): File(s) to minify.

Can be a .js file, a rule producing .js files as its default output, or a rule producing a directory of .js files.

Note that you can pass multiple files to terser, which it will bundle together. If you want to do this, you can pass a filegroup here.

terser_bin

(Label): An executable target that runs Terser

Defaults to @npm//@bazel/terser/bin:terser

版本与发布记录

@bazel/terser 共发布 100 个版本,首次发布于 2019/09/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.8.1latest2023/02/173 年前
5.7.32022/12/093 年前
5.7.22022/11/273 年前
5.7.12022/10/274 年前
5.7.02022/10/074 年前
5.6.02022/09/274 年前

其他发布通道:next → 5.0.0-rc.2,lts → 4.6.2。

关于 @bazel/terser 的常见问题

@bazel/terser 是什么?

Run Terser JS optimizer under Bazel

如何安装 @bazel/terser?

在项目目录运行 npm install @bazel/terser,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:terser(>=4.0.0 <5.9.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 terser,可以用 npx terser 直接运行。

@bazel/terser 支持 TypeScript 吗?

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

@bazel/terser 可以商用吗?

@bazel/terser 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bazel/terser 的下载量有多少?

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

@bazel/terser 还在维护吗?

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