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): do not error when ngDevMode is undeclared #39415

Closed
wants to merge 1 commit into from

Conversation

JoostK
Copy link
Member

@JoostK JoostK commented Oct 24, 2020

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:

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

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 angular#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 angular#31595
@JoostK JoostK added type: bug/fix freq1: low area: core Issues related to the framework runtime target: patch This PR is targeted for the next patch release labels Oct 24, 2020
@google-cla google-cla bot added the cla: yes label Oct 24, 2020
@ngbot ngbot bot modified the milestone: needsTriage Oct 24, 2020
@JoostK JoostK marked this pull request as ready for review October 25, 2020 10:34
@@ -38,7 +38,7 @@ export {Sanitizer} from './sanitization/sanitizer';
export * from './codegen_private_exports';

import {global} from './util/global';
if (ngDevMode) {
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
Copy link
Member

Choose a reason for hiding this comment

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

Drive-by observation: the typeof check is something that can easily be missed. Maybe we can add a custom lint rule to check for it?

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree this is indeed error-prone and easy to regress in some way or another. There are however plenty of bare reads of ngDevMode in places that can depend on initNgDevMode to have been executed. A potential lint rule would therefore likely have to only consider top-level statements or something like that, which would also make it not entirely fool proof.

Copy link
Member

@josephperrott josephperrott left a comment

Choose a reason for hiding this comment

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

LGTM

@JoostK JoostK added the action: merge The PR is ready for merge by the caretaker label Oct 26, 2020
alxhub pushed a commit that referenced this pull request Oct 27, 2020
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
alxhub pushed a commit that referenced this pull request Oct 27, 2020
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
@alxhub alxhub closed this in 3c6edcd Oct 27, 2020
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime cla: yes freq1: low target: patch This PR is targeted for the next patch release type: bug/fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ReferenceError: ngDevMode is not defined
5 participants