├── .gitignore ├── LICENSE ├── README.md ├── index.html ├── package.json ├── src ├── app.ts ├── main.ts ├── menu │ ├── index.ts │ └── menu_cmp.ts ├── messages │ ├── index.ts │ ├── message_cmp.ts │ └── messages_cmp.ts ├── repository.ts ├── settings │ ├── index.ts │ ├── page_size_cmp.ts │ └── settings_cmp.ts └── vendor.ts ├── tsconfig.json └── webpack.config.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /dist/ 3 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2010-2016 Google, Inc. http://angularjs.org 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Refactoring an Angular 2 Application to Enable Code Splitting and Lazy Loading 2 | 3 | This repository shows how to migrate an Angular 2 application to use lazy loading and code splitting. It uses WebPack 2.0 and AoT compilation. 4 | 5 | ## How to Run 6 | 7 | - clone this repo 8 | - `npm install` 9 | - `npm start` 10 | - open `localhost:8080` 11 | 12 | - optionally `npm run build` to build bundle 13 | 14 | ## Applicaiton 15 | 16 | ### Modules 17 | 18 | The application has three modules: 19 | 20 | * Main Menu 21 | * Messages 22 | * Settings 23 | 24 | ### Routes 25 | 26 | These are the routes the application supports: 27 | 28 | * / 29 | * /messages/:folder 30 | * /messages/:folder/:id 31 | * /settings 32 | * /settings/pagesize 33 | 34 | ## Refactoring 35 | 36 | The repository has three commits: 37 | 38 | - **everything loaded in a single bundle** loads everything in a single bundle. No lazy loading or code splitting. 39 | - **load settings lazily** extracts SettingsModule into its own bundle. 40 | - **load messages lazily** extracts MessagesModule into its own bundle. 41 | 42 | As you can see extracting a module into its own bundle requires removing an import and updating two lines the configuration. The behavior of the application stays the same, the webpack configuratrion stays the same, no components have to be updated. -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Document 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "LazyLoadingExample", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "webpack-dev-server", 9 | "build": "webpack -p" 10 | }, 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "@angular/common": "2.4.2", 15 | "@angular/compiler": "2.4.2", 16 | "@angular/core": "2.4.2", 17 | "@angular/forms": "2.4.2", 18 | "@angular/platform-browser": "2.4.2", 19 | "@angular/router": "3.4.2", 20 | 21 | "core-js": "^2.4.1", 22 | "rxjs": "5.0.0-rc.1", 23 | "zone.js": "^0.6.12" 24 | }, 25 | "devDependencies": { 26 | "clang-format": "^1.0.32", 27 | 28 | "typescript": "2.0.10", 29 | "webpack": "2.1.0-beta.25", 30 | "webpack-dev-server": "2.1.0-beta.10", 31 | 32 | "@angular/platform-browser-dynamic": "2.4.2", 33 | "@angular/platform-server": "2.4.2", 34 | "@angular/compiler-cli": "2.4.2", 35 | "@ngtools/webpack": "1.2.3" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/app.ts: -------------------------------------------------------------------------------- 1 | import {NgModule, Component} from '@angular/core'; 2 | import {Router, RouterModule} from '@angular/router'; 3 | import {BrowserModule} from '@angular/platform-browser'; 4 | 5 | import {MenuModule, MENU_ROUTES} from './menu'; 6 | import {Repository} from './repository'; 7 | 8 | @Component({ 9 | selector: 'root-cmp', 10 | template: ` 11 | 12 | `, 13 | }) 14 | export class RootCmp {} 15 | 16 | @NgModule({ 17 | imports: [ 18 | BrowserModule, 19 | 20 | MenuModule, 21 | 22 | RouterModule.forRoot([ 23 | ...MENU_ROUTES, 24 | { path: 'messages', loadChildren: './messages/index#MessagesModule' }, 25 | { path: 'settings', loadChildren: './settings/index#SettingsModule' } 26 | ], {enableTracing: true}) 27 | ], 28 | 29 | providers: [Repository], 30 | bootstrap: [RootCmp], 31 | declarations: [RootCmp] 32 | }) 33 | export class AppModule {} -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | // import polyfills 2 | import 'core-js/es7/reflect' 3 | import 'zone.js/dist/zone' 4 | 5 | // import angular2 dpes 6 | import {platformBrowserDynamic} from '@angular/platform-browser-dynamic'; 7 | import {AppModule} from './app'; 8 | 9 | platformBrowserDynamic().bootstrapModule(AppModule); -------------------------------------------------------------------------------- /src/menu/index.ts: -------------------------------------------------------------------------------- 1 | import {NgModule} from '@angular/core'; 2 | import {RouterModule} from '@angular/router'; 3 | import {MenuCmp} from './menu_cmp'; 4 | 5 | export const MENU_ROUTES = [ 6 | { path: '', component: MenuCmp } 7 | ]; 8 | 9 | @NgModule({ 10 | imports: [RouterModule], 11 | declarations: [MenuCmp] 12 | }) 13 | export class MenuModule {} -------------------------------------------------------------------------------- /src/menu/menu_cmp.ts: -------------------------------------------------------------------------------- 1 | import {Component} from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'menu-cmp', 5 | template: ` 6 |

Messages

7 | 12 | ` 13 | }) 14 | export class MenuCmp {} -------------------------------------------------------------------------------- /src/messages/index.ts: -------------------------------------------------------------------------------- 1 | import {NgModule} from '@angular/core'; 2 | import {CommonModule} from '@angular/common'; 3 | import {RouterModule} from '@angular/router'; 4 | import {MessageCmp} from './message_cmp'; 5 | import {MessagesCmp} from './messages_cmp'; 6 | 7 | export const MESSAGES_ROUTES = [ 8 | { path: ':folder', component: MessagesCmp }, 9 | { path: ':folder/:id', component: MessageCmp } 10 | ]; 11 | 12 | @NgModule({ 13 | imports: [CommonModule, RouterModule.forChild(MESSAGES_ROUTES)], 14 | declarations: [MessageCmp, MessagesCmp], 15 | }) 16 | export class MessagesModule {} 17 | -------------------------------------------------------------------------------- /src/messages/message_cmp.ts: -------------------------------------------------------------------------------- 1 | import {Component} from '@angular/core'; 2 | import {ActivatedRoute} from '@angular/router'; 3 | import {Message, Repository} from '../repository'; 4 | 5 | @Component({ 6 | selector: 'message-cmp', 7 | template: ` 8 |

Message {{id}}

9 |

Text: {{message.text}}

10 |
11 | Go to Folder 12 |
13 | ` 14 | }) 15 | export class MessageCmp { 16 | folder: string; 17 | id: number; 18 | message: Message; 19 | 20 | constructor(route: ActivatedRoute, repository: Repository) { 21 | this.folder = route.snapshot.params['folder']; 22 | this.id = +route.snapshot.params['id']; 23 | this.message = repository.message(this.id); 24 | } 25 | } -------------------------------------------------------------------------------- /src/messages/messages_cmp.ts: -------------------------------------------------------------------------------- 1 | import {Component} from '@angular/core'; 2 | import {ActivatedRoute} from '@angular/router'; 3 | import {Message, Repository} from '../repository'; 4 | 5 | @Component({ 6 | selector: 'message-cmp', 7 | template: ` 8 |

Messages

9 | 14 | Change Page Size 15 | Back 16 | ` 17 | }) 18 | export class MessagesCmp { 19 | folder: string; 20 | messages: Message[]; 21 | 22 | constructor(route: ActivatedRoute, repository: Repository) { 23 | this.folder = route.snapshot.params['folder']; 24 | this.messages = repository.messagesFor(this.folder); 25 | } 26 | } -------------------------------------------------------------------------------- /src/repository.ts: -------------------------------------------------------------------------------- 1 | export type Message = { 2 | id: number; 3 | folder: string; 4 | text: string; 5 | }; 6 | 7 | export class Repository { 8 | pageSize: {[k:string]:number} = {inbox: 10, draft: 10}; 9 | 10 | messages: Message[] = [ 11 | {id: 1, folder: 'inbox', text: 'Message 1 from Inbox'}, 12 | {id: 2, folder: 'inbox', text: 'Message 2 from Inbox'}, 13 | {id: 3, folder: 'inbox', text: 'Message 3 from Inbox'}, 14 | {id: 4, folder: 'inbox', text: 'Message 4 from Inbox'}, 15 | {id: 5, folder: 'inbox', text: 'Message 5 from Inbox'}, 16 | {id: 6, folder: 'inbox', text: 'Message 6 from Inbox'}, 17 | {id: 7, folder: 'inbox', text: 'Message 7 from Inbox'}, 18 | {id: 8, folder: 'inbox', text: 'Message 8 from Inbox'}, 19 | {id: 9, folder: 'inbox', text: 'Message 9 from Inbox'}, 20 | {id: 10, folder: 'inbox', text: 'Message 10 from Inbox'}, 21 | {id: 11, folder: 'inbox', text: 'Message 11 from Inbox'}, 22 | {id: 12, folder: 'inbox', text: 'Message 12 from Inbox'}, 23 | {id: 13, folder: 'inbox', text: 'Message 13 from Inbox'}, 24 | {id: 14, folder: 'inbox', text: 'Message 14 from Inbox'}, 25 | {id: 15, folder: 'inbox', text: 'Message 15 from Inbox'}, 26 | 27 | {id: 16, folder: 'draft', text: 'Message 1 from Drafts'}, 28 | {id: 17, folder: 'draft', text: 'Message 2 from Drafts'}, 29 | {id: 18, folder: 'draft', text: 'Message 3 from Drafts'}, 30 | {id: 19, folder: 'draft', text: 'Message 4 from Drafts'}, 31 | {id: 20, folder: 'draft', text: 'Message 5 from Drafts'}, 32 | ]; 33 | 34 | get folders(): string[] { 35 | return ['inbox', 'draft']; 36 | } 37 | 38 | messagesFor(folder: string): Message[] { 39 | const filtered = this.messages.filter(m => m.folder === folder); 40 | return filtered.slice(0, this.pageSize[folder]); 41 | } 42 | 43 | message(id: number): Message { 44 | return this.messages.filter(m => m.id === id)[0]; 45 | } 46 | } -------------------------------------------------------------------------------- /src/settings/index.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { CommonModule } from '@angular/common'; 3 | import { RouterModule } from '@angular/router'; 4 | import { FormsModule } from '@angular/forms'; 5 | import { SettingsCmp } from './settings_cmp'; 6 | import { PageSizeCmp } from './page_size_cmp'; 7 | 8 | export const SETTINGS_ROUTES = [ 9 | { path: '', component: SettingsCmp }, 10 | { path: 'pagesize', component: PageSizeCmp } 11 | ]; 12 | 13 | @NgModule({ 14 | imports: [ 15 | CommonModule, 16 | FormsModule, 17 | RouterModule.forChild(SETTINGS_ROUTES) 18 | ], 19 | declarations: [SettingsCmp, PageSizeCmp] 20 | }) 21 | export class SettingsModule { } -------------------------------------------------------------------------------- /src/settings/page_size_cmp.ts: -------------------------------------------------------------------------------- 1 | import {Component} from '@angular/core'; 2 | import {Repository} from '../repository' 3 | 4 | @Component({ 5 | selector: 'page-settings', 6 | template: ` 7 |

Page Size Settings

8 | 9 |
10 | Page size for '{{p}}' (open) 11 |
12 | 13 | Back 14 | ` 15 | }) 16 | export class PageSizeCmp { 17 | constructor(public repository: Repository) {} 18 | } -------------------------------------------------------------------------------- /src/settings/settings_cmp.ts: -------------------------------------------------------------------------------- 1 | import {Component} from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'settings', 5 | template: ` 6 |

Settings

7 | Page Size Settings 8 | Back 9 | ` 10 | }) 11 | export class SettingsCmp { 12 | } -------------------------------------------------------------------------------- /src/vendor.ts: -------------------------------------------------------------------------------- 1 | import '@angular/core'; 2 | import '@angular/forms'; 3 | import '@angular/router'; 4 | import '@angular/platform-browser'; -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "es2015", 4 | "moduleResolution": "node", 5 | "target": "es5", 6 | "noImplicitAny": false, 7 | "sourceMap": true, 8 | "mapRoot": "", 9 | "emitDecoratorMetadata": true, 10 | "experimentalDecorators": true, 11 | "lib": [ 12 | "es2015", 13 | "dom" 14 | ], 15 | "outDir": "dist", 16 | "skipLibCheck": true, 17 | "rootDir": "." 18 | }, 19 | "angularCompilerOptions": { 20 | "genDir": "./dist/aot" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const ngtools = require('@ngtools/webpack'); 3 | const webpack = require("webpack"); 4 | const CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin; 5 | 6 | module.exports = { 7 | devtool: 'sourcemap', 8 | resolve: { 9 | extensions: ['.ts', '.js'] 10 | }, 11 | entry: { 12 | main: './src/main.ts', 13 | vendor: './src/vendor.ts' 14 | }, 15 | output: { 16 | path: path.join(process.cwd(), 'dist'), 17 | publicPath: 'dist/', 18 | filename: "[name].js" 19 | }, 20 | plugins: [ 21 | new ngtools.AotPlugin({ 22 | tsConfigPath: './tsconfig.json', 23 | mainPath: "./src/main.ts" 24 | }), 25 | new CommonsChunkPlugin({ 26 | name: 'vendor' 27 | }), 28 | new CommonsChunkPlugin({ 29 | async: true 30 | }) 31 | ], 32 | module: { 33 | loaders: [ 34 | { 35 | test: /\.ts$/, 36 | loader: '@ngtools/webpack' 37 | }, 38 | { 39 | test: /\.css$/, 40 | loader: 'style-loader' 41 | } 42 | ] 43 | }, 44 | 45 | devServer: { 46 | historyApiFallback: true 47 | } 48 | }; --------------------------------------------------------------------------------