Skip to content

Commit f25f4e8

Browse files
committed
feat(lazy-loading): showcase lazy-loading & chunking example
Webpack2 chunking & lazy-loading example Closes TrilonIO#9
1 parent a645fd5 commit f25f4e8

File tree

11 files changed

+109
-9
lines changed

11 files changed

+109
-9
lines changed

Client/app/app.routes.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ export const ROUTES: Route[] = [
2020
{ path: 'login', component: LoginComponent },
2121
{ path: 'examples', component: ExamplesComponent },
2222

23+
{ // ** LAZY-LOADING EXAMPLE **
24+
// Notice we don't reference the file anywhere else, imports, declarations, anywhere
25+
// Webpack2 will provide the separate "chunk" due to System.import
26+
path: 'faq',
27+
loadChildren : () => System.import('../containers/+faq/faq.module').then((file: any) => {
28+
// We use .default here since we use `export default`
29+
// in the FAQModule NgModule
30+
return file.default;
31+
})
32+
},
33+
// loadChildren: '../containers/+faq/faq.module#FAQModule' },
34+
2335
// All else fails - go home
2436
{ path: '**', redirectTo: 'home' }
2537
];

Client/app/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ export * from './state/app.reducer';
2020
export * from './state/app-state';
2121
export * from './state/hmr';
2222

23+
export * from './shared-module/base.shared.module';
24+

Client/app/platform-modules/app.browser.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Store, StoreModule } from '@ngrx/store';
55
// for AoT we need to manually split universal packages (/browser & /node)
66
import { UniversalModule, isBrowser, isNode, AUTO_PREBOOT } from 'angular2-universal/browser';
77

8-
import { AppCommonModule } from './common.module';
8+
import { AppCommonModule } from './app.common.module';
99
import { AppComponent } from 'app';
1010
// Universal : XHR Cache
1111
import { CacheService } from 'app-shared';

Client/app/platform-modules/common.module.ts renamed to Client/app/platform-modules/app.common.module.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
/*
2-
* _Common_ NgModule to share between Browser & Server platforms
2+
* _Common_ NgModule to share between our "BASE" App.Browser & App.Server module platforms
33
*
44
* If something belongs to BOTH, just put it Here.
55
* - If you need something to be very "platform"-specific, put it
66
* in the specific one (app.browser or app.server)
77
*/
88

99
import { NgModule } from '@angular/core';
10-
import { CommonModule } from '@angular/common';
11-
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
1210
import { RouterModule } from '@angular/router';
1311

1412
import { Store, StoreModule } from '@ngrx/store';
@@ -17,7 +15,7 @@ import { EffectsModule } from '@ngrx/effects';
1715
import { Ng2BootstrapModule } from 'ng2-bootstrap/ng2-bootstrap';
1816

1917
// Main "APP" Root Component
20-
import { AppComponent, ROUTES, appReducer } from 'app';
18+
import { BaseSharedModule, AppComponent, ROUTES, appReducer } from 'app';
2119

2220
// Component imports
2321
import { NavMenuComponent } from 'app-components';
@@ -47,11 +45,12 @@ import { appState } from 'app';
4745
const MODULES = [
4846
// Do NOT include UniversalModule, HttpModule, or JsonpModule here
4947

48+
// This has ALL the "Common" stuff (CommonModule, FormsModule, ReactiveFormsModule, etc etc)
49+
// You would import this into your child NgModules so you don't need to duplicate so much code
50+
BaseSharedModule,
51+
5052
// Angular
51-
CommonModule,
5253
RouterModule,
53-
FormsModule,
54-
ReactiveFormsModule,
5554

5655
// NgRx
5756
StoreModule.provideStore(appReducer, appState),

Client/app/platform-modules/app.server.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Store, StoreModule } from '@ngrx/store';
66
// for AoT we need to manually split universal packages (/browser & /node)
77
import { UniversalModule, isBrowser, isNode } from 'angular2-universal/node';
88

9-
import { AppCommonModule } from './common.module';
9+
import { AppCommonModule } from './app.common.module';
1010
import { AppComponent } from 'app';
1111
// Universal : XHR Cache
1212
import { CacheService } from 'app-shared';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* --Base-- SHARED Module
3+
*
4+
* This has the most "basic" Shared imports that can be imported into
5+
* all children (lazy-loaded for example) NgModules.
6+
* (ie: Admin NgModule can import this, to import all the basic App functionality, FormsModule, CommonModule etc)
7+
*/
8+
9+
import { NgModule } from '@angular/core';
10+
import { CommonModule } from '@angular/common';
11+
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
12+
13+
@NgModule({
14+
imports: [
15+
CommonModule
16+
],
17+
exports: [
18+
CommonModule, FormsModule, ReactiveFormsModule
19+
]
20+
})
21+
export class BaseSharedModule {
22+
23+
}
24+
25+

Client/components/navmenu/navmenu.component.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
<span class="glyphicon glyphicon-education"></span> RestAPI Demo
2121
</a>
2222
</li>
23+
<li [routerLinkActive]="['link-active']">
24+
<a [routerLink]="['/faq']">
25+
<span class="glyphicon glyphicon-education"></span> FAQ
26+
</a>
27+
</li>
2328
<li *ngIf="!loggedIn$" [routerLinkActive]="['link-active']">
2429
<a [routerLink]="['/login']">
2530
<span class="glyphicon glyphicon-user"></span> Login
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Component, Inject, ChangeDetectionStrategy, ViewEncapsulation, OnInit } from '@angular/core';
2+
3+
@Component({
4+
changeDetection: ChangeDetectionStrategy.Default,
5+
encapsulation: ViewEncapsulation.Emulated,
6+
selector: 'app-faq',
7+
template: `
8+
<h1>FAQ</h1>
9+
<blockquote>
10+
Note: <strong>This component was Lazy-loaded!</strong>
11+
</blockquote>
12+
`
13+
})
14+
export class FAQComponent implements OnInit {
15+
16+
// Use "constructor"s only for dependency injection
17+
constructor() { }
18+
19+
// Here you want to handle anything with @Input()'s @Output()'s
20+
// Data retrieval / etc - this is when the Component is "ready" and wired up
21+
ngOnInit() {
22+
console.log('\n\nFAQ Component lazy-loaded!!\n\n');
23+
}
24+
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Lazy-Loaded Module & Component
3+
* You can see that it wasn't referenced anywhere in the app / modules
4+
* Except for in the app.routes.ts file
5+
*/
6+
7+
import { NgModule } from '@angular/core';
8+
import { RouterModule } from '@angular/router';
9+
10+
import { BaseSharedModule } from 'app';
11+
import { FAQComponent } from './faq.component';
12+
13+
@NgModule({
14+
imports: [
15+
BaseSharedModule,
16+
RouterModule.forChild([
17+
{ path: '', component: FAQComponent }
18+
])
19+
],
20+
declarations: [
21+
FAQComponent
22+
]
23+
})
24+
export default class FAQModule { }

Client/typings.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ declare module 'modern-lru' {
3131
export = x;
3232
}
3333

34+
declare var System: SystemJS;
35+
36+
interface SystemJS {
37+
import: (path?: string) => Promise<any>;
38+
}
39+
3440
// Extra variables that live on Global that will be replaced by webpack DefinePlugin
3541
declare var ENV: string;
3642
declare var HMR: boolean;

0 commit comments

Comments
 (0)