gpt4 book ai didi

javascript - Babel 和 Webpack 正在抛出 "Can' t resolve 'regenerator-runtime/runtime' "

转载 作者:行者123 更新时间:2023-12-03 19:22:39 31 4
gpt4 key购买 nike

我正在开发一个需要与 IE11 兼容的基于浏览器的项目(叹气)。 Webpack 在 async/await 上卡住了.这是我的控制台的输出:

Based on your code and targets, added regenerator-runtime.
...
Module not found: Error: Can't resolve 'regenerator-runtime/runtime'

我看过 许多 SO问题与我的类似,没有运气。许多人建议使用 @babel/polyfill我正在避免,因为它已被弃用。

是什么导致了这个问题?我希望它可以通过手动导入来修复 regenerator-runtime/runtime ,但似乎是 babel-env的主要卖点之一不必手动导入 polyfill,所以我假设我错过了一个步骤。谢谢!

这是我正在尝试运行的内容,正在导入到另一个文件中:
class Fetcher {
constructor() {
this.form = document.querySelector('form');
this.form.addEventListener('submit', this.onSubmit.bind(this));
}

async onSubmit(event) {
event.preventDefault();

const apiResponse = await fetch(`${WP_url}/api`);
const apiJson = await apiResponse.json();
console.log(apiJson);
}
}
export default Fetcher;

webpack.config.js :
const path = require('path');

function pathTo(filepath) {
return path.join(__dirname, filepath);
}

module.exports = function(env, argv) {
const isProd = Boolean(argv.mode === 'production');

const config = {
entry: {
index: [
pathTo('index.js'),
],
},
externals: {
jquery: 'jQuery',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
corejs: 3,
debug: true,
targets: {
browsers: [
'IE 11',
],
},
useBuiltIns: 'usage',
},
],
],
},
},
],
},
optimization: {
minimize: isProd,
},
output: {
filename: '[name].js',
path: pathTo('web'),
},
};

return config;
};

包.json
{
"private": true,
"dependencies": {
"core-js": "^3.4.1",
"focus-within-polyfill": "^5.0.5"
},
"devDependencies": {
"@babel/core": "^7.7.2",
"@babel/preset-env": "^7.7.1",
"babel-eslint": "^10.0.3",
"babel-loader": "^8.0.6",
"css-loader": "^3.2.0",
"eslint": "^6.6.0",
"mini-css-extract-plugin": "^0.8.0",
"node-sass": "^4.13.0",
"sass-loader": "^8.0.0",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10"
},
"scripts": {
"dev": "webpack --mode=development --display-modules",
"dev:watch": "npm run dev -- --watch",
"prod": "webpack --mode=production --display-modules",
"prod:watch": "npm run prod -- --watch"
}
}

最佳答案

简单运行npm i regenerator-runtime修复它,实际上。

useBuiltIns: 'usage' , 拥有所有 import我猜没有必要声明。

¯\_(ツ)_/¯

关于javascript - Babel 和 Webpack 正在抛出 "Can' t resolve 'regenerator-runtime/runtime' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58919016/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com