generated from bazel-contrib/rules-template
-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: pass all srcs to ts_project transpilers
Fixes #219
- Loading branch information
Showing
5 changed files
with
106 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,68 @@ | ||
"Fixture to demonstrate a custom transpiler for ts_project" | ||
|
||
load("@bazel_skylib//rules:copy_file.bzl", "copy_file") | ||
load("@bazel_skylib//rules:write_file.bzl", "write_file") | ||
|
||
_DUMMY_SOURCEMAP = """{"version":3,"sources":["%s"],"mappings":"AAAO,KAAK,CAAC","file":"in.js","sourcesContent":["fake"]}""" | ||
|
||
def mock(name, srcs, js_outs, map_outs, **kwargs): | ||
"""Mock transpiler macro. | ||
def _mock_impl(ctx): | ||
src_files = [src for src in ctx.files.srcs if src.short_path.endswith(".ts") and not src.short_path.endswith(".d.ts")] | ||
out_files = [] | ||
|
||
for src in src_files: | ||
out_path = src.short_path[len(ctx.attr.pkg_dir) + 1:] if src.short_path.startswith(ctx.attr.pkg_dir) else src.short_path | ||
|
||
js_file = ctx.actions.declare_file(out_path.replace(".ts", ".js")) | ||
map_file = ctx.actions.declare_file(out_path.replace(".ts", ".js.map")) | ||
|
||
In real usage you would wrap a rule like | ||
https://github.com/aspect-build/rules_swc/blob/main/docs/swc.md | ||
""" | ||
out_files.append(js_file) | ||
out_files.append(map_file) | ||
|
||
for i, s in enumerate(srcs): | ||
copy_file( | ||
name = "_{}_{}_js".format(name, s), | ||
src = s, | ||
out = js_outs[i], | ||
ctx.actions.run_shell( | ||
inputs = [src], | ||
outputs = [js_file], | ||
command = "cp $@", | ||
arguments = [src.path, js_file.path.replace(".ts", ".js")], | ||
) | ||
|
||
write_file( | ||
name = "_{}_{}_map".format(name, s), | ||
out = map_outs[i], | ||
content = [_DUMMY_SOURCEMAP % s], | ||
ctx.actions.write( | ||
output = map_file, | ||
content = _DUMMY_SOURCEMAP % src.short_path, | ||
) | ||
|
||
return DefaultInfo(files = depset(out_files)) | ||
|
||
mock_impl = rule( | ||
attrs = { | ||
"srcs": attr.label_list( | ||
doc = "TypeScript source files", | ||
allow_files = True, | ||
mandatory = True, | ||
), | ||
|
||
# Used only to predeclare outputs | ||
"outs": attr.output_list(), | ||
|
||
# TODO: why is this needed? | ||
"pkg_dir": attr.string(), | ||
}, | ||
implementation = _mock_impl, | ||
) | ||
|
||
def mock(name, srcs, source_map = False, **kwargs): | ||
# Calculate pre-declared outputs so they can be referenced as targets. | ||
# This is an optional transpiler feature aligning with the default tsc transpiler. | ||
js_outs = [ | ||
src.replace(".ts", ".js") | ||
for src in srcs | ||
if (src.endswith(".ts") and not src.endswith(".d.ts")) | ||
] | ||
map_outs = [js.replace(".js", ".js.map") for js in js_outs] if source_map else [] | ||
|
||
# Run the rule producing those pre-declared outputs as well as any other outputs | ||
# which can not be determined ahead of time such as within directories, goruped | ||
# within a filegroup() etc. | ||
mock_impl( | ||
name = name, | ||
srcs = srcs, | ||
outs = js_outs + map_outs, | ||
pkg_dir = native.package_name(), | ||
**kwargs | ||
) |