简体   繁体   English

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

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

In my Node app I use import , arrow functions, the spread operator, object destructuring, let , and const .在我的 Node 应用程序中,我使用了import 、箭头函数、展开运算符、对象解构、 letconst

In my package.json I include the following在我的package.json我包含以下内容

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

as well as

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

and

"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"
},

The code works and is transpliled but I've noticed that, looking at the transpiled files, it's converting let to var , which seems pointless given Node 6.9.4 fully supports the use of let natively.代码有效并被转译,但我注意到,查看转译后的文件,它正在将let转换为var ,鉴于 Node 6.9.4 完全支持原生使用let ,这似乎毫无意义。

What is the most minimal set of babel plugins that will allow my code to run under Node 6.9.4 or better and will maximise use of its native language features?允许我的代码在 Node 6.9.4 或更高版本下运行并最大限度地利用其本地语言功能的最小 babel 插件集是什么?

The easiest option would be to use https://github.com/babel/babel-preset-env .最简单的选择是使用https://github.com/babel/babel-preset-env So you can install that and then in your Babel config do所以你可以安装它,然后在你的 Babel 配置中做

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

and it will automatically configure the plugins for your current Node version.它会自动为您当前的 Node 版本配置插件。

Given the set of language features you mention, I don't think you need stage-0 .鉴于您提到的一组语言功能,我认为您不需要stage-0

You might consider just using 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 at the time of this writing).您可能会考虑只使用babel-preset-es2015 ,它支持arrow-functionsdestructuringimport语句和Arrayspread运算符(对象 rest/spread仍然需要一个 单独的插件,因为在撰写本文时它只是第 3 阶段) .

TL;DR; TL; 博士; - For what you've described, I think you can probably just use: - 对于您所描述的,我认为您可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM