将文件移动到/ app文件夹后,HMR无法正常工作

时间:2016-03-06 23:16:08

标签: javascript express server redux webpack-hmr

您好,我目前正在尝试学习如何使用express和webpacks HMR,每次我更新并保存文件时都会给我错误:

"以下模块无法热更新:(需要完全重新加载)" " ./应用/组件/ App.js"

我当前的配置文件有问题吗?

所以我有一个像这样的目录结构:

todos
    |
    app
      |
    package.son
    server.js
    webpack.config.js

我有一个webpack.config.js文件和一个如下所示的server.js文件:

var path = require('path');
var webpack = require('webpack');


module.exports = {
  context: path.join(__dirname + "/app"),

  entry: ['./index'],
  output: {
    path: "/bundle",
    publicPath: '/static/',
    filename: "bundle.js"
  },
  module: {
    loaders: [
     {
        test: /\.jsx?$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel',
        query: {
          presets: ['react', 'es2015']
        }
      }
    ]
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  }

};

server.js

var webpack = require('webpack')
var webpackDevMiddleware = require('webpack-dev-middleware')
var webpackHotMiddleware = require('webpack-hot-middleware')
var config = require('./webpack.config')

var app = new (require('express'))()
var port = 3000

var compiler = webpack(config)
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath }))
app.use(webpackHotMiddleware(compiler))

app.get("/", function(req, res) {
  res.sendFile(__dirname + '/app/index.html')
})

app.listen(port, function(error) {
  if (error) {
    console.error(error)
  } else {
    console.info("==>   Listening on port %s. Open up http://localhost:%s/ in your browser.", port, port)
  }
})

1 个答案:

答案 0 :(得分:2)

您是否尝试过接受该模块?在Webpack HMR documentation中,它说明如下:

  

只有在您“接受”模块时才能更新模块。所以你需要module.hot.accept父母或父母的父母的模块。例如,路由器或子视图将是一个好地方。

在index.js文件或希望HMR重新加载的模块中尝试此操作:

if (module.hot) {  
 module.hot.accept();
}