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 a third-party polyfill such as core-js)
  • Source code transformations (codemods)
  • And more! (check out these videos for inspiration)

JavaScript

  1. // Babel Input: ES2015 arrow function
  2. [1, 2, 3].map(n => n + 1);
  3. // Babel Output: ES5 equivalent
  4. [1, 2, 3].map(function(n) {
  5. return n + 1;
  6. });

What is Babel? - 图1tip

For an awesome tutorial on compilers, check out the-super-tiny-compiler, which also explains how Babel itself works on a high level.

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 React

Babel can convert JSX syntax! 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
  • Yarn
  • pnpm
  1. npm install --save-dev @babel/preset-react
  1. yarn add --dev @babel/preset-react
  1. pnpm add --save-dev @babel/preset-react

and add @babel/preset-react to your Babel configuration.

JSX

  1. export default function DiceRoll(){
  2. const getRandomNumber = () => {
  3. return Math.ceil(Math.random() * 6);
  4. };
  5. const [num, setNum] = useState(getRandomNumber());
  6. const handleClick = () => {
  7. const newNum = getRandomNumber();
  8. setNum(newNum);
  9. };
  10. return (
  11. <div>
  12. Your dice roll: {num}.
  13. <button onClick={handleClick}>Click to get a new number</button>
  14. </div>
  15. );
  16. };

What is Babel? - 图2tip

Learn more about JSX

Type Annotations (Flow and TypeScript)

Babel can strip out type annotations! Check out either our Flow preset or TypeScript preset to get started. Keep in mind that Babel doesn’t do type checking; you’ll still have to install and use Flow or TypeScript to check types.

You can install the flow preset with

  • npm
  • Yarn
  • pnpm
  1. npm install --save-dev @babel/preset-flow
  1. yarn add --dev @babel/preset-flow
  1. pnpm add --save-dev @babel/preset-flow

JavaScript

  1. // @flow
  2. function square(n: number): number {
  3. return n * n;
  4. }

or the typescript preset with

  • npm
  • Yarn
  • pnpm
  1. npm install --save-dev @babel/preset-typescript
  1. yarn add --dev @babel/preset-typescript
  1. pnpm add --save-dev @babel/preset-typescript

JavaScript

  1. function Greeter(greeting: string) {
  2. this.greeting = greeting;
  3. }

What is Babel? - 图3tip

Learn more about Flow and TypeScript!

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.

example-babel-plugin.js

  1. // A plugin is just a function
  2. export default function({ types: t }) {
  3. return {
  4. visitor: {
  5. Identifier(path) {
  6. let name = path.node.name; // reverse the name: JavaScript -> tpircSavaJ
  7. path.node.name = [...name]
  8. .reverse()
  9. .join("");
  10. },
  11. },
  12. };
  13. }

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 using the least amount of code possible with no dependence on a bulky runtime.

This may be difficult to do in cases, and there are “assumptions” options that tradeoff spec compliancy for readability, file size, and speed.