异步/等待ReferenceError:找不到变量:regeneratorRuntime

时间:2019-12-03 08:39:41

标签: javascript webpack async-await babel

我一直在大量搜索此错误,发现与babel有关,后者无法生成与我的async / await调用有关的代码。

由于我不想删除它们,所以我想解决此问题。我已经尝试了很多在网上找到的东西,但不能摆脱它。这是我的代码:

.babelrc

{
  "presets": [
    ["@babel/preset-env", { "useBuiltIns": "usage", "corejs": 3 }],
    "@babel/preset-react",
    "@babel/preset-typescript"
  ],
  "plugins": [
    "@babel/plugin-transform-async-to-generator",
    "@babel/plugin-proposal-class-properties",
    [
      "@babel/plugin-transform-runtime",
      {
        "regenerator": true
      }
    ],
    "@babel/plugin-transform-regenerator"
  ]
}

webpack.config.js

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = (env, { mode = 'development' }) => {
  const config = {
    mode,
    entry: ['./src/index.tsx'],
    devtool: '',
    resolve: {
      modules: ['src', 'node_modules'],
      extensions: ['.js', '.jsx', '.ts', '.tsx'],
    },
    module: {
      rules: [
        {
          test: /\.(js|jsx|tsx|ts)$/,
          enforce: 'pre',
          loader: 'eslint-loader',
          exclude: /node_modules/,
          options: {
            emitError: true,
            configFile: './.eslintrc.js',
          },
        },
        {
          test: /\.(png|svg|jpg|gif)$/,
          use: [
            {
              loader: 'file-loader',
            },
          ],
        },
        {
          test: /\.scss$/,
          use: [
            { loader: 'style-loader' },
            { loader: 'css-loader' },
            { loader: 'sass-loader' },
          ],
        },
        {
          test: /\.less$/,
          use: [
            { loader: 'style-loader' },
            { loader: 'css-loader' },
            { loader: 'less-loader' },
          ],
        },
        {
          test: /\.css$/,
          use: [{ loader: 'style-loader' }, { loader: 'css-loader' }],
        },
        {
          test: /\.(js|jsx|tsx|ts)$/,
          exclude: /node_modules/,
          // use: {
          //   loader: 'babel-loader',
          //   options: {
          //     presets: [
          //       ['@babel/preset-env', { useBuiltIns: false }],
          //       '@babel/preset-react',
          //       '@babel/preset-typescript',
          //     ],
          //     plugins: [
          //       '@babel/plugin-transform-async-to-generator',
          //       '@babel/plugin-proposal-class-properties',
          //       [
          //         '@babel/plugin-transform-runtime',
          //         {
          //           helpers: true,
          //           regenerator: true,
          //         },
          //       ],
          //       '@babel/plugin-transform-regenerator',
          //     ],
          //   },
          // },
          use: 'babel-loader',
        },
      ],
    },
    output: {
      path: path.resolve(__dirname, 'dist'),
      filename: 'bundle.js',
      libraryTarget: 'umd',
      publicPath: '/dist/',
      umdNamedDefine: true,
    },
    optimization: {
      mangleWasmImports: true,
      mergeDuplicateChunks: true,
      minimize: true,
      nodeEnv: 'production',
    },
    plugins: [
      new webpack.DefinePlugin({
        'process.env.NODE_ENV': '"production"',
      }),
      new HtmlWebpackPlugin({
        filename: path.resolve(__dirname, 'dist/index.html'),
        template: path.resolve(__dirname, 'src', 'index.html'),
      }),
    ],
  };

  /**
   * If in development mode adjust the config accordingly
   */
  if (mode === 'development') {
    config.devtool = 'source-map';
    config.output = {
      filename: '[name]/index.js',
    };
    config.module.rules.push({
      loader: 'source-map-loader',
      test: /\.js$/,
      exclude: /node_modules/,
      enforce: 'pre',
    });
    config.plugins = [
      new webpack.DefinePlugin({
        'process.env.NODE_ENV': '"development"',
      }),
      new HtmlWebpackPlugin({
        filename: path.resolve(__dirname, 'dist/index.html'),
        template: path.resolve(__dirname, 'src', 'index.html'),
      }),
      new webpack.HotModuleReplacementPlugin(),
    ];
    config.devServer = {
      contentBase: path.resolve(__dirname, 'dist'),
      publicPath: '/',
      stats: {
        colors: true,
        hash: false,
        version: false,
        timings: true,
        assets: true,
        chunks: false,
        modules: false,
        reasons: false,
        children: false,
        source: false,
        errors: true,
        errorDetails: true,
        warnings: false,
        publicPath: false,
      },
    };
    config.optimization = {
      mangleWasmImports: true,
      mergeDuplicateChunks: true,
      minimize: false,
      nodeEnv: 'development',
    };
  }
  return config;
};

我将它们导入了我的根索引文件:

import 'core-js/stable';
import 'regenerator-runtime/runtime';

我读了很多关于babel-polyfill的信息,但是根据官方文档,自babel 7起不推荐使用它,所以我想在没有它的情况下解决这个问题。

1 个答案:

答案 0 :(得分:0)

遇到类似问题时,我使用了java.lang.NullPointerException: Attempt to invoke a virtual method on a null object reference at com.frontrow.vlog.ui.personalpage.PersonalPageFragment.musicClicked(SourceFile:1) // The line number is error. at com.frontrow.vlog.ui.personalpage.PersonalPageFragment_ViewBinding$e.a(SourceFile:1) at butterknife.internal.b.onClick(SourceFile:4) 文件夹中的polyills,并在webpack config中使用了Enter键,如下所示: core-js/modules/