Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fcebc83

Browse files
JoostKalxhub
authored andcommitted
fix(core): do not error when ngDevMode is undeclared (#39415)
In production mode, the `ngDevMode` global may not have been declared. This is typically not a problem, as optimizers should have removed all usages of the `ngDevMode` variables. This does however require the bundler/optimizer to have been configured in a certain way, as to allow for `ngDevMode` guarded code to be removed. As an example, Terser can be configured to remove the `ngDevMode` guarded code using the following configuration: ```js const terserOptions = { // ... compress: { // ... global_defs: require('@angular/compiler-cli').GLOBAL_DEFS_FOR_TERSER, } } ``` (Taken from #31595 (comment)) If this is not done, however, the bundle should still work (albeit with larger code size due to missed tree-shaking opportunities). This commit adds a check for whether `ngDevMode` has been declared, as it is a top-level statement that executes before `ngDevMode` has been initialized. Fixes #31595 PR Close #39415
1 parent b33956b commit fcebc83

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

‎integration/side-effects/snapshots/core/esm2015.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ const __global = "undefined" !== typeof global && global;
1212

1313
const _global = __globalThis || __global || __window || __self;
1414

15-
if (ngDevMode) _global.$localize = _global.$localize || function() {
15+
if ("undefined" !== typeof ngDevMode && ngDevMode) _global.$localize = _global.$localize || function() {
1616
throw new Error("It looks like your application or one of its dependencies is using i18n.\n" + "Angular 9 introduced a global `$localize()` function that needs to be loaded.\n" + "Please run `ng add @angular/localize` from the Angular CLI.\n" + "(For non-CLI projects, add `import '@angular/localize/init';` to your `polyfills.ts` file.\n" + "For server-side rendering applications add the import to your `main.server.ts` file.)");
1717
};

‎packages/core/src/core.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export {Sanitizer} from './sanitization/sanitizer';
3838
export * from './codegen_private_exports';
3939

4040
import {global} from './util/global';
41-
if (ngDevMode) {
41+
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
4242
// This helper is to give a reasonable error message to people upgrading to v9 that have not yet
4343
// installed `@angular/localize` in their app.
4444
// tslint:disable-next-line: no-toplevel-property-access

0 commit comments

Comments
 (0)