繁体   English   中英

Babel 和 Webpack 正在抛出“无法解析 'regenerator-runtime/runtime'”

[英]Babel and Webpack are throwing "Can't resolve 'regenerator-runtime/runtime'"

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

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

实际上,只需运行npm i regenerator-runtime就修复了它。

使用useBuiltIns: 'usage' ,我猜不需要所有import语句。

¯\_(ツ)_/¯

只需在您拥有 async/await 的文件中添加import 'regenerator-runtime/runtime'

就我而言,您的回答还不够,我需要按照此处的建议将 babel sourceType设置为unambiguous的,以允许正确编译项目。 这个选项是必需的,因为@babel/runtime/regenerator/index.js文件使用module.exports = require("regenerator-runtime");导出它的引用。 这会破坏 ES6 编译。

另一个解决类似但不相关的编译问题的有用说明是使用/node_modules\/(css-loader|@babel|core-js|promise-polyfill|webpack|html-webpack-plugin|whatwg-fetch)\//作为exclude用于 babel 规则以避免解析循环,而不会丢失外部库编译(必要时),如此处所建议

TL;博士

在您的特定情况下,babel 规则将变为:

{
  test: /\.js$/,
  exclude: /node_modules/,
  loader: 'babel-loader',
  options: {
    sourceType: 'unambiguous',
    presets: [
      ['babel/preset-env', {
        corejs: 3,
        debug: true,
        targets: {
          browsers: [ 'IE 11', ],
        },
        useBuiltIns: 'usage',
      }],
    ],
  },
},

transpileDependencies: ['openplayerjs'],添加到我的vue.config.js时,我开始收到此错误,因为带有@vue/cli-plugin-babel/preset的 Babel 不会触及node_modules中的依赖项,go 说明了原因。 安装regenerator-runtime没有帮助,我认为问题在于 Yarn 的 pnp 模块:

This dependency was not found:

* regenerator-runtime/runtime.js in /Volumes/Backup/home/Documents/.yarn/unplugged/openplayerjs-npm-2.9.3-aa4692035d/node_modules/openplayerjs/dist/esm/media.js, /Volumes/Backup/home/Documents/.yarn/unplugged/openplayerjs-npm-2.9.3-aa4692035d/node_modules/openplayerjs/dist/esm/media/ads.js and 1 other

To install it, you can run: npm install --save regenerator-runtime/runtime.js

所以我尝试通过yarn unplug <module_name>拔掉所有东西,仍然没有用。

最终将useBuiltIns: 'usage'更改为useBuiltIns: 'entry' babel.config.js解决了这个问题。

按照官方文档

  1. 安装所需的软件包。
npm install --save-dev @babel/plugin-transform-runtime
npm install --save @babel/runtime

  1. 将插件添加到配置文件中。
{
  "plugins": ["@babel/plugin-transform-runtime"]
}

这是官方推荐的方法,在提到的文档中有更多可用的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM