Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ivy): retain JIT metadata unless JIT mode is explicitly disabled #33671

Closed
wants to merge 1 commit into from

Conversation

@alxhub
Copy link
Contributor

alxhub commented Nov 7, 2019

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.=

@googlebot googlebot added the cla: yes label Nov 7, 2019
@kara kara added the comp: compiler label Nov 8, 2019
@ngbot ngbot bot added this to the needsTriage milestone Nov 8, 2019
@alxhub alxhub force-pushed the alxhub:ngtsc/jitmode-setngmodulescope branch from 6af6458 to da29864 Nov 16, 2019
@alxhub alxhub marked this pull request as ready for review Nov 18, 2019
@alxhub alxhub requested review from angular/fw-compiler as code owners Nov 18, 2019
filipesilva added a commit to filipesilva/angular-cli that referenced this pull request Nov 19, 2019
… calls

These will be automatically removed after angular/angular#33671 lands because they are in a side-effect free IIFE.
@filipesilva

This comment has been minimized.

Copy link
Member

filipesilva commented Nov 19, 2019

Build Optimizer update in angular/angular-cli#16228

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.
@alxhub alxhub force-pushed the alxhub:ngtsc/jitmode-setngmodulescope branch from da29864 to bd2edc4 Nov 20, 2019
@alxhub

This comment has been minimized.

Copy link
Contributor Author

alxhub commented Nov 20, 2019

@alxhub alxhub closed this in b54ed98 Nov 20, 2019
alxhub added a commit that referenced this pull request Nov 20, 2019
…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
vikerman added a commit to filipesilva/angular-cli that referenced this pull request Nov 20, 2019
… calls

These will be automatically removed after angular/angular#33671 lands because they are in a side-effect free IIFE.
vikerman added a commit to angular/angular-cli that referenced this pull request Nov 21, 2019
… calls

These will be automatically removed after angular/angular#33671 lands because they are in a side-effect free IIFE.
vikerman added a commit to angular/angular-cli that referenced this pull request Nov 21, 2019
… calls

These will be automatically removed after angular/angular#33671 lands because they are in a side-effect free IIFE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.