我正在尝试在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的情况下正常使用它,效果很好。知道我做错了什么吗?


当前回答

当我尝试使用ES6生成器时,使用gulf with rollup时会出现以下错误:

gulp.task('scripts', () => {
  return rollup({
    entry: './app/scripts/main.js',
    format: "iife",
    sourceMap: true,
    plugins: [babel({
      exclude: 'node_modules/**',
      "presets": [
        [
          "es2015-rollup"
        ]
      ],
      "plugins": [
        "external-helpers"
      ]
    }),
    includePaths({
      include: {},
      paths: ['./app/scripts'],
      external: [],
      extensions: ['.js']
    })]
  })

  .pipe(source('app.js'))
  .pipe(buffer())
  .pipe(sourcemaps.init({
    loadMaps: true
  }))
  .pipe(sourcemaps.write('.'))
  .pipe(gulp.dest('.tmp/scripts'))
  .pipe(reload({ stream: true }));
});

我可能认为解决方案是将babel polyfill作为bower组件:

bower install babel-polyfill --save

并将其作为依赖项添加到index.html中:

<script src="/bower_components/babel-polyfill/browser-polyfill.js"></script>

其他回答

我有regeneratorRuntime未定义错误当我在react应用程序中使用“异步”和“等待”时“async”和“await”是ES7中的新关键字为此,您应该使用babel-preset-es2017安装此devDependencies:

`

“babel-preset-es2017”:“^6.24.1”,“babel预设反应”:“^6.24.1”,“babel-preset-stage-0”:“^6.24.1”`

并使用此

“预设”:[“es2017”,“stage-0”,“react”]

截至2019年10月,这对我有效:

将其添加到预设中。

 "presets": [
      "@babel/preset-env"
    ]

然后使用npm安装再生器运行时。

npm i regenerator-runtime

然后在主文件中使用:(此导入仅使用一次)

import "regenerator-runtime/runtime";

这将使您能够在文件中使用异步等待并删除再生器错误

我正在使用React和Django项目,并通过使用再生器运行时使其工作。你应该这样做,因为@babel/polyfill会增加你的应用程序的大小,而且也被弃用。我还遵循本教程的第1集和第2集创建了我的项目结构。

*包.json*

...
"devDependencies": {
    "regenerator-runtime": "^0.13.3",
    ...
}

巴氏合金

{
   "presets": ["@babel/preset-env", "@babel/preset-react"],
   "plugins": ["transform-class-properties"]
}

索引js

...
import regeneratorRuntime from "regenerator-runtime";
import "regenerator-runtime/runtime";
ReactDOM.render(<App />, document.getElementById('app'));
...

在2019年Babel 7.4.0+中,Babel polyfill包已被弃用,转而直接包含core js/stable(core-js@3+,到polyfill ECMAScript功能)和再生器运行时/运行时(需要使用转译生成器函数):

import "core-js/stable";
import "regenerator-runtime/runtime";

babel polyfill文档中的信息。

我的工作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";

....