Tooling & Build

Babel & Transpilation

Babel is a JavaScript compiler that transforms modern syntax (ES2020+, JSX, TypeScript) into code that runs in older environments. It's the backbone of most React/Node.js build pipelines and Jest test setups.

What is Babel used for?

Babel is a compiler that converts modern JavaScript, plus syntax like JSX and TypeScript, into JavaScript that older browsers understand. It's configured with presets, such as @babel/preset-env, which reads your browser targets. Many projects now use faster tools (esbuild, SWC) for the same job, often without configuring Babel directly.

// babel.config.json
{
  "presets": [
    ["@babel/preset-env", { "targets": "> 0.5%, not dead" }],
    "@babel/preset-react"
  ]
}

Babel Pipeline

Source Code
Modern JS / JSX / TS
Parser
@babel/parser → AST
Transformer
Plugins manipulate AST
Generator
@babel/generator → output JS

Transpilation (syntax)

Converts new syntax into equivalent older syntax.

Arrow functions → function expressions
Class fields → constructor assignments
Optional chaining → ternary chains
Async/await → generator functions
Destructuring → variable assignments
Template literals → string concatenation
JSX → React.createElement() calls

Polyfills (runtime APIs)

Adds missing APIs that don't exist in older environments.

Promise → polyfilled class
Array.from / Array.prototype.includes
Object.assign / Object.entries
Map, Set, WeakMap, WeakSet
Symbol, Symbol.iterator
fetch (via whatwg-fetch)
structuredClone, queueMicrotask

Common Pitfalls

Using Babel without specifying targets — transpiles everything to ES5 unnecessarily, slowing builds and increasing bundle size
Set targets in @babel/preset-env to match your supported browsers. Modern targets = smaller output.
Using useBuiltIns: 'entry' without an entry import — no polyfills are injected at all
Use useBuiltIns: 'usage' so Babel auto-injects only the polyfills your code actually needs.
Transpiling node_modules — extremely slow and usually unnecessary
Exclude node_modules in babel config: 'ignore': ['node_modules']. They're pre-compiled already.
Using Babel in 2024 for simple React projects — often slower than modern alternatives
Consider SWC (used by Next.js, Vite 6+) for much faster transpilation with identical output.

Babel in Action Demo

Code Editor

<div class="cols">
<pre>// you write (modern JS)
const greet = (user) =>
  `Hi ${user?.name ?? "guest"}`;

class Timer { #ticks = 0; }</pre>
<pre>// Babel output for old browsers
var greet = function (user) {
  var _ref;
  return "Hi " + ((_ref = user == null
    ? void 0 : user.name) != null
    ? _ref : "guest");
};
// + helper code for the private field</pre>
</div>
<p id="out"></p>

Preview

Babel: summary

Key points

  • Babel transforms syntax; missing APIs (like Promise.any in old browsers) need polyfills such as core-js.
  • browserslist defines which browsers to support, so you don't over-transpile.
  • Next.js and Vite use SWC or esbuild by default.

Frequently asked questions

›Do I still need Babel?

Usually not directly. Modern browsers support modern JavaScript, and frameworks handle JSX and TypeScript with faster compilers. Babel remains common in older setups and for custom plugins.