简体   繁体   English

在Webpack中为HMR配置babel-loader的正确方法

[英]Correct way to configure babel-loader in webpack for HMR

About a week ago I ran into a problem while adding HMR to my project, the problem was that it just didn't work, the console showed HMR Enabled and also detected changes on the files, but it didn't re-render the view, the console would log: 大约一周前,我在向项目中添加HMR时遇到问题,问题在于它只是无法正常工作,控制台显示了HMR Enabled,并且还检测到文件上的更改,但是没有重新呈现视图,控制台将记录:

[HMR] Updated modules:
[HMR]  - ./app/src/components/app.jsx
[HMR] App is up to date.

but nothing would change visually, neither in the code inspector. 但是在代码检查器中,视觉上都不会改变。

After lots of experimenting I found out that the problem was being caused by the babel-loader, it somehow interfered with webpacks HMR motor or something like that. 经过大量的实验,我发现问题是由babel-loader造成的,它以某种方式干扰了webpacks HMR电机或类似的东西。 I solved it by excluding the index file in the babel loader, however I don't think that's the best approach since now I can't use some js features in my index.jsx file. 我通过在babel加载程序中排除索引文件来解决它,但是我认为这不是最好的方法,因为现在我无法在index.jsx文件中使用某些js功能。 I want to know if theres a better way to solve this issue, perhaps something in my configuration or in the way of setting up webpacks hot middleware. 我想知道是否有更好的方法来解决此问题,也许是在我的配置中还是在设置Webpacks热中间件方面。

This is my webpack configuration: 这是我的webpack配置:

module.exports = {
  resolve: {
    extensions: ['*', '.js', '.jsx']
  },
  entry: [
    'webpack-hot-middleware/client', './app/src/index.jsx'
  ],
  output: {
    path: path.resolve(__dirname, 'build/js'),
    filename: 'bundle.js',
    publicPath: '/public'
  },
  devtool: 'cheap-module-source-map',
  module: {
    rules: [{
      test: /\.jsx?$/,
      exclude: /node_modules/,
      include: path.resolve(__dirname, 'app/src/'),
      exclude: path.resolve(__dirname, 'app/src/index.jsx'),
      use: [{
        loader: 'babel-loader',
        options: {
          presets: [
            'react-hmre'
          ],
          plugins: [
            'transform-object-rest-spread'
          ]
        }
      }],
    }]
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin()
  ]
}

And this is how I setup the hot middleware and react-hot-loader in my server: 这就是我在服务器中设置热中间件和react-hot-loader的方式:

const webpack = require('webpack')
const webpackDevMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')

const webpackConfig = require('./webpack.dev.config')
const compiler = webpack(webpackConfig)

app.use(webpackDevMiddleware(compiler, {
  noInfo: true,
  publicPath: webpackConfig.output.publicPath
}))

app.use(webpackHotMiddleware(compiler))

Thanks in advance. 提前致谢。

So it was the babel configuration as I thought, you need the option modules: false in the env preset so it lets webpack handle the modules, it is a noob mistake but man, it drove me crazy for days. 所以这就是我所想的babel配置,您需要选项modules: false env预设中为modules: false ,以便让webpack处理这些模块,这是一个菜鸟的错误,但是,伙计,这让我发疯了好几天。

The correct config for the env preset its like so: 正确的环境预设配置如下:

['env', {modules: false}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM