Skip to content

Commit 88523c3

Browse files
crisbetojelbourn
authored andcommitted
build: lazily compile examples in dev app (#14522)
Currently we compile all example up-front which adds a bit to the scripting time in the dev app, even if the example page hasn't been visited. These changes switch to only rendering the examples that are active on the current page.
1 parent 48d6907 commit 88523c3

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/dev-app/dev-app-module.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ import {LayoutModule} from '@angular/cdk/layout';
1010
import {FullscreenOverlayContainer, OverlayContainer} from '@angular/cdk/overlay';
1111
import {CommonModule} from '@angular/common';
1212
import {HttpClientModule} from '@angular/common/http';
13-
import {Injector, NgModule} from '@angular/core';
14-
import {createCustomElement} from '@angular/elements';
13+
import {NgModule} from '@angular/core';
1514
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
16-
import {EXAMPLE_COMPONENTS, ExampleModule} from '@angular/material-examples';
15+
import {ExampleModule} from '@angular/material-examples';
1716
import {BrowserModule} from '@angular/platform-browser';
1817
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
1918
import {RouterModule} from '@angular/router';
@@ -152,13 +151,4 @@ import {VirtualScrollDemo} from './virtual-scroll/virtual-scroll-demo';
152151
],
153152
bootstrap: [DevAppComponent],
154153
})
155-
export class DevAppModule {
156-
157-
constructor(injector: Injector) {
158-
// Register examples as custom elements so that they can be inserted into the DOM dynamically
159-
Object.keys(EXAMPLE_COMPONENTS).forEach(key => {
160-
const element = createCustomElement(EXAMPLE_COMPONENTS[key].component, {injector});
161-
customElements.define(key, element);
162-
});
163-
}
164-
}
154+
export class DevAppModule {}

src/dev-app/example/example-list.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Component, Input} from '@angular/core';
9+
import {Component, Input, SimpleChanges, OnChanges, Injector} from '@angular/core';
1010
import {EXAMPLE_COMPONENTS} from '@angular/material-examples';
1111
import {coerceBooleanProperty} from '@angular/cdk/coercion';
12+
import {createCustomElement} from '@angular/elements';
1213

1314
/** Displays a set of material examples in a mat-accordion. */
1415
@Component({
@@ -51,7 +52,10 @@ import {coerceBooleanProperty} from '@angular/cdk/coercion';
5152
}
5253
`]
5354
})
54-
export class ExampleList {
55+
export class ExampleList implements OnChanges {
56+
/** Keeps track of the example ids that have been compiled already. */
57+
private static _compiledComponents = new Set<string>();
58+
5559
/** Type of examples being displayed. */
5660
@Input() type: string;
5761

@@ -64,4 +68,20 @@ export class ExampleList {
6468
_expandAll: boolean;
6569

6670
exampleComponents = EXAMPLE_COMPONENTS;
71+
72+
constructor(private _injector: Injector) {}
73+
74+
ngOnChanges(changes: SimpleChanges) {
75+
if (changes.ids) {
76+
(changes.ids.currentValue as string[])
77+
.filter(id => !ExampleList._compiledComponents.has(id))
78+
.forEach(id => {
79+
const element = createCustomElement(EXAMPLE_COMPONENTS[id].component, {
80+
injector: this._injector
81+
});
82+
customElements.define(id, element);
83+
ExampleList._compiledComponents.add(id);
84+
});
85+
}
86+
}
6787
}

0 commit comments

Comments
 (0)