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: support non-file transpiler srcs (#236)
- Loading branch information
Showing
7 changed files
with
154 additions
and
55 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
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,59 @@ | ||
"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") | ||
load("@aspect_rules_ts//ts/private:ts_lib.bzl", "lib") | ||
|
||
_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 = [] | ||
|
||
In real usage you would wrap a rule like | ||
https://github.com/aspect-build/rules_swc/blob/main/docs/swc.md | ||
""" | ||
for src in src_files: | ||
out_path = src.short_path | ||
|
||
for i, s in enumerate(srcs): | ||
copy_file( | ||
name = "_{}_{}_js".format(name, s), | ||
src = s, | ||
out = js_outs[i], | ||
js_file = ctx.actions.declare_file(lib.relative_to_package(out_path.replace(".ts", ".js"), ctx)) | ||
map_file = ctx.actions.declare_file(lib.relative_to_package(out_path.replace(".ts", ".js.map"), ctx)) | ||
|
||
out_files.append(js_file) | ||
out_files.append(map_file) | ||
|
||
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, | ||
), | ||
"js_outs": attr.output_list(), | ||
"map_outs": attr.output_list(), | ||
}, | ||
implementation = _mock_impl, | ||
) | ||
|
||
def mock(name, srcs, source_map = False, **kwargs): | ||
# 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, | ||
# 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 = lib.calculate_js_outs(srcs), | ||
map_outs = lib.calculate_map_outs(srcs) if source_map else [], | ||
**kwargs | ||
) |
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