【问题标题】:Async/Await ReferenceError: Can't find variable: regeneratorRuntimeAsync/Await ReferenceError:找不到变量:regeneratorRuntime
【发布时间】:2019-12-03 08:39:41
【问题描述】:

我一直在搜索这个错误,发现它与 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 以来已被弃用,所以我试图在没有它的情况下解决这个问题。

【问题讨论】:

    标签: javascript webpack async-await babeljs


    【解决方案1】:

    当我遇到类似的问题时,我使用了 core-js/modules/ 文件夹中的 polyills,并在 webpack 配置中使用了入口键作为 { entry: ['core-js/modules/%polyfill_name%', '%path_to_entry_file%',...] }

    【讨论】:

    • 根据core-js的文档,我从根索引文件导入应该够了吧?
    • 是的,应该,但我什至在 core-js 中找不到 stable 文件夹
    • 嗯,我在 node_modules 的 core-js 文件夹下得到了它
    【解决方案2】:

    只需将此 babel 插件添加到您的 .babelrc 文件中,如下所示:

      "plugins": [
        ...other plugins,
        "@babel/plugin-transform-runtime"
      ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 2018-04-24
      • 2019-12-15
      • 1970-01-01
      相关资源
      最近更新 更多