Skip to content

Commit 4b1c334

Browse files
committed
docs(@angular/cli): update hmr documentation
Update documentation for the hmr story to show proper configurations in angular.json and src/tsconfig.app.json. Closes angular#11028, angular#10668, angular#10663
1 parent 7130847 commit 4b1c334

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

docs/documentation/stories/configure-hmr.md

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,39 @@ Next we need to update the bootstrap process of our app to enable the
1515
Create a file called `src/environments/environment.hmr.ts` with the following contents:
1616

1717
```typescript
18-
1918
export const environment = {
20-
production: false,
21-
hmr: true
19+
production: false,
20+
hmr: true
2221
};
2322
```
2423

2524
Update `src/environments/environment.prod.ts` and add the `hmr: false` flag to the environment:
2625

2726
```typescript
2827
export const environment = {
29-
production: true,
30-
hmr: false
28+
production: true,
29+
hmr: false
3130
};
3231
```
3332

3433
Update `src/environments/environment.ts` and add the `hmr: false` flag to the environment:
3534

3635
```typescript
3736
export const environment = {
38-
production: false,
39-
hmr: false
37+
production: false,
38+
hmr: false
4039
};
4140
```
4241

43-
44-
Update `angular.json` to include an hmr environment as explained [here](./application-environments) and add a configuration within serve to enable hmr.
42+
Update `angular.json` to include an hmr environment as explained [here](./application-environments)
43+
and add configurations within build and serve to enable hmr. Note that `<project-name>` here
44+
represents the name of the project you are adding this configuration to in `angular.json`.
4545

4646
```json
47-
"serve": {
48-
"configuration": {
47+
"build": {
48+
"configurations": {
4949
...
5050
"hmr": {
51-
"hmr": true,
5251
"fileReplacements": [
5352
{
5453
"replace": "src/environments/environment.ts",
@@ -57,16 +56,37 @@ Update `angular.json` to include an hmr environment as explained [here](./applic
5756
],
5857
}
5958
}
59+
},
60+
...
61+
"serve": {
62+
"configurations": {
63+
...
64+
"hmr": {
65+
"hmr": true,
66+
"browserTarget": "<project-name>:build:hmr"
67+
}
68+
}
6069
}
6170
```
6271

72+
Add the necessary types to `src/tsconfig.app.json`
73+
74+
````json
75+
{
76+
...
77+
"compilerOptions": {
78+
...
79+
"types": ["node"]
80+
},
81+
}
82+
6383
Run `ng serve` with the flag `--configuration hmr` to enable hmr and select the new environment:
6484

6585
```bash
6686
ng serve --configuration hmr
67-
```
87+
````
6888

69-
Create a shortcut for this by updating `package.json` and adding an entry to the script object:
89+
Create a shortcut for this by updating `package.json` and adding an entry to the script object:
7090

7191
```json
7292
"scripts": {
@@ -75,40 +95,39 @@ Create a shortcut for this by updating `package.json` and adding an entry to th
7595
}
7696
```
7797

78-
7998
### Add dependency for @angularclass/hmr and configure app
8099

81100
In order to get HMR working we need to install the dependency and configure our app to use it.
82101

83-
84102
Install the `@angularclass/hmr` module as a dev-dependency
85103

86104
```bash
87105
$ npm install --save-dev @angularclass/hmr
88106
```
89107

90-
91108
Create a file called `src/hmr.ts` with the following content:
92109

93110
```typescript
94111
import { NgModuleRef, ApplicationRef } from '@angular/core';
95112
import { createNewHosts } from '@angularclass/hmr';
96113

97-
export const hmrBootstrap = (module: any, bootstrap: () => Promise<NgModuleRef<any>>) => {
114+
export const hmrBootstrap = (
115+
module: any,
116+
bootstrap: () => Promise<NgModuleRef<any>>
117+
) => {
98118
let ngModule: NgModuleRef<any>;
99119
module.hot.accept();
100-
bootstrap().then(mod => ngModule = mod);
120+
bootstrap().then((mod) => (ngModule = mod));
101121
module.hot.dispose(() => {
102122
const appRef: ApplicationRef = ngModule.injector.get(ApplicationRef);
103-
const elements = appRef.components.map(c => c.location.nativeElement);
123+
const elements = appRef.components.map((c) => c.location.nativeElement);
104124
const makeVisible = createNewHosts(elements);
105125
ngModule.destroy();
106126
makeVisible();
107127
});
108128
};
109129
```
110130

111-
112131
Update `src/main.ts` to use the file we just created:
113132

114133
```typescript
@@ -127,7 +146,7 @@ if (environment.production) {
127146
const bootstrap = () => platformBrowserDynamic().bootstrapModule(AppModule);
128147

129148
if (environment.hmr) {
130-
if (module[ 'hot' ]) {
149+
if (module['hot']) {
131150
hmrBootstrap(module, bootstrap);
132151
} else {
133152
console.error('HMR is not enabled for webpack-dev-server!');
@@ -138,7 +157,6 @@ if (environment.hmr) {
138157
}
139158
```
140159

141-
142160
### Starting the development environment with HMR enabled
143161

144162
Now that everything is set up we can run the new configuration:
@@ -149,10 +167,6 @@ $ npm run hmr
149167

150168
When starting the server Webpack will tell you that it’s enabled:
151169

152-
153170
NOTICE Hot Module Replacement (HMR) is enabled for the dev server.
154171

155-
156172
Now if you make changes to one of your components they changes should be visible automatically without a complete browser refresh.
157-
158-

0 commit comments

Comments
 (0)