Skip to content

Commit

Permalink
feat(typescript): support for rootdir on ts_project
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic authored and alexeagle committed Jul 14, 2020
1 parent 07d9bb8 commit bc88536
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ node_modules
!/third_party/npm/node_modules
yarn-error.log
internal/node/_node_bin
.DS_Store
22 changes: 14 additions & 8 deletions packages/typescript/internal/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ _ATTRS = {
"deps": attr.label_list(providers = [DeclarationInfo]),
"extends": attr.label_list(allow_files = [".json"]),
"outdir": attr.string(),
"rootdir": attr.string(),
# NB: no restriction on extensions here, because tsc sometimes adds type-check support
# for more file kinds (like require('some.json')) and also
# if you swap out the `compiler` attribute (like with ngtsc)
Expand Down Expand Up @@ -56,9 +57,8 @@ def _ts_project_impl(ctx):
ctx.file.tsconfig.path,
"--outDir",
_join(ctx.bin_dir.path, ctx.label.package, ctx.attr.outdir),
# Make sure TypeScript writes outputs to same directory structure as inputs
"--rootDir",
ctx.label.package if ctx.label.package else ".",
_join(ctx.label.package, ctx.attr.rootdir) if ctx.label.package else ".",
])
if len(ctx.outputs.typings_outs) > 0:
arguments.add_all([
Expand Down Expand Up @@ -211,8 +211,9 @@ validate_options = rule(
},
)

def _out_paths(srcs, outdir, ext):
return [_join(outdir, f[:f.rindex(".")] + ext) for f in srcs if not f.endswith(".d.ts") and not f.endswith(".json")]
def _out_paths(srcs, outdir, rootdir, ext):
rootdir_replace_pattern = rootdir + "/" if rootdir else ""
return [_join(outdir, f[:f.rindex(".")].replace(rootdir_replace_pattern, "") + ext) for f in srcs if not f.endswith(".d.ts") and not f.endswith(".json")]

def ts_project_macro(
name = "tsconfig",
Expand All @@ -230,6 +231,7 @@ def ts_project_macro(
tsc = None,
validate = True,
outdir = None,
rootdir = None,
**kwargs):
"""Compiles one TypeScript project using `tsc --project`
Expand Down Expand Up @@ -360,6 +362,9 @@ def ts_project_macro(
so if your rule appears in path/to/my/package/BUILD.bazel and outdir = "foo" then the .js files
will appear in bazel-out/[arch]/bin/path/to/my/package/foo/*.js
rootdir: a string specifying a subdirectory under the input package which should be consider the
root directory of all the input files.
declaration: if the `declaration` bit is set in the tsconfig.
Instructs Bazel to expect a `.d.ts` output for each `.ts` source.
source_map: if the `sourceMap` bit is set in the tsconfig.
Expand Down Expand Up @@ -405,10 +410,11 @@ def ts_project_macro(
tsconfig = tsconfig,
extends = extends,
outdir = outdir,
js_outs = _out_paths(srcs, outdir, ".js") if not emit_declaration_only else [],
map_outs = _out_paths(srcs, outdir, ".js.map") if source_map and not emit_declaration_only else [],
typings_outs = _out_paths(srcs, outdir, ".d.ts") if declaration or composite else [],
typing_maps_outs = _out_paths(srcs, outdir, ".d.ts.map") if declaration_map else [],
rootdir = rootdir,
js_outs = _out_paths(srcs, outdir, rootdir, ".js") if not emit_declaration_only else [],
map_outs = _out_paths(srcs, outdir, rootdir, ".js.map") if source_map and not emit_declaration_only else [],
typings_outs = _out_paths(srcs, outdir, rootdir, ".d.ts") if declaration or composite else [],
typing_maps_outs = _out_paths(srcs, outdir, rootdir, ".d.ts.map") if declaration_map else [],
buildinfo_out = tsconfig[:-5] + ".tsbuildinfo" if composite or incremental else None,
tsc = tsc,
**kwargs
Expand Down

0 comments on commit bc88536

Please sign in to comment.