问题描述
我一直在大量搜索此错误,发现与babel有关,后者无法生成与我的async/await调用有关的代码.
I've been searching a lot on this error and found that is related to babel that cannot generate the code related to my async/await calls.
由于我不想删除它们,所以我想解决此问题.我已经尝试了很多在网络上找到的东西,但不能摆脱它.这是我的代码:
As I don't want to remove them, I would like to solve this issue. I've tried many things found on the web but can't get rid of it. Here is my code:
.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;
};
我将它们导入了我的根索引文件:
I imported these in my root index file:
import 'core-js/stable';
import 'regenerator-runtime/runtime';
我阅读了很多有关babel-polyfill的信息,但根据官方文档,自babel 7起不推荐使用它,所以我正尝试在没有它的情况下解决该问题.
I read a lot about babel-polyfill but according to the official documentation, it's deprecated since babel 7 so I'm trying to solve this without it.
推荐答案
遇到类似问题时,我使用了 core-js/modules/
文件夹中的polyills,并在webpack配置中使用了输入密钥作为 {条目:['core-js/modules/%polyfill_name%','%path_to_entry_file%',...]}
When I met the similar problem, I used polyills from core-js/modules/
folder, and used entry key in webpack config as{ entry: ['core-js/modules/%polyfill_name%', '%path_to_entry_file%',...] }
这篇关于异步/等待ReferenceError:找不到变量:regeneratorRuntime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!