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 self-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:
nodejs_binary(
name = "terser_bin",
entry_point = "//:node_modules/terser/bin/uglifyjs",
node_modules = ["//: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.
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 //@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 //@bazel/terser/bin:terser
3.0.0-rc.0 (2020-12-11)
Bug Fixes
- builtin: --nobazel_run_linker implies --bazel_patch_module_resolver (7100277)
- remove jasmine-core as a peer dep (#2336) (bb2a302)
- builtin: give a longer timeout for _create_build_files (5d405a7), closes #2231
- builtin: give better error when linker runs on Node <10 (b9dc2c1), closes #2304
- builtin: make linker deterministic when resolving from manifest & fix link_workspace_root with no runfiles (f7c342f)
- examples: fix jest example on windows (3ffefa1), closes #1454
- exmaples/nestjs: add module_name field in ts_library (3a4155c)
- typescript: don't depend on protobufjs, it's transitive (1b344db)
- typescript: fail the build when ts_project produces zero outputs (3ca6cac), closes #2301
- npm_package.pack on Windows should not generate undefined.tgz (715ad22)
- typescript: specify rootDir as absolute path (535fa51)
- npm_package.pack should work in windows os (503d6fb)
- typescript: don't include _valid_options marker file in outs (570e34d), closes #2078
chore
Code Refactoring
Features
- builtin: flip the default of the strict_visibility flag on the npm and yarn install rules to True (2c34857)
- concatjs: ts_devserver -> concatjs_devserver; move to @bazel/concatjs (baeae89), closes #1082
- cypress: remove browiserify preprocessor (98ee87d)
- examples: adds example for running jest with typescript (#2245) (d977c73)
- node_repositories: Added auth option for downloading nodejs and yarn (c89ff38)
- typescript: add allow_js support to ts_project (91a95b8)
- typescript: worker mode for ts_project (#2136) (5d70997)
Performance Improvements
- cypress: pack cypress runfiles into a single tar (e8484a0)
BREAKING CHANGES
- By default, we no longer patch the require() function, instead you should rely on the linker to make node modules resolvable at the standard location
if this breaks you, the quickest fix is to flip the flag back on a nodejs_binary/nodejs_test/npm_package_bin with
templated_args = ["--bazel_patch_module_resolver"]
, see https://github.com/bazelbuild/rules_nodejs/pull/2344 as an example.
Another fix is to explicitly use our runfiles helper library, see https://github.com/bazelbuild/rules_nodejs/pull/2341 as an example. packages/karma:package.bzl
is gone, in your WORKSPACE replace
load("//packages/karma:package.bzl", "npm_bazel_karma_dependencies")
npm_bazel_karma_dependencies()
with the equivalent
http_archive(
name = "io_bazel_rules_webtesting",
sha256 = "9bb461d5ef08e850025480bab185fd269242d4e533bca75bfb748001ceb343c3",
urls = ["https://github.com/bazelbuild/rules_webtesting/releases/download/0.3.3/rules_webtesting.tar.gz"],
)
Then in BUILD files replace
load("@npm//@bazel/karma:index.bzl", "karma_web_test_suite")
with
load("@npm//@bazel/concatjs:index.bzl", "concatjs_web_test_suite")
finally drop npm dependencies on @bazel/karma
and depend on @bazel/concatjs
instead
- concatjs_web back to karma_web
- typescript: any ts_project rule that produces no outputs must be fixed or removed
- pkg_web#move_files helper is now a private API
-
- rollup_bundle config_file no longer has substitutions from a "bazel_stamp_file" - use bazel_version_file instead
- pkg_npm no longer has replace_with_version attribute, use substitutions instead
- concatjs: users need to change their load statements for ts_devserver
- Users will need to rename
build_bazel_rules_typescript
to npm_bazel_typescript
and build_bazel_rules_karma
to npm_bazel_karma
in their projects - If you use the internal API of tsc_wrapped you need to update the CompilerHost constructor calls.