Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 280
Dan Ross Avatar asked Jun 24 '15 05:06

Dan Ross


People also ask

Should you use Babel with node?

If you've been active as a Node. js developer, or even dabbled in front-end libraries like React or Vue. js, then there's no doubt that you've likely run across Babel.

Can I use Babel node in production?

You should not be using babel-node in production. It is unnecessarily heavy, with high memory usage due to the cache being stored in memory. You will also always experience a startup performance penalty as the entire app needs to be compiled on the fly.


2 Answers

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.

like image 190
loganfsmyth Avatar answered Sep 23 '22 14:09

loganfsmyth


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;
like image 36
Jastrzebowski Avatar answered Sep 23 '22 14:09

Jastrzebowski