首页 文章

带有React Hot Loader的Webpack Dev Server

提问于
浏览
2

我有一个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 回答

  • 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到他们的应用程序 . 希望能帮助到你

相关问题