[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.node

 

package.json:react

"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:npm

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

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

 

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

  "presets": ["react", "es2015", ["env", {
    "targets": {
      "node": 7.6
    }
  }]]
相關文章
相關標籤/搜索