Skip to content

Commit 79e4d28

Browse files
authored
perf: allow assertions to be removed in production mode (#20146)
Wraps all assertions in `ngDevMode` checks so that they can be stripped away in production mode. Furthermore, changes all the places where we were using the old `isDevMode` check.
1 parent f343ded commit 79e4d28

File tree

138 files changed

+453
-347
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+453
-347
lines changed

src/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ load("@npm_bazel_typescript//:index.bzl", "ts_config")
22
load("//src/cdk:config.bzl", "CDK_ENTRYPOINTS")
33
load("//src/material:config.bzl", "MATERIAL_ENTRYPOINTS", "MATERIAL_TESTING_ENTRYPOINTS")
44
load("//tools/dgeni:index.bzl", "dgeni_api_docs")
5+
load("//tools:defaults.bzl", "ts_library")
56

67
package(default_visibility = ["//visibility:public"])
78

@@ -43,3 +44,8 @@ dgeni_api_docs(
4344
},
4445
tags = ["docs-package"],
4546
)
47+
48+
ts_library(
49+
name = "dev_mode_types",
50+
srcs = ["dev-mode-types.d.ts"],
51+
)

src/cdk-experimental/combobox/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ng_module(
1010
),
1111
module_name = "@angular/cdk-experimental/combobox",
1212
deps = [
13+
"//src:dev_mode_types",
1314
"//src/cdk/a11y",
1415
"//src/cdk/bidi",
1516
"//src/cdk/collections",

src/cdk-experimental/combobox/combobox.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
Directive,
1616
ElementRef,
1717
EventEmitter,
18-
Input, isDevMode,
18+
Input,
1919
OnDestroy,
2020
Optional,
2121
Output, ViewContainerRef
@@ -273,7 +273,8 @@ export class CdkCombobox<T = unknown> implements OnDestroy, AfterContentInit {
273273

274274
private _coerceOpenActionProperty(input: string | OpenAction[]): OpenAction[] {
275275
let actions = typeof input === 'string' ? input.trim().split(/[ ,]+/) : input;
276-
if (isDevMode() && actions.some(a => allowedOpenActions.indexOf(a) === -1)) {
276+
if ((typeof ngDevMode === 'undefined' || ngDevMode) &&
277+
actions.some(a => allowedOpenActions.indexOf(a) === -1)) {
277278
throw Error(`${input} is not a support open action for CdkCombobox`);
278279
}
279280
return actions as OpenAction[];

src/cdk-experimental/dialog/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ng_module(
1111
assets = [":dialog-container.css"] + glob(["**/*.html"]),
1212
module_name = "@angular/cdk-experimental/dialog",
1313
deps = [
14+
"//src:dev_mode_types",
1415
"//src/cdk/a11y",
1516
"//src/cdk/bidi",
1617
"//src/cdk/keycodes",

src/cdk-experimental/dialog/dialog-container.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
171171
* @param portal Portal to be attached as the dialog content.
172172
*/
173173
attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T> {
174-
if (this._portalHost.hasAttached()) {
174+
if (this._portalHost.hasAttached() && (typeof ngDevMode === 'undefined' || ngDevMode)) {
175175
throwDialogContentAlreadyAttachedError();
176176
}
177177

@@ -183,7 +183,7 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
183183
* @param portal Portal to be attached as the dialog content.
184184
*/
185185
attachTemplatePortal<C>(portal: TemplatePortal<C>): EmbeddedViewRef<C> {
186-
if (this._portalHost.hasAttached()) {
186+
if (this._portalHost.hasAttached() && (typeof ngDevMode === 'undefined' || ngDevMode)) {
187187
throwDialogContentAlreadyAttachedError();
188188
}
189189

@@ -197,7 +197,7 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
197197
* @breaking-change 10.0.0
198198
*/
199199
attachDomPortal = (portal: DomPortal) => {
200-
if (this._portalHost.hasAttached()) {
200+
if (this._portalHost.hasAttached() && (typeof ngDevMode === 'undefined' || ngDevMode)) {
201201
throwDialogContentAlreadyAttachedError();
202202
}
203203

src/cdk-experimental/dialog/dialog.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class Dialog implements OnDestroy {
106106
openFromComponent<T>(component: ComponentType<T>, config?: DialogConfig): DialogRef<any> {
107107
config = this._applyConfigDefaults(config);
108108

109-
if (config.id && this.getById(config.id)) {
109+
if (config.id && this.getById(config.id) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
110110
throw Error(`Dialog with id "${config.id}" exists already. The dialog id must be unique.`);
111111
}
112112

@@ -125,7 +125,7 @@ export class Dialog implements OnDestroy {
125125
openFromTemplate<T>(template: TemplateRef<T>, config?: DialogConfig): DialogRef<any> {
126126
config = this._applyConfigDefaults(config);
127127

128-
if (config.id && this.getById(config.id)) {
128+
if (config.id && this.getById(config.id) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
129129
throw Error(`Dialog with id "${config.id}" exists already. The dialog id must be unique.`);
130130
}
131131

src/cdk-experimental/menu/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ng_module(
1010
),
1111
module_name = "@angular/cdk-experimental/menu",
1212
deps = [
13+
"//src:dev_mode_types",
1314
"//src/cdk/a11y",
1415
"//src/cdk/bidi",
1516
"//src/cdk/coercion",

src/cdk-experimental/menu/context-menu.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
Inject,
1818
Injectable,
1919
InjectionToken,
20-
isDevMode,
2120
} from '@angular/core';
2221
import {DOCUMENT} from '@angular/common';
2322
import {Directionality} from '@angular/cdk/bidi';
@@ -110,9 +109,7 @@ export class CdkContextMenuTrigger implements OnDestroy {
110109
return this._menuPanel;
111110
}
112111
set menuPanel(panel: CdkMenuPanel) {
113-
// If the provided panel already has a stack, that means it already has a trigger configured
114-
// TODO refactor once https://github.com/angular/components/pull/20146 lands
115-
if (isDevMode() && panel._menuStack) {
112+
if ((typeof ngDevMode === 'undefined' || ngDevMode) && panel._menuStack) {
116113
throwExistingMenuStackError();
117114
}
118115
this._menuPanel = panel;

src/cdk-experimental/menu/menu-item-trigger.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
Inject,
1717
OnDestroy,
1818
Optional,
19-
isDevMode,
2019
} from '@angular/core';
2120
import {Directionality} from '@angular/cdk/bidi';
2221
import {TemplatePortal} from '@angular/cdk/portal';
@@ -64,8 +63,7 @@ export class CdkMenuItemTrigger implements OnDestroy {
6463
// If the provided panel already has a stack, that means it already has a trigger configured.
6564
// Note however that there are some edge cases where two triggers **may** share the same menu,
6665
// e.g. two triggers in two separate menus.
67-
// TODO refactor once https://github.com/angular/components/pull/20146 lands
68-
if (isDevMode() && panel?._menuStack) {
66+
if ((typeof ngDevMode === 'undefined' || ngDevMode) && panel?._menuStack) {
6967
throwExistingMenuStackError();
7068
}
7169

src/cdk-experimental/scrolling/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ng_module(
1111
),
1212
module_name = "@angular/cdk-experimental/scrolling",
1313
deps = [
14+
"//src:dev_mode_types",
1415
"//src/cdk/coercion",
1516
"//src/cdk/collections",
1617
"//src/cdk/scrolling",

0 commit comments

Comments
 (0)