Skip to content

Commit 0d7db6d

Browse files
committed
perf: allow assertions to be removed in production mode
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 23d3c21 commit 0d7db6d

File tree

127 files changed

+441
-325
lines changed

Some content is hidden

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

127 files changed

+441
-325
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/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/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",

src/cdk-experimental/scrolling/auto-size-virtual-scroll.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ export class AutoSizeVirtualScrollStrategy implements VirtualScrollStrategy {
7272
/** @docs-private Implemented as part of VirtualScrollStrategy. */
7373
scrolledIndexChange = new Observable<number>(() => {
7474
// TODO(mmalerba): Implement.
75-
throw Error('cdk-virtual-scroll: scrolledIndexChange is currently not supported for the' +
76-
' autosize scroll strategy');
75+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
76+
throw Error('cdk-virtual-scroll: scrolledIndexChange is currently not supported for the' +
77+
' autosize scroll strategy');
78+
}
7779
});
7880

7981
/** The attached viewport. */
@@ -164,9 +166,11 @@ export class AutoSizeVirtualScrollStrategy implements VirtualScrollStrategy {
164166

165167
/** Scroll to the offset for the given index. */
166168
scrollToIndex(): void {
167-
// TODO(mmalerba): Implement.
168-
throw Error('cdk-virtual-scroll: scrollToIndex is currently not supported for the autosize'
169-
+ ' scroll strategy');
169+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
170+
// TODO(mmalerba): Implement.
171+
throw Error('cdk-virtual-scroll: scrollToIndex is currently not supported for the autosize'
172+
+ ' scroll strategy');
173+
}
170174
}
171175

172176
/**

src/cdk-experimental/tsconfig-tests.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
},
2222
"include": [
2323
"**/index.ts",
24-
"**/*.spec.ts"
24+
"**/*.spec.ts",
25+
"../dev-mode-types.d.ts"
2526
],
2627
"exclude": [
2728
"**/*.e2e.spec.ts"

src/cdk-experimental/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"@angular/cdk-experimental/*": ["../cdk-experimental/*"]
1010
}
1111
},
12-
"include": ["./**/*.ts"]
12+
"include": ["./**/*.ts", "../dev-mode-types.d.ts"]
1313
}

src/cdk/a11y/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ng_module(
1818
),
1919
module_name = "@angular/cdk/a11y",
2020
deps = [
21+
"//src:dev_mode_types",
2122
"//src/cdk/coercion",
2223
"//src/cdk/keycodes",
2324
"//src/cdk/observers",

0 commit comments

Comments
 (0)