使用transform-async-to-generator插件时,未定义Babel - regeneratorRuntime [英] Babel - regeneratorRuntime is not defined, when using transform-async-to-generator plugin

查看:530
本文介绍了使用transform-async-to-generator插件时,未定义Babel - regeneratorRuntime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法正确设置babel以使用async / await。

I am not able to setup babel correctly for the usage of async / await.

我使用的是babel 7和webpack 4.

I am using babel 7 and webpack 4.

如果可能,我不想使用babel-polyfill!

I do not want to use babel-polyfill if possible!

我的babelrc文件:

My babelrc file:

{
    "presets": [[
        "@babel/env",
        {"modules": false}
    ]],
    "plugins": [
      "syntax-dynamic-import",
      "transform-async-to-generator"
    ]
}

代码:

async function init() {
  const loaderData = await initLoader();
  initCmp(loaderData)
    .then(initApi(loaderData.key))
    .catch();
}
init();

错误:

refactor.main.js:18 Uncaught ReferenceError: regeneratorRuntime is not defined
    at eval (refactor.main.js:18)
    at eval (refactor.main.js:47)
    at Object../client/refactor.main.js (cmp.bundle.js:312)
    at __webpack_require__ (cmp.bundle.js:62)
    at eval (main.js:6)
    at Object../client/main.js (cmp.bundle.js:300)
    at __webpack_require__ (cmp.bundle.js:62)
    at cmp.bundle.js:179
    at cmp.bundle.js:182


推荐答案

此处的最新文档非常清楚:
https ://babeljs.io/docs/en/next/babel-plugin-transform-runtime

The latest documentation here is pretty clear: https://babeljs.io/docs/en/next/babel-plugin-transform-runtime

对我来说有用的是安装两个包用于构建和运行时(浏览器的最终脚本):

What worked for me is installing the two packages for build and for runtime (the final script for the browser):

npm install --save-dev @babe l / plugin-transform-runtime

npm install --save @ babel / runtime

在我的webpack配置的插件数组中,我刚添加了@ babel / plugin-transform-runtime,没有任何选项。 (请同时查看上面链接的文档,因为一些选项(您可能会在较旧的教程或答案中找到)已被删除。)

In the plugin Array of my webpack configuration I just added '@babel/plugin-transform-runtime' without any options. (Please also have a look at the documentation linked above, since some options (that you might find in older tutorials or answers) have been removed.)

plugins: [
'@babel/plugin-transform-runtime'
]

我现在可以使用异步函数而不会出错,并且它没有在生成版本中添加太多代码。

I now can use async functions without errors, and it didn't add much code in the production build.

这篇关于使用transform-async-to-generator插件时,未定义Babel - regeneratorRuntime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆