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(core): update isDevMode to rely on ngDevMode #47475

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions packages/compiler/src/config.ts
Expand Up @@ -12,29 +12,25 @@ 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;

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;
Expand Down
31 changes: 9 additions & 22 deletions packages/core/src/util/is_dev_mode.ts
Expand Up @@ -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;
}
Comment on lines 20 to 22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @alan-agius4. I have a question, so to better understand:
Now isDevMode() explicitly depends on ngDevMode. Does it by any chance help to tree-shake code in custom applications, that is conditionally wrapped with isDevMode()? e.g.

if(isDevMode()){ 
   console.error('Some very long string message in custom app....');
}

If not, would it help to expose ngDevMode in Angular public API, so it could be used it in custom applications?

if(ngDevMode){ 
   console.error('Some very long string message in custom app....');
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested this isDevMode(). It doesn't help tree-shake code.

Screenshot 2022-09-29 at 12 29 55 PM

Screenshot 2022-09-29 at 12 30 09 PM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed this does not aid tree-shaking.

ngDevMode is still considered as a private API, but we do have a project to make this available as a public API.


/**
Expand All @@ -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 {
alan-agius4 marked this conversation as resolved.
Show resolved Hide resolved
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;
}
3 changes: 1 addition & 2 deletions packages/platform-browser-dynamic/src/compiler_factory.ts
Expand Up @@ -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');

Expand Down Expand Up @@ -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,
Expand Down