@@ -213,6 +213,7 @@ def _validate_options_impl(ctx):
213213
214214 arguments = ctx .actions .args ()
215215 arguments .add_all ([ctx .file .tsconfig .path , marker .path , ctx .attr .target , struct (
216+ allow_js = ctx .attr .allow_js ,
216217 declaration = ctx .attr .declaration ,
217218 declaration_map = ctx .attr .declaration_map ,
218219 composite = ctx .attr .composite ,
@@ -242,6 +243,7 @@ def _validate_options_impl(ctx):
242243validate_options = rule (
243244 implementation = _validate_options_impl ,
244245 attrs = {
246+ "allow_js" : attr .bool (),
245247 "composite" : attr .bool (),
246248 "declaration" : attr .bool (),
247249 "declaration_map" : attr .bool (),
@@ -256,12 +258,17 @@ validate_options = rule(
256258 },
257259)
258260
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 ):
260267 rootdir_replace_pattern = rootdir + "/" if rootdir else ""
261268 return [
262269 _join (outdir , f [:f .rindex ("." )].replace (rootdir_replace_pattern , "" ) + ext )
263270 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 )
265272 ]
266273
267274def ts_project_macro (
@@ -271,6 +278,7 @@ def ts_project_macro(
271278 args = [],
272279 deps = [],
273280 extends = None ,
281+ allow_js = False ,
274282 declaration = False ,
275283 source_map = False ,
276284 declaration_map = False ,
@@ -459,6 +467,9 @@ def ts_project_macro(
459467 will appear in bazel-out/[arch]/bin/path/to/my/package/foo/*.js.
460468 By default the out_dir is '.', meaning the packages folder in bazel-out.
461469
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+
462473 declaration_dir: a string specifying a subdirectory under the bazel-out folder where generated declaration
463474 outputs are written. Equivalent to the TypeScript --declarationDir option.
464475 By default declarations are written to the out_dir.
@@ -485,7 +496,10 @@ def ts_project_macro(
485496 """
486497
487498 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" ])
489503 extra_deps = []
490504
491505 if type (tsconfig ) == type (dict ()):
@@ -500,6 +514,7 @@ def ts_project_macro(
500514 declaration = compiler_options .setdefault ("declaration" , declaration )
501515 declaration_map = compiler_options .setdefault ("declarationMap" , declaration_map )
502516 emit_declaration_only = compiler_options .setdefault ("emitDeclarationOnly" , emit_declaration_only )
517+ allow_js = compiler_options .setdefault ("allowJs" , allow_js )
503518
504519 # These options are always passed on the tsc command line so don't include them
505520 # in the tsconfig. At best they're redundant, but at worst we'll have a conflict
@@ -538,6 +553,7 @@ def ts_project_macro(
538553 incremental = incremental ,
539554 ts_build_info_file = ts_build_info_file ,
540555 emit_declaration_only = emit_declaration_only ,
556+ allow_js = allow_js ,
541557 tsconfig = tsconfig ,
542558 extends = extends ,
543559 )
@@ -560,10 +576,10 @@ def ts_project_macro(
560576 declaration_dir = declaration_dir ,
561577 out_dir = out_dir ,
562578 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 [],
567583 buildinfo_out = tsbuildinfo_path if composite or incremental else None ,
568584 tsc = tsc ,
569585 link_workspace_root = link_workspace_root ,
0 commit comments