Skip to content

Commit 76d11f3

Browse files
authoredJan 14, 2022
Update/modernize the Bazel build tooling. (google#474)
This includes: - Adding a .bazelignore so building `...` doesn't try to build the node_modules directory. - Removing a bazelrc flag that no longer exists and isn't needed anymore. - Switching to a modern rollup_bundle implementation. This requires: - Adding a config file to set the global name and license banner. - Generating output formats separately, since it no longer generates all of them. - Using the Rollup Buble plugin to downlevel (transpile) to ES5. - Switching npm_package to the modern pkg_npm. - This cleans up some "replacements" (formerly called "substitutions") - Switching from `bazel` to `bazelisk` as `bazel` is now [deprecated](https://www.npmjs.com/package/@bazel/bazel) for this use case. - Upgrading to rules_nodejs version 4.6.0. - Switching `ts_web_test` to the modern `karma_web_test` (which is in the `@bazel/concatjs` package). The JS output is roughly the same. There are some small changes related to variable renaming and optimizations that shouldn't affect runtime behavior. The main difference is that the const `DEBUG` replacements now work (before the `DEBUG` const got inlined before the replacement step).
1 parent caef363 commit 76d11f3

File tree

11 files changed

+7473
-577
lines changed

11 files changed

+7473
-577
lines changed
 

Diff for: ‎.bazelignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Diff for: ‎.bazelrc

-10
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ test --nolegacy_external_runfiles
3636
# Prevent TypeScript worker seeing files not declared as inputs
3737
build --worker_sandboxing
3838

39-
# Turn on the "Managed Directories" feature.
40-
# This allows Bazel to share the same node_modules directory with other tools
41-
# NB: this option was introduced in Bazel 0.26
42-
# See https://docs.bazel.build/versions/master/command-line-reference.html#flag--experimental_allow_incremental_repository_updates
43-
common --experimental_allow_incremental_repository_updates
44-
4539
# Turn on --incompatible_strict_action_env which was on by default
4640
# in Bazel 0.21.0 but turned off again in 0.22.0. Follow
4741
# https://github.com/bazelbuild/bazel/issues/7026 for more details.
@@ -60,10 +54,6 @@ run --incompatible_strict_action_env
6054
# rather than user.bazelrc as suggested in the Bazel docs)
6155
try-import %workspace%/.bazelrc.user
6256

63-
# Turn on managed directories feature in Bazel
64-
# This allows us to avoid installing a second copy of node_modules
65-
common --experimental_allow_incremental_repository_updates
66-
6757
# Prevent TS worker from trying to expand the `includes` section in tsconfig.json.
6858
# It would find the "test/*.ts" reference when compiling //src:src, and the FileCache will then error
6959
# when TS attempts to read one of these files that doesn't belong in the compilation.

Diff for: ‎BUILD

+85-60
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,70 @@
11
package(default_visibility = ["//:__subpackages__"])
22

3-
load("@npm_bazel_typescript//:defs.bzl", "ts_devserver", "ts_library")
4-
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package")
5-
load("@build_bazel_rules_nodejs//:defs.bzl", "rollup_bundle")
3+
load("@npm//@bazel/typescript:index.bzl", "ts_library")
4+
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
5+
load("@npm//@bazel/rollup:index.bzl", "rollup_bundle")
66

77
### Produce umd and cjs bundles
88

99
ts_library(
1010
name = "dev",
1111
srcs = ["index.ts"],
12-
deps = ["//src"],
1312
tsickle_typed = True,
13+
deps = ["//src"],
1414
)
1515

16-
rollup_bundle(
17-
name = "bundle",
18-
entry_point = ":index.ts",
19-
deps = [":dev"],
20-
global_name = "IncrementalDOM",
21-
license_banner = "conf/license_header.txt",
22-
)
16+
[
17+
rollup_bundle(
18+
name = "bundle.%s" % format,
19+
args = args,
20+
config_file = "rollup.config.js",
21+
entry_point = ":index.ts",
22+
format = format,
23+
sourcemap = "true",
24+
deps = [
25+
":dev",
26+
"@npm//@rollup/plugin-buble",
27+
],
28+
)
29+
for format, args in {
30+
"cjs": [],
31+
"umd": [
32+
# Downlevel (transpile) to ES5.
33+
"-p",
34+
"@rollup/plugin-buble",
35+
],
36+
}.items()
37+
]
2338

2439
genrule(
25-
name = "incremental-dom",
26-
srcs = [":bundle.es5umd.js"],
27-
outs = ["dist/incremental-dom.js"],
28-
cmd = "cp $(locations :bundle.es5umd.js) $@",
40+
name = "incremental-dom",
41+
srcs = [":bundle.umd.js"],
42+
outs = ["dist/incremental-dom.js"],
43+
cmd = "cp $(locations :bundle.umd.js) $@",
2944
)
3045

31-
npm_package(
32-
name = "npm-umd",
33-
deps = [
34-
":incremental-dom",
35-
],
36-
replacements = {
37-
"DEBUG" : "true"
38-
}
46+
pkg_npm(
47+
name = "npm-umd",
48+
deps = [
49+
":incremental-dom",
50+
],
3951
)
40-
52+
4153
genrule(
42-
name = "incremental-dom-cjs",
43-
srcs = [":bundle.cjs.js"],
44-
outs = ["dist/incremental-dom-cjs.js"],
45-
cmd = "cp $(locations :bundle.cjs.js) $@",
54+
name = "incremental-dom-cjs",
55+
srcs = [":bundle.cjs.js"],
56+
outs = ["dist/incremental-dom-cjs.js"],
57+
cmd = "cp $(locations :bundle.cjs.js) $@",
4658
)
4759

48-
npm_package(
49-
name = "npm-cjs",
50-
deps = [
51-
":incremental-dom-cjs",
52-
],
53-
replacements = {
54-
"DEBUG" : "process.env.NODE_ENV != \"production\""
55-
}
60+
pkg_npm(
61+
name = "npm-cjs",
62+
substitutions = {
63+
"const DEBUG = true;": "const DEBUG = process.env.NODE_ENV != \"production\";",
64+
},
65+
deps = [
66+
":incremental-dom-cjs",
67+
],
5668
)
5769

5870
### Produce minified bundle
@@ -69,44 +81,57 @@ genrule(
6981
ts_library(
7082
name = "release",
7183
srcs = [":release_index"],
72-
deps = ["//release"],
7384
tsickle_typed = True,
85+
deps = ["//release"],
7486
)
7587

7688
rollup_bundle(
77-
name = "min-bundle",
78-
entry_point = ":release_index.ts",
79-
deps = [":release"],
80-
global_name = "IncrementalDOM",
81-
license_banner = "conf/license_header.txt",
89+
name = "min-bundle",
90+
args = [
91+
# Downlevel (transpile) to ES5.
92+
"-p",
93+
"@rollup/plugin-buble",
94+
],
95+
config_file = "rollup.config.js",
96+
entry_point = ":release_index.ts",
97+
format = "umd",
98+
deps = [
99+
":release",
100+
"@npm//@rollup/plugin-buble",
101+
],
82102
)
83103

84104
## Need to run uglify to minify instead of using .min.es5umd.js, since it uses
85105
## Terser, which has some performance issues with the output in how it inlines
86106
## functions.
87107
genrule(
88-
name = "incremental-dom-min",
89-
srcs = [":min-bundle.es5umd.js"],
90-
outs = ["dist/incremental-dom-min.js"],
91-
cmd = "$(location node_modules/.bin/uglifyjs) --comments --source-map=url -m -o $@ $(location min-bundle.es5umd.js)",
92-
tools = ["node_modules/.bin/uglifyjs"],
108+
name = "incremental-dom-min",
109+
srcs = [":min-bundle.js"],
110+
outs = ["dist/incremental-dom-min.js"],
111+
cmd = "$(location node_modules/.bin/uglifyjs) --comments --source-map=url -m -o $@ $(location min-bundle.js)",
112+
tools = ["node_modules/.bin/uglifyjs"],
93113
)
94114

95-
npm_package(
96-
name = "npm-min",
97-
deps = [
98-
":incremental-dom-min",
99-
],
115+
pkg_npm(
116+
name = "npm-min",
117+
deps = [
118+
":incremental-dom-min",
119+
],
100120
)
101121

102122
### Emit TS files
103123

104-
npm_package(
105-
name = "npm",
106-
srcs = ["package.json", "index.ts", "//src:all_files"],
107-
packages = [
108-
":npm-min",
109-
":npm-umd",
110-
":npm-cjs",
111-
]
124+
pkg_npm(
125+
name = "npm",
126+
package_name = "incremental-dom",
127+
srcs = [
128+
"index.ts",
129+
"package.json",
130+
"//src:all_files",
131+
],
132+
nested_packages = [
133+
":npm-cjs",
134+
":npm-min",
135+
":npm-umd",
136+
],
112137
)

Diff for: ‎README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,25 @@ npm i
9191
To run once:
9292

9393
```sh
94-
./node_modules/.bin/bazel test ...
94+
./node_modules/.bin/bazelisk test ...
9595
```
9696

9797
To run on change:
9898

9999
```sh
100-
./node_modules/.bin/ibazel run ...
100+
./node_modules/.bin/ibazel run //test:unit_tests
101101
```
102102

103103
### Building
104104

105105
To build once:
106106

107107
```sh
108-
./node_modules/.bin/bazel build ...
108+
./node_modules/.bin/bazelisk build ...
109109
```
110110

111111
To build on change:
112112

113113
```sh
114-
./node_modules/.bin/ibazel ...
114+
./node_modules/.bin/ibazel build ...
115115
```

Diff for: ‎WORKSPACE

+20-23
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
88
# Fetch rules_nodejs so we can install our npm dependencies
99
http_archive(
1010
name = "build_bazel_rules_nodejs",
11-
patch_args = ["-p1"],
12-
patches = ["//:rules_nodejs_pr915.patch"],
13-
sha256 = "3b0116a8a91a75678a57ba676c246ac0fa9c90dc3d46daef305b11b54ed4467e",
14-
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.33.1/rules_nodejs-0.33.1.tar.gz"],
11+
sha256 = "ddb78717b802f8dd5d4c01c340ecdc007c8ced5c1df7db421d0df3d642ea0580",
12+
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/4.6.0/rules_nodejs-4.6.0.tar.gz"],
1513
)
1614

15+
load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories")
16+
17+
node_repositories(package_json = ["//:package.json"])
18+
1719
# Check the bazel version and download npm dependencies
18-
load("@build_bazel_rules_nodejs//:defs.bzl", "check_bazel_version", "npm_install")
20+
load("@build_bazel_rules_nodejs//:index.bzl", "check_bazel_version", "npm_install")
1921

2022
# Bazel version must be at least v0.21.0 because:
2123
# - 0.21.0 Using --incompatible_strict_action_env flag fixes cache when running `yarn bazel`
@@ -37,25 +39,20 @@ npm_install(
3739
package_lock_json = "//:package-lock.json",
3840
)
3941

40-
# Install all bazel dependencies of our npm packages
41-
load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")
42-
43-
install_bazel_dependencies()
44-
45-
# Setup the rules_typescript tooolchain
46-
load("@npm_bazel_typescript//:defs.bzl", "ts_setup_workspace")
47-
48-
ts_setup_workspace()
49-
50-
# Fetch transitive Bazel dependencies of npm_bazel_karma
51-
load("@npm_bazel_karma//:package.bzl", "rules_karma_dependencies")
52-
53-
rules_karma_dependencies()
42+
http_archive(
43+
name = "io_bazel_rules_webtesting",
44+
sha256 = "e9abb7658b6a129740c0b3ef6f5a2370864e102a5ba5ffca2cea565829ed825a",
45+
urls = ["https://github.com/bazelbuild/rules_webtesting/releases/download/0.3.5/rules_webtesting.tar.gz"],
46+
)
5447

55-
# Setup web testing, choose browsers we can test on
48+
# Set up web testing, choose browsers we can test on
5649
load("@io_bazel_rules_webtesting//web:repositories.bzl", "web_test_repositories")
57-
50+
5851
web_test_repositories()
5952

60-
load("@npm_bazel_karma//:browser_repositories.bzl", "browser_repositories")
61-
browser_repositories()
53+
load("@io_bazel_rules_webtesting//web/versioned:browsers-0.3.3.bzl", "browser_repositories")
54+
55+
browser_repositories(
56+
chromium = True,
57+
firefox = True,
58+
)

0 commit comments

Comments
 (0)
Please sign in to comment.