我如何使用像(es6)polyfill这样的babel 6(es7)插件?

时间:2015-11-05 09:28:14

标签: node.js babeljs

问题

我在项目中使用Babel有相当多的经验,但我遇到了一个我不确定的情况:

我有 es5项目,它具有 es7节点模块的依赖 (部署工具)。

我希望es5项目能够使用es7模块,而不会使用Babel基础设施“污染”es5项目。

如果我的部署工具模块仅使用es6,我可以包含es6 polyfill

但由于没有es7 polyfill,我看起来应该使用[plugins](https://www.npmjs.com/package/babel-plugin-array-includes)。

编辑#1 :作为类比:如果我使用的是coffeescript,我只需要在my-coffee-module/dist/index.min.js中进行转换并保存输出。我怎么能对巴贝尔做同样的事呢?

我不想使用CLI或其他工具(比如rollupjs - 这很好,但似乎没必要)。

以前的工作

当我在我的模块中放置.babelrc(或使用 require hook )时:

require("babel-core").transform("code", {
    presets: ['babel-preset-es2015'],
    plugins: ["array-includes"]
});

然后从我的es5项目中使用它:

ReferenceError: Unknown plugin "babel-array-includes" specified in "base" at 0

如果我使用API​​(通过gulp-babel),我会得到:

TypeError: undefined is not a function
  at exports.default (/Users/me/deploy-tool/node_modules/babel-plugin-array-includes/lib/index.js:11:10)

这是对插件中Plugin未定义的抱怨:

  /*
   ... babel-plugin-array-includes/lib/index.js: lines 11-13
  */
  return new Plugin("array-includes", {
      metadata: {
        group: "builtin-post"
      },

任何建议或意见将不胜感激,谢谢

编辑#2:部分解决方案:此时似乎无法使用Babel CLI,我将使用npm build scripts

我会暂时搁置这个问题,等待github issue

0 个答案:

没有答案