坑
.babelrc 文件的配置必须指定 module: false
1 | { |
相关解释:
Note that because webpack 2 has built-in support for ES2015 modules, you won’t need to re-require your root component in module.hot.accept.
https://webpack.js.org/guides/hmr-react/
几种实现方式
- 使用 npm scripts 执行 webpack-dev-server
1 | "scripts": { |
这种方式的完整代码:
https://github.com/xwillmadeit/webpack2-hmr/tree/master
- 在 webpack.config.js 中配置 devServer 参数
1 | plugins: [ |
注意:这种方式必须配置 hot: true,同时在插件中加入 new webpack.HotModuleReplacementPlugin()
这种方式的完整代码:
https://github.com/xwillmadeit/webpack2-hmr/tree/option2
- 使用 webpack-dev-middleware 和 webpack-hot-middleware
1 | // webpack.config.js |
完整代码:
https://github.com/xwillmadeit/webpack2-hmr/tree/middleware