-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(terser): introduce @bazel/terser package
This is a clean implementation starting from what we were running in the built-in rollup_bundle rule. It doesn't yet incorporate our new JS Providers design; that will come next. For now it just understands immediate .js inputs
- Loading branch information
Showing
40 changed files
with
776 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ module.exports = { | |
'protractor', | ||
'stylus', | ||
'rollup', | ||
'terser', | ||
'typescript', | ||
'worker', | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
--- | ||
title: Terser | ||
layout: default | ||
stylesheet: docs | ||
--- | ||
# Terser rules for Bazel | ||
|
||
**WARNING: this is beta-quality software. Breaking changes are likely. Not recommended for production use without expert support.** | ||
|
||
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`. | ||
|
||
Your `WORKSPACE` should declare a `yarn_install` or `npm_install` rule named `npm`. | ||
It should then install the rules found in the npm packages using the `install_bazel_dependencies` function. | ||
See https://github.com/bazelbuild/rules_nodejs/#quickstart | ||
|
||
This causes the `@bazel/terser` package to be installed as a Bazel workspace named `npm_bazel_terser`. | ||
|
||
|
||
## Installing with self-managed dependencies | ||
|
||
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: | ||
|
||
```python | ||
# Create a terser rule to use in terser_minified#terser_bin | ||
# attribute when using self-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 | ||
node_modules = ["//:node_modules"], | ||
) | ||
``` | ||
|
||
[name]: https://bazel.build/docs/build-ref.html#name | ||
[label]: https://bazel.build/docs/build-ref.html#labels | ||
[labels]: https://bazel.build/docs/build-ref.html#labels | ||
|
||
|
||
## terser_minified | ||
|
||
Run the terser minifier. | ||
|
||
Typical example: | ||
```python | ||
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. | ||
|
||
|
||
|
||
### Usage | ||
|
||
``` | ||
terser_minified(name, config_file, debug, sourcemap, src, terser_bin) | ||
``` | ||
|
||
|
||
|
||
#### `name` | ||
(*[name], mandatory*): A unique name for this target. | ||
|
||
|
||
#### `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. | ||
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 `--define=DEBUG=true` bazel flag. | ||
|
||
If this isn't supplied, Bazel will use a default config file. | ||
|
||
|
||
#### `debug` | ||
(*Boolean*): Configure terser to produce more readable output. | ||
|
||
Instead of setting this attribute, consider setting the DEBUG variable instead | ||
bazel build --define=DEBUG=true //my/terser:target | ||
so that it only affects the current build. | ||
|
||
|
||
#### `sourcemap` | ||
(*Boolean*): Whether to produce a .js.map file for each .js output | ||
|
||
|
||
#### `src` | ||
(*[label], mandatory*): A JS file, or a rule producing .js as its default output | ||
|
||
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import %workspace%/../../common.bazelrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_test") | ||
load("@npm_bazel_terser//:index.bzl", "terser_minified") | ||
|
||
terser_minified( | ||
name = "out.min", | ||
src = "input.js", | ||
) | ||
|
||
nodejs_test( | ||
name = "test", | ||
data = ["out.min.js"], | ||
entry_point = ":test.js", | ||
) | ||
|
||
# For testing from the root workspace of this repository with bazel_integration_test. | ||
filegroup( | ||
name = "all_files", | ||
srcs = glob( | ||
include = ["**/*"], | ||
exclude = [ | ||
"bazel-out/**/*", | ||
"dist/**/*", | ||
"node_modules/**/*", | ||
], | ||
), | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Copyright 2018 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
workspace( | ||
name = "e2e_terser", | ||
managed_directories = {"@npm": ["node_modules"]}, | ||
) | ||
|
||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
http_archive( | ||
name = "build_bazel_rules_nodejs", | ||
sha256 = "3356c6b767403392bab018ce91625f6d15ff8f11c6d772dc84bc9cada01c669a", | ||
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.36.1/rules_nodejs-0.36.1.tar.gz"], | ||
) | ||
|
||
load("@build_bazel_rules_nodejs//:defs.bzl", "yarn_install") | ||
|
||
yarn_install( | ||
name = "npm", | ||
package_json = "//:package.json", | ||
yarn_lock = "//:yarn.lock", | ||
) | ||
|
||
load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies") | ||
|
||
install_bazel_dependencies() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const somelongname = 1; | ||
console.error(somelongname); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "e2e-terser", | ||
"private": true, | ||
"devDependencies": { | ||
"@bazel/terser": "latest" | ||
}, | ||
"scripts": { | ||
"test": "bazel test ..." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const fs = require('fs'); | ||
const content = fs.readFileSync(require.resolve(__dirname + '/out.min.js'), 'utf-8'); | ||
if (content.indexOf('console.error(1)') < 1) { | ||
console.error(content), process.exitCode = 1; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
exports_files(["bin.js"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const unidiff = require('unidiff'); | ||
|
||
function main(args) { | ||
const [mode, golden, actual] = args; | ||
const actualContents = fs.readFileSync(require.resolve(actual), 'utf-8').replace(/\r\n/g, '\n'); | ||
const goldenContents = fs.readFileSync(require.resolve(golden), 'utf-8').replace(/\r\n/g, '\n'); | ||
|
||
if (actualContents !== goldenContents) { | ||
if (mode === '--out') { | ||
// Write to golden file | ||
fs.writeFileSync(require.resolve(golden), actualContents); | ||
console.error(`Replaced ${path.join(process.cwd(), golden)}`); | ||
} else if (mode === '--verify') { | ||
// Generated does not match golden | ||
const diff = unidiff.diffLines(goldenContents, actualContents); | ||
const prettyDiff = unidiff.formatLines(diff, {aname: golden, bname: actual}); | ||
throw new Error(`Actual output doesn't match golden file: | ||
${prettyDiff} | ||
Update the golden file: | ||
bazel run ${process.env['BAZEL_TARGET'].replace(/_bin$/, '')}.accept | ||
`); | ||
} else { | ||
throw new Error('unknown mode', mode); | ||
} | ||
} | ||
} | ||
|
||
if (require.main === module) { | ||
main(process.argv.slice(2)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"Convenience for testing that an output matches a file" | ||
|
||
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary", "nodejs_test") | ||
|
||
def golden_file_test(name, golden, actual, **kwargs): | ||
data = [golden, actual, "@npm//unidiff"] | ||
|
||
loc = "$(location %s)" | ||
nodejs_test( | ||
name = name, | ||
entry_point = "@build_bazel_rules_nodejs//internal/golden_file_test:bin.js", | ||
templated_args = ["--verify", loc % golden, loc % actual], | ||
data = data, | ||
**kwargs | ||
) | ||
|
||
nodejs_binary( | ||
name = name + ".accept", | ||
testonly = True, | ||
entry_point = "@build_bazel_rules_nodejs//internal/golden_file_test:bin.js", | ||
templated_args = ["--out", loc % golden, loc % actual], | ||
data = data, | ||
**kwargs | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ NESTED_PACKAGES = [ | |
"less", | ||
"protractor", | ||
"stylus", | ||
"terser", | ||
"typescript", | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright 2017 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
load("@build_bazel_rules_nodejs//:tools/defaults.bzl", "npm_package") | ||
|
||
# Ugly genrule depending on local linux environment to build the README out of skylark doc generation. | ||
# Only referenced when we do a release. | ||
# TODO: This ought to be possible with stardoc alone. Need to coordinate with Chris Parsons. | ||
genrule( | ||
name = "generate_README", | ||
srcs = [ | ||
"//packages/terser/docs:index.md", | ||
"//packages/terser/docs:install.md", | ||
], | ||
outs = ["README.md"], | ||
cmd = """cat $(location //packages/terser/docs:install.md) $(location //packages/terser/docs:index.md) | sed 's/^##/\\\n##/' > $@""", | ||
visibility = ["//docs:__pkg__"], | ||
) | ||
|
||
npm_package( | ||
name = "npm_package", | ||
srcs = [ | ||
"@npm_bazel_terser//:package_contents", | ||
], | ||
vendor_external = [ | ||
"npm_bazel_terser", | ||
], | ||
deps = [ | ||
":generate_README", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
load("//tools/stardoc:index.bzl", "stardoc") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
exports_files(["install.md"]) | ||
|
||
stardoc( | ||
name = "docs", | ||
testonly = True, | ||
out = "index.md", | ||
input = "@npm_bazel_terser//:index.bzl", | ||
deps = ["@npm_bazel_terser//:bzl"], | ||
) |
Oops, something went wrong.