22
33load ("@build_bazel_rules_nodejs//:providers.bzl" , "DeclarationInfo" , "NpmPackageInfo" , "declaration_info" , "js_module_info" , "run_node" )
44load ("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl" , "module_mappings_aspect" )
5+ load ("@build_bazel_rules_nodejs//internal/node:node.bzl" , "nodejs_binary" )
56load (":ts_config.bzl" , "TsConfigInfo" , "write_tsconfig" )
67
78_ValidOptionsInfo = provider ()
@@ -13,6 +14,20 @@ _DEFAULT_TSC = (
1314 "//typescript/bin:tsc"
1415)
1516
17+ _DEFAULT_TSC_BIN = (
18+ # BEGIN-INTERNAL
19+ "@npm" +
20+ # END-INTERNAL
21+ "//:node_modules/typescript/bin/tsc"
22+ )
23+
24+ _DEFAULT_TYPESCRIPT_MODULE = (
25+ # BEGIN-INTERNAL
26+ "@npm" +
27+ # END-INTERNAL
28+ "//typescript"
29+ )
30+
1631_ATTRS = {
1732 "args" : attr .string_list (),
1833 "declaration_dir" : attr .string (),
@@ -33,7 +48,14 @@ _ATTRS = {
3348 # if you swap out the `compiler` attribute (like with ngtsc)
3449 # that compiler might allow more sources than tsc does.
3550 "srcs" : attr .label_list (allow_files = True , mandatory = True ),
36- "tsc" : attr .label (default = Label (_DEFAULT_TSC ), executable = True , cfg = "host" ),
51+ "supports_workers" : attr .bool (
52+ doc = """Experimental! Use only with caution.
53+
54+ Allows you to enable the Bazel Worker strategy for this project.
55+ This requires that the tsc binary support it.""" ,
56+ default = False ,
57+ ),
58+ "tsc" : attr .label (default = Label (_DEFAULT_TSC ), executable = True , cfg = "target" ),
3759 "tsconfig" : attr .label (mandatory = True , allow_single_file = [".json" ]),
3860}
3961
@@ -56,6 +78,16 @@ def _join(*elements):
5678
5779def _ts_project_impl (ctx ):
5880 arguments = ctx .actions .args ()
81+ execution_requirements = {}
82+ progress_prefix = "Compiling TypeScript project"
83+
84+ if ctx .attr .supports_workers :
85+ # Set to use a multiline param-file for worker mode
86+ arguments .use_param_file ("@%s" , use_always = True )
87+ arguments .set_param_file_format ("multiline" )
88+ execution_requirements ["supports-workers" ] = "1"
89+ execution_requirements ["worker-key-mnemonic" ] = "TsProject"
90+ progress_prefix = "Compiling TypeScript project (worker mode)"
5991
6092 generated_srcs = False
6193 for src in ctx .files .srcs :
@@ -162,7 +194,9 @@ def _ts_project_impl(ctx):
162194 arguments = [arguments ],
163195 outputs = outputs ,
164196 executable = "tsc" ,
165- progress_message = "Compiling TypeScript project %s [tsc -p %s]" % (
197+ execution_requirements = execution_requirements ,
198+ progress_message = "%s %s [tsc -p %s]" % (
199+ progress_prefix ,
166200 ctx .label ,
167201 ctx .file .tsconfig .short_path ,
168202 ),
@@ -287,7 +321,10 @@ def ts_project_macro(
287321 emit_declaration_only = False ,
288322 ts_build_info_file = None ,
289323 tsc = None ,
324+ worker_tsc_bin = _DEFAULT_TSC_BIN ,
325+ worker_typescript_module = _DEFAULT_TYPESCRIPT_MODULE ,
290326 validate = True ,
327+ supports_workers = False ,
291328 declaration_dir = None ,
292329 out_dir = None ,
293330 root_dir = None ,
@@ -453,8 +490,28 @@ def ts_project_macro(
453490 For example, `tsc = "@my_deps//typescript/bin:tsc"`
454491 Or you can pass a custom compiler binary instead.
455492
493+ worker_tsc_bin: Label of the TypeScript compiler binary to run when running in worker mode.
494+
495+ For example, `tsc = "@my_deps//node_modules/typescript/bin/tsc"`
496+ Or you can pass a custom compiler binary instead.
497+
498+ worker_typescript_module: Label of the package containing all data deps of worker_tsc_bin.
499+
500+ For example, `tsc = "@my_deps//typescript"`
501+
456502 validate: boolean; whether to check that the tsconfig settings match the attributes.
457503
504+ supports_workers: Experimental! Use only with caution.
505+
506+ Allows you to enable the Bazel Persistent Workers strategy for this project.
507+ See https://docs.bazel.build/versions/master/persistent-workers.html
508+
509+ This requires that the tsc binary support a `--watch` option.
510+
511+ NOTE: this does not work on Windows yet.
512+ We will silently fallback to non-worker mode on Windows regardless of the value of this attribute.
513+ Follow https://github.com/bazelbuild/rules_nodejs/issues/2277 for progress on this feature.
514+
458515 root_dir: a string specifying a subdirectory under the input package which should be consider the
459516 root directory of all the input files.
460517 Equivalent to the TypeScript --rootDir option.
@@ -559,6 +616,38 @@ def ts_project_macro(
559616 )
560617 extra_deps .append ("_validate_%s_options" % name )
561618
619+ if supports_workers :
620+ tsc_worker = "%s_worker" % name
621+ protobufjs = (
622+ # BEGIN-INTERNAL
623+ "@npm" +
624+ # END-INTERNAL
625+ "//protobufjs"
626+ )
627+ nodejs_binary (
628+ name = tsc_worker ,
629+ data = [
630+ Label ("//packages/typescript/internal/worker:worker" ),
631+ Label (worker_tsc_bin ),
632+ Label (worker_typescript_module ),
633+ Label (protobufjs ),
634+ tsconfig ,
635+ ],
636+ entry_point = Label ("//packages/typescript/internal/worker:worker_adapter" ),
637+ templated_args = [
638+ "--nobazel_patch_module_resolver" ,
639+ "$(execpath {})" .format (Label (worker_tsc_bin )),
640+ "--project" ,
641+ "$(execpath {})" .format (tsconfig ),
642+ # FIXME: should take out_dir into account
643+ "--outDir" ,
644+ "$(RULEDIR)" ,
645+ # FIXME: what about other settings like declaration_dir, root_dir, etc
646+ ],
647+ )
648+
649+ tsc = ":" + tsc_worker
650+
562651 typings_out_dir = declaration_dir if declaration_dir else out_dir
563652 tsbuildinfo_path = ts_build_info_file if ts_build_info_file else name + ".tsbuildinfo"
564653
@@ -583,5 +672,9 @@ def ts_project_macro(
583672 buildinfo_out = tsbuildinfo_path if composite or incremental else None ,
584673 tsc = tsc ,
585674 link_workspace_root = link_workspace_root ,
675+ supports_workers = select ({
676+ "@bazel_tools//src/conditions:host_windows" : False ,
677+ "//conditions:default" : supports_workers ,
678+ }),
586679 ** kwargs
587680 )
0 commit comments