在节点6中用Babel使用异步/ wait

时间:2021-10-11 15:36:58

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

我正在为节点v6.9.2配置Babel。我想使用异步/等待结构。

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

因为我对Babel和所有节点基础设施都不熟悉,所以我不知道如何正确配置它:

  • 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.

    我应该使用什么预置?Node已经实现了ES6的大部分特性。因此,我不希望Babel显示节点6.9已经支持的特性。x(箭头函数、新导入机制等)因性能原因。

  • 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.

    我应该包含哪些插件才能使用异步/等待?在那里我也感到困惑,因为经过一些研究,我发现了几个插件:语法-异步函数、转换-异步-生成器等等。

Example of .babelrc will help.

babelrc的例子将会有所帮助。

Thanks

谢谢

2 个解决方案

#1


14  

What preset should I use?

我应该使用什么预设?

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.

你不需要使用任何预设。预置只是插件的集合,如果您想要传递一组特性(例如,所有ES2015都带有预置ES2015),它使使用变得更容易。但是,当您想只呈现这些特性的一个选择时,您只包含相应的插件。

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

我应该包括哪些插件以便使用异步/等待?

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

由于节点6支持生成器,您可以使用以下.babelrc:

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

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

当然,如果您需要传播更多不支持的特性,您需要添加插件。

Alternative babel-preset-env

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:

babel-preset-env自动确定在指定的环境中需要哪些插件。这将不包括任何不必要的插件。要指定当前节点版本,可以使用以下.babelrc:

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

#2


10  

Short answer

Use Babel preset for Node 6.x:

节点6.x使用Babel预设:

Long answer

To see what ES feature is supported in a given Node version, see:

要查看给定节点版本中支持的ES特性,请参见:

For async/await support in particular, see:

对于异步/等待支持,请参见:

If you use Node v7.x (the current version) then you can use the --harmony flag and use async/await natively without transpilation.

如果使用节点v7。x(当前版本),然后您可以使用—和谐标志,并使用异步/等待本机不进行传输。

Node v8.x (available as nightly builds) doesn't even need the --harmony flag for that.

节点v8。x(可作为夜间构建使用)甚至不需要—harmony标志。

But note that Node doesn't support import/export - to know why see:

但是请注意,Node不支持导入/导出—要知道为什么要看:

#1


14  

What preset should I use?

我应该使用什么预设?

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.

你不需要使用任何预设。预置只是插件的集合,如果您想要传递一组特性(例如,所有ES2015都带有预置ES2015),它使使用变得更容易。但是,当您想只呈现这些特性的一个选择时,您只包含相应的插件。

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

我应该包括哪些插件以便使用异步/等待?

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

由于节点6支持生成器,您可以使用以下.babelrc:

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

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

当然,如果您需要传播更多不支持的特性,您需要添加插件。

Alternative babel-preset-env

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:

babel-preset-env自动确定在指定的环境中需要哪些插件。这将不包括任何不必要的插件。要指定当前节点版本,可以使用以下.babelrc:

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

#2


10  

Short answer

Use Babel preset for Node 6.x:

节点6.x使用Babel预设:

Long answer

To see what ES feature is supported in a given Node version, see:

要查看给定节点版本中支持的ES特性,请参见:

For async/await support in particular, see:

对于异步/等待支持,请参见:

If you use Node v7.x (the current version) then you can use the --harmony flag and use async/await natively without transpilation.

如果使用节点v7。x(当前版本),然后您可以使用—和谐标志,并使用异步/等待本机不进行传输。

Node v8.x (available as nightly builds) doesn't even need the --harmony flag for that.

节点v8。x(可作为夜间构建使用)甚至不需要—harmony标志。

But note that Node doesn't support import/export - to know why see:

但是请注意,Node不支持导入/导出—要知道为什么要看: