You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/guides/hot-module-replacement.md
+38-38Lines changed: 38 additions & 38 deletions
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ W> __HMR__ is not intended for use in production, meaning it should only be used
28
28
29
29
## Enabling HMR
30
30
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.
32
32
33
33
__webpack.config.js__
34
34
@@ -39,8 +39,9 @@ __webpack.config.js__
39
39
40
40
module.exports = {
41
41
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'
44
45
},
45
46
devtool: 'inline-source-map',
46
47
devServer: {
@@ -72,13 +73,6 @@ __index.js__
72
73
import _ from 'lodash';
73
74
import printMe from './print.js';
74
75
75
-
+ if (module.hot) {
76
-
+ module.hot.accept('./print.js', function() {
77
-
+ console.log('Accepting the updated printMe module!');
78
-
+ printMe();
79
-
+ })
80
-
+ }
81
-
82
76
function component() {
83
77
var element = document.createElement('div');
84
78
var btn = document.createElement('button');
@@ -94,6 +88,13 @@ __index.js__
94
88
}
95
89
96
90
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
+
+ }
97
98
```
98
99
99
100
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
131
132
132
133
To make this work with HMR we need to update that binding to the new `printMe` function using `module.hot.accept`:
133
134
134
-
__print.js__
135
+
__index.js__
135
136
136
137
```diff
137
138
import _ from 'lodash';
138
139
import printMe from './print.js';
139
140
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
-
152
141
function component() {
153
142
var element = document.createElement('div');
154
143
var btn = document.createElement('button');
@@ -163,7 +152,19 @@ __print.js__
163
152
return element;
164
153
}
165
154
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
+
}
167
168
```
168
169
169
170
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
173
174
174
175
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.
175
176
176
-
First let's install both loaders with the following command:
177
+
First let's install both loaders with the following command:
177
178
178
179
```bash
179
180
npm install --save-dev style-loader css-loader
@@ -190,8 +191,7 @@ __webpack.config.js__
190
191
191
192
module.exports = {
192
193
entry: {
193
-
app: './src/index.js',
194
-
print: './src/print.js'
194
+
app: './src/index.js'
195
195
},
196
196
devtool: 'inline-source-map',
197
197
devServer: {
@@ -250,17 +250,6 @@ __index.js__
250
250
import printMe from './print.js';
251
251
+ import './styles.css';
252
252
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
-
264
253
function component() {
265
254
var element = document.createElement('div');
266
255
var btn = document.createElement('button');
@@ -275,7 +264,18 @@ __index.js__
275
264
return element;
276
265
}
277
266
267
+
let element = component();
278
268
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
+
279
279
```
280
280
281
281
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