Skip to content

Commit

Permalink
refactor: consider json a js source (if resolve_json_module)
Browse files Browse the repository at this point in the history
This allows us to use the _out_paths machinery for json sources as
well.

To achieve this, we add a resolve_json_module parameter to relevant
methods in ts_lib (ts_lib is private so this is a backwards compatible
change).

There are two small side-effects to this refactor:
- We use _lib.relative_to_package rather than a prefix removal.
- json inputs that are not sources and do not have an input / output
  collision are now declared.
  • Loading branch information
gzm0 authored and alexeagle committed Apr 28, 2023
1 parent 583132f commit 12012a4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 36 deletions.
3 changes: 2 additions & 1 deletion ts/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def ts_project(
tsc_js_outs = []
tsc_map_outs = []
if not transpiler:
tsc_js_outs = _lib.calculate_js_outs(srcs, out_dir, root_dir, allow_js, preserve_jsx, emit_declaration_only)
tsc_js_outs = _lib.calculate_js_outs(srcs, out_dir, root_dir, allow_js, resolve_json_module, preserve_jsx, emit_declaration_only)
tsc_map_outs = _lib.calculate_map_outs(srcs, out_dir, root_dir, source_map, preserve_jsx, emit_declaration_only)
tsc_target_name = name
else:
Expand Down Expand Up @@ -412,6 +412,7 @@ def ts_project(
deps = tsc_deps,
tsconfig = tsconfig,
allow_js = allow_js,
resolve_json_module = resolve_json_module,
extends = extends,
incremental = incremental,
preserve_jsx = preserve_jsx,
Expand Down
2 changes: 1 addition & 1 deletion ts/private/ts_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def _filter_input_files(files, allow_js, resolve_json_module):
f
for f in files
# include typescript, json & declaration sources
if _lib.is_ts_src(f.basename, allow_js) or _lib.is_json_src(f.basename, resolve_json_module) or _lib.is_typings_src(f.basename)
if _lib.is_ts_src(f.basename, allow_js, resolve_json_module) or _lib.is_typings_src(f.basename)
]

def _write_tsconfig_rule(ctx):
Expand Down
34 changes: 19 additions & 15 deletions ts/private/ts_lib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,20 @@ def _relative_to_package(path, ctx):
def _is_typings_src(src):
return src.endswith(".d.ts") or src.endswith(".d.mts") or src.endswith(".d.cts")

def _is_ts_src(src, allow_js):
if (src.endswith(".ts") or src.endswith(".tsx") or src.endswith(".mts") or src.endswith(".cts")):
return not _is_typings_src(src)
def _is_js_src(src, allow_js, resolve_json_module):
if src.endswith(".js") or src.endswith(".jsx") or src.endswith(".mjs") or src.endswith(".cjs"):
return allow_js

if allow_js:
return (src.endswith(".js") or src.endswith(".jsx") or src.endswith(".mjs") or src.endswith(".cjs"))
if src.endswith(".json"):
return resolve_json_module

return False

def _is_json_src(src, resolve_json_module):
return resolve_json_module and src.endswith(".json")
def _is_ts_src(src, allow_js, resolve_json_module):
if (src.endswith(".ts") or src.endswith(".tsx") or src.endswith(".mts") or src.endswith(".cts")):
return not _is_typings_src(src)

return _is_js_src(src, allow_js, resolve_json_module)

def _replace_ext(f, ext_map):
cur_ext = f[f.rindex("."):]
Expand All @@ -194,11 +197,11 @@ def _replace_ext(f, ext_map):
return new_ext
return None

def _out_paths(srcs, out_dir, root_dir, allow_js, ext_map):
def _out_paths(srcs, out_dir, root_dir, allow_js, resolve_json_module, ext_map):
rootdir_replace_pattern = root_dir + "/" if root_dir else ""
outs = []
for f in srcs:
if _is_ts_src(f, allow_js):
if _is_ts_src(f, allow_js, resolve_json_module):
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
Expand All @@ -207,7 +210,7 @@ def _out_paths(srcs, out_dir, root_dir, allow_js, ext_map):
outs.append(out)
return outs

def _calculate_js_outs(srcs, out_dir = ".", root_dir = ".", allow_js = False, preserve_jsx = False, emit_declaration_only = False):
def _calculate_js_outs(srcs, out_dir = ".", root_dir = ".", allow_js = False, resolve_json_module = False, preserve_jsx = False, emit_declaration_only = False):
if emit_declaration_only:
return []

Expand All @@ -217,13 +220,14 @@ def _calculate_js_outs(srcs, out_dir = ".", root_dir = ".", allow_js = False, pr
".mjs": ".mjs",
".cjs": ".cjs",
".cts": ".cjs",
".json": ".json",
}

if preserve_jsx:
exts[".jsx"] = ".jsx"
exts[".tsx"] = ".jsx"

return _out_paths(srcs, out_dir, root_dir, allow_js, exts)
return _out_paths(srcs, out_dir, root_dir, allow_js, resolve_json_module, exts)

def _calculate_map_outs(srcs, out_dir = ".", root_dir = ".", source_map = True, preserve_jsx = False, emit_declaration_only = False):
if not source_map or emit_declaration_only:
Expand All @@ -239,7 +243,7 @@ def _calculate_map_outs(srcs, out_dir = ".", root_dir = ".", source_map = True,
if preserve_jsx:
exts[".tsx"] = ".jsx.map"

return _out_paths(srcs, out_dir, root_dir, False, exts)
return _out_paths(srcs, out_dir, root_dir, False, False, exts)

def _calculate_typings_outs(srcs, typings_out_dir, root_dir, declaration, composite, allow_js, include_srcs = True):
if not (declaration or composite):
Expand All @@ -253,7 +257,7 @@ def _calculate_typings_outs(srcs, typings_out_dir, root_dir, declaration, compos
".cjs": ".d.cts",
}

return _out_paths(srcs, typings_out_dir, root_dir, allow_js, exts)
return _out_paths(srcs, typings_out_dir, root_dir, allow_js, False, exts)

def _calculate_typing_maps_outs(srcs, typings_out_dir, root_dir, declaration_map, allow_js):
if not declaration_map:
Expand All @@ -267,7 +271,7 @@ def _calculate_typing_maps_outs(srcs, typings_out_dir, root_dir, declaration_map
".cjs": ".d.cts.map",
}

return _out_paths(srcs, typings_out_dir, root_dir, allow_js, exts)
return _out_paths(srcs, typings_out_dir, root_dir, allow_js, False, exts)

def _calculate_root_dir(ctx):
return _join(
Expand All @@ -288,7 +292,7 @@ lib = struct(
relative_to_package = _relative_to_package,
is_typings_src = _is_typings_src,
is_ts_src = _is_ts_src,
is_json_src = _is_json_src,
is_js_src = _is_js_src,
out_paths = _out_paths,
calculate_js_outs = _calculate_js_outs,
calculate_map_outs = _calculate_map_outs,
Expand Down
22 changes: 3 additions & 19 deletions ts/private/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ 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))
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.resolve_json_module, 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))
Expand Down Expand Up @@ -145,30 +145,14 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details.
tsconfig_inputs = copy_files_to_bin_actions(ctx, _validate_lib.tsconfig_inputs(ctx).to_list())
inputs.extend(tsconfig_inputs)

# We do not try to predeclare json_outs, because their output locations generally conflict with their path in the source tree.
# (The exception is when out_dir is used, then the .json output is a different path than the input.)
# However tsc will copy .json srcs to the output tree so we want to declare these outputs to include along with .js Default outs
# NB: We don't have emit_declaration_only setting here, so use presence of any JS outputs as an equivalent.
# tsc will only produce .json if it also produces .js
if len(js_outs):
pkg_len = len(ctx.label.package) + 1 if len(ctx.label.package) else 0
rootdir_replace_pattern = ctx.attr.root_dir + "/" if ctx.attr.root_dir else ""
json_outs = _lib.declare_outputs(ctx, [
_lib.join(ctx.attr.out_dir, src.short_path[pkg_len:].replace(rootdir_replace_pattern, ""))
for src in srcs_inputs
if src.basename.endswith(".json") and src.is_source
])
else:
json_outs = []

outputs = json_outs + js_outs + map_outs + typings_outs + typing_maps_outs
outputs = js_outs + map_outs + typings_outs + typing_maps_outs
if ctx.outputs.buildinfo_out:
arguments.add_all([
"--tsBuildInfoFile",
to_output_relative_path(ctx.outputs.buildinfo_out),
])
outputs.append(ctx.outputs.buildinfo_out)
output_sources = json_outs + js_outs + map_outs + copy_files_to_bin_actions(ctx, ctx.files.assets)
output_sources = js_outs + map_outs + copy_files_to_bin_actions(ctx, ctx.files.assets)
typings_srcs = [s for s in srcs_inputs if _lib.is_typings_src(s.path)]

if len(js_outs) + len(typings_outs) < 1:
Expand Down

0 comments on commit 12012a4

Please sign in to comment.