[Node] Use babel-preset-env with Native Node Features and Also Use Babel Plugins

In this lesson we'll show how to setup a .babelrc file with presets and plugins. Then create npm scripts that use babel-node and babel. With babel-preset-env we'll show how to target specific versions of node and how to use babel plugins, while not transpiling features (like async await) that are already supported by node natively.

 

package.json:

"devDependencies": {
    "babel-cli": "^6.24.1",
    "babel-plugin-transform-object-rest-spread": "^6.23.0",
    "babel-preset-env": "^1.5.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1"
  },

 

.babelrc:

{
  "presets": ["react", "es2015", ["env", {
    "targets": {
      "node": "current"
    }
  }]],
  "plugins": ["transform-object-rest-spread"]
}

node: "current", compile code based on your node version.

 

Node version larger than 7.6 will have async/await support by defualt. So if we already have 7.6 above, we don't want Babel to compile async/await to ES5 code.

  "presets": ["react", "es2015", ["env", {
    "targets": {
      "node": 7.6
    }
  }]]

 

posted @ 2017-06-06 14:13  Zhentiw  阅读(229)  评论(0编辑  收藏  举报