angular-cn/packages/compiler-cli/src/tooling.ts

28 lines
617 B
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* @module
* @description
* Tooling support helpers.
*/
/**
* Known values for global variables in `@angular/core` that Terser should set using
* https://github.com/terser-js/terser#conditional-compilation
*/
export const GLOBAL_DEFS_FOR_TERSER = {
ngDevMode: false,
ngI18nClosureMode: false,
};
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
2019-11-07 17:18:44 -05:00
export const GLOBAL_DEFS_FOR_TERSER_WITH_AOT = {
...GLOBAL_DEFS_FOR_TERSER,
ngJitMode: false,
};