Please sign in to comment.
fix(ivy): retain JIT metadata unless JIT mode is explicitly disabled (#…
…33671) NgModules in Ivy have a definition which contains various different bits of metadata about the module. In particular, this metadata falls into two categories: * metadata required to use the module at runtime (for bootstrapping, etc) in AOT-only applications. * metadata required to depend on the module from a JIT-compiled app. The latter metadata consists of the module's declarations, imports, and exports. To support JIT usage, this metadata must be included in the generated code, especially if that code is shipped to NPM. However, because this metadata preserves the entire NgModule graph (references to all directives and components in the app), it needs to be removed during optimization for AOT-only builds. Previously, this was done with a clever design: 1. The extra metadata was added by a function called `setNgModuleScope`. A call to this function was generated after each NgModule. 2. This function call was marked as "pure" with a comment and used `noSideEffects` internally, which causes optimizers to remove it. The effect was that in dev mode or test mode (which use JIT), no optimizer runs and the full NgModule metadata was available at runtime. But in production (presumably AOT) builds, the optimizer runs and removes the JIT- specific metadata. However, there are cases where apps that want to use JIT in production, and still make an optimized build. In this case, the JIT-specific metadata would be erroneously removed. This commit solves that problem by adding an `ngJitMode` global variable which guards all `setNgModuleScope` calls. An optimizer can be configured to statically define this global to be `false` for AOT-only builds, causing the extra metadata to be stripped. A configuration for Terser used by the CLI is provided in `tooling.ts` which sets `ngJitMode` to `false` when building AOT apps. PR Close #33671
- Loading branch information
Showing with 63 additions and 22 deletions.
- +16 −7 packages/compiler-cli/src/ngtsc/translator/src/translator.ts
- +5 −0 packages/compiler-cli/src/tooling.ts
- +1 −1 packages/compiler-cli/test/ngtsc/ngtsc_spec.ts
- +2 −2 packages/compiler-cli/test/ngtsc/scope_spec.ts
- +0 −1 packages/compiler/src/compiler_facade_interface.ts
- +18 −6 packages/compiler/src/render3/r3_module_compiler.ts
- +21 −3 packages/compiler/src/render3/util.ts
- +0 −1 packages/core/src/compiler/compiler_facade_interface.ts
- +0 −1 packages/core/src/render3/jit/module.ts