Dave Sag
Dave Sag

Reputation: 13486

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?

Upvotes: 3

Views: 352

Answers (2)

loganfsmyth
loganfsmyth

Reputation: 161677

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.

Upvotes: 5

Troy Alford
Troy Alford

Reputation: 27256

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

Upvotes: 0

Related Questions