|
| 1 | +--- |
| 2 | +title: Terser |
| 3 | +layout: default |
| 4 | +stylesheet: docs |
| 5 | +--- |
| 6 | +# Terser rules for Bazel |
| 7 | + |
| 8 | +**WARNING: this is beta-quality software. Breaking changes are likely. Not recommended for production use without expert support.** |
| 9 | + |
| 10 | +The Terser rules run the Terser JS minifier with Bazel. |
| 11 | + |
| 12 | +Wraps the Terser CLI documented at https://github.com/terser-js/terser#command-line-usage |
| 13 | + |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +Add the `@bazel/terser` npm package to your `devDependencies` in `package.json`. |
| 18 | + |
| 19 | +Your `WORKSPACE` should declare a `yarn_install` or `npm_install` rule named `npm`. |
| 20 | +It should then install the rules found in the npm packages using the `install_bazel_dependencies` function. |
| 21 | +See https://github.com/bazelbuild/rules_nodejs/#quickstart |
| 22 | + |
| 23 | +This causes the `@bazel/terser` package to be installed as a Bazel workspace named `npm_bazel_terser`. |
| 24 | + |
| 25 | + |
| 26 | +## Installing with self-managed dependencies |
| 27 | + |
| 28 | +If you didn't use the `yarn_install` or `npm_install` rule to create an `npm` workspace, you'll have to declare a rule in your root `BUILD.bazel` file to execute terser: |
| 29 | + |
| 30 | +```python |
| 31 | +# Create a terser rule to use in terser_minified#terser_bin |
| 32 | +# attribute when using self-managed dependencies |
| 33 | +nodejs_binary( |
| 34 | + name = "terser_bin", |
| 35 | + entry_point = "//:node_modules/terser/bin/uglifyjs", |
| 36 | + # Point bazel to your node_modules to find the entry point |
| 37 | + node_modules = ["//:node_modules"], |
| 38 | +) |
| 39 | +``` |
| 40 | + |
| 41 | +[name]: https://bazel.build/docs/build-ref.html#name |
| 42 | +[label]: https://bazel.build/docs/build-ref.html#labels |
| 43 | +[labels]: https://bazel.build/docs/build-ref.html#labels |
| 44 | + |
| 45 | + |
| 46 | +## terser_minified |
| 47 | + |
| 48 | +Run the terser minifier. |
| 49 | + |
| 50 | +Typical example: |
| 51 | +```python |
| 52 | +load("@npm_bazel_terser//:index.bzl", "terser_minified") |
| 53 | + |
| 54 | +terser_minified( |
| 55 | + name = "out.min", |
| 56 | + src = "input.js", |
| 57 | + config_file = "terser_config.json", |
| 58 | +) |
| 59 | +``` |
| 60 | + |
| 61 | +Note that the `name` attribute determines what the resulting files will be called. |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +### Usage |
| 66 | + |
| 67 | +``` |
| 68 | +terser_minified(name, config_file, debug, sourcemap, src, terser_bin) |
| 69 | +``` |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +#### `name` |
| 74 | +(*[name], mandatory*): A unique name for this target. |
| 75 | + |
| 76 | + |
| 77 | +#### `config_file` |
| 78 | +(*[label]*): A JSON file containing Terser minify() options. |
| 79 | + |
| 80 | +This is the file you would pass to the --config-file argument in terser's CLI. |
| 81 | +https://github.com/terser-js/terser#minify-options documents the content of the file. |
| 82 | + |
| 83 | +Bazel will make a copy of your config file, treating it as a template. |
| 84 | +If you use the magic strings `bazel_debug` or `bazel_no_debug`, these will be |
| 85 | +replaced with `true` and `false` respecting the value of the `debug` attribute |
| 86 | +or the `--define=DEBUG=true` bazel flag. |
| 87 | + |
| 88 | +If this isn't supplied, Bazel will use a default config file. |
| 89 | + |
| 90 | + |
| 91 | +#### `debug` |
| 92 | +(*Boolean*): Configure terser to produce more readable output. |
| 93 | + |
| 94 | +Instead of setting this attribute, consider setting the DEBUG variable instead |
| 95 | +bazel build --define=DEBUG=true //my/terser:target |
| 96 | +so that it only affects the current build. |
| 97 | + |
| 98 | + |
| 99 | +#### `sourcemap` |
| 100 | +(*Boolean*): Whether to produce a .js.map file for each .js output |
| 101 | + |
| 102 | + |
| 103 | +#### `src` |
| 104 | +(*[label], mandatory*): A JS file, or a rule producing .js as its default output |
| 105 | + |
| 106 | +Note that you can pass multiple files to terser, which it will bundle together. |
| 107 | +If you want to do this, you can pass a filegroup here. |
| 108 | + |
| 109 | + |
| 110 | +#### `terser_bin` |
| 111 | +(*[label]*): An executable target that runs Terser |
| 112 | + |
| 113 | + |
0 commit comments