Skip to content

Commit

Permalink
fix: support non-file transipler srcs
Browse files Browse the repository at this point in the history
Fixes #219
  • Loading branch information
jbedard committed Nov 8, 2022
1 parent b7b01c2 commit 17a80d5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 39 deletions.
11 changes: 7 additions & 4 deletions examples/transpiler/babel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ def babel(name, srcs, js_outs, map_outs, **kwargs):
# - make sure the input directory only contains files listed in srcs
# - make sure the js_outs are actually created in the expected path
for idx, src in enumerate(srcs):
if not js_outs[src]:
fail("babel transpiler only supports file inputs, received: %s" % src)

# see https://babeljs.io/docs/en/babel-cli
args = [
"{}/$(location {})".format(execroot, src),
"--presets=@babel/preset-typescript",
"--out-file",
"{}/$(location {})".format(execroot, js_outs[idx]),
"{}/$(location {})".format(execroot, js_outs[src]),
]
outs = [js_outs[idx]]
outs = [js_outs[src]]

if len(map_outs) > 0:
if src in map_outs:
args.append("--source-maps")
outs.append(map_outs[idx])
outs.append(map_outs[src])

bin.babel(
name = "{}_{}".format(name, idx),
Expand Down
21 changes: 7 additions & 14 deletions ts/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ def ts_project(
tsc_js_outs = []
tsc_map_outs = []
if not transpiler:
tsc_js_outs = js_outs
tsc_map_outs = map_outs
tsc_js_outs = js_outs.values()
tsc_map_outs = map_outs.values()
tsc_target_name = name
else:
# To stitch together a tree of ts_project where transpiler is a separate rule,
Expand All @@ -323,17 +323,10 @@ def ts_project(
typecheck_target_name = "%s_typecheck" % name
test_target_name = "%s_typecheck_test" % name

transpile_srcs = [s for s in srcs if _lib.is_ts_src(s, allow_js)]
if (len(transpile_srcs) != len(js_outs)):
fail("ERROR: illegal state: transpile_srcs has length {} but js_outs has length {}".format(
len(transpile_srcs),
len(js_outs),
))

if type(transpiler) == "function" or type(transpiler) == "rule":
transpiler(
name = transpile_target_name,
srcs = transpile_srcs,
srcs = srcs,
js_outs = js_outs,
map_outs = map_outs,
**common_kwargs
Expand All @@ -342,7 +335,7 @@ def ts_project(
partial.call(
transpiler,
name = transpile_target_name,
srcs = transpile_srcs,
srcs = srcs,
js_outs = js_outs,
map_outs = map_outs,
**common_kwargs
Expand Down Expand Up @@ -371,7 +364,7 @@ def ts_project(
name = name,
# Include the tsc target in srcs to pick-up both the direct & transitive declaration outputs so
# that this js_library can be a valid dep for downstream ts_project or other rules_js derivative rules.
srcs = js_outs + map_outs + [tsc_target_name],
srcs = js_outs.values() + map_outs.values() + [tsc_target_name],
deps = deps,
**common_kwargs
)
Expand All @@ -396,8 +389,8 @@ def ts_project(
root_dir = root_dir,
js_outs = tsc_js_outs,
map_outs = tsc_map_outs,
typings_outs = typings_outs,
typing_maps_outs = typing_maps_outs,
typings_outs = typings_outs.values(),
typing_maps_outs = typing_maps_outs.values(),
buildinfo_out = tsbuildinfo_path if composite or incremental else None,
emit_declaration_only = emit_declaration_only,
tsc = tsc,
Expand Down
13 changes: 6 additions & 7 deletions ts/private/ts_lib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,20 @@ def _replace_ext(f, ext_map):

def _out_paths(srcs, out_dir, root_dir, allow_js, ext_map):
rootdir_replace_pattern = root_dir + "/" if root_dir else ""
outs = []
outs = {}
for f in srcs:
if _is_ts_src(f, allow_js):
out = _join(out_dir, f[:f.rindex(".")].replace(rootdir_replace_pattern, "", 1) + _replace_ext(f, ext_map))

# Don't declare outputs that collide with inputs
# for example, a.js -> a.js
if out != f:
outs.append(out)
outs.setdefault(f, out)
return outs

def _calculate_js_outs(srcs, out_dir, root_dir, allow_js, preserve_jsx, emit_declaration_only):
if emit_declaration_only:
return []
return {}

exts = {
"*": ".js",
Expand All @@ -207,7 +207,7 @@ def _calculate_js_outs(srcs, out_dir, root_dir, allow_js, preserve_jsx, emit_dec

def _calculate_map_outs(srcs, out_dir, root_dir, source_map, preserve_jsx, emit_declaration_only):
if not source_map or emit_declaration_only:
return []
return {}

exts = {
"*": ".js.map",
Expand All @@ -223,7 +223,7 @@ def _calculate_map_outs(srcs, out_dir, root_dir, source_map, preserve_jsx, emit_

def _calculate_typings_outs(srcs, typings_out_dir, root_dir, declaration, composite, allow_js, include_srcs = True):
if not (declaration or composite):
return []
return {}

exts = {
"*": ".d.ts",
Expand All @@ -237,7 +237,7 @@ def _calculate_typings_outs(srcs, typings_out_dir, root_dir, declaration, compos

def _calculate_typing_maps_outs(srcs, typings_out_dir, root_dir, declaration_map, allow_js):
if not declaration_map:
return []
return {}

exts = {
"*": ".d.ts.map",
Expand Down Expand Up @@ -269,7 +269,6 @@ lib = struct(
is_typings_src = _is_typings_src,
is_ts_src = _is_ts_src,
is_json_src = _is_json_src,
out_paths = _out_paths,
calculate_js_outs = _calculate_js_outs,
calculate_map_outs = _calculate_map_outs,
calculate_typings_outs = _calculate_typings_outs,
Expand Down
8 changes: 4 additions & 4 deletions ts/private/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ def _ts_project_impl(ctx):
# However, it is not possible to evaluate files in outputs of other rules such as filegroup, therefore the outs are
# recalculated here.
typings_out_dir = ctx.attr.declaration_dir or ctx.attr.out_dir
js_outs = _lib.declare_outputs(ctx, [] if not ctx.attr.transpile else _lib.calculate_js_outs(srcs, ctx.attr.out_dir, ctx.attr.root_dir, ctx.attr.allow_js, ctx.attr.preserve_jsx, ctx.attr.emit_declaration_only))
map_outs = _lib.declare_outputs(ctx, [] if not ctx.attr.transpile else _lib.calculate_map_outs(srcs, ctx.attr.out_dir, ctx.attr.root_dir, ctx.attr.source_map, ctx.attr.preserve_jsx, ctx.attr.emit_declaration_only))
typings_outs = _lib.declare_outputs(ctx, _lib.calculate_typings_outs(srcs, typings_out_dir, ctx.attr.root_dir, ctx.attr.declaration, ctx.attr.composite, ctx.attr.allow_js))
typing_maps_outs = _lib.declare_outputs(ctx, _lib.calculate_typing_maps_outs(srcs, typings_out_dir, ctx.attr.root_dir, ctx.attr.declaration_map, ctx.attr.allow_js))
js_outs = _lib.declare_outputs(ctx, [] if not ctx.attr.transpile else _lib.calculate_js_outs(srcs, ctx.attr.out_dir, ctx.attr.root_dir, ctx.attr.allow_js, ctx.attr.preserve_jsx, ctx.attr.emit_declaration_only).values())
map_outs = _lib.declare_outputs(ctx, [] if not ctx.attr.transpile else _lib.calculate_map_outs(srcs, ctx.attr.out_dir, ctx.attr.root_dir, ctx.attr.source_map, ctx.attr.preserve_jsx, ctx.attr.emit_declaration_only).values())
typings_outs = _lib.declare_outputs(ctx, _lib.calculate_typings_outs(srcs, typings_out_dir, ctx.attr.root_dir, ctx.attr.declaration, ctx.attr.composite, ctx.attr.allow_js).values())
typing_maps_outs = _lib.declare_outputs(ctx, _lib.calculate_typing_maps_outs(srcs, typings_out_dir, ctx.attr.root_dir, ctx.attr.declaration_map, ctx.attr.allow_js).values())

arguments = ctx.actions.args()
execution_requirements = {}
Expand Down
22 changes: 12 additions & 10 deletions ts/test/mock_transpiler.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ def mock(name, srcs, js_outs, map_outs, **kwargs):
"""

for i, s in enumerate(srcs):
copy_file(
name = "_{}_{}_js".format(name, s),
src = s,
out = js_outs[i],
)
if s in js_outs:
copy_file(
name = "_{}_{}_js".format(name, s),
src = s,
out = js_outs[s],
)

write_file(
name = "_{}_{}_map".format(name, s),
out = map_outs[i],
content = [_DUMMY_SOURCEMAP % s],
)
if s in map_outs:
write_file(
name = "_{}_{}_map".format(name, s),
out = map_outs[s],
content = [_DUMMY_SOURCEMAP % s],
)

0 comments on commit 17a80d5

Please sign in to comment.