webpackDevMiddleware不会自动重新加载

时间:2017-03-15 16:21:14

标签: javascript express webpack html-webpack-plugin webpack-dev-middleware

所以我使用webpack dev中间件如下:

const compiledWebpack = webpack(config),
          app             = express(),
          devMiddleware   = webpackDevMiddleware(compiledWebpack, {
            historyApiFallback: true,
            publicPath: config.output.publicPath,
            overlay: {
              warnings: true,
              errors: true
            },
            compress: true,
            stats: { colors: true }
          })


    app.use(devMiddleware)




    app.get('*', (req, res) => {
      // Here is it! Get the index.html from the fileSystem
      const htmlBuffer = devMiddleware.fileSystem.readFileSync(`${config.output.path}/index.html`)

      res.send(htmlBuffer.toString())
    })

    app.listen(PORT, function () {})

    console.log('Running on port ' + PORT)

然而,出于某种原因,我没有得到实时重装。我也没有获得叠加功能。我正在使用此设置,因为我使用的是webpackhtmlplugin。

我觉得我在这里错过了一个简单的概念:(任何想法?

1 个答案:

答案 0 :(得分:11)

对于实时重新加载,您还需要添加webpack-hot-middleware

在您的服务器中,您必须添加:

const webpackHotMiddleware = require('webpack-hot-middleware');

const hotMiddleware = webpackHotMiddleware(compiledWebpack);
app.use(hotMiddleware);

您还需要在条目中添加'webpack-hot-middleware/client',在webpack配置中添加webpack.HotModuleReplacementPlugin插件:

entry: [
  'webpack-hot-middleware/client',
  './src/index.js' // Your entry point
],
plugins: [
  new webpack.HotModuleReplacementPlugin()
]

有关详细信息,请参阅Installation & Usage