Skip to content

Commit 6e3d072

Browse files
maheshpecskipjack
authored andcommitted
docs(guides): fix snippet in hot-module-replacement (#1464)
Refactor examples in hot-module-replacement that yielded build errors and warnings. Resolves #1461
1 parent 8daac33 commit 6e3d072

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

content/guides/hot-module-replacement.md

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ W> __HMR__ is not intended for use in production, meaning it should only be used
2828

2929
## Enabling HMR
3030

31-
This feature is great for productivity. All we need to do is update our [webpack-dev-server](https://github.com/webpack/webpack-dev-server) configuration, and use webpack's built in HMR plugin.
31+
This feature is great for productivity. All we need to do is update our [webpack-dev-server](https://github.com/webpack/webpack-dev-server) configuration, and use webpack's built in HMR plugin. We'll also remove the entry point for `print.js` as it will now be consumed by the `index.js` module.
3232

3333
__webpack.config.js__
3434

@@ -39,8 +39,9 @@ __webpack.config.js__
3939

4040
module.exports = {
4141
entry: {
42-
app: './src/index.js',
43-
print: './src/print.js'
42+
- app: './src/index.js',
43+
- print: './src/print.js'
44+
+ app: './src/index.js'
4445
},
4546
devtool: 'inline-source-map',
4647
devServer: {
@@ -72,13 +73,6 @@ __index.js__
7273
import _ from 'lodash';
7374
import printMe from './print.js';
7475

75-
+ if (module.hot) {
76-
+ module.hot.accept('./print.js', function() {
77-
+ console.log('Accepting the updated printMe module!');
78-
+ printMe();
79-
+ })
80-
+ }
81-
8276
function component() {
8377
var element = document.createElement('div');
8478
var btn = document.createElement('button');
@@ -94,6 +88,13 @@ __index.js__
9488
}
9589

9690
document.body.appendChild(component());
91+
+
92+
+ if (module.hot) {
93+
+ module.hot.accept('./print.js', function() {
94+
+ console.log('Accepting the updated printMe module!');
95+
+ printMe();
96+
+ })
97+
+ }
9798
```
9899

99100
Start changing the `console.log` statement in `print.js`, and you should see the following output in the browser console.
@@ -131,24 +132,12 @@ This is happening because the button's `onclick` event handler is still bound to
131132

132133
To make this work with HMR we need to update that binding to the new `printMe` function using `module.hot.accept`:
133134

134-
__print.js__
135+
__index.js__
135136

136137
``` diff
137138
import _ from 'lodash';
138139
import printMe from './print.js';
139140

140-
if (module.hot) {
141-
module.hot.accept('./print.js', function() {
142-
console.log('Accepting the updated printMe module!');
143-
- printMe();
144-
+ document.body.removeChild(element);
145-
+ element = component(); // Re-render the "component" to update the click handler
146-
+ document.body.appendChild(element);
147-
})
148-
}
149-
150-
let element = component();
151-
152141
function component() {
153142
var element = document.createElement('div');
154143
var btn = document.createElement('button');
@@ -163,7 +152,19 @@ __print.js__
163152
return element;
164153
}
165154

166-
document.body.appendChild(element);
155+
- document.body.appendChild(component());
156+
+ let element = component(); // Store the element to re-render on print.js changes
157+
+ document.body.appendChild(element);
158+
159+
if (module.hot) {
160+
module.hot.accept('./print.js', function() {
161+
console.log('Accepting the updated printMe module!');
162+
- printMe();
163+
+ document.body.removeChild(element);
164+
+ element = component(); // Re-render the "component" to update the click handler
165+
+ document.body.appendChild(element);
166+
})
167+
}
167168
```
168169

169170
This is just one example, but there are many others that can easily trip people up. Luckily, there are a lot of loaders out there (some of which are mentioned below) that will make hot module replacement much easier.
@@ -173,7 +174,7 @@ This is just one example, but there are many others that can easily trip people
173174

174175
Hot Module Replacement with CSS is actually fairly straightforward with the help of the `style-loader`. This loader uses `module.hot.accept` behind the scenes to patch `<style>` tags when CSS dependencies are updated.
175176

176-
First let's install both loaders with the following command:
177+
First let's install both loaders with the following command:
177178

178179
```bash
179180
npm install --save-dev style-loader css-loader
@@ -190,8 +191,7 @@ __webpack.config.js__
190191

191192
module.exports = {
192193
entry: {
193-
app: './src/index.js',
194-
print: './src/print.js'
194+
app: './src/index.js'
195195
},
196196
devtool: 'inline-source-map',
197197
devServer: {
@@ -250,17 +250,6 @@ __index.js__
250250
import printMe from './print.js';
251251
+ import './styles.css';
252252

253-
if (module.hot) {
254-
module.hot.accept('./print.js', function() {
255-
console.log('Accepting the updated printMe module!');
256-
document.body.removeChild(element);
257-
element = component(); // Re-render the "component" to update the click handler
258-
document.body.appendChild(element);
259-
})
260-
}
261-
262-
let element = component();
263-
264253
function component() {
265254
var element = document.createElement('div');
266255
var btn = document.createElement('button');
@@ -275,7 +264,18 @@ __index.js__
275264
return element;
276265
}
277266

267+
let element = component();
278268
document.body.appendChild(element);
269+
270+
if (module.hot) {
271+
module.hot.accept('./print.js', function() {
272+
console.log('Accepting the updated printMe module!');
273+
document.body.removeChild(element);
274+
element = component(); // Re-render the "component" to update the click handler
275+
document.body.appendChild(element);
276+
})
277+
}
278+
279279
```
280280

281281
Change the style on `body` to `background: red;` and you should immediately see the page's background color change without a full refresh.

0 commit comments

Comments
 (0)