Skip to content

Commit

Permalink
perf(forms): use ngDevMode to tree-shake _ngModelWarning (#39964)
Browse files Browse the repository at this point in the history
This commit adds `ngDevMode` guard to call `_ngModelWarning` only
in dev mode (similar to how things work in other parts of Ivy runtime code).
The `ngDevMode` flag helps to tree-shake this function from production builds
(since it will act as no-op, in dev mode everything will work as it works right now)
to decrease production bundle size.

PR Close #39964
  • Loading branch information
arturovt authored and mhevery committed Dec 5, 2020
1 parent 9ebe423 commit 7954c8d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
Expand Up @@ -125,7 +125,9 @@ export class FormControlDirective extends NgControl implements OnChanges {
this.form.updateValueAndValidity({emitEvent: false});
}
if (isPropertyUpdated(changes, this.viewModel)) {
_ngModelWarning('formControl', FormControlDirective, this, this._ngModelWarningConfig);
if (typeof ngDevMode === 'undefined' || ngDevMode) {
_ngModelWarning('formControl', FormControlDirective, this, this._ngModelWarningConfig);
}
this.form.setValue(this.model);
this.viewModel = this.model;
}
Expand Down
Expand Up @@ -145,7 +145,9 @@ export class FormControlName extends NgControl implements OnChanges, OnDestroy {
ngOnChanges(changes: SimpleChanges) {
if (!this._added) this._setUpControl();
if (isPropertyUpdated(changes, this.viewModel)) {
_ngModelWarning('formControlName', FormControlName, this, this._ngModelWarningConfig);
if (typeof ngDevMode === 'undefined' || ngDevMode) {
_ngModelWarning('formControlName', FormControlName, this, this._ngModelWarningConfig);
}
this.viewModel = this.model;
this.formDirective.updateModel(this, this.model);
}
Expand Down
8 changes: 2 additions & 6 deletions packages/forms/src/directives/shared.ts
Expand Up @@ -6,8 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/

import {isDevMode} from '@angular/core';

import {AbstractControl, FormArray, FormControl, FormGroup} from '../model';
import {getControlAsyncValidators, getControlValidators, mergeValidators} from '../validators';

Expand Down Expand Up @@ -324,13 +322,11 @@ export function removeListItem<T>(list: T[], el: T): void {
export function _ngModelWarning(
name: string, type: {_ngModelWarningSentOnce: boolean},
instance: {_ngModelWarningSent: boolean}, warningConfig: string|null) {
if (!isDevMode() || warningConfig === 'never') return;
if (warningConfig === 'never') return;

if (((warningConfig === null || warningConfig === 'once') && !type._ngModelWarningSentOnce) ||
(warningConfig === 'always' && !instance._ngModelWarningSent)) {
if (typeof ngDevMode === 'undefined' || ngDevMode) {
ReactiveErrors.ngModelWarning(name);
}
ReactiveErrors.ngModelWarning(name);
type._ngModelWarningSentOnce = true;
instance._ngModelWarningSent = true;
}
Expand Down

0 comments on commit 7954c8d

Please sign in to comment.