我正在尝试在Babel 6上从头开始使用async/await,但我得到的是regeneratorRuntime没有定义。

.babelrc文件

{
    "presets": [ "es2015", "stage-0" ]
}

package.json文件

"devDependencies": {
    "babel-core": "^6.0.20",
    "babel-preset-es2015": "^6.0.15",
    "babel-preset-stage-0": "^6.0.15"
}

.js文件

"use strict";
async function foo() {
  await bar();
}
function bar() { }
exports.default = foo;

在没有async/await的情况下正常使用它,效果很好。知道我做错了什么吗?


当前回答

安装@babel polyfill并将其保存在开发依赖项中

npm安装--保存dev@babel/polyfill

复制和粘贴需要(“@babel/polyfill”);在输入文件的顶部

入口.js

require("@babel/polyfill"); // this should be include at the top

在条目数组中添加@babel/polyfill您需要在预设数组中预设env

webpack.config.js

常量路径=require('path');要求(“@babel/polyfill”);//必要的,必要的模块导出={条目:['@babel/polyfill','./src/js/index.js'],输出:{路径:路径.解析(__dirname,'to_be_deployed/js'),文件名:'main.js'},模块:{规则:[{测试:/\.m?js$/,exclude:/node_modules/,使用:{loader:“babel loader”,选项:{预设:['@babel/preset-env']}}}]},mode:'开发'}

其他回答

我的工作babel 7样板,用于与再生器运行时反应:

巴氏合金

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": true,
        },
      },
    ],
    "@babel/preset-react",
  ],
  "plugins": [
    "@babel/plugin-syntax-class-properties",
    "@babel/plugin-proposal-class-properties"
  ]
}

包.json

...

"devDependencies": {
  "@babel/core": "^7.0.0-0",
  "@babel/plugin-proposal-class-properties": "^7.4.4",
  "@babel/plugin-syntax-class-properties": "^7.2.0",
  "@babel/polyfill": "^7.4.4",
  "@babel/preset-env": "^7.4.5",
  "@babel/preset-react": "^7.0.0",
  "babel-eslint": "^10.0.1",
...

main.js

import "@babel/polyfill";

....

如果使用babel-preset-stage-2,则只需使用--requirebabelpolyfill启动脚本即可。

在我的案例中,这个错误是由Mocha测试引发的。

以下修复了问题

mocha\“server/tests/**/*.test.js\”--编译器js:babel寄存器--需要babel polyfill

使现代化

如果你将目标设置为Chrome,它就会起作用。但它可能不适用于其他目标,请参阅:https://github.com/babel/babel-preset-env/issues/112

因此,这个答案对于最初的问题来说不太合适。我将把它保存在这里,作为babel预置env的参考。

一个简单的解决方案是在代码开头添加import“babel polyfill”。

如果您使用webpack,快速解决方案是添加babel polyfill,如下所示:

entry: {
    index: ['babel-polyfill', './index.js']
}

我相信我找到了最新的最佳实践。

检查此项目:https://github.com/babel/babel-preset-env

yarn add --dev babel-preset-env

将以下内容用作babel配置:

{
  "presets": [
    ["env", {
      "targets": {
        "browsers": ["last 2 Chrome versions"]
      }
    }]
  ]
}

那么你的应用程序应该可以在最近两个版本的Chrome浏览器中使用。

您还可以将节点设置为目标,或根据https://github.com/ai/browserslist

告诉我什么,不要告诉我怎么做。

我真的很喜欢babel preset env的哲学:告诉我你想要支持哪个环境,而不是告诉我如何支持它们。这是声明式编程的优点。

我已经测试了异步等待,它们确实有效。我不知道它们是如何工作的,我真的不想知道。我想把时间花在我自己的代码和业务逻辑上。多亏了babel预置env,它将我从babel配置地狱中解放出来。

1-安装babel插件,将异步转换为模块方法,babel polyfil,蓝鸟,babel-reset-es2015,babel芯:

npm install babel-plugin-transform-async-to-module-method babel-polyfill bluebird babel-preset-es2015 babel-core

2-添加js babel polyfill:

导入“babel polyfill”;

3-在.babelrc中添加插件:

{
    "presets": ["es2015"],
    "plugins": [
      ["transform-async-to-module-method", {
        "module": "bluebird",
        "method": "coroutine"
      }]
    ]
}

资料来源:http://babeljs.io/docs/plugins/transform-async-to-module-method/

如果您正在使用next js,请从“再生器运行时”添加import regeneratorRuntime;对于引发错误的文件,对我来说,该文件是document.js。

并添加

[
  "@babel/plugin-transform-regenerator",
  {
    "asyncGenerators": false,
    "generators": false,
    "async": false
  }
]

在插件中,位于.babelrc文件内。