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
Previously, the config had been split between the config.js file and the CLI; everything has been moved into the config file.
The explanation for the config entries was moved inline, as comments.
Hot Module Replacement (HMR) exchanges, adds, or removes modules while an application is running without a page reload. HMR is particularly useful in applications using a single state tree, since components are "dumb" and will reflect the latest application state, even after their source is changed and they are replaced.
5
-
6
-
Webpack's power lies in its customizablity, and there are MANY ways of configuring HMR given the needs of a particular project. The approach described below uses Babel and React, but these tools are not necessary for HMR to work. If you'd like to see examples of other approaches, please request them or, better yet, open up a PR with an addition!
4
+
Hot Module Replacement (HMR) exchanges, adds, or removes modules while an
5
+
application is running without a page reload.
6
+
HMR is particularly useful in applications using a single state tree,
7
+
since components are "dumb" and will reflect the latest application state, even
8
+
after their source is changed and they are replaced.
9
+
10
+
Webpack's power lies in its customizablity, and there are MANY ways of configuring HMR
11
+
given the needs of a particular project. The approach described below uses Babel and
12
+
React, but these tools are not necessary for HMR to work.
13
+
If you'd like to see examples of other approaches,
14
+
please request them or, better yet,
15
+
[open up a PR with an addition](https://github.com/webpack/webpack.io)!
7
16
8
17
##Project Config
9
-
This guide will be demonstrating the use of HMR with Babel, React, and PostCSS (using CSS Modules). To follow along, please add the following deps to your `package.json`:
18
+
This guide will be demonstrating the use of HMR with Babel,
19
+
React, and PostCSS (using CSS Modules).
20
+
To follow along, please add the following deps to your `package.json`:
10
21
11
22
To use HMR, you'll need the following dependencies:
@@ -27,10 +38,10 @@ Your `.babelrc` file should look like the following:
27
38
```js
28
39
{
29
40
"presets": [
30
-
["es2015", {"modules":false}],
41
+
["es2015", {"modules":false}],
31
42
//Webpack understands the native import syntax, and uses it for tree shaking
32
43
33
-
"stage-2",
44
+
"stage-2",
34
45
//Specifies what level of language features to activate.
35
46
//State 2 is "draft", 4 is finished, 0 is strawman.
36
47
//See https://tc39.github.io/process-document/
@@ -46,62 +57,104 @@ Your `.babelrc` file should look like the following:
46
57
```
47
58
48
59
###Webpack config
49
-
While there's many ways of setting up your Webpack config - via API, via multiple or single config files, etc - here is the basic information you should have available:
60
+
While there's many ways of setting up your Webpack config - via API,
61
+
via multiple or single config files, etc - here is the basic information
//prints more readable module names in the browser console on HMR updates
91
136
],
92
137
}
93
138
};
94
139
```
95
140
96
-
There's a lot going on above, and not all of it is related to HMR. If you are new to Webpack configs, please see the documentation elsewhere on this site. The basic assumption here is that your JS entry is located at `./src/index.js`, and that you're using CSS Modules for your styling. Let's look at the HMR specific items.
141
+
There's a lot going on above, and not all of it is related to HMR.
on webpack dev server, and the [other articles](https://webpack.github.io/webpack.io/concepts/)
145
+
here on webpack.io.
97
146
98
-
First, the plugins. While only the HMRPlugin itself is necessary to enable HMR, the NamedModulesPlugin will make your life easier by showing an intelligible module id in your browser console whenever a file is changed.
147
+
The basic assumption here is that your JS entry is located at `./src/index.js`,
148
+
and that you're using CSS Modules for your styling.
99
149
100
-
Next, working backward, is the `devServer` key. While it's possible to configure webpack-dev-server directly from the CLI, or more specifically in a server script from the Node module API, this example approach is using the `devServer` key in the overall webpack config. `hot: true` turns on HMR. `publicPath` denotes the root from wich the server will serve up your code (e.g. `localhost:8080${PUBLIC_PATH}`, so in this case `localhost:8080/`). the `publicPath` should match the `output.publicPath`.
150
+
Please see the comments inline that explain each portion of the config. The main
151
+
areas to look are the `devServer` key and the `entry` key. The `HotModuleReplacementPlugin` is
152
+
also necessary to include in the `plugins` array.
101
153
102
-
Finally, your entry file should be preceded by the elements included here: `'react-hot-loader/patch'`, `'webpack-dev-server/client?http://localhost:8080'`, and `'webpack/hot/only-dev-server'`. The react-hot-loader entry is only necessary if you are using React. The `webpack-dev-server/client?` line should make sure to point at the dev server instance you are running (in case you have configured a different port, for example). Finally, the `only` in `hot/only-dev-server` means that updates will only be sent on clean builds/bundles; you can drop the `only-` if you would prefer seeing error output in your browser via HMR.
103
-
104
-
The module loaders in this config, while not specific to HMR, are worth noting here; we're making sure to use Babel (as configued in the .babelrc) for our JS files, and setting up CSS Modules for our CSS code.
154
+
There are two modules included here for the purposes of this guide.
155
+
The react-hot-loader addition to the entry, as noted above, is necessary to enable
156
+
HMR with React components. The NamedModulesPlugin is a useful addition
157
+
to better understand what modules are being updated when using HMR.
105
158
106
159
###Code
107
160
In this guide, we're using the following files:
@@ -154,23 +207,42 @@ export default App;
154
207
}
155
208
```
156
209
157
-
Now, the above code is using React, but it doesn't need to be. In fact, the only thing that matters above is the code refering to `module`. First, we wrap the HMR code inside of `module.hot` check; webpack exposes `module` to the code, and if we are running with `hot: true` configured, we'll enter the inside of the conditional. While the module API offers more options than what's above, the most important element is the `module.hot.accept` call. It specific how to handle changes to specific dependencies. So in this, it will file the `render` method ONLY when `src/components/App.js` changes! Note that would also include when the dependencies of `App.js` change - so the `render` method will file not just for changes made directly to the source of `App.js`, but also changes made to `App.css`, since `App.css` is included in `App.js`.
210
+
Now, the above code is using React, but it doesn't need to be. In fact,
211
+
the only thing that matters above is the code refering to `module`.
212
+
First, we wrap the HMR code inside of `module.hot` check;
213
+
webpack exposes `module` to the code, and if we are running with `hot: true` configured,
214
+
we'll enter the inside of the conditional.
215
+
216
+
While the module API offers more options than what's above, the most
217
+
important element is the `module.hot.accept` call.
218
+
It specific how to handle changes to specific dependencies.
219
+
220
+
So in this case, `module.hot` will fire the `render` method ONLY
221
+
when `src/components/App.js` changes! Note that would also include when the
222
+
dependencies of `App.js` change -
223
+
so the `render` method will file not just for changes made directly to the
224
+
source of `App.js`, but also changes made to `App.css`, since `App.css`
225
+
is included in `App.js`.
158
226
159
227
###Package.json
160
-
Finally, we need to start up webpack dev server to bundle our code and see HMR in action. We can use the following package.json entry:
228
+
Finally, we need to start up webpack dev server to bundle our code and see HMR in action.
0 commit comments