HMR 3 Ways
There are 3 ways to use Webpack HMR. This repository contains a minimal example of each way. In order from simplest to most complex:
- Using the webpack-dev-server CLI
- Using the webpack-dev-server API
- Using express and webpack-hot-middleware
Here is a short discussion on how to pick the method you want to use.
If you want the simplest possible setup then use the webpack-dev-server CLI.
If you are using a task runner like grunt or gulp you'll want to use the webpack-dev-server API.
Or similarly, if you have your own node scripts to run webpack, you'll want to use the webpack-dev-server API.
If you are already using express for your website, it is very convenient to use webpack-hot-middleware.
About the Code
js/index.js: Entry point.
require()s the other 2 files,
js/math-doer.js: An extremely simple file to illustrate that dependencies are automatically updated by HMR.
js/box-creator.js: An example of a dependency that has side effects, with an example of how to use
module.hot.dispose to handle such side effects.