与 webpack-dev-middleware 一起使用
-
通过 npm 安装 webpack-dev-middleware
npm i -D webpack-dev-middleware webpack-hot-middleware
-
修改 webpack.config.js
-
将
webpack-hot-middleware/client
添加到entry
中定义的每个项目 -
将
new webpack.HotModuleReplacementPlugin()
添加到plugins
module.exports = { entry: { js: [ './index.js', 'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000&reload=true' ] }, plugins: [ new webpack.HotModuleReplacementPlugin() ] };
-
-
将这些添加到 index.js
var webpack = require('webpack'); var webpackDevMiddleware = require('webpack-dev-middleware'); var webpackHotMiddleware = require('webpack-hot-middleware'); var config = require('./webpack.config.js'); var compiler = webpack(config); app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath, stats: { colors: true }, watchOptions: { aggregateTimeout: 300, poll: true }, })); app.use(webpackHotMiddleware(compiler, { log: console.log, }));