Skip to content

Commit aa139a4

Browse files
authored
docs: improve HMR example (#3950)
1 parent 4afc727 commit aa139a4

File tree

8 files changed

+84
-23
lines changed

8 files changed

+84
-23
lines changed

Diff for: examples/hmr/README.md renamed to examples/hmr/boolean/README.md

+7-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Hot Module Reloading
22

3-
Hot Module Replacement (HMR) exchanges, adds, or removes modules while an
4-
application is running, without a full reload of the page.
3+
Hot Module Replacement (HMR) exchanges, adds, or removes modules while an application is running, without a full reload of the page.
54

65
## true
76

@@ -44,43 +43,29 @@ In the devtools console you should see:
4443
You should also see the text on the page itself change to match your edits in
4544
`example.js`.
4645

47-
## only
46+
## false
4847

49-
Enables Hot Module Replacement without page refresh as a fallback in case of build failures.
48+
Disable webpack's [Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/) feature:
5049

5150
**webpack.config.js**
5251

5352
```js
5453
module.exports = {
5554
// ...
5655
devServer: {
57-
hot: "only",
56+
hot: false,
5857
},
5958
};
6059
```
6160

6261
Usage via CLI:
6362

6463
```console
65-
npx webpack serve --open --hot only
64+
npx webpack serve --open --no-hot
6665
```
6766

6867
## What Should Happen
6968

7069
1. The script should open `http://localhost:8080/` in your default browser.
71-
2. In your editor, open `example.js` and change `const` keyword to `constt` to cause build error.
72-
3. Open the console in your browser's devtools.
73-
4. Revert the change in step 2 and change any part of the `innerHTML` string.
74-
75-
In the devtools console you should see:
76-
77-
```
78-
[webpack-dev-server] App updated. Recompiling...
79-
[webpack-dev-server] App hot update...
80-
[HMR] Checking for updates on the server...
81-
⚠️ Ignored an update to unaccepted module ./example.js -> ./app.js
82-
[HMR] Nothing hot Updated.
83-
[HMR] App is up to date.
84-
```
85-
86-
5. Refresh the page and see the text on the page itself change to match your edits in `example.js`.
70+
2. In your editor, open `example.js` and change any part of the `innerHTML` string.
71+
3. text on the page shouldn't change itself to match your edits in `example.js`, without reloading the page.
File renamed without changes.
File renamed without changes.

Diff for: examples/hmr/webpack.config.js renamed to examples/hmr/boolean/webpack.config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
// our setup function adds behind-the-scenes bits to the config that all of our
44
// examples need
5-
const { setup } = require("../util");
5+
const { setup } = require("../../util");
66

77
module.exports = setup({
88
context: __dirname,
99
entry: "./app.js",
10+
devServer: {
11+
hot: true,
12+
},
1013
});

Diff for: examples/hmr/only/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Hot Module Reloading
2+
3+
Hot Module Replacement (HMR) exchanges, adds, or removes modules while an application is running, without a full reload of the page.
4+
5+
## only
6+
7+
Enables Hot Module Replacement without page refresh as a fallback in case of build failures.
8+
9+
**webpack.config.js**
10+
11+
```js
12+
module.exports = {
13+
// ...
14+
devServer: {
15+
hot: "only",
16+
},
17+
};
18+
```
19+
20+
Usage via CLI:
21+
22+
```console
23+
npx webpack serve --open --hot only
24+
```
25+
26+
## What Should Happen
27+
28+
1. The script should open `http://localhost:8080/` in your default browser.
29+
2. In your editor, open `example.js` and change any part of the `innerHTML` string.
30+
3. Open the console in your browser's devtools.
31+
32+
In the devtools console you should see:
33+
34+
```
35+
[webpack-dev-server] App updated. Recompiling...
36+
[webpack-dev-server] App hot update...
37+
[HMR] Checking for updates on the server...
38+
⚠️ Ignored an update to unaccepted module ./example.js -> ./app.js
39+
[HMR] Nothing hot Updated.
40+
[HMR] App is up to date.
41+
```
42+
43+
5. Refresh the page and see the text on the page itself change to match your edits in `example.js`.

Diff for: examples/hmr/only/app.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
require("./example");
4+
5+
if (module.hot) {
6+
module.hot.accept((err) => {
7+
if (err) {
8+
console.error("Cannot apply HMR update.", err);
9+
}
10+
});
11+
}

Diff for: examples/hmr/only/example.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
const target = document.querySelector("#target");
4+
5+
target.innerHTML =
6+
"Modify and save <code>/examples/hmr/example.js</code> to update this element without reloading the page.";

Diff for: examples/hmr/only/webpack.config.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
// our setup function adds behind-the-scenes bits to the config that all of our
4+
// examples need
5+
const { setup } = require("../../util");
6+
7+
module.exports = setup({
8+
context: __dirname,
9+
entry: "./app.js",
10+
devServer: {
11+
hot: "only",
12+
},
13+
});

0 commit comments

Comments
 (0)