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/concepts/hot-module-replacement.md
+59-21Lines changed: 59 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -4,54 +4,92 @@ sort: 11
4
4
contributors:
5
5
- SpaceK33z
6
6
- sokra
7
+
- GRardB
7
8
---
8
9
9
-
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.
10
+
Hot Module Replacement (HMR) exchanges, adds, or removes
11
+
[modules](/concepts/modules/) while an application is running without a
12
+
page reload. This allows you to speed up development time by updating
13
+
individual modules when they are changed without refreshing the page.
11
14
12
15
## How Does It Work?
13
16
14
17
### From The App View
15
18
16
-
The app code asks the HMR runtime to check for 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).
19
+
1. The app code asks the HMR runtime to check for updates.
20
+
2. The HMR runtime downloads the updates (asynchronously) and tells the app
21
+
code that an update is available.
22
+
3. The app code then asks the HMR runtime to apply the updates.
23
+
4. The HMR runtime applies the update (synchronously).
24
+
25
+
You can set up HMR so that this process happens automatically, or you can
26
+
choose to require user interaction for updates to occur.
17
27
18
28
### From The Compiler (webpack) View
19
29
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:
30
+
In addition to the normal assets, the compiler needs to emit an "update"
31
+
to allow updating from previous version to the new version. The "update"
32
+
consists of two parts:
21
33
22
-
1.the update manifest (JSON)
23
-
2.one or multiple update chunks (JavaScript)
34
+
1.The update manifest (JSON)
35
+
2.One or more update chunks (JavaScript)
24
36
25
-
The manifest contains the new compilation hash and a list of all update chunks (2.).
37
+
The manifest contains the new compilation hash and a list of all update chunks.
26
38
27
-
The update chunks contain code for all updated modules in this chunk (or a flag if a module was removed).
39
+
Each update chunk contains code for all updated modules in the respective chunk
40
+
(or a flag indicating that the module was removed).
28
41
29
-
The compiler addtionally makes sure that module and chunk ids are consistent between these builds. It uses a "records" json file to store them between builds (or it stores them in memory).
42
+
The compiler makes sure that module IDs and chunk IDs are consistent
43
+
between these builds. It typically stores these IDs in memory (for example, when
44
+
using [webpack-dev-server](/configuration/dev-server/)), but it's also possible to
45
+
store them in a JSON file.
30
46
31
47
### From The Module View
32
48
33
-
HMR is an opt-in feature that affects only modules containing HMR code. You can consider patching styling through style-loader as a good example. To make this work, style-loader implements the HMR interface. Implementing it you describe what should happen when a module is updated. In this case we simply replace the styling with the one received through HMR.
49
+
HMR is an opt-in feature that only affects modules containing HMR code. One example
50
+
would be patching styling through the [style-loder](https://github.com/webpack/style-loader).
51
+
In order for patching to work, style-loader implements the HMR interface; when it
52
+
receives an update through HMR, it replaces the old styles with the new ones.
34
53
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 an 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).
54
+
Similarly, when implementing the HMR interface in a module, you can describe what should
55
+
happen when the module is updated. However, in most cases, it's not mandatory to write
56
+
HMR code in every module. If a module has no HMR handlers, the update bubbles up. This
57
+
means that a single handler can handle an update to a complete module tree. If a single
58
+
module in this tree is updated, the complete module tree is reloaded (only reloaded,
59
+
not transferred).
36
60
37
61
### From The HMR Runtime View (Technical)
38
62
39
-
For the module system runtime additional code is emitted to track module `parents` and `children`.
40
-
41
-
On the management side the runtime supports two methods: `check` and `apply`.
63
+
For the module system runtime, additional code is emitted to track module `parents` and `children`.
42
64
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.
65
+
On the management side, the runtime supports two methods: `check`and `apply`.
44
66
45
-
For each new chunk request in the ready state the update chunk is also downloaded.
67
+
A `check` makes an HTTP request to the update manifest. If this request fails,
68
+
there is no update available. If it succeeds, the list of updated chunks is compared
69
+
to the list of currently loaded chunks. For each loaded chunk, the corresponding
70
+
update chunk is downloaded. All module updates are stored in the runtime.
71
+
When all update chunks have been downloaded and are ready to be applied, the runtime
72
+
switches into the `ready` state.
46
73
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 marks 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.
74
+
The `apply` method flags all updated modules as invalid. For each invalid module,
75
+
there needs to be an update handler in the module or update handlers in its parent(s).
76
+
Otherwise, the invalid flag bubbles up and marks its parent(s) as invalid too. Each bubble
77
+
continues until the app's entry point or a module with an update handler is reached
78
+
(whichever comes first). If it bubbles up from an entry point, the process fails.
48
79
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.
80
+
Afterwards, all invalid modules are disposed (via the dispose handler) and unloaded.
81
+
The current hash is then updated and all "accept" handlers are called. The runtime
82
+
switches back to the `idle` state and everything continues as normal.
50
83
51
84
## What can I do with it?
52
85
53
-
You can use it in development as a 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.
86
+
You can use it in development as a LiveReload replacement.
87
+
[webpack-dev-server](/configuration/dev-server/) supports a
88
+
hot mode in which it tries to update with HMR before trying to reload the whole page. See how
89
+
to implement [HMR with React](/guides/hmr-react) as an example.
54
90
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.
91
+
Some loaders already generate modules that are hot-updateable. For example, the `style-loader`
92
+
can swap out a page's stylesheets. For modules like this, you don't need to do anything special.
56
93
57
-
webpack's power lies in its customizability, and there are *many* ways of configuring HMR given the needs of a particular project.
94
+
webpack's power lies in its customizability, and there are *many* ways of configuring HMR
0 commit comments