在 Node 6 中使用 async/await 和 Babel [英] Using async/await in Node 6 with Babel

查看:25
本文介绍了在 Node 6 中使用 async/await 和 Babel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Node v6.9.2 配置 Babel.我想使用 async/await 结构.

I'm trying to configure Babel for Node v6.9.2. I want to use async/await constructs.

因为我是 Babel 和所有 Node 基础设施的新手,我对如何正确配置它感到困惑:

Because I'm new to Babel and all Node infrastructure, I confused how to configure it properly:

  • 我应该使用什么预设?Node 已经实现了大部分 ES6 特性.因此,出于性能原因,我不希望 Babel 转译 Node 6.9.x 已经支持的功能(箭头函数、新的导入机制等).

  • What preset should I use? Node is already implemented most of the ES6 features. So I don't want Babel to transpile features already supported by Node 6.9.x (arrow functions, new import mechanism etc) for performance reasons.

我应该包含哪些插件才能使用 async/await?在那里我也很困惑,因为经过一些研究我发现了几个插件:syntax-async-functionstransform-async-to-generator 等等.

What plugins should I include so I can use async/await? There I also confused, because after some researching I found several plugins: syntax-async-functions, transform-async-to-generator and some more.

.babelrc 的例子会有所帮助.

谢谢

推荐答案

我应该使用什么预设?

What preset should I use?

您不需要使用任何预设.预设只是一组插件,如果您想转换一组功能(例如所有带有 preset-es2015 的 ES2015),它会更容易使用.但是当您只想转译这些功能的一部分时,您只需包含相应的插件.

You don't need to use any preset. Presets are just a collection of plugins which makes it easier to use if you want to transpile a set of features (for instance all ES2015 with preset-es2015). But when you want to transpile only a selection of these features, you only include the corresponding plugins.

我应该包含哪些插件才能使用 async/await?

What plugins should I include so I can use async/await?

因为 Node 6 支持生成器,你可以将 transform-async-to-generator 与以下 .babelrc 一起使用:

Because Node 6 supports generators, you can use transform-async-to-generator with the following .babelrc:

{
  "plugins": ["transform-async-to-generator"]
}

当然,如果您需要转译更多不受支持的功能,则需要添加插件.

And of course you would need to add plugins if you need to transpile more unsupported features.

babel-preset-env 会自动确定指定环境所需的插件.这将不包括任何不必要的插件.要指定您当前的 Node 版本,您可以使用这个 .babelrc:

babel-preset-env automatically determines what plugins you need for the specified environment. This will not include any plugins that are not necessary. To specify your current Node version you would use this .babelrc:

{
  "presets": [
    ["env", {
      "targets": {
        "node": "current"
      }
    }]
  ]
}

这篇关于在 Node 6 中使用 async/await 和 Babel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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