From d4cee514f60fbf99d863ee8e7a4b8410bda151c6 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Wed, 17 Oct 2018 15:44:44 -0700 Subject: [PATCH] refactor(ivy): obviate the Bazel component of the ivy_switch (#26550) Originally, the ivy_switch mechanism used Bazel genrules to conditionally compile one TS file or another depending on whether ngc or ngtsc was the selected compiler. This was done because we wanted to avoid importing certain modules (and thus pulling them into the build) if Ivy was on or off. This mechanism had a major drawback: ivy_switch became a bottleneck in the import graph, as it both imports from many places in the codebase and is imported by many modules in the codebase. This frequently resulted in cyclic imports which caused issues both with TS and Closure compilation. It turns out ngcc needs both code paths in the bundle to perform the switch during its operation anyway, so import switching was later abandoned. This means that there's no real reason why the ivy_switch mechanism needed to operate at the Bazel level, and for the ivy_switch file to be a bottleneck. This commit removes the Bazel-level ivy_switch mechanism, and introduces an additional TypeScript transform in ngtsc (and the pass-through tsc compiler used for testing JIT) to perform the same operation that ngcc does, and flip the switch during ngtsc compilation. This allows the ivy_switch file to be removed, and the individual switches to be located directly next to their consumers in the codebase, greatly mitigating the circular import issues and making the mechanism much easier to use. As part of this commit, the tag for marking switched variables was changed from __PRE_NGCC__ to __PRE_R3__, since it's no longer just ngcc which flips these tags. Most variables were renamed from R3_* to SWITCH_* as well, since they're referenced mostly in render2 code. Test strategy: existing test coverage is more than sufficient - if this didn't work correctly it would break the hello world and todo apps. PR Close #26550 --- docs/BAZEL.md | 2 +- integration/ngcc/test.sh | 6 +- packages/compiler-cli/BUILD.bazel | 1 + .../src/ngcc/src/host/ngcc_host.ts | 4 +- .../analysis/switch_marker_analyzer_spec.ts | 12 +- .../src/ngcc/test/host/esm2015_host_spec.ts | 8 +- .../src/ngcc/test/host/fesm2015_host_spec.ts | 8 +- .../test/rendering/esm2015_renderer_spec.ts | 20 ++- .../ngcc/test/rendering/esm5_renderer_spec.ts | 20 ++- packages/compiler-cli/src/ngtsc/program.ts | 4 + .../compiler-cli/src/ngtsc/switch/BUILD.bazel | 18 +++ .../src/ngtsc/switch/index.ts} | 3 +- .../src/ngtsc/switch/src/switch.ts | 152 ++++++++++++++++++ .../src/transformers/tsc_pass_through.ts | 9 +- packages/core/BUILD.bazel | 36 +---- packages/core/src/application_ref.ts | 6 +- .../change_detection/change_detector_ref.ts | 11 +- packages/core/src/core_private_export.ts | 2 +- .../core/src/core_render3_private_export.ts | 60 ++++--- packages/core/src/di/injectable.ts | 29 +++- packages/core/src/di/util.ts | 55 +++++++ .../compiler/jit.ts => ivy_switch.ts} | 4 +- .../core/src/ivy_switch/compiler/index.ts | 17 -- .../src/ivy_switch/compiler/ivy_switch_on.ts | 21 --- .../core/src/ivy_switch/compiler/legacy.ts | 126 --------------- packages/core/src/ivy_switch/runtime/index.ts | 15 -- .../src/ivy_switch/runtime/ivy_switch_on.ts | 15 -- .../core/src/ivy_switch/runtime/legacy.ts | 34 ---- packages/core/src/ivy_switch/runtime/local.ts | 10 -- packages/core/src/linker/element_ref.ts | 10 +- packages/core/src/linker/template_ref.ts | 10 +- .../core/src/linker/view_container_ref.ts | 10 +- packages/core/src/metadata/directives.ts | 25 ++- packages/core/src/metadata/ng_module.ts | 24 ++- packages/core/src/render/api.ts | 10 +- .../compiler/local.ts => util/noop.ts} | 4 +- packages/core/src/view/services.ts | 2 +- .../bundle.golden_symbols.json | 18 +-- .../hello_world_r2/bundle.golden_symbols.json | 52 +++--- .../injection/bundle.golden_symbols.json | 6 +- .../bundling/todo/bundle.golden_symbols.json | 18 +-- .../todo_r2/bundle.golden_symbols.json | 39 +++-- packages/core/test/render3/ivy/jit_spec.ts | 2 +- packages/core/test/render3/render_util.ts | 6 +- 44 files changed, 507 insertions(+), 437 deletions(-) create mode 100644 packages/compiler-cli/src/ngtsc/switch/BUILD.bazel rename packages/{core/src/ivy_switch/runtime/jit.ts => compiler-cli/src/ngtsc/switch/index.ts} (80%) create mode 100644 packages/compiler-cli/src/ngtsc/switch/src/switch.ts create mode 100644 packages/core/src/di/util.ts rename packages/core/src/{ivy_switch/compiler/jit.ts => ivy_switch.ts} (57%) delete mode 100644 packages/core/src/ivy_switch/compiler/index.ts delete mode 100644 packages/core/src/ivy_switch/compiler/ivy_switch_on.ts delete mode 100644 packages/core/src/ivy_switch/compiler/legacy.ts delete mode 100644 packages/core/src/ivy_switch/runtime/index.ts delete mode 100644 packages/core/src/ivy_switch/runtime/ivy_switch_on.ts delete mode 100644 packages/core/src/ivy_switch/runtime/legacy.ts delete mode 100644 packages/core/src/ivy_switch/runtime/local.ts rename packages/core/src/{ivy_switch/compiler/local.ts => util/noop.ts} (76%) diff --git a/docs/BAZEL.md b/docs/BAZEL.md index d72c3e554c..bd13324161 100644 --- a/docs/BAZEL.md +++ b/docs/BAZEL.md @@ -76,7 +76,7 @@ See also: [`//tools/bazel.rc`](https://github.com/angular/angular/blob/master/to - `--config=debug`: build and launch in debug mode (see [debugging](#debugging) instructions below) - `--test_arg=--node_options=--inspect=9228`: change the inspector port. -- `--define=compile=