巴贝尔和webpack正在抛出无法解决的再生器-Runtime/Runtime&39;(#39;t Resolve't Resolve'Runtime/Runtime&39;&q;)

2022-02-25 00:00:00 javascript webpack babeljs

我正在进行一个基于浏览器的项目,该项目需要与IE11兼容(叹息)。webpack被async/await哽咽了。以下是我的控制台的输出:

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

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

此问题的原因是什么?我希望通过手动导入regenerator-runtime/runtime就可以解决这个问题,但似乎babel-env的主要卖点之一是不必手动导入多边形填充,所以我假设我错过了一个步骤。谢谢!

以下是我尝试运行的内容,正在导入到另一个文件中:

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"
  }
}

解决方案

只需运行npm i regenerator-runtime即可实际修复该问题。

使用useBuiltIns: 'usage'时,我想没有必要拥有所有import语句。

`\_(ツ)_/`

相关文章