@@ -213,6 +213,7 @@ def _validate_options_impl(ctx):
213
213
214
214
arguments = ctx .actions .args ()
215
215
arguments .add_all ([ctx .file .tsconfig .path , marker .path , ctx .attr .target , struct (
216
+ allow_js = ctx .attr .allow_js ,
216
217
declaration = ctx .attr .declaration ,
217
218
declaration_map = ctx .attr .declaration_map ,
218
219
composite = ctx .attr .composite ,
@@ -242,6 +243,7 @@ def _validate_options_impl(ctx):
242
243
validate_options = rule (
243
244
implementation = _validate_options_impl ,
244
245
attrs = {
246
+ "allow_js" : attr .bool (),
245
247
"composite" : attr .bool (),
246
248
"declaration" : attr .bool (),
247
249
"declaration_map" : attr .bool (),
@@ -256,12 +258,17 @@ validate_options = rule(
256
258
},
257
259
)
258
260
259
- def _out_paths (srcs , outdir , rootdir , ext ):
261
+ def _is_ts_src (src , allow_js ):
262
+ if not src .endswith (".d.ts" ) and (src .endswith (".ts" ) or src .endswith (".tsx" )):
263
+ return True
264
+ return allow_js and (src .endswith (".js" ) or src .endswith (".jsx" ))
265
+
266
+ def _out_paths (srcs , outdir , rootdir , allow_js , ext ):
260
267
rootdir_replace_pattern = rootdir + "/" if rootdir else ""
261
268
return [
262
269
_join (outdir , f [:f .rindex ("." )].replace (rootdir_replace_pattern , "" ) + ext )
263
270
for f in srcs
264
- if not f . endswith ( ".d.ts" ) and ( f . endswith ( ".ts" ) or f . endswith ( ".tsx" ) )
271
+ if _is_ts_src ( f , allow_js )
265
272
]
266
273
267
274
def ts_project_macro (
@@ -271,6 +278,7 @@ def ts_project_macro(
271
278
args = [],
272
279
deps = [],
273
280
extends = None ,
281
+ allow_js = False ,
274
282
declaration = False ,
275
283
source_map = False ,
276
284
declaration_map = False ,
@@ -459,6 +467,9 @@ def ts_project_macro(
459
467
will appear in bazel-out/[arch]/bin/path/to/my/package/foo/*.js.
460
468
By default the out_dir is '.', meaning the packages folder in bazel-out.
461
469
470
+ allow_js: boolean; Specifies whether TypeScript will read .js and .jsx files. When used with declaration,
471
+ TypeScript will generate .d.ts files from .js files.
472
+
462
473
declaration_dir: a string specifying a subdirectory under the bazel-out folder where generated declaration
463
474
outputs are written. Equivalent to the TypeScript --declarationDir option.
464
475
By default declarations are written to the out_dir.
@@ -485,7 +496,10 @@ def ts_project_macro(
485
496
"""
486
497
487
498
if srcs == None :
488
- srcs = native .glob (["**/*.ts" , "**/*.tsx" ])
499
+ if allow_js == True :
500
+ srcs = native .glob (["**/*.ts" , "**/*.tsx" , "**/*.js" , "**/*.jsx" ])
501
+ else :
502
+ srcs = native .glob (["**/*.ts" , "**/*.tsx" ])
489
503
extra_deps = []
490
504
491
505
if type (tsconfig ) == type (dict ()):
@@ -500,6 +514,7 @@ def ts_project_macro(
500
514
declaration = compiler_options .setdefault ("declaration" , declaration )
501
515
declaration_map = compiler_options .setdefault ("declarationMap" , declaration_map )
502
516
emit_declaration_only = compiler_options .setdefault ("emitDeclarationOnly" , emit_declaration_only )
517
+ allow_js = compiler_options .setdefault ("allowJs" , allow_js )
503
518
504
519
# These options are always passed on the tsc command line so don't include them
505
520
# in the tsconfig. At best they're redundant, but at worst we'll have a conflict
@@ -538,6 +553,7 @@ def ts_project_macro(
538
553
incremental = incremental ,
539
554
ts_build_info_file = ts_build_info_file ,
540
555
emit_declaration_only = emit_declaration_only ,
556
+ allow_js = allow_js ,
541
557
tsconfig = tsconfig ,
542
558
extends = extends ,
543
559
)
@@ -560,10 +576,10 @@ def ts_project_macro(
560
576
declaration_dir = declaration_dir ,
561
577
out_dir = out_dir ,
562
578
root_dir = root_dir ,
563
- js_outs = _out_paths (srcs , out_dir , root_dir , ".js" ) if not emit_declaration_only else [],
564
- map_outs = _out_paths (srcs , out_dir , root_dir , ".js.map" ) if source_map and not emit_declaration_only else [],
565
- typings_outs = _out_paths (srcs , typings_out_dir , root_dir , ".d.ts" ) if declaration or composite else [],
566
- typing_maps_outs = _out_paths (srcs , typings_out_dir , root_dir , ".d.ts.map" ) if declaration_map else [],
579
+ js_outs = _out_paths (srcs , out_dir , root_dir , False , ".js" ) if not emit_declaration_only else [],
580
+ map_outs = _out_paths (srcs , out_dir , root_dir , False , ".js.map" ) if source_map and not emit_declaration_only else [],
581
+ typings_outs = _out_paths (srcs , typings_out_dir , root_dir , allow_js , ".d.ts" ) if declaration or composite else [],
582
+ typing_maps_outs = _out_paths (srcs , typings_out_dir , root_dir , allow_js , ".d.ts.map" ) if declaration_map else [],
567
583
buildinfo_out = tsbuildinfo_path if composite or incremental else None ,
568
584
tsc = tsc ,
569
585
link_workspace_root = link_workspace_root ,
0 commit comments