@@ -215,6 +215,7 @@ def _validate_options_impl(ctx):
215215
216216 arguments = ctx .actions .args ()
217217 arguments .add_all ([ctx .file .tsconfig .path , marker .path , ctx .attr .target , struct (
218+ allow_js = ctx .attr .allow_js ,
218219 declaration = ctx .attr .declaration ,
219220 declaration_map = ctx .attr .declaration_map ,
220221 composite = ctx .attr .composite ,
@@ -244,6 +245,7 @@ def _validate_options_impl(ctx):
244245validate_options = rule (
245246 implementation = _validate_options_impl ,
246247 attrs = {
248+ "allow_js" : attr .bool (),
247249 "composite" : attr .bool (),
248250 "declaration" : attr .bool (),
249251 "declaration_map" : attr .bool (),
@@ -258,12 +260,17 @@ validate_options = rule(
258260 },
259261)
260262
261- def _out_paths (srcs , outdir , rootdir , ext ):
263+ def _is_ts_src (src , allow_js ):
264+ if not src .endswith (".d.ts" ) and (src .endswith (".ts" ) or src .endswith (".tsx" )):
265+ return True
266+ return allow_js and (src .endswith (".js" ) or src .endswith (".jsx" ))
267+
268+ def _out_paths (srcs , outdir , rootdir , allow_js , ext ):
262269 rootdir_replace_pattern = rootdir + "/" if rootdir else ""
263270 return [
264271 _join (outdir , f [:f .rindex ("." )].replace (rootdir_replace_pattern , "" ) + ext )
265272 for f in srcs
266- if not f . endswith ( ".d.ts" ) and ( f . endswith ( ".ts" ) or f . endswith ( ".tsx" ) )
273+ if _is_ts_src ( f , allow_js )
267274 ]
268275
269276def ts_project_macro (
@@ -273,6 +280,7 @@ def ts_project_macro(
273280 args = [],
274281 deps = [],
275282 extends = None ,
283+ allow_js = False ,
276284 declaration = False ,
277285 source_map = False ,
278286 declaration_map = False ,
@@ -456,6 +464,9 @@ def ts_project_macro(
456464 will appear in bazel-out/[arch]/bin/path/to/my/package/foo/*.js.
457465 By default the out_dir is '.', meaning the packages folder in bazel-out.
458466
467+ allow_js: boolean; Specifies whether TypeScript will read .js and .jsx files. When used with declaration,
468+ TypeScript will generate .d.ts files from .js files.
469+
459470 declaration_dir: a string specifying a subdirectory under the bazel-out folder where generated declaration
460471 outputs are written. Equivalent to the TypeScript --declarationDir option.
461472 By default declarations are written to the out_dir.
@@ -482,7 +493,10 @@ def ts_project_macro(
482493 """
483494
484495 if srcs == None :
485- srcs = native .glob (["**/*.ts" , "**/*.tsx" ])
496+ if allow_js == True :
497+ srcs = native .glob (["**/*.ts" , "**/*.tsx" , "**/*.js" , "**/*.jsx" ])
498+ else :
499+ srcs = native .glob (["**/*.ts" , "**/*.tsx" ])
486500 extra_deps = []
487501
488502 if type (extends ) == type ([]):
@@ -497,6 +511,7 @@ def ts_project_macro(
497511 declaration = compiler_options .setdefault ("declaration" , declaration )
498512 declaration_map = compiler_options .setdefault ("declarationMap" , declaration_map )
499513 emit_declaration_only = compiler_options .setdefault ("emitDeclarationOnly" , emit_declaration_only )
514+ allow_js = compiler_options .setdefault ("allowJs" , allow_js )
500515
501516 # These options are always passed on the tsc command line so don't include them
502517 # in the tsconfig. At best they're redundant, but at worst we'll have a conflict
@@ -535,6 +550,7 @@ def ts_project_macro(
535550 incremental = incremental ,
536551 ts_build_info_file = ts_build_info_file ,
537552 emit_declaration_only = emit_declaration_only ,
553+ allow_js = allow_js ,
538554 tsconfig = tsconfig ,
539555 extends = extends ,
540556 )
@@ -553,10 +569,10 @@ def ts_project_macro(
553569 declaration_dir = declaration_dir ,
554570 out_dir = out_dir ,
555571 root_dir = root_dir ,
556- js_outs = _out_paths (srcs , out_dir , root_dir , ".js" ) if not emit_declaration_only else [],
557- map_outs = _out_paths (srcs , out_dir , root_dir , ".js.map" ) if source_map and not emit_declaration_only else [],
558- typings_outs = _out_paths (srcs , typings_out_dir , root_dir , ".d.ts" ) if declaration or composite else [],
559- typing_maps_outs = _out_paths (srcs , typings_out_dir , root_dir , ".d.ts.map" ) if declaration_map else [],
572+ js_outs = _out_paths (srcs , out_dir , root_dir , False , ".js" ) if not emit_declaration_only else [],
573+ map_outs = _out_paths (srcs , out_dir , root_dir , False , ".js.map" ) if source_map and not emit_declaration_only else [],
574+ typings_outs = _out_paths (srcs , typings_out_dir , root_dir , allow_js , ".d.ts" ) if declaration or composite else [],
575+ typing_maps_outs = _out_paths (srcs , typings_out_dir , root_dir , allow_js , ".d.ts.map" ) if declaration_map else [],
560576 buildinfo_out = tsbuildinfo_path if composite or incremental else None ,
561577 tsc = tsc ,
562578 link_workspace_root = link_workspace_root ,
0 commit comments