-
Notifications
You must be signed in to change notification settings - Fork 363
/
Copy pathjs_file.bzl
103 lines (85 loc) · 2.8 KB
/
js_file.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
"""
Give a collection of js files a JsInfo provider so it can be used as a dependency for aspect_rules.
"""
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "COPY_FILE_TO_BIN_TOOLCHAINS", "copy_file_to_bin_action")
load("@aspect_rules_js//js:providers.bzl", "JsInfo", "js_info")
_ATTRS = {
"srcs": attr.label_list(
allow_files = True,
),
"deps": attr.label_list(),
}
def _gather_sources_and_types(ctx, targets, files):
"""Gathers sources and types from a list of targets
Args:
ctx: the rule context
targets: List of targets to gather sources and types from their JsInfo providers.
These typically come from the `srcs` and/or `data` attributes of a rule
files: List of files to gather as sources and types.
These typically come from the `srcs` and/or `data` attributes of a rule
Returns:
Sources & declaration files depsets in the sequence (sources, types)
"""
sources = []
types = []
for file in files:
if file.is_source:
file = copy_file_to_bin_action(ctx, file)
if file.is_directory:
# assume a directory contains types since we can't know that it doesn't
types.append(file)
sources.append(file)
elif (
file.path.endswith((".d.ts", ".d.ts.map", ".d.mts", ".d.mts.map", ".d.cts", ".d.cts.map"))
):
types.append(file)
elif file.path.endswith(".json"):
# Any .json can produce types: https://www.typescriptlang.org/tsconfig/#resolveJsonModule
# package.json may be required to resolve types with the "typings" key
types.append(file)
sources.append(file)
else:
sources.append(file)
# sources as depset
sources = depset(sources, transitive = [
target[JsInfo].sources
for target in targets
if JsInfo in target
])
# types as depset
types = depset(types, transitive = [
target[JsInfo].types
for target in targets
if JsInfo in target
])
return (sources, types)
def _js_file_impl(ctx):
sources, types = _gather_sources_and_types(
ctx = ctx,
targets = ctx.attr.srcs,
files = ctx.files.srcs,
)
return [
js_info(
target = ctx.label,
sources = sources,
types = types,
),
DefaultInfo(
files = sources,
),
OutputGroupInfo(
types = types,
),
]
js_file_lib = struct(
attrs = _ATTRS,
implementation = _js_file_impl,
provides = [DefaultInfo, JsInfo, OutputGroupInfo],
)
js_file = rule(
implementation = js_file_lib.implementation,
attrs = js_file_lib.attrs,
provides = js_file_lib.provides,
toolchains = COPY_FILE_TO_BIN_TOOLCHAINS,
)