@@ -14,6 +14,7 @@ _ATTRS = {
1414 "deps" : attr .label_list (providers = [DeclarationInfo ]),
1515 "extends" : attr .label_list (allow_files = [".json" ]),
1616 "outdir" : attr .string (),
17+ "rootdir" : attr .string (),
1718 # NB: no restriction on extensions here, because tsc sometimes adds type-check support
1819 # for more file kinds (like require('some.json')) and also
1920 # if you swap out the `compiler` attribute (like with ngtsc)
@@ -56,9 +57,8 @@ def _ts_project_impl(ctx):
5657 ctx .file .tsconfig .path ,
5758 "--outDir" ,
5859 _join (ctx .bin_dir .path , ctx .label .package , ctx .attr .outdir ),
59- # Make sure TypeScript writes outputs to same directory structure as inputs
6060 "--rootDir" ,
61- ctx .label .package if ctx .label .package else "." ,
61+ _join ( ctx .label .package , ctx . attr . rootdir ) if ctx .label .package else "." ,
6262 ])
6363 if len (ctx .outputs .typings_outs ) > 0 :
6464 arguments .add_all ([
@@ -211,8 +211,9 @@ validate_options = rule(
211211 },
212212)
213213
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" )]
216217
217218def ts_project_macro (
218219 name = "tsconfig" ,
@@ -230,6 +231,7 @@ def ts_project_macro(
230231 tsc = None ,
231232 validate = True ,
232233 outdir = None ,
234+ rootdir = None ,
233235 ** kwargs ):
234236 """Compiles one TypeScript project using `tsc --project`
235237
@@ -360,6 +362,9 @@ def ts_project_macro(
360362 so if your rule appears in path/to/my/package/BUILD.bazel and outdir = "foo" then the .js files
361363 will appear in bazel-out/[arch]/bin/path/to/my/package/foo/*.js
362364
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+
363368 declaration: if the `declaration` bit is set in the tsconfig.
364369 Instructs Bazel to expect a `.d.ts` output for each `.ts` source.
365370 source_map: if the `sourceMap` bit is set in the tsconfig.
@@ -405,10 +410,11 @@ def ts_project_macro(
405410 tsconfig = tsconfig ,
406411 extends = extends ,
407412 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 [],
412418 buildinfo_out = tsconfig [:- 5 ] + ".tsbuildinfo" if composite or incremental else None ,
413419 tsc = tsc ,
414420 ** kwargs
0 commit comments