简体   繁体   中英

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 .

In my package.json I include the following

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

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?

The easiest option would be to use https://github.com/babel/babel-preset-env . So you can install that and then in your Babel config do

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

and it will automatically configure the plugins for your current Node version.

Given the set of language features you mention, I don't think you need 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).

TL;DR; - For what you've described, I think you can probably just use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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