Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

reactjs - How to fix a Babel/runtime/helper issue in Webpack 5?

I want to upgrade to webpack 5. I've followed the official guide, upgraded all critical libraries (react17, babel, loaders, etc.). When launching the app, it crashes with 23 errors. 21 of them come from @babel/runtime/helpers.

A typical error looks like this:

ERROR in ../../node_modules/@babel/runtime/helpers/esm/createSuper.js 1:0-46 Module not found: Error: Can't resolve './getPrototypeOf' in '/Users/myName/Desktop/myapp/node_modules/@babel/runtime/helpers/esm'

The two other errors are:

Module not found: Error: Can't resolve 'url-loader' ERROR in FaviconsWebpackPlugin - This FaviconsWebpackPlugin version is not compatible with your current HtmlWebpackPlugin version. Please upgrade to HtmlWebpackPlugin >= 5 OR downgrade to FaviconsWebpackPlugin 2.x

Note: My html-webpack-plugin version is above 5 and favicons-webpack-plugin is the latest version as well...

Anyway, here is my webpack file:

const FaviconsWebpackPlugin = require("favicons-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const path = require("path");
const TerserPlugin = require("terser-webpack-plugin");
const webpack = require("webpack");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const getKonf = require("./konf");
const getWebpackServerOptions = require("./server");

function buildWebpackConfiguration() {
  const konfiguration = getKonf("development"); // returns a large json with proxies, token, etc.
  const rootPath = path.resolve(__dirname, "../../../");
  return {
    devtool: "eval-source-map",
    mode: "development",
    node: {
      global: false,
      __filename: false,
      __dirname: false,
    },
    resolve: {
      extensions: [".json", ".jsx", ".js", ".tsx", ".ts"],
      alias: {
        "@componens": "./components",
        "react-dom": "@hot-loader/react-dom",
      },
    },
    module: {
      rules: [
        {
          test: /.jsx?$/,
          exclude: /node_modules/(?!(redux-logger|strict-uri-encode|query-string)/).*/,
          use: [
            {
              loader: "babel-loader",
              options: {
                configFile: path.resolve(rootPath, "./babel.config.js"),
              },
            },
            "react-hot-loader/webpack",
          ],
        },
        {
          test: /.html$/,
          use: ["html-loader"],
        },
        {
          test: /.less$/,
          exclude: /.m.less$/,
          use: [MiniCssExtractPlugin.loader, "css-loader", "less-loader"],
        },
        {
          test: /.(eot|gif|jpg|png|svg|ttf|woff)$/,
          exclude: [
            path.resolve(rootPath, "./assets/svg"),
            path.resolve(rootPath, "./icon/glyphs"),
            path.resolve(rootPath, "./search/assets"),
          ],
          use: "url-loader?limit=1024",
        },
        {
          test: /.m.less$/,
          use: [
            MiniCssExtractPlugin.loader,
            {
              loader: "css-loader",
              options: {
                modules: {
                  localIdentName: "[local]___[hash:base64:5]",
                },
              },
            },
            "less-loader",
          ],
        },
        {
          test: /.svg$/,
          use: {
            loader:
              "svg-inline-loader?{'removeTags': true, 'removingTags': ['title', 'desc']",
          },
        },
        {
          test: /.tsx?$/,
          use: [{ loader: "ts-loader", options: { transpileOnly: true } }],
          exclude: /node_modules/,
        },
        {
          include: /assets/sw/,
          test: /.js$/,
          loader: "file-loader",
        },
      ],
    },
    plugins: [
      new HtmlWebpackPlugin({
        title: "My app",
        template: path.resolve(__dirname, "./template/index.ejs"),
        minify: true,
        hash: true,
        scripts: [],
        styles: [],
      }),
      new webpack.DefinePlugin({
        "process.env.NODE_ENV": JSON.stringify("development"),
        "process.env.MOCK_REQUESTS": JSON.stringify(
          process.env.MOCK_REQUESTS || "0"
        ),
        KONF: JSON.stringify(konfiguration),
      }),
      new FaviconsWebpackPlugin({
        logo: path.resolve(rootPath, "./assets/logo.svg"),
        inject: true,
        title: "My App",
      }),
      new webpack.HotModuleReplacementPlugin(),
      new webpack.ContextReplacementPlugin(/moment[/\]locale$/, /de|en|fr|zh/),
      new MiniCssExtractPlugin({
        filename: "[name].[contenthash].css",
        chunkFilename: "[id].[contenthash].css",
      }),
      new BundleAnalyzerPlugin(),
    ],
    optimization: {
      minimize: true,
      minimizer: [new OptimizeCSSAssetsPlugin({}), new TerserPlugin()],
      moduleIds: "deterministic",
      splitChunks: {
        chunks: "all",
        cacheGroups: {
          defaultVendors: {
            test: /[\/]node_modules[\/]/,
            name: "node_vendors",
            chunks: "all",
          },
        },
      },
    },
    devServer: getWebpackServerOptions(konfiguration),
  };
}

module.exports = buildWebpackConfiguration;
question from:https://stackoverflow.com/questions/65905907/how-to-fix-a-babel-runtime-helper-issue-in-webpack-5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...