From 85330f3fd9ac6381ce3aa18479ed8195d2ac215e Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 23 Sep 2022 09:50:09 +0000 Subject: [PATCH] fix(core): update `isDevMode` to rely on `ngDevMode` (#47475) This commits update `isDevMode` to rely on the `ngDevMode` which in the CLI is set by the bundler. We also update `@angular/platform-dynamic-browser` and `@angular/compiler` to remove usage of `jitDevMode`, with this change we remove all internal usages of `isDevMode`. PR Close #47475 --- packages/compiler/src/config.ts | 4 --- packages/core/src/util/is_dev_mode.ts | 31 ++++++------------- .../src/compiler_factory.ts | 3 +- 3 files changed, 10 insertions(+), 28 deletions(-) diff --git a/packages/compiler/src/config.ts b/packages/compiler/src/config.ts index bb651366cdf0e..5f36f2f7ef686 100644 --- a/packages/compiler/src/config.ts +++ b/packages/compiler/src/config.ts @@ -12,7 +12,6 @@ import {noUndefined} from './util'; export class CompilerConfig { public defaultEncapsulation: ViewEncapsulation|null; public useJit: boolean; - public jitDevMode: boolean; public missingTranslation: MissingTranslationStrategy|null; public preserveWhitespaces: boolean; public strictInjectionParameters: boolean; @@ -20,21 +19,18 @@ export class CompilerConfig { constructor({ defaultEncapsulation = ViewEncapsulation.Emulated, useJit = true, - jitDevMode = false, missingTranslation = null, preserveWhitespaces, strictInjectionParameters }: { defaultEncapsulation?: ViewEncapsulation, useJit?: boolean, - jitDevMode?: boolean, missingTranslation?: MissingTranslationStrategy|null, preserveWhitespaces?: boolean, strictInjectionParameters?: boolean, } = {}) { this.defaultEncapsulation = defaultEncapsulation; this.useJit = !!useJit; - this.jitDevMode = !!jitDevMode; this.missingTranslation = missingTranslation; this.preserveWhitespaces = preserveWhitespacesDefault(noUndefined(preserveWhitespaces)); this.strictInjectionParameters = strictInjectionParameters === true; diff --git a/packages/core/src/util/is_dev_mode.ts b/packages/core/src/util/is_dev_mode.ts index ad8184c92272f..4164aaf9ab778 100644 --- a/packages/core/src/util/is_dev_mode.ts +++ b/packages/core/src/util/is_dev_mode.ts @@ -9,27 +9,16 @@ import {global} from './global'; /** - * This file is used to control if the default rendering pipeline should be `ViewEngine` or `Ivy`. + * Returns whether Angular is in development mode. * - * For more information on how to run and debug tests with either Ivy or View Engine (legacy), - * please see [BAZEL.md](./docs/BAZEL.md). - */ - -let _devMode: boolean = true; -let _runModeLocked: boolean = false; - - -/** - * Returns whether Angular is in development mode. After called once, - * the value is locked and won't change any more. - * - * By default, this is true, unless a user calls `enableProdMode` before calling this. + * By default, this is true, unless `enableProdMode` is invoked prior to calling this method or the + * application is built using the Angular CLI with the `optimization` option. + * @see {@link cli/build ng build} * * @publicApi */ export function isDevMode(): boolean { - _runModeLocked = true; - return _devMode; + return typeof ngDevMode === 'undefined' || !!ngDevMode; } /** @@ -40,18 +29,16 @@ export function isDevMode(): boolean { * does not result in additional changes to any bindings (also known as * unidirectional data flow). * + * Using this method is discouraged as the Angular CLI will set production mode when using the + * `optimization` option. + * @see {@link cli/build ng build} + * * @publicApi */ export function enableProdMode(): void { - if (_runModeLocked) { - throw new Error('Cannot enable prod mode after platform setup.'); - } - // The below check is there so when ngDevMode is set via terser // `global['ngDevMode'] = false;` is also dropped. if (typeof ngDevMode === undefined || !!ngDevMode) { global['ngDevMode'] = false; } - - _devMode = false; } diff --git a/packages/platform-browser-dynamic/src/compiler_factory.ts b/packages/platform-browser-dynamic/src/compiler_factory.ts index 5d4a4086b2ff9..6d63e4ac7013c 100644 --- a/packages/platform-browser-dynamic/src/compiler_factory.ts +++ b/packages/platform-browser-dynamic/src/compiler_factory.ts @@ -7,7 +7,7 @@ */ import {CompilerConfig} from '@angular/compiler'; -import {Compiler, CompilerFactory, CompilerOptions, InjectionToken, Injector, isDevMode, MissingTranslationStrategy, PACKAGE_ROOT_URL, StaticProvider, ViewEncapsulation} from '@angular/core'; +import {Compiler, CompilerFactory, CompilerOptions, InjectionToken, Injector, MissingTranslationStrategy, PACKAGE_ROOT_URL, StaticProvider, ViewEncapsulation} from '@angular/core'; export const ERROR_COLLECTOR_TOKEN = new InjectionToken('ErrorCollector'); @@ -52,7 +52,6 @@ export class JitCompilerFactory implements CompilerFactory { // let explicit values from the compiler options overwrite options // from the app providers useJit: opts.useJit, - jitDevMode: isDevMode(), // let explicit values from the compiler options overwrite options // from the app providers defaultEncapsulation: opts.defaultEncapsulation,