Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 异步/等待引用错误:Can';找不到变量:regeneratorRuntime_Javascript_Webpack_Async Await_Babeljs - Fatal编程技术网

Javascript 异步/等待引用错误:Can';找不到变量:regeneratorRuntime

Javascript 异步/等待引用错误:Can';找不到变量:regeneratorRuntime,javascript,webpack,async-await,babeljs,Javascript,Webpack,Async Await,Babeljs,我一直在搜索这个错误,发现它与babel有关,babel无法生成与我的异步/等待调用相关的代码 因为我不想删除它们,所以我想解决这个问题。我试过很多在网上找到的东西,但都无法摆脱。这是我的密码: .babelrc { "presets": [ ["@babel/preset-env", { "useBuiltIns": "usage", "corejs": 3 }], "@babel/preset-react", "@babel/preset-typescript"

我一直在搜索这个错误,发现它与babel有关,babel无法生成与我的异步/等待调用相关的代码

因为我不想删除它们,所以我想解决这个问题。我试过很多在网上找到的东西,但都无法摆脱。这是我的密码:

.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;
};
我在我的根索引文件中导入了以下内容:

导入'core js/stable';
导入“再生器运行时/运行时”;

我读了很多关于babel polyfill的书,但是根据官方文档,它从babel 7开始就被弃用了,所以我尝试在没有它的情况下解决这个问题。

当我遇到类似的问题时,我使用了
core js/modules/
文件夹中的PolyIll,并使用了webpack config中的entry key作为
{entry:['core-js/modules/%polyfill_name%','%path_to_entry_file%',…])

根据core js的文档,我从根索引文件导入的内容应该足够了?是的,应该足够了,但我甚至在core js中找不到稳定的文件夹。我在node_modules中的core js文件夹下找到了它