Skip to content

Commit

Permalink
feat: support custom dts transpiler
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Sep 20, 2024
1 parent a8dd4c0 commit 89f0e93
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
7 changes: 4 additions & 3 deletions docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 53 additions & 11 deletions ts/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def ts_project(
no_emit = False,
emit_declaration_only = False,
transpiler = None,
declaration_transpiler = None,
ts_build_info_file = None,
tsc = _tsc,
tsc_worker = _tsc_worker,
Expand Down Expand Up @@ -172,6 +173,12 @@ def ts_project(
See [docs/transpiler.md](/docs/transpiler.md) for more details.
declaration_transpiler: A custom transpiler tool to run that produces the TypeScript declaration outputs instead of `tsc`.
This is only used when `transpiler` is set to a custom transpiler tool.
See [docs/transpiler.md](/docs/transpiler.md) for more details.
validate: Whether to check that the dependencies are valid and the tsconfig JSON settings match the attributes on this target.
Set this to `False` to skip running our validator, in case you have a legitimate reason for these to differ,
e.g. you have a setting enabled just for the editor but you want different behavior when Bazel runs `tsc`.
Expand Down Expand Up @@ -318,19 +325,43 @@ def ts_project(
typings_out_dir = declaration_dir if declaration_dir else out_dir
tsbuildinfo_path = ts_build_info_file if ts_build_info_file else name + ".tsbuildinfo"

tsc_typings_outs = _lib.calculate_typings_outs(srcs, typings_out_dir, root_dir, declaration, composite, allow_js, no_emit)
tsc_typing_maps_outs = _lib.calculate_typing_maps_outs(srcs, typings_out_dir, root_dir, declaration_map, allow_js, no_emit)
# Target names for tsc, dts+js transpilers
tsc_target_name = name
declarations_target_name = None
transpile_target_name = None

# typing outputs
tsc_typings_outs = []
tsc_typing_maps_outs = []
if not declaration_transpiler or declaration_transpiler == "tsc":
tsc_typings_outs = _lib.calculate_typings_outs(srcs, typings_out_dir, root_dir, declaration, composite, allow_js, no_emit)
tsc_typing_maps_outs = _lib.calculate_typing_maps_outs(srcs, typings_out_dir, root_dir, declaration_map, allow_js, no_emit)
else:
declarations_target_name = "%s_declarations" % name

if type(declaration_transpiler) == "function" or type(declaration_transpiler) == "rule":
declaration_transpiler(
name = declarations_target_name,
srcs = srcs,
**common_kwargs
)
elif partial.is_instance(declaration_transpiler):
partial.call(
declaration_transpiler,
name = declarations_target_name,
srcs = srcs,
**common_kwargs
)
else:
fail("declaration_transpiler attribute should be a rule/macro or a skylib partial. Got " + type(declaration_transpiler))

# js outputs
tsc_js_outs = []
tsc_map_outs = []
if no_emit or not transpiler or transpiler == "tsc":
tsc_js_outs = _lib.calculate_js_outs(srcs, out_dir, root_dir, allow_js, resolve_json_module, preserve_jsx, no_emit, emit_declaration_only)
tsc_map_outs = _lib.calculate_map_outs(srcs, out_dir, root_dir, source_map, preserve_jsx, no_emit, emit_declaration_only)
tsc_target_name = name
else:
# To stitch together a tree of ts_project where transpiler is a separate rule,
# we have to produce a few targets
tsc_target_name = "%s_typings" % name
transpile_target_name = "%s_transpile" % name

if type(transpiler) == "function" or type(transpiler) == "rule":
Expand All @@ -349,19 +380,30 @@ def ts_project(
else:
fail("transpiler attribute should be a rule/macro or a skylib partial. Got " + type(transpiler))

# Default target produced by the macro gives the js and map outs, with the transitive dependencies.
# Default target produced by the macro gives the js and map outs, with the transitive dependencies.
if transpile_target_name or declarations_target_name:
tsc_target_name = "%s_tsc" % 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.
lib_srcs = [tsc_target_name] + assets

# Include the transpiler targets for both js+dts if they exist.
if transpile_target_name:
lib_srcs.append(transpile_target_name)
if declarations_target_name:
lib_srcs.append(declarations_target_name)

js_library(
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 = [transpile_target_name, tsc_target_name] + assets,
srcs = lib_srcs,
deps = deps,
data = data,
**common_kwargs
)

# If the primary target does not output dts files then type-checking has a separate target.
if no_emit or (transpiler and transpiler != "tsc"):
if no_emit or transpile_target_name or declarations_target_name:
types_target_name = "%s_types" % name
typecheck_target_name = "%s_typecheck" % name
test_target_name = "%s_typecheck_test" % name
Expand Down

0 comments on commit 89f0e93

Please sign in to comment.