【发布时间】:2020-03-14 02:03:09
【问题描述】:
我正在开发一个需要与 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;
};
package.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"
}
}
【问题讨论】:
标签: javascript webpack babeljs