26
26
"tsc_wrapped_tsconfig" ,
27
27
)
28
28
29
+ # enable_perf_logging controls whether Ivy's performance tracing system will be enabled for any
30
+ # compilation which includes this provider.
31
+ NgPerfInfo = provider (fields = ["enable_perf_logging" ])
32
+
29
33
_FLAT_DTS_FILE_SUFFIX = ".bundle.d.ts"
30
34
_R3_SYMBOLS_DTS_FILE = "src/r3_symbols.d.ts"
31
35
36
+ def is_perf_requested (ctx ):
37
+ enable_perf_logging = ctx .attr .perf_flag != None and ctx .attr .perf_flag [NgPerfInfo ].enable_perf_logging == True
38
+ if enable_perf_logging and not is_ivy_enabled (ctx ):
39
+ fail ("Angular View Engine does not support performance tracing" )
40
+ return enable_perf_logging
41
+
32
42
def is_ivy_enabled (ctx ):
33
43
"""Determine if the ivy compiler should be used to by the ng_module.
34
44
@@ -278,6 +288,15 @@ def _expected_outs(ctx):
278
288
else :
279
289
i18n_messages_files = []
280
290
291
+ dev_perf_files = []
292
+ prod_perf_files = []
293
+
294
+ # In Ivy mode, dev and prod builds both produce a .json output containing performance metrics
295
+ # from the compiler for that build.
296
+ if is_perf_requested (ctx ):
297
+ dev_perf_files = [ctx .actions .declare_file (ctx .label .name + "_perf_dev.json" )]
298
+ prod_perf_files = [ctx .actions .declare_file (ctx .label .name + "_perf_prod.json" )]
299
+
281
300
return struct (
282
301
closure_js = closure_js_files ,
283
302
devmode_js = devmode_js_files ,
@@ -288,6 +307,8 @@ def _expected_outs(ctx):
288
307
dts_bundles = dts_bundles ,
289
308
bundle_index_typings = bundle_index_typings ,
290
309
i18n_messages = i18n_messages_files ,
310
+ dev_perf_files = dev_perf_files ,
311
+ prod_perf_files = prod_perf_files ,
291
312
)
292
313
293
314
# Determines if we need to generate View Engine shims (.ngfactory and .ngsummary files)
@@ -336,6 +357,15 @@ def _ngc_tsconfig(ctx, files, srcs, **kwargs):
336
357
"_useManifestPathsAsModuleName" : (not _is_bazel ()),
337
358
}
338
359
360
+ if is_perf_requested (ctx ):
361
+ # In Ivy mode, set the `tracePerformance` Angular compiler option to enable performance
362
+ # metric output.
363
+ if "devmode_manifest" in kwargs :
364
+ perf_path = outs .dev_perf_files [0 ].path
365
+ else :
366
+ perf_path = outs .prod_perf_files [0 ].path
367
+ angular_compiler_options ["tracePerformance" ] = perf_path
368
+
339
369
if _should_produce_flat_module_outs (ctx ):
340
370
angular_compiler_options ["flatModuleId" ] = ctx .attr .module_name
341
371
angular_compiler_options ["flatModuleOutFile" ] = _flat_module_out_file (ctx )
@@ -519,6 +549,7 @@ def _compile_action(
519
549
outputs ,
520
550
dts_bundles_out ,
521
551
messages_out ,
552
+ perf_out ,
522
553
tsconfig_file ,
523
554
node_opts ,
524
555
compile_mode ):
@@ -563,12 +594,12 @@ def _compile_action(
563
594
564
595
def _prodmode_compile_action (ctx , inputs , outputs , tsconfig_file , node_opts ):
565
596
outs = _expected_outs (ctx )
566
- return _compile_action (ctx , inputs , outputs + outs .closure_js , None , outs .i18n_messages , tsconfig_file , node_opts , "prodmode" )
597
+ return _compile_action (ctx , inputs , outputs + outs .closure_js + outs . prod_perf_files , None , outs .i18n_messages , outs . prod_perf_files , tsconfig_file , node_opts , "prodmode" )
567
598
568
599
def _devmode_compile_action (ctx , inputs , outputs , tsconfig_file , node_opts ):
569
600
outs = _expected_outs (ctx )
570
- compile_action_outputs = outputs + outs .devmode_js + outs .declarations + outs .summaries + outs .metadata
571
- _compile_action (ctx , inputs , compile_action_outputs , outs .dts_bundles , None , tsconfig_file , node_opts , "devmode" )
601
+ compile_action_outputs = outputs + outs .devmode_js + outs .declarations + outs .summaries + outs .metadata + outs . dev_perf_files
602
+ _compile_action (ctx , inputs , compile_action_outputs , outs .dts_bundles , None , outs . dev_perf_files , tsconfig_file , node_opts , "devmode" )
572
603
573
604
def _ts_expected_outs (ctx , label , srcs_files = []):
574
605
# rules_typescript expects a function with two or more arguments, but our
@@ -711,6 +742,16 @@ NG_MODULE_ATTRIBUTES = {
711
742
executable = True ,
712
743
cfg = "host" ,
713
744
),
745
+ # In the angular/angular monorepo, //tools:defaults.bzl wraps the ng_module rule in a macro
746
+ # which sets this attribute to the //packages/compiler-cli:ng_perf flag.
747
+ # This is done to avoid exposing the flag to user projects, which would require:
748
+ # * defining the flag within @angular/bazel and referencing it correctly here, and
749
+ # * committing to the flag and its semantics (including the format of perf JSON files)
750
+ # as something users can depend upon.
751
+ "perf_flag" : attr .label (
752
+ providers = [NgPerfInfo ],
753
+ doc = "Private API to control production of performance metric JSON files" ,
754
+ ),
714
755
"_supports_workers" : attr .bool (default = True ),
715
756
}
716
757
0 commit comments