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
* Improve HMR docs
* Fix link
* Add concept page for HMR
* Small improvements
Really I just want to trigger a rebuild because the clabot is not working.
Hot Module Replacement (HMR) exchanges, adds, or removes modules while an
10
+
application is running without a page reload. You basically can update changed modules without a full page reload.
11
+
12
+
## How Does It Work?
13
+
14
+
### From The App View
15
+
16
+
The app code asks the HMR runtime to check of updates. The HMR runtime downloads the updates (async) and tell the app code that an update is available. The app code asks the HMR runtime to apply updates. The HMR runtime applies the update (sync). The app code may or may not require user interaction in this process (you decide).
17
+
18
+
### From The Compiler (webpack) View
19
+
20
+
In addition to the normal assets the compiler need to emit the "Update" to allow updating from previous version to this version. The "Update" contains two parts:
21
+
22
+
1. the update manifest (JSON)
23
+
2. one or multiple update chunks (JavaScript)
24
+
25
+
The manifest contains the new compilation hash and a list of all update chunks (2.).
26
+
27
+
The update chunks contains code for all updated modules in this chunk (or a flag if a module was removed).
28
+
29
+
The compiler addtionally makes sure that module and chunk ids as consistent between these builds. It uses a "records" json file to store them between builds (on it store them in memory).
30
+
31
+
### From The Module View
32
+
33
+
HMR is an opt-in feature, so it only affects modules that contains HMR code. The documentation describes the API that is available in modules. In general the module developer writes handlers that are called when a dependency of this module is updated. The developer can also write a handler that is called when this module is updated.
34
+
35
+
In most cases it's not mandatory to write HMR code in every module. If a module has no HMR handlers the update bubbles up. This means a single handler can handle a update to a complete module tree. If a single module in this tree is updated, the complete module tree is reloaded (only reloaded not transferred).
36
+
37
+
### From The HMR Runtime View (Technical)
38
+
39
+
For the module system runtime is additional code emitted to track module `parents` and `children`.
40
+
41
+
On the management side the runtime supports two methods: `check` and `apply`.
42
+
43
+
A `check` does a HTTP request to the update manifest. When this request fails, there is no update available. Elsewise the list of updated chunks is compared to the list of currently loaded chunks. For each loaded chunk the corresponding update chunk is downloaded. All module updates as stored in the runtime as update. The runtime switches into the `ready` state, meaning an update has been downloaded and is ready to be applied.
44
+
45
+
For each new chunk request in the ready state the update chunk is also downloaded.
46
+
47
+
The `apply` method flags all updated modules as invalid. For each invalid module there need to be a update handler in the module or update handlers in every parent. Else the invalid buddles up and mark all parents as invalid too. This process continues until no more "bubble up" occurs. If it bubbles up from an entry point the process fails.
48
+
49
+
Now all invalid modules are disposed (dispose handler) and unloaded. Then the current hash is updated and all "accept" handlers are called. The runtime switches back to the `idle` state and everything continues as normal.
50
+
51
+
## What can I do with it?
52
+
53
+
You can use it in development as LiveReload replacement. webpack-dev-server supports a hot mode which tries to update with HMR before trying to reload the whole page. See how to implement [HMR with React](/guides/hmr-react) for example.
54
+
55
+
Some loaders already generate modules that are hot-updateable. i.e. the `style-loader` can exchange the stylesheet. You don't need to do anything special.
56
+
57
+
webpack's power lies in its customizability, and there are *many* ways of configuring HMR given the needs of a particular project.
Copy file name to clipboardExpand all lines: content/guides/development.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -86,7 +86,7 @@ The command above should automatically open your browser on `http://localhost:80
86
86
87
87
Make a change in one of your files and hit save. You should see that the console is recompiling. After that's done, the page should be refreshed. If nothing happens in the console, you may need to fiddle with [`watchOptions`](/configuration/dev-server#devserver-watchoptions-).
88
88
89
-
Now you have live reloading working, you can take it even a step further: Hot Module Replacement. This is an interface that makes it possible to swap modules **without a page refresh**. Find out how to [configure HMR](/guides/hmr).
89
+
Now you have live reloading working, you can take it even a step further: Hot Module Replacement. This is an interface that makes it possible to swap modules **without a page refresh**. Find out how to [configure HMR](/guides/hmr-react).
90
90
91
91
By default **inline mode** is used. This mode injects the client - needed for live reloading and showing build errors - in your bundle. With inline mode you will get build errors and warnings in your DevTools console.
Copy file name to clipboardExpand all lines: content/guides/hmr-react.md
+85-85Lines changed: 85 additions & 85 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,12 +6,16 @@ contributors:
6
6
- jhnns
7
7
---
8
8
9
-
Hot Module Replacement (HMR) exchanges, adds, or removes modules while an
10
-
application is running without a page reload.
9
+
As explained in detail on the [concept page](/concepts/hot-module-replacement), Hot Module Replacement (HMR)exchanges, adds, or removes modules while an application is running without a page reload.
11
10
HMR is particularly useful in applications using a single state tree,
12
11
since components are "dumb" and will reflect the latest application state, even
13
12
after their source is changed and they are replaced.
14
13
14
+
The approach described below uses Babel and
15
+
React, but these tools are not necessary for HMR to work.
16
+
17
+
T> If you'd like to see examples of other approaches, please request them or better yet, [open up a PR with an addition](https://github.com/webpack/webpack.js.org).
18
+
15
19
## Project Config
16
20
17
21
This guide will be demonstrating the use of HMR with Babel,
@@ -39,113 +43,109 @@ Your `.babelrc` file should look like the following:
39
43
{
40
44
"presets": [
41
45
["es2015", {"modules": false}],
42
-
//Webpack understands the native import syntax, and uses it for tree shaking
46
+
// webpack understands the native import syntax, and uses it for tree shaking
43
47
44
48
"stage-2",
45
-
//Specifies what level of language features to activate.
46
-
//Stage 2 is "draft", 4 is finished, 0 is strawman.
47
-
//See https://tc39.github.io/process-document/
49
+
//Specifies what level of language features to activate.
50
+
//Stage 2 is "draft", 4 is finished, 0 is strawman.
51
+
//See https://tc39.github.io/process-document/
48
52
49
53
"react"
50
-
//Transpile React components to JavaScript
54
+
//Transpile React components to JavaScript
51
55
],
52
56
"plugins": [
53
57
"react-hot-loader/babel"
54
-
//Enables React code to work with HMR.
58
+
//Enables React code to work with HMR.
55
59
]
56
60
}
57
61
```
58
62
59
-
### Webpack config
63
+
### webpack Config
60
64
61
-
While there's many ways of setting up your Webpack config - via API,
65
+
While there's many ways of setting up your webpack config - via API,
62
66
via multiple or single config files, etc - here is the basic information
0 commit comments