|
| 1 | +--- |
| 2 | +title: Hot Module Replacement |
| 3 | +contributors: |
| 4 | + - jmreidy |
| 5 | + - jhnns |
| 6 | + - sararubin |
| 7 | + - aiduryagin |
| 8 | + - rohannair |
| 9 | + - joshsantos |
| 10 | + - drpicox |
| 11 | + - skipjack |
| 12 | +related: |
| 13 | + - title: Concepts - Hot Module Replacement |
| 14 | + url: TODO |
| 15 | +--- |
| 16 | + |
| 17 | +Hot Module Replacement (or HMR) is one of the most useful features offered by webpack. It allows modules of all kinds to be updated at runtime without the need for a full refresh. This allows you to maintain application state in development and shave countless time in your workflow. This page focuses on __implementation__ while the [concepts page](/concepts/hot-module-replacement) gives more details on how it works and why it's useful. |
| 18 | + |
| 19 | +W> __HMR__ is not intended for use in production, meaning it should only be used in development. See the [building for production guide](/guides/production-build) for more information. |
| 20 | + |
| 21 | + |
| 22 | +## Enabling HMR |
| 23 | + |
| 24 | +Enabling this feature is actually fairly simple. Let's take a look at how to set it up with [webpack-dev-server]()... |
| 25 | + |
| 26 | +``` js |
| 27 | +const path = require('path'); |
| 28 | +const webpack = require('webpack'); |
| 29 | + |
| 30 | +module.exports = { |
| 31 | + entry: './index.js', |
| 32 | + |
| 33 | + plugins: [ |
| 34 | + new webpack.HotModuleReplacementPlugin() // Enable HMR |
| 35 | + ], |
| 36 | + |
| 37 | + output: { |
| 38 | + filename: 'main.js', |
| 39 | + path: path.resolve(__dirname, 'dist'), |
| 40 | + publicPath: '/' |
| 41 | + }, |
| 42 | + |
| 43 | + devServer: { |
| 44 | + hot: true, // Tell the dev-server we're using HMR |
| 45 | + contentBase: resolve(__dirname, 'dist'), |
| 46 | + publicPath: '/' |
| 47 | + } |
| 48 | +}; |
| 49 | +``` |
| 50 | + |
| 51 | +Not too bad, huh? Let's test it out using `module.hot.accept`... |
| 52 | + |
| 53 | +__index.js__ |
| 54 | + |
| 55 | +``` js |
| 56 | +import Lib from './library'; |
| 57 | + |
| 58 | +if (module.hot) { |
| 59 | + module.hot.accept('./library', function() { |
| 60 | + console.log('Accepting the updated library module!'); |
| 61 | + Library.log(); |
| 62 | + }) |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +__library.js__ |
| 67 | + |
| 68 | +``` js |
| 69 | +export default { |
| 70 | + log() { |
| 71 | + // Change this after the server is started to test |
| 72 | + console.log('Initial log...') |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +Start changing the `console.log` statement in `library.js`, to `'Second log...'` for example, and you should see the following output in the browser console... |
| 78 | + |
| 79 | +``` diff |
| 80 | +[HMR] Waiting for update signal from WDS... |
| 81 | +main.js:9998 Initial log... |
| 82 | +main.js:9468 [WDS] Hot Module Replacement enabled. |
| 83 | ++ 2main.js:9468 [WDS] App updated. Recompiling... |
| 84 | ++ main.js:9468 [WDS] App hot update... |
| 85 | ++ main.js:9912 [HMR] Checking for updates on the server... |
| 86 | ++ main.js:9982 Accepting the updated library module! |
| 87 | ++ 0.1bafc70….hot-update.js:11 Second log... |
| 88 | ++ main.js:9955 [HMR] Updated modules: |
| 89 | ++ main.js:9957 [HMR] - ./src/library.js |
| 90 | ++ main.js:9894 [HMR] App is up to date. |
| 91 | +``` |
| 92 | + |
| 93 | + |
| 94 | +## Gotchas |
| 95 | + |
| 96 | +Hot Module Replacement can be tricky. For example, let's say I have the following class: |
| 97 | + |
| 98 | +``` js |
| 99 | +class MyClass { |
| 100 | + constructor() { |
| 101 | + super() |
| 102 | + |
| 103 | + this._options = {} |
| 104 | + } |
| 105 | + |
| 106 | + log(text) { |
| 107 | + console.log('Logging some text: ', text) |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +Even if the underlying module containing this class is patched with new code, any existing instances of the class still have the old `log` method. Meaning if we changed what that method does, it wouldn't be reflected in those old instances unless we re-instantiate them somehow using `module.hot.accept`. |
| 113 | + |
| 114 | +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 to help you out. Let's start with stylesheets... |
| 115 | + |
| 116 | + |
| 117 | +## HMR with Stylesheets |
| 118 | + |
| 119 | +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. So, with the following webpack configuration... |
| 120 | + |
| 121 | +``` js |
| 122 | +module.exports = { |
| 123 | + // ... |
| 124 | + module: { |
| 125 | + rules: [ |
| 126 | + { |
| 127 | + test: /\.css$/, |
| 128 | + use: [ 'style-loader', 'css-loader' ] |
| 129 | + } |
| 130 | + ] |
| 131 | + }, |
| 132 | + // ... |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +hot loading stylesheets is a breeze... |
| 137 | + |
| 138 | +__index.js__ |
| 139 | + |
| 140 | +``` js |
| 141 | +import Lib from './library'; |
| 142 | +import './styles.css'; |
| 143 | + |
| 144 | +// ... |
| 145 | +``` |
| 146 | + |
| 147 | +__styles.css__ |
| 148 | + |
| 149 | +``` css |
| 150 | +body { |
| 151 | + background: blue; |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +Change the style on `body` to `background: red;` and you should immediately see the page's background color change without a full refresh. |
| 156 | + |
| 157 | + |
| 158 | +## Other Code and Frameworks |
| 159 | + |
| 160 | +There are many other loaders out in the community to make HMR interact smoothly with a variety of frameworks and libraries... |
| 161 | + |
| 162 | +- [React Hot Loader](https://github.com/gaearon/react-hot-loader): Tweak react components in real time. |
| 163 | +- [Vue Loader](https://github.com/vuejs/vue-loader): This loader supports HMR for vue components out of the box. |
| 164 | +- [Elm Hot Loader](https://github.com/fluxxu/elm-hot-loader): Supports HMR for the Elm programming language. |
| 165 | + |
| 166 | +T> If you know of any other loaders or plugins that help with or enhance Hot Module Replacement please submit a pull request to add to this list! |
| 167 | + |
0 commit comments