为 Node 6 转译所需的最少一组 babel 插件是什么

What is the most minimal set of babel plugins needed to transpile for Node 6

在我的 Node 应用程序中,我使用 import、箭头函数、展开运算符、对象解构、letconst

在我的 package.json 中包含以下内容

"engines": {
  "node": ">=6.9.4",
  "npm": "^3"
},

以及

"babel": {
  "presets": [
    "node6",
    "stage-0"
  ]
},

"scripts": {
  "clean": "rm -rf bin/",
  "start": "node bin/index.js",
  "babel": "babel src --out-dir bin",
  "build": "npm run clean && npm run babel",
  "dev": "babel-node src/index.js",
  "test": "find ./test -name '*_spec.js' | NODE_ENV=test xargs mocha --compilers js:babel-core/register --require ./test/test_helper.js"
},

代码有效并被转换,但我注意到,查看转换后的文件,它正在将 let 转换为 var,鉴于 Node 6.9.4 完全支持使用,这似乎毫无意义let 个本地人。

允许我的代码在 Node 6.9.4 或更高版本下 运行 并最大限度地利用其本地语言功能的最小 babel 插件集是什么?

最简单的选择是使用 https://github.com/babel/babel-preset-env。所以你可以安装它,然后在你的 Babel 配置中做

{
  presets: [['env', {targets: {node: true}}]]
}

它会自动为您当前的 Node 版本配置插件。

鉴于您提到的一组语言功能,我认为您不需要 stage-0

您可能会考虑在撰写本文时仅使用 babel-preset-es2015, which supports arrow-functions, destructuring, import statements and the spread operator for Array (Object rest/spread still requires a separate plugin as it's only stage-3

TL;DR; - 对于您所描述的内容,我认为您可以只使用:

"babel": {
  "presets": ["es2015"]
}