What is Babel?
Babel is a JavaScript compiler
Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments. Here are the main things Babel can do for you:
- Transform syntax
- Polyfill features that are missing in your target environment (through core-js)
- Code transformations (codemods)
- More (check out these videos for inspiration)
ES2015 and beyond
Babel has support for the latest version of JavaScript through syntax transformers.
These plugins allow you to use new syntax, right now without waiting for browser support. Check out our usage guide to get started.
JSX and Flow
Babel can convert JSX syntax and strip out type annotations. Check out our React preset to get started. Use it together with the babel-sublime package to bring syntax highlighting to a whole new level.
You can install this preset with
npm install --save-dev @babel/preset-react
and don't forget to add @babel/preset-react
to your Babel configuration.
export default React.createClass({
getInitialState() {
return { num: this.getRandomNumber() };
},
getRandomNumber(): number {
return Math.ceil(Math.random() * 6);
},
render(): any {
return <div>
Your dice roll:
{this.state.num}
</div>;
}
});
Pluggable
Babel is built out of plugins. Compose your own transformation pipeline using existing plugins or write your own. Easily use a set of plugins by using or creating a preset. Learn more →
Create a plugin on the fly with astexplorer.net or use generator-babel-plugin to generate a plugin template.
// A plugin is just a function
export default function ({types: t}) {
return {
visitor: {
Identifier(path) {
let name = path.node.name; // reverse the name: JavaScript -> tpircSavaJ
path.node.name = name.split('').reverse().join('');
}
}
};
}
Debuggable
Source map support so you can debug your compiled code with ease.
Spec Compliant
Babel tries to stay true to the ECMAScript standard, as much as reasonably possible. It may also have specific options to be more spec compliant as a tradeoff to performance.
Compact
Babel tries uses the least amount of code possible with no dependence on a bulky runtime.
This may be difficult to do in cases, and there are "loose" options for specific transforms that may tradeoff spec compliancy for readability, file size, and speed.