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/entry-points.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
@@ -97,4 +97,4 @@ const config = {
97
97
98
98
- Use `CommonsChunkPlugin` to create bundles of shared application code between each page. Multi-page applications that reuse a lot of code/modules between entry points can greatly benefit from these techniques, as the amount of entry points increase.
99
99
100
-
T> As a rule of thumb: for one HTML use exactly one entry point.
100
+
T> As a rule of thumb: for each HTML document use exactly one entry point.
Copy file name to clipboardExpand all lines: content/concepts/hot-module-replacement.md
+59-21Lines changed: 59 additions & 21 deletions
Display the source diff
Display the rich diff
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
Copy file name to clipboardExpand all lines: content/concepts/index.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -86,6 +86,8 @@ const config = {
86
86
]
87
87
}
88
88
};
89
+
90
+
module.exports= config;
89
91
```
90
92
91
93
In the configuration above we have defined a `rules` property for a single module with two required properties: `test`, and `use`. This tells webpack's compiler the following:
Copy file name to clipboardExpand all lines: content/concepts/plugins.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
@@ -87,4 +87,4 @@ module.exports = config;
87
87
});
88
88
```
89
89
90
-
T> Did you know: The example seen above is extremely similar to the [webpack runtime itself!](https://github.com/webpack/webpack/blob/master/bin/webpack.js#L290-L292) There are lots of great usage examples hiding in the [webpack source code](https://github.com/webpack/webpack) that you can apply to your own configurations and scripts!
90
+
T> Did you know: The example seen above is extremely similar to the [webpack runtime itself!](https://github.com/webpack/webpack/blob/e7087ffeda7fa37dfe2ca70b5593c6e899629a2c/bin/webpack.js#L290-L292) There are lots of great usage examples hiding in the [webpack source code](https://github.com/webpack/webpack) that you can apply to your own configurations and scripts!
Copy file name to clipboardExpand all lines: content/configuration/index.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
@@ -12,7 +12,7 @@ Webpack is fed via a configuration object. It is passed in one of two ways depen
12
12
13
13
T> New to webpack? Check out our guide to some of webpack's [core concepts](/concepts) to get started!
14
14
15
-
T> Notice that throughout the configuration we use Node's built-in [path module](https://nodejs.org/api/path.html). This prevents file path issues between operating systems. See [this section](https://nodejs.org/api/path.html#path_windows_vs_posix) for more.
15
+
T> Notice that throughout the configuration we use Node's built-in [path module](https://nodejs.org/api/path.html) and prefix it with the [__dirname](https://nodejs.org/docs/latest/api/globals.html#globals_dirname) global. This prevents file path issues between operating systems and allows relative paths to work as expected. See [this section](https://nodejs.org/api/path.html#path_windows_vs_posix) for more info on POSIX vs. Windows paths.
0 commit comments