-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathtfjs_bundle.bzl
221 lines (204 loc) · 7.15 KB
/
tfjs_bundle.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Copyright 2021 Google LLC. 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("@bazel_skylib//lib:new_sets.bzl", "sets")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@npm//@bazel/rollup:index.bzl", "rollup_bundle")
def _make_rollup_config_impl(ctx):
output_file = ctx.actions.declare_file(ctx.label.name + ".mjs")
# TODO(mattsoulanille): This stats file is not declared as an output
# of any rule. It should be declared as an output of the rollup_bundle
# rule, but there doesn't seem to be an easy way to do that.
stats_file_path = paths.join(
ctx.bin_dir.path,
paths.dirname(ctx.build_file_path),
ctx.attr.stats,
)
external = sets.to_list(sets.make(
ctx.attr.external + list(ctx.attr.globals.keys()),
))
ctx.actions.expand_template(
template = ctx.file.template,
output = ctx.outputs.config_file,
substitutions = {
"TEMPLATE_es5": "true" if ctx.attr.es5 else "false",
"TEMPLATE_external": str(external),
"TEMPLATE_globals": str(ctx.attr.globals),
"TEMPLATE_leave_as_require": str(ctx.attr.leave_as_require),
"TEMPLATE_minify": "true" if ctx.attr.minify else "false",
"TEMPLATE_stats": stats_file_path,
},
)
return [DefaultInfo(files = depset([output_file]))]
_make_rollup_config = rule(
implementation = _make_rollup_config_impl,
attrs = {
"es5": attr.bool(
default = False,
doc = "Whether to transpile to es5",
mandatory = False,
),
"external": attr.string_list(
default = [],
doc = """A list of module IDs to exclude.
Keys from 'globals' are automatically added to this attribute, but additional external modules can be specified.
""",
mandatory = False,
),
"globals": attr.string_dict(
default = {},
doc = "A dict from module IDs to global variables" +
" used to resolve external modules",
mandatory = False,
),
"leave_as_require": attr.string_list(
default = [],
doc = """A list of modules to leave as 'require()' statements.
We use the commonjs rollup plugin to load commonjs modules in rollup. This plugin converts 'require()' calls to 'import' statements, but sometimes, they should be left as 'require()'.
""",
),
"minify": attr.bool(
default = False,
doc = "Whether to minify with terser",
mandatory = False,
),
"stats": attr.string(
mandatory = True,
doc = "The name of the stats file",
),
"template": attr.label(
default = Label("@//tools:rollup_template.config.mjs"),
allow_single_file = True,
doc = "The karma config template to expand",
),
},
outputs = {"config_file": "%{name}.mjs"},
)
def tfjs_rollup_bundle(
name,
deps,
entry_point,
format,
minify = False,
umd_name = None,
es5 = False,
external = [],
globals = {},
leave_as_require = [],
**kwargs):
config_file = name + "_config"
_make_rollup_config(
name = config_file,
stats = name + "_stats.html",
es5 = es5,
minify = minify,
external = external,
globals = globals,
leave_as_require = leave_as_require,
)
rollup_deps = deps + [
"@npm//@rollup/plugin-commonjs",
"@npm//@rollup/plugin-node-resolve",
"@npm//rollup-plugin-sourcemaps",
"@npm//rollup-plugin-terser",
"@npm//rollup-plugin-visualizer",
"@npm//typescript",
"@npm//tslib", # downlevel_to_es5_plugin uses importHelpers.
"@//tools:downlevel_to_es5_plugin",
"@//tools:make_rollup_config",
]
rollup_args = []
if umd_name:
rollup_args += ["--output.name={}".format(umd_name)]
rollup_bundle(
name = name,
args = rollup_args,
format = format,
sourcemap = "true",
entry_point = entry_point,
config_file = config_file,
deps = rollup_deps,
**kwargs
)
def tfjs_bundle(
name,
entry_point,
umd_name,
deps = [],
external = [],
testonly = False,
globals = {},
leave_as_require = []):
# A note on minification: While it would be more efficient to create
# unminified bundles and then run them through terser separately, that
# would prevent us from creating bundle visualizations for minified bundles
# with rollup-plugin-visualizer. If / when there is a standalone visualizer
# available, it may make sense to switch to it, since that would make this
# build more efficient and would simplify some of the rules used.
for minify in [True, False]:
dot_min = ".min" if minify else ""
# UMD ES2017
tfjs_rollup_bundle(
name = name + ".es2017" + dot_min,
testonly = testonly,
deps = deps,
entry_point = entry_point,
umd_name = umd_name,
format = "umd",
minify = minify,
external = external,
globals = globals,
leave_as_require = leave_as_require,
)
# UMD es5
tfjs_rollup_bundle(
name = name + dot_min,
testonly = testonly,
deps = deps,
entry_point = entry_point,
umd_name = umd_name,
format = "umd",
minify = minify,
es5 = True,
external = external,
globals = globals,
leave_as_require = leave_as_require,
)
# FESM ES2017
# TODO(mattsoulanille): Check that this is actually
# generating flat esm modules.
tfjs_rollup_bundle(
name = name + ".fesm" + dot_min,
testonly = testonly,
deps = deps,
entry_point = entry_point,
format = "esm",
minify = minify,
external = external,
globals = globals,
leave_as_require = leave_as_require,
)
# cjs es5 node bundle
tfjs_rollup_bundle(
name = name + ".node" + dot_min,
testonly = testonly,
deps = deps,
entry_point = entry_point,
format = "cjs",
minify = minify,
es5 = True,
external = external,
globals = globals,
leave_as_require = leave_as_require,
)