|
| 1 | +# Configure Hot Module Replacement |
| 2 | + |
| 3 | +Hot Module Replacement (HMR) is a WebPack feature to update code in a running app without rebuilding it. |
| 4 | +This results in faster updates and less full page-reloads. |
| 5 | + |
| 6 | +You can read more about HRM by visiting [this page](https://webpack.github.io/docs/hot-module-replacement.html). |
| 7 | + |
| 8 | +In order to get HMR working with Angular CLI we first need to add a new environment and enable it. |
| 9 | + |
| 10 | +Next we need to update the bootstrap process of our app to enable the |
| 11 | +[@angularclass/hmr](https://github.com/AngularClass/angular-hmr) module. |
| 12 | + |
| 13 | +### Add environment for HMR |
| 14 | + |
| 15 | +Create a file called `src/environments/environment.hmr.ts` with the following contents: |
| 16 | + |
| 17 | +```typescript |
| 18 | + |
| 19 | +export const environment = { |
| 20 | + production: false, |
| 21 | + hmr: true |
| 22 | +}; |
| 23 | +``` |
| 24 | + |
| 25 | +Update `src/environments/environment.prod.ts` and add the `hrm: false` flag to the environment: |
| 26 | + |
| 27 | +```typescript |
| 28 | +export const environment = { |
| 29 | + production: true, |
| 30 | + hmr: false |
| 31 | +}; |
| 32 | +``` |
| 33 | + |
| 34 | +Update `src/environments/environment.ts` and add the `hrm: false` flag to the environment: |
| 35 | + |
| 36 | +```typescript |
| 37 | +export const environment = { |
| 38 | + production: false, |
| 39 | + hmr: false |
| 40 | +}; |
| 41 | +``` |
| 42 | + |
| 43 | + |
| 44 | +Update `.angular-cli.json` by adding the new environment the existing environments object: |
| 45 | + |
| 46 | +```json |
| 47 | +"environmentSource": "environments/environment.ts", |
| 48 | +"environments": { |
| 49 | + "dev": "environments/environment.ts", |
| 50 | + "hmr": "environments/environment.hmr.ts", |
| 51 | + "prod": "environments/environment.prod.ts" |
| 52 | +}, |
| 53 | +``` |
| 54 | + |
| 55 | +Run `ng serve` with the flag `--hmr -e=hmr` to enable hmr and select the new environment: |
| 56 | + |
| 57 | +```bash |
| 58 | +ng serve --hmr -e=hmr |
| 59 | +``` |
| 60 | + |
| 61 | +Create a shortcut for this by updating `package.json` and adding an entry to the script object: |
| 62 | + |
| 63 | +```json |
| 64 | +"scripts": { |
| 65 | + ... |
| 66 | + "hmr": "ng serve --hmr -e=hmr" |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | + |
| 71 | +### Add dependency for @angularclass/hmr and configure app |
| 72 | + |
| 73 | +In order to get HMR working we need to install the dependency and configure our app to use it. |
| 74 | + |
| 75 | + |
| 76 | +Install the `@angularclass/hmr` module as a dev-dependency |
| 77 | + |
| 78 | +```bash |
| 79 | +$ npm install --save-dev @angularclass/hmr |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | +Create a file called `src/hmr.ts` with the following content: |
| 84 | + |
| 85 | +```typescript |
| 86 | +import { NgModuleRef, ApplicationRef } from '@angular/core'; |
| 87 | +import { createNewHosts } from '@angularclass/hmr'; |
| 88 | + |
| 89 | +export const hmrBootstrap = (module: any, bootstrap: () => Promise<NgModuleRef<any>>) => { |
| 90 | + let ngModule: NgModuleRef<any>; |
| 91 | + module.hot.accept(); |
| 92 | + bootstrap().then(mod => ngModule = mod); |
| 93 | + module.hot.dispose(() => { |
| 94 | + const appRef: ApplicationRef = ngModule.injector.get(ApplicationRef); |
| 95 | + const elements = appRef.components.map(c => c.location.nativeElement); |
| 96 | + const makeVisible = createNewHosts(elements); |
| 97 | + ngModule.destroy(); |
| 98 | + makeVisible(); |
| 99 | + }); |
| 100 | +}; |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | +Update `src/main.ts` to use the file we just created: |
| 105 | + |
| 106 | +```typescript |
| 107 | +import { enableProdMode } from '@angular/core'; |
| 108 | +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; |
| 109 | + |
| 110 | +import { AppModule } from './app/app.module'; |
| 111 | +import { environment } from './environments/environment'; |
| 112 | + |
| 113 | +import { hmrBootstrap } from './hmr'; |
| 114 | + |
| 115 | +if (environment.production) { |
| 116 | + enableProdMode(); |
| 117 | +} |
| 118 | + |
| 119 | +const bootstrap = () => platformBrowserDynamic().bootstrapModule(AppModule); |
| 120 | + |
| 121 | +if (environment.hmr) { |
| 122 | + if (module[ 'hot' ]) { |
| 123 | + hmrBootstrap(module, bootstrap); |
| 124 | + } else { |
| 125 | + console.error('HMR is not enabled for webpack-dev-server!'); |
| 126 | + console.log('Are you using the --hmr flag for ng serve?'); |
| 127 | + } |
| 128 | +} else { |
| 129 | + bootstrap(); |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | +### Starting the development environment with HMR enabled |
| 135 | + |
| 136 | +Now that everything is set up we can run the new configuration: |
| 137 | + |
| 138 | +```bash |
| 139 | +$ npm run hmr |
| 140 | +``` |
| 141 | + |
| 142 | +When starting the server Webpack will tell you that it’s enabled: |
| 143 | + |
| 144 | + |
| 145 | + NOTICE Hot Module Replacement (HMR) is enabled for the dev server. |
| 146 | + |
| 147 | + |
| 148 | +Now if you make changes to one of your components they changes should be visible automatically without a complete browser refresh. |
| 149 | + |
| 150 | + |
0 commit comments