Skip to content

Commit 7231aaa

Browse files
committed
feat(examples): demonstrate that a macro assembles a workflow
introduce differential_loading.bzl that contains all the logic
1 parent c6cd91c commit 7231aaa

4 files changed

Lines changed: 69 additions & 59 deletions

File tree

examples/webapp/BUILD.bazel

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,17 @@
1-
# TODO(alexeagle): promote web_package rule to the public API
2-
load("@build_bazel_rules_nodejs//internal/web_package:web_package.bzl", "web_package")
3-
load("@npm//@babel/cli:index.bzl", "babel")
41
load("@npm//http-server:index.bzl", "http_server")
52
load("@npm_bazel_protractor//:index.bzl", "protractor_web_test_suite")
6-
load("@npm_bazel_rollup//:index.bzl", "rollup_bundle")
7-
load("@npm_bazel_terser//:index.bzl", "terser_minified")
3+
load(":differential_loading.bzl", "differential_loading")
84

9-
rollup_bundle(
10-
name = "chunks",
5+
differential_loading(
6+
name = "app",
117
srcs = glob(["*.js"]),
128
entry_point = "index.js",
13-
output_dir = True,
14-
)
15-
16-
# For older browsers, we'll transform the output chunks to es5 + systemjs loader
17-
babel(
18-
name = "chunks_es5",
19-
args = [
20-
"$(location chunks)",
21-
"--config-file",
22-
"$(location es5.babelrc)",
23-
"--out-dir",
24-
"$@",
25-
],
26-
data = [
27-
"chunks",
28-
"es5.babelrc",
29-
"@npm//@babel/preset-env",
30-
],
31-
output_dir = True,
32-
)
33-
34-
# Run terser against both modern and legacy browser chunks
35-
terser_minified(
36-
name = "chunks_es5.min",
37-
src = "chunks_es5",
38-
)
39-
40-
terser_minified(
41-
name = "chunks.min",
42-
src = "chunks",
43-
)
44-
45-
web_package(
46-
name = "package",
47-
# FIXME: need something like:
48-
# entry_point = "index.js",
49-
assets = [
50-
"styles.css",
51-
],
52-
data = [
53-
"favicon.png",
54-
# For differential loading, we supply both ESModule chunks and es5 chunks.
55-
# The injector will put two complimentary script tags in the index.html
56-
":chunks.min",
57-
":chunks_es5.min",
58-
],
59-
index_html = "index.html",
609
)
6110

6211
http_server(
6312
name = "server",
64-
data = [":package"],
65-
templated_args = ["package"],
13+
data = [":app"],
14+
templated_args = ["app"],
6615
)
6716

6817
# BazelCI docker images are missing shares libs to run a subset browser tests:
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"Illustrates how a reusable high-level workflow can be assembled from individual tools"
2+
3+
# TODO(alexeagle): promote web_package rule to the public API
4+
load("@build_bazel_rules_nodejs//internal/web_package:web_package.bzl", "web_package")
5+
load("@npm//@babel/cli:index.bzl", "babel")
6+
load("@npm_bazel_rollup//:index.bzl", "rollup_bundle")
7+
load("@npm_bazel_terser//:index.bzl", "terser_minified")
8+
9+
def differential_loading(name, entry_point, srcs):
10+
"Common workflow to serve native ESModules to modern browsers"
11+
12+
rollup_bundle(
13+
name = name + "_chunks",
14+
srcs = srcs,
15+
entry_points = {
16+
entry_point: "index",
17+
},
18+
output_dir = True,
19+
)
20+
21+
# For older browsers, we'll transform the output chunks to es5 + systemjs loader
22+
babel(
23+
name = name + "_chunks_es5",
24+
data = [
25+
name + "_chunks",
26+
"es5.babelrc",
27+
"@npm//@babel/preset-env",
28+
],
29+
output_dir = True,
30+
args = [
31+
"$(location %s_chunks)" % name,
32+
"--config-file",
33+
"$(location es5.babelrc)",
34+
"--out-dir",
35+
"$@",
36+
],
37+
)
38+
39+
# Run terser against both modern and legacy browser chunks
40+
terser_minified(
41+
name = name + "_chunks_es5.min",
42+
src = name + "_chunks_es5",
43+
)
44+
45+
terser_minified(
46+
name = name + "_chunks.min",
47+
src = name + "_chunks",
48+
)
49+
50+
web_package(
51+
name = name,
52+
assets = [
53+
"styles.css",
54+
],
55+
data = [
56+
"favicon.png",
57+
name + "_chunks.min",
58+
name + "_chunks_es5.min",
59+
],
60+
index_html = "index.html",
61+
)

examples/webapp/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
</head>
77
<!-- TODO: figure out how diff. loading interacts with
88
https://www.npmjs.com/package/rollup-plugin-bundle-html -->
9-
<script type="module" src="/chunks.min/chunks.js"></script>
10-
<script nomodule="" src="/chunks_es5.min/chunks.js"></script>
9+
<script type="module" src="/app_chunks.min/index.js"></script>
10+
<script nomodule="" src="/app_chunks_es5.min/index.js"></script>
1111
</html>

internal/node/npm_package_bin.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def npm_package_bin(tool = None, package = None, package_bin = None, **kwargs):
6767
may also include targets that produce or reference npm packages which are needed by the tool
6868
outs: similar to [genrule.outs](https://docs.bazel.build/versions/master/be/general.html#genrule.outs)
6969
output_dir: set to True if you want the output to be a directory
70-
Exactly one of `outs`, `out_dir` may be used.
70+
Exactly one of `outs`, `output_dir` may be used.
7171
If you output a directory, there can only be one output, which will be named the same as the target.
7272
7373
args: Command-line arguments to the tool.

0 commit comments

Comments
 (0)