简体   繁体   English

如何使用 Webpack 5 运行热重载 (HMR)?

[英]How can I get hot reloading (HMR) running with Webpack 5?

I am trying to get HMR running with webpack v5, but it does not work.我试图让 HMR 与 webpack v5 一起运行,但它不起作用。 When I modify and save a file, webpack re-compiles the project correctly, but the frontend does not update.当我修改并保存文件时,webpack 重新正确编译项目,但前端没有更新。

I read this article and followed the instructions: https://webpack.js.org/guides/hot-module-replacement/我阅读了这篇文章并按照说明操作: https://webpack.js.org/guides/hot-module-replacement/

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

{
  mode: 'development',
  entry: {
    babelpoly: 'babel-polyfill',
    app: [ './src/index.js', './src/app.js' ]
  },
  plugins: [
    BundleStatsWebpackPlugin { ... },
    DefinePlugin { ... },
    HtmlWebpackPlugin { ... }
  ],
  stats: { ... },
  output: {
    path: '[pathTo]/dist',
    filename: '[name].bundle.js',
    chunkFilename: '[name].bundle.js'
  },
  optimization: {
    splitChunks: { chunks: 'all' },
    runtimeChunk: 'single',
    usedExports: true,
    mergeDuplicateChunks: true
  },
  module: {    
    rules: [
      {
        test: /\.(s[ac]ss)$/i,
        include: path.resolve(ROOT, 'src'),
        use: [
            'style-loader', // Creates `style` nodes from JS strings
            {
            loader: 'css-loader', // Translates CSS into CommonJS
            options: {
                modules: {
                mode: 'local',
                localIdentName: devMode
                    ? '[name]_[local]-[hash:base64:3]'
                    : '[local]-[hash:base64:5]',
                },
            },
            },
            {
            loader: 'sass-loader', // compiles Sass to CSS
            options: {
                implementation: require('sass'),
            },
            },
        ],
      },
      {
        test: /\.css$/,
        include: [path.join(ROOT, 'node_modules')],
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: ['file-loader'],
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: ['file-loader'],
      },
      {
        test: /\.m?jsx?$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              [
                '@babel/preset-env',
                {
                  useBuiltIns: 'entry',
                  corejs: 3,
                },
              ],
              '@babel/preset-react',
            ],
            plugins: [['@babel/plugin-proposal-class-properties', {loose: true}]],
          },
        },
      },
    ],
  },
  resolve: {
    extensions: [ ... ],
    alias: {
      ...
    },
    modules: [
      ...
    ]
  },
  devtool: 'inline-source-map',
  devServer: {
    port: 3003,
    contentBase: '[pathTo]/dist',
    host: '0.0.0.0',
    hot: true,
    compress: true,
    disableHostCheck: true,
    historyApiFallback: true,
    overlay: { warnings: true, errors: true },
    stats: { ... }
  }
}

I am using:我在用:

  • webpack 5.7.0 webpack 5.7.0
  • webpack-cli 4.2.0 webpack-cli 4.2.0
  • react 16.13反应 16.13
  • node 14.15.1节点 14.15.1
  • npm 6.14.8 npm 6.14.8

I start webpack with this command: webpack serve --host 0.0.0.0 --config config/webpack.dev.js我使用以下命令启动 webpack: webpack serve --host 0.0.0.0 --config config/webpack.dev.js

What do I do wrong?我做错了什么? Thanks for your help.谢谢你的帮助。 :) :)

I believe it's a bug in webpack-dev-server v3 https://github.com/webpack/webpack-dev-server/issues/2758 when you're using it with webpack 5 and browserslist .当您将它与webpack 5 和browserslist一起使用时,我相信这是webpack-dev-server v3 https://github.com/webpack/webpack-dev-server/issues/2758中的一个错误。 You can wait for webpack-dev-server v4 https://github.com/webpack/webpack-dev-server/pull/2592#issuecomment-734400875 , it will come soon, or use target: 'web' for the moment under your development mode.您可以等待webpack-dev-server v4 https://github.com/webpack/webpack-dev-server/pull/2592#issuecomment-734400875 ,它很快就会到来,或者使用target: 'web'下你的development模式。

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

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