With the release of Node 6.0.0, the surface of code that needs transpilation to use ES6 features has been reduced very dramatically.
This is what my current workflow looks like to set up a minimalistic and fast microservice using micro and async
+ await
.
To write a service that displays the current Bitcoin price we should be able to write an lib/index.js
like so:
import request from 'request-promise';
const URL = 'https://api.bitcoinaverage.com/ticker/global/USD';
export default async () => {
const price = await request(URL, { json: true });
return `The price is ${price.last} as of ${price.timestamp}`;
}
and to run it with node run.js
:
import serve from 'micro-core';
import api from './lib';
serve(api).listen(8080, (err) => {
if (err) throw err;
console.log('Listening on *:8080');
});
The only notable feature that’s missing in V8 as far as ES6 support is import
and export
(i.e: the module syntax).
async
and await
is coming to V8 but it’s not ready yet. This means that our babel configuration in package.json
is simply down to two modules.
{
"babel": {
"plugins": [
"transform-es2015-modules-commonjs",
"transform-async-to-generator"
]
}
}
Since we can compile to generators instead of regenerator and Promise
is built-in, Babel can do exclusively the job it was designed to do: transform code. No babel-runtime
!
If you’ve used babel-node
in the past you probably noticed it could be slow and resorted to more complicated pipelines with gulp
or make
.
Since the amount of work babel has to do has now been decreased to a minimum, it’s now feasible to run your programs with it again!
During development you could then execute.
$ babel-node run.js
When you make a change, you just re-run your code without an explicit build step and with great support for source maps.
To optimize startup time to the fullest, you probably still want to pre-compile before you ship. For that, you can define a task like this:
"scripts": {
"build": "babel lib --out-dir dist"
}
If you plan to publish, make sure that you only publish that directory:
"files": ["dist"],
"scripts": {
"prepublish": "npm run build"
}
Finally, micro-core
gives you a little binary to launch the exported microservice easily:
"scripts": {
// …
"start": "micro-serve -p 3000 dist/"
}
babel-runtime
isn't actually the problem, it just reduces a bunch of code duplication. Are you thinking ofbabel-runtime/regenerator
?