Can Babel compile for "node - harmony" instead of ES5?

I am trying to compile a Koa application, and Koa has statements that verify that I pass the generator functions as middleware. However, I would like to compile my server side code from ES7 using Babel for interface consistency.

Is it possible to configure the target node, not ES5? I do not see anything promising in the settings, but the choice of purpose seems standard, which can be done with the compiler.

Update

Blacklisting Babel regenerator conversion seems inefficient, although I use stage: 1 .

index.js:

 require( "babel/register" )({ sourceMaps: "inline", stage: 1, blacklist: [ "regenerator" ], optional: [ "asyncToGenerator" ] }); var app = require( "./src/server" ); app.listen( process.env.port || 3000 ); 

Src / server.js:

 import koa from "koa"; import router from "koa-router"; router.get( "/", function *( next ) { this.body = "Hi!"; }); let app = koa(); app.use( router() ); export default app; 

Run: node --harmony index.js

 node --version v0.12.4 
+5
source share
2 answers

There is actually no standard definition of --harmony , as it will depend on which version of Node or iojs you are using. The best thing you can do with Babel is clearly deciding which transforms to run. Babel allows you to provide a whitelist and / or blacklist option, for example.

 { blacklist: [ 'es6.classes' ] } 

for example, it stops ES6 class forwarding and relies on a platform that supports them. The basic list of conversions is here .

'regenerator' in this case will disable the generators with the extension. If you disable this and you use async functions, you must then pass optional: ['asyncToGenerator'] to enable the conversion of asynchronous functions to standard generators using the wrapper function, since they would end up in the end result.

+4
source

Strange, it seems to work from the CLI (with minor changes to server.js)

 babel-node --blacklist regenerator --harmony server.js 

Code for server.js:

 import koa from "koa"; import router from "koa-router"; const Router = router(); Router.get( "/", function *( next ) { this.body = "Hi foo!"; }); let app = koa(); app.use( Router.routes() ); export default app; 
+1
source

All Articles