【问题标题】:How to use ES6 and ES7 in my Gruntfile如何在我的 Gruntfile 中使用 ES6 和 ES7
【发布时间】:2017-03-04 17:48:17
【问题描述】:

我想在我的 Gruntfile 中使用 ES6 和 ES7。原因是我想写一个任务,其中包括git-repository module。正如您从文档中看到的那样,该模块仅在 ES6 和 ES7 中可用,我希望尽可能简单地集成该模块。有没有一种方法可以在我的 Gruntfile 中使用 ES6 和 ES7 - 类似于babel grunt?不幸的是,我在谷歌上没有找到任何东西,因此我希望你能帮助我。

提前感谢您! :-)

【问题讨论】:

  • 一种方法是在 es6 或 es7 中写入文件,然后在执行之前将其编译为 es5
  • 是的,我知道这一点,但通常应该有一种方法可以在 ES6 中编写你的任务,不是吗?
  • 你已经在 Node 环境中拥有了 ES6 特性集(除了 ES6 模块)。您所指的功能(async/await)没有标准化,属于所谓的 ES.next。

标签: node.js git gruntjs ecmascript-6 ecmascript-next


【解决方案1】:

通常您可能希望使用构建工具进行转译,因此这是一个“谁将构建构建工具”的问题。

NPM 上的公共包通常仅在 ES.next 或 ES6 中可用,并且具有 Node 不支持的功能集。 git-repository 也不例外。它肯定在包中已经转译了代码,并且可以在没有 Babel 的情况下使用。

因为包是用babel-plugin-transform-runtime 转译的,所以它需要babel-polyfill 才能工作。

文档仅使用async...await 作为示例,因为它适合工作流程。 async 函数使用 Promise,在 ES5/ES6 中是

require('babel-polyfill');

Repo.open('./example', { init: true })
.then(repo =>
  repo.setRemote('origin', 'https://github.com/user/example.git')
  .then(() => repo.add('--all .'))
  .then(() => repo.commit('Commit message'))
  ...
);

对于不需要转译器的 Node ES6 功能集,coasync...await 的绝佳替代品。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 2017-10-29
    • 2020-12-06
    • 2016-07-19
    • 1970-01-01
    • 2017-10-17
    • 2016-02-13
    相关资源
    最近更新 更多