带有React Hot Loader的Webpack Dev Server

时间:2016-08-30 11:06:39

标签: reactjs webpack webpack-dev-server react-hot-loader hot-module-replacement

我有一个webpack配置,它本身就很完美。我正在尝试按照建议安装React Hot Loader和HMR,这需要webpack-dev-server。在这里,我无法让它发挥作用。我无法找到我的捆绑所在的位置。我希望它只是在localhost:3000

我的webpack.config.js

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

module.exports = {
  watch: true,

  devtool: 'eval',

  // entry: './src/main.js', This runs just for webpack bundling

  entry:[
    'webpack-dev-server/client?http:localhost:9000', // WebpackDevServer host and port
    'webpack/hot/only-dev-server', // "only" prevents reload on syntax errors
    './src/main.js' // Your appʼs entry point
  ],

  output: {
    path: path.resolve(__dirname, 'public', 'dist'),
    filename: 'main.js'/*,
    publicPath: '/dist/'*/
  },

  module: {
    loaders: [{
      test: /\.js$/,
      loaders: ['react-hot', 'babel-loader?cacheDirectory=true,presets[]=react,presets[]=es2015'],
      exclude: function(path) {
        var isModule = path.indexOf('node_modules') > -1;
        var isJsaudio = path.indexOf('jsaudio') > -1;

        if (isModule && !isJsaudio) {
          return true;
        }
      }
    }, {
      test: /\.json$/,
      loader: "json-loader"
    }]
  },

  plugins: [
    new webpack.HotModuleReplacementPlugin()
  ],

  resolve: {
    extensions: ['', '.js', '.json', 'index.js'],
    root: [
      path.resolve(__dirname, 'src'),
      path.resolve(__dirname, 'node_modules'),
      path.resolve(__dirname, 'node_modules', 'jsaudio', 'src')
    ]
  },

  target: 'web',

  node: {
    fs: 'empty',
    net: 'empty',
    tls: 'empty'
  }
};

webpack-server.js

var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');

new WebpackDevServer(webpack(config), {
    publicPath: '/dist/',
  hot: true,
  historyApiFallback: true
}).listen(9000, 'localhost', function (err, result) {
  if (err) {
    return console.log(err);
  }

  console.log('Listening at http://localhost:9000/');
});

更新:链接的问题没有帮助,特别是因为它甚至没有确认答案。

1 个答案:

答案 0 :(得分:1)

我建议尝试使用react-hot-loader v3,因为更新了热重载工作已经简化了(在我看来!)。

在webpack然后尝试点现在只需要:

entry: {
    app: [
        'react-hot-loader/patch',
        'webpack-hot-middleware/client',
        `${SRC}/client-entry.js`
    ]
}

此处是指向我已创建的示例应用react-lego的链接,以帮助人add react-hot-loader v3加入他们的应用。希望它有所帮助