This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
/
build_defs.bzl
400 lines (344 loc) · 15.4 KB
/
build_defs.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# 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.
"TypeScript compilation"
load("@build_bazel_rules_nodejs//internal/common:node_module_info.bzl", "NodeModuleInfo", "collect_node_modules_aspect")
# pylint: disable=unused-argument
# pylint: disable=missing-docstring
load(":common/compilation.bzl", "COMMON_ATTRIBUTES", "DEPS_ASPECTS", "compile_ts", "ts_providers_dict_to_struct")
load(":common/tsconfig.bzl", "create_tsconfig")
load(":ts_config.bzl", "TsConfigInfo")
_DEFAULT_COMPILER = "@build_bazel_rules_typescript//:@bazel/typescript/tsc_wrapped"
def _trim_package_node_modules(package_name):
# trim a package name down to its path prior to a node_modules
# segment. 'foo/node_modules/bar' would become 'foo' and
# 'node_modules/bar' would become ''
segments = []
for n in package_name.split("/"):
if n == "node_modules":
break
segments += [n]
return "/".join(segments)
def _compute_node_modules_root(ctx):
"""Computes the node_modules root from the node_modules and deps attributes.
Args:
ctx: the skylark execution context
Returns:
The node_modules root as a string
"""
node_modules_root = None
if ctx.files.node_modules:
# ctx.files.node_modules is not an empty list
node_modules_root = "/".join([f for f in [
ctx.attr.node_modules.label.workspace_root,
_trim_package_node_modules(ctx.attr.node_modules.label.package),
"node_modules",
] if f])
for d in ctx.attr.deps:
if NodeModuleInfo in d:
possible_root = "/".join(["external", d[NodeModuleInfo].workspace, "node_modules"])
if not node_modules_root:
node_modules_root = possible_root
elif node_modules_root != possible_root:
fail("All npm dependencies need to come from a single workspace. Found '%s' and '%s'." % (node_modules_root, possible_root))
if not node_modules_root:
# there are no fine grained deps and the node_modules attribute is an empty filegroup
# but we still need a node_modules_root even if its empty
node_modules_root = "/".join([f for f in [
ctx.attr.node_modules.label.workspace_root,
ctx.attr.node_modules.label.package,
"node_modules",
] if f])
return node_modules_root
def _filter_ts_inputs(all_inputs):
return [
f
for f in all_inputs
if f.path.endswith(".js") or f.path.endswith(".ts") or f.path.endswith(".json")
]
def _compile_action(ctx, inputs, outputs, tsconfig_file, node_opts, description = "prodmode"):
externs_files = []
action_inputs = []
action_outputs = []
for output in outputs:
if output.basename.endswith(".externs.js"):
externs_files.append(output)
elif output.basename.endswith(".es5.MF"):
ctx.actions.write(output, content = "")
else:
action_outputs.append(output)
# TODO(plf): For now we mock creation of files other than {name}.js.
for externs_file in externs_files:
ctx.actions.write(output = externs_file, content = "")
# A ts_library that has only .d.ts inputs will have no outputs,
# therefore there are no actions to execute
if not action_outputs:
return None
action_inputs.extend(_filter_ts_inputs(ctx.files.node_modules))
# Also include files from npm fine grained deps as action_inputs.
# These deps are identified by the NodeModuleInfo provider.
for d in ctx.attr.deps:
if NodeModuleInfo in d:
action_inputs.extend(_filter_ts_inputs(d.files))
if ctx.file.tsconfig:
action_inputs.append(ctx.file.tsconfig)
if TsConfigInfo in ctx.attr.tsconfig:
action_inputs.extend(ctx.attr.tsconfig[TsConfigInfo].deps)
# Pass actual options for the node binary in the special "--node_options" argument.
arguments = ["--node_options=%s" % opt for opt in node_opts]
# One at-sign makes this a params-file, enabling the worker strategy.
# Two at-signs escapes the argument so it's passed through to tsc_wrapped
# rather than the contents getting expanded.
if ctx.attr.supports_workers:
arguments.append("@@" + tsconfig_file.path)
mnemonic = "TypeScriptCompile"
else:
arguments.append("-p")
arguments.append(tsconfig_file.path)
mnemonic = "tsc"
ctx.actions.run(
progress_message = "Compiling TypeScript (%s) %s" % (description, ctx.label),
mnemonic = mnemonic,
inputs = depset(action_inputs, transitive = [inputs]),
outputs = action_outputs,
# Use the built-in shell environment
# Allow for users who set a custom shell that can locate standard binaries like tr and uname
# See https://github.com/NixOS/nixpkgs/issues/43955#issuecomment-407546331
use_default_shell_env = True,
arguments = arguments,
executable = ctx.executable.compiler,
execution_requirements = {
"supports-workers": str(int(ctx.attr.supports_workers)),
},
)
# Enable the replay_params in case an aspect needs to re-build this library.
return struct(
label = ctx.label,
tsconfig = tsconfig_file,
inputs = depset(action_inputs, transitive = [inputs]),
outputs = action_outputs,
compiler = ctx.executable.compiler,
)
def _devmode_compile_action(ctx, inputs, outputs, tsconfig_file, node_opts):
_compile_action(
ctx,
inputs,
outputs,
tsconfig_file,
node_opts,
description = "devmode",
)
def tsc_wrapped_tsconfig(
ctx,
files,
srcs,
devmode_manifest = None,
jsx_factory = None,
**kwargs):
"""Produce a tsconfig.json that sets options required under Bazel.
"""
# The location of tsconfig.json is interpreted as the root of the project
# when it is passed to the TS compiler with the `-p` option:
# https://www.typescriptlang.org/docs/handbook/tsconfig-json.html.
# Our tsconfig.json is in bazel-foo/bazel-out/local-fastbuild/bin/{package_path}
# because it's generated in the execution phase. However, our source files are in
# bazel-foo/ and therefore we need to strip some parent directories for each
# f.path.
node_modules_root = _compute_node_modules_root(ctx)
config = create_tsconfig(
ctx,
files,
srcs,
devmode_manifest = devmode_manifest,
node_modules_root = node_modules_root,
**kwargs
)
config["bazelOptions"]["nodeModulesPrefix"] = node_modules_root
# If the user gives a tsconfig attribute, the generated file should extend
# from the user's tsconfig.
# See https://github.com/Microsoft/TypeScript/issues/9876
# We subtract the ".json" from the end before handing to TypeScript because
# this gives extra error-checking.
if ctx.file.tsconfig:
workspace_path = config["compilerOptions"]["rootDir"]
config["extends"] = "/".join([workspace_path, ctx.file.tsconfig.path[:-len(".json")]])
if jsx_factory:
config["compilerOptions"]["jsxFactory"] = jsx_factory
tsetse_disabled_rules = []
# Matches section in javascript/typescript/tsconfig.bzl
# TODO(alexeagle): make them share code
if ctx.label.workspace_root.startswith("external/"):
# Violated by rxjs
tsetse_disabled_rules += ["ban-promise-as-condition"]
# For local testing
tsetse_disabled_rules += ["check-return-value"]
config["compilerOptions"]["plugins"] = [{
"name": "@bazel/tsetse",
"disabledRules": tsetse_disabled_rules,
}]
return config
# ************ #
# ts_library #
# ************ #
def _ts_library_impl(ctx):
"""Implementation of ts_library.
Args:
ctx: the context.
Returns:
the struct returned by the call to compile_ts.
"""
ts_providers = compile_ts(
ctx,
is_library = True,
# Filter out the node_modules from deps passed to TypeScript compiler
# since they don't have the required providers.
# They were added to the action inputs for tsc_wrapped already.
# strict_deps checking currently skips node_modules.
# TODO(alexeagle): turn on strict deps checking when we have a real
# provider for JS/DTS inputs to ts_library.
deps = [d for d in ctx.attr.deps if not NodeModuleInfo in d],
compile_action = _compile_action,
devmode_compile_action = _devmode_compile_action,
tsc_wrapped_tsconfig = tsc_wrapped_tsconfig,
)
return ts_providers_dict_to_struct(ts_providers)
local_deps_aspects = [collect_node_modules_aspect]
# Workaround skydoc bug which assumes DEPS_ASPECTS is a str type
[local_deps_aspects.append(a) for a in DEPS_ASPECTS]
ts_library = rule(
_ts_library_impl,
attrs = dict(COMMON_ATTRIBUTES, **{
"srcs": attr.label_list(
doc = "The TypeScript source files to compile.",
allow_files = [".ts", ".tsx"],
mandatory = True,
),
"compiler": attr.label(
doc = """Sets a different TypeScript compiler binary to use for this library.
For example, we use the vanilla TypeScript tsc.js for bootstrapping,
and Angular compilations can replace this with `ngc`.
The default ts_library compiler depends on the `@npm//@bazel/typescript`
target which is setup for projects that use bazel managed npm deps that
fetch the @bazel/typescript npm package. It is recommended that you use
the workspace name `@npm` for bazel managed deps so the default
compiler works out of the box. Otherwise, you'll have to override
the compiler attribute manually.
""",
default = Label(_DEFAULT_COMPILER),
allow_files = True,
executable = True,
cfg = "host",
),
"internal_testing_type_check_dependencies": attr.bool(default = False, doc = "Testing only, whether to type check inputs that aren't srcs."),
"node_modules": attr.label(
doc = """The npm packages which should be available during the compile.
The default value is `@npm//typescript:typescript__typings` is setup
for projects that use bazel managed npm deps that. It is recommended
that you use the workspace name `@npm` for bazel managed deps so the
default node_modules works out of the box. Otherwise, you'll have to
override the node_modules attribute manually. This default is in place
since ts_library will always depend on at least the typescript
default libs which are provided by `@npm//typescript:typescript__typings`.
This attribute is DEPRECATED. As of version 0.18.0 the recommended
approach to npm dependencies is to use fine grained npm dependencies
which are setup with the `yarn_install` or `npm_install` rules.
For example, in targets that used a `//:node_modules` filegroup,
```
ts_library(
name = "my_lib",
...
node_modules = "//:node_modules",
)
```
which specifies all files within the `//:node_modules` filegroup
to be inputs to the `my_lib`. Using fine grained npm dependencies,
`my_lib` is defined with only the npm dependencies that are
needed:
```
ts_library(
name = "my_lib",
...
deps = [
"@npm//@types/foo",
"@npm//@types/bar",
"@npm//foo",
"@npm//bar",
...
],
)
```
In this case, only the listed npm packages and their
transitive deps are includes as inputs to the `my_lib` target
which reduces the time required to setup the runfiles for this
target (see https://github.com/bazelbuild/bazel/issues/5153).
The default typescript libs are also available via the node_modules
default in this case.
The @npm external repository and the fine grained npm package
targets are setup using the `yarn_install` or `npm_install` rule
in your WORKSPACE file:
yarn_install(
name = "npm",
package_json = "//:package.json",
yarn_lock = "//:yarn.lock",
)
""",
default = Label("@npm//typescript:typescript__typings"),
),
"supports_workers": attr.bool(
doc = """Intended for internal use only.
Allows you to disable the Bazel Worker strategy for this library.
Typically used together with the "compiler" setting when using a
non-worker aware compiler binary.""",
default = True,
),
# TODO(alexeagle): reconcile with google3: ts_library rules should
# be portable across internal/external, so we need this attribute
# internally as well.
"tsconfig": attr.label(
doc = """A tsconfig.json file containing settings for TypeScript compilation.
Note that some properties in the tsconfig are governed by Bazel and will be
overridden, such as `target` and `module`.
The default value is set to `//:tsconfig.json` by a macro. This means you must
either:
- Have your `tsconfig.json` file in the workspace root directory
- Use an alias in the root BUILD.bazel file to point to the location of tsconfig:
`alias(name="tsconfig.json", actual="//path/to:tsconfig-something.json")`
- Give an explicit `tsconfig` attribute to all `ts_library` targets
""",
allow_single_file = True,
),
"tsickle_typed": attr.bool(default = True),
"deps": attr.label_list(aspects = local_deps_aspects),
}),
outputs = {
"tsconfig": "%{name}_tsconfig.json",
},
)
"""
`ts_library` type-checks and compiles a set of TypeScript sources to JavaScript.
It produces declarations files (`.d.ts`) which are used for compiling downstream
TypeScript targets and JavaScript for the browser and Closure compiler.
"""
def ts_library_macro(tsconfig = None, **kwargs):
"""Wraps `ts_library` to set the default for the `tsconfig` attribute.
This must be a macro so that the string is converted to a label in the context of the
workspace that declares the `ts_library` target, rather than the workspace that defines
`ts_library`, or the workspace where the build is taking place.
This macro is re-exported as `ts_library` in the public API.
Args:
tsconfig: the label pointing to a tsconfig.json file
**kwargs: remaining args to pass to the ts_library rule
"""
if not tsconfig:
tsconfig = "//:tsconfig.json"
ts_library(tsconfig = tsconfig, **kwargs)