@@ -14,6 +14,7 @@ _ATTRS = {
14
14
"deps" : attr .label_list (providers = [DeclarationInfo ]),
15
15
"extends" : attr .label_list (allow_files = [".json" ]),
16
16
"outdir" : attr .string (),
17
+ "rootdir" : attr .string (),
17
18
# NB: no restriction on extensions here, because tsc sometimes adds type-check support
18
19
# for more file kinds (like require('some.json')) and also
19
20
# if you swap out the `compiler` attribute (like with ngtsc)
@@ -56,9 +57,8 @@ def _ts_project_impl(ctx):
56
57
ctx .file .tsconfig .path ,
57
58
"--outDir" ,
58
59
_join (ctx .bin_dir .path , ctx .label .package , ctx .attr .outdir ),
59
- # Make sure TypeScript writes outputs to same directory structure as inputs
60
60
"--rootDir" ,
61
- ctx .label .package if ctx .label .package else "." ,
61
+ _join ( ctx .label .package , ctx . attr . rootdir ) if ctx .label .package else "." ,
62
62
])
63
63
if len (ctx .outputs .typings_outs ) > 0 :
64
64
arguments .add_all ([
@@ -211,8 +211,9 @@ validate_options = rule(
211
211
},
212
212
)
213
213
214
- def _out_paths (srcs , outdir , ext ):
215
- return [_join (outdir , f [:f .rindex ("." )] + ext ) for f in srcs if not f .endswith (".d.ts" ) and not f .endswith (".json" )]
214
+ def _out_paths (srcs , outdir , rootdir , ext ):
215
+ rootdir_replace_pattern = rootdir + "/" if rootdir else ""
216
+ 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" )]
216
217
217
218
def ts_project_macro (
218
219
name = "tsconfig" ,
@@ -230,6 +231,7 @@ def ts_project_macro(
230
231
tsc = None ,
231
232
validate = True ,
232
233
outdir = None ,
234
+ rootdir = None ,
233
235
** kwargs ):
234
236
"""Compiles one TypeScript project using `tsc --project`
235
237
@@ -360,6 +362,9 @@ def ts_project_macro(
360
362
so if your rule appears in path/to/my/package/BUILD.bazel and outdir = "foo" then the .js files
361
363
will appear in bazel-out/[arch]/bin/path/to/my/package/foo/*.js
362
364
365
+ rootdir: a string specifying a subdirectory under the input package which should be consider the
366
+ root directory of all the input files.
367
+
363
368
declaration: if the `declaration` bit is set in the tsconfig.
364
369
Instructs Bazel to expect a `.d.ts` output for each `.ts` source.
365
370
source_map: if the `sourceMap` bit is set in the tsconfig.
@@ -405,10 +410,11 @@ def ts_project_macro(
405
410
tsconfig = tsconfig ,
406
411
extends = extends ,
407
412
outdir = outdir ,
408
- js_outs = _out_paths (srcs , outdir , ".js" ) if not emit_declaration_only else [],
409
- map_outs = _out_paths (srcs , outdir , ".js.map" ) if source_map and not emit_declaration_only else [],
410
- typings_outs = _out_paths (srcs , outdir , ".d.ts" ) if declaration or composite else [],
411
- typing_maps_outs = _out_paths (srcs , outdir , ".d.ts.map" ) if declaration_map else [],
413
+ rootdir = rootdir ,
414
+ js_outs = _out_paths (srcs , outdir , rootdir , ".js" ) if not emit_declaration_only else [],
415
+ map_outs = _out_paths (srcs , outdir , rootdir , ".js.map" ) if source_map and not emit_declaration_only else [],
416
+ typings_outs = _out_paths (srcs , outdir , rootdir , ".d.ts" ) if declaration or composite else [],
417
+ typing_maps_outs = _out_paths (srcs , outdir , rootdir , ".d.ts.map" ) if declaration_map else [],
412
418
buildinfo_out = tsconfig [:- 5 ] + ".tsbuildinfo" if composite or incremental else None ,
413
419
tsc = tsc ,
414
420
** kwargs
0 commit comments