From e5905bb035d0e1f2eb9af2c5c6f09eb51fb0bff0 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Tue, 16 Apr 2019 16:52:44 +0100 Subject: [PATCH] fix(core): use shakeable global definitions (#29929) The `ngDevMode` and `ngI18nClosureMode` are special in that they should be set to `false` on production builds in order to shake out code associated with it. Angular CLI does this in https://github.com/angular/angular-cli/blob/5fc1f2499cbe57f9a95e4b0dfced130eb3a8046d/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts#L279-L282. But in https://github.com/angular/angular/pull/28689 the toplevel usage was changed from `ngDevMode` to `global['ngDevMode']` (and the same for `ngI18nClosureMode`). This indirection prevents the static analysis in Terser from effecting the replacement. PR Close #29929 --- packages/core/src/util/ng_dev_mode.ts | 5 ++++- packages/core/src/util/ng_i18n_closure_mode.ts | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/core/src/util/ng_dev_mode.ts b/packages/core/src/util/ng_dev_mode.ts index 3b5c9308be..10bc750635 100644 --- a/packages/core/src/util/ng_dev_mode.ts +++ b/packages/core/src/util/ng_dev_mode.ts @@ -82,7 +82,10 @@ export function ngDevModeResetPerfCounters(): NgDevModePerfCounters { * The idea is that unless we are doing production build where we explicitly * set `ngDevMode == false` we should be helping the developer by providing * as much early warning and errors as possible. + * + * NOTE: changes to the `ngDevMode` name must be synced with the CLI and + * possibly other third party tooling. Check with them before altering it. */ -if (typeof global['ngDevMode'] === 'undefined' || global['ngDevMode']) { +if (typeof ngDevMode === 'undefined' || ngDevMode) { ngDevModeResetPerfCounters(); } diff --git a/packages/core/src/util/ng_i18n_closure_mode.ts b/packages/core/src/util/ng_i18n_closure_mode.ts index 7ee5abc919..752e4f24bf 100644 --- a/packages/core/src/util/ng_i18n_closure_mode.ts +++ b/packages/core/src/util/ng_i18n_closure_mode.ts @@ -12,7 +12,11 @@ declare global { const ngI18nClosureMode: boolean; } -if (typeof global['ngI18nClosureMode'] === 'undefined') { +/** + * NOTE: changes to the `ngI18nClosureMode` name must be synced with the CLI and + * possibly other third party tooling. Check with them before altering it. + */ +if (typeof ngI18nClosureMode === 'undefined') { // Make sure to refer to ngI18nClosureMode as ['ngI18nClosureMode'] for closure. global['ngI18nClosureMode'] = // TODO(FW-1250): validate that this actually, you know, works.