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

2.3k views Asked by At

I am trying to compile a Koa app, and Koa has assertions that check to make sure that I am passing generator functions as middleware. However, I would like to compile my server side code from ES7 using Babel, for consistency with the front end code.

Is it possible to target node harmony, instead of ES5? I don't see anything promising in the options, but choosing a target seems like a standard thing to be able to do with a compiler.

update

Blacklisting Babel's regenerator transform seems to have no effect, even though I am using 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;

Execute: node --harmony index.js

node --version
v0.12.4
2

There are 2 answers

4
loganfsmyth On BEST ANSWER

There isn't really a standard definition of --harmony since it would depend on what version of Node or iojs you happen to be using. The best you can do with Babel is explicitly decide what transformations to run. Babel allows you to provide a whitelist and/or blacklist option e.g.

{
  blacklist: [
    'es6.classes'
  ]
}

for instance would stop transpiling ES6 classes and rely on your platform supporting them. The main list of transforms is here.

'regenerator' in this case would disable transpiling generators. If you disable that however, and you are using async functions, you'd want to then pass optional: ['asyncToGenerator'] to enable the transformation of async functions into standard generators with a wrapper function, as they'd otherwise end up in the final output.

1
Jastrzebowski On

Strange it's seems to be working from the CLI (with minor changes in 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;