第一步:安装HMR中间件:

npm install --save-dev webpack-hot-middleware


第二步:webpack配置中引入webpack对象

    const webpack = require('webpack’);


第三步:增加devServer配置项:

    hot: true


第四步:增加热模块替换插件:

    new webpack.HotModuleReplacementPlugin()


注意:

1、ExtractTextPlugin插件可能会导致HMR无效。

另外,还可以通过使用web-dev-server —hotOnly参数来启用HMR。

参考官网链接:

https://webpack.js.org/guides/hot-module-replacement/

https://github.com/webpack-contrib/webpack-hot-middleware

React、Vue、Angular相关技术,请参考以下内容:

  • React Hot Loader: Tweak react components in real time.

  • Vue Loader: This loader supports HMR for vue components out of the box.

  • Elm Hot Loader: Supports HMR for the Elm programming language.

  • Angular HMR: No loader necessary! A simple change to your main NgModule file is all that's required to have full control over the HMR APIs.