-
Notifications
You must be signed in to change notification settings - Fork 19
Babel.js
Babel is a JavaScript transpiler that compiles not-yet-supported JavaScript, aka ESNext, into readable currently-supported JavaScript. For example, Babel allows the use of async/await, not currently supported in V8.
With Babel, applications can utilize superior next generation JavaScript features in advance of VM support. All ESNext features will soon be included in the V8 JavaScript VM upon which both node.js and io.js are built. As ESNext feature support is added to V8, Babel transpilation can be turned off feature-by-feature until it is no longer necessary. One recent example is the inclusion of Promise.
Essentially, Babel gives us a head start on the new JavaScript standard when writing node.js applications.
You should already have Babel installed after going through the Set-up guide. If not, install it:
npm install -g babel
Which will install two CLI executables: babel and babel-node. Use babel-node as a drop-in replacement for node to execute files containing ESNext:
//index.js
const name = 'World'
process.nextTick(()=> console.log(`Hello ${name}`))And run with:
$ babel-node index.jsFor convenience, Set-up also includes instructions for creating a babel-node alias, bode, and a Babel enabled nodemon alias, bodemon. Both use the --stage 1 Babel setting for experimental features like async/await.
Compiles files without running them. babel <file> with no options will output the file content to stdout:
$ babel index.js
//index.js
'use strict';
var name = 'World';
process.nextTick(function () {
return console.log('Hello ' + name);
});You can also compile to a dist/ directory, with the --out-dir option:
$ babel index.js --out-dir dist
index.js -> dist/index.jsWith the contents of index.js compiled to supported JavaScript in dist/index.js, start the server with node dist.
Compiles files and runs them. babel-node <file> works exactly like node <file> after compilation is complete.
$ babel-node index.js
Hello WorldA number of options can be used to enable/disable features, including:
Enable an array of optional transformers. Notable transformers include:
-
es7.asyncFunctions: Enablesasync/await -
strict: Automatically write"use strict"at the top of all compiled files. (Used in the Set-up alias)
$ babel-node --optional [es7.asyncFunctions, strict] index.js
$ babel --optional [es7.asyncFunctions, strict] index.jsEnable a group of experimental features.
The bode and bodemon aliases from Set-up use --stage 1 to enable experimental features including async functions as a convenient alternative to --optional above.
$ babel-node --stage 1 index.js
$ babel --stage 1 index.jsBoth --only and --ignore take a space separated array of glob patterns to determine which files to compile, but work oppositely. Patterns used with only are compiled, while those with ignore are not. Everything that is not compiled is outputted verbatim, regardless of the option used.
$ babel-node --only **/*.js .
$ babel --ignore **/*.html .Options can also be enabled using a .babelrc file in the current or user home directory. Enabling the above options would look like:
{
"stage": 1,
"optional: ["strict", "es7.classProperties"],
"ignore": [
"foo.js",
"bar/**/*.js"
]
}- Test features with the Babel REPL
- Overview of available ESNext features