What is a transpiler?

A transpiler is a source-to-source compiler that converts code from one language or language version into another at the same level, such as TypeScript or JSX into JavaScript, or modern JavaScript into older JavaScript for older browsers.

What transpilers do in front-end projects

  • Strip types: TypeScript → JavaScript.
  • Transform JSX into React.createElement / jsx() calls.
  • Downlevel syntax for target browsers: optional chaining, class fields, async/await (for very old targets).
  • Experimental features and framework transforms (decorators, the React Compiler's automatic memoisation).
  • Emit source maps so DevTools show the original code.

Common tools

  • Babel: the most configurable, with a huge plugin ecosystem.
  • SWC (Rust): very fast, used by Next.js, Parcel, Deno and others.
  • esbuild (Go): very fast, used by Vite in development and many CLIs.
  • TypeScript's tsc: type-checks and emits JavaScript; many projects use tsc --noEmit only for type checking and let SWC or esbuild do the fast transpiling.
  • Node.js can now run many TypeScript files directly by stripping types.

Transpiler vs compiler vs bundler

  • A compiler usually translates to a lower level (C to machine code, TypeScript type-checking plus emit).
  • A bundler (webpack, Rollup, Vite, Turbopack) combines modules into files for the browser; it calls a transpiler for each file.
  • A polyfill adds missing runtime APIs; transpilers only handle syntax.

Configuring targets

browserslist ("> 0.5%, last 2 versions, not dead") or explicit targets tell the transpiler which syntax to keep, so modern browsers get smaller, faster code.

Code example

js
1// INPUT: modern TypeScript + JSX
2//
3// type User = { name?: string; tags?: string[] };
4// export const Badge = ({ user }: { user?: User }) => (
5// <span title={user?.name ?? "Guest"}>{user?.tags?.length ?? 0}</span>
6// );
7
8// OUTPUT (roughly what SWC/Babel emit for older browsers): types removed,
9// JSX turned into function calls, optional chaining and ?? downlevelled.
10function jsx(type, props) {
11 return { type, props }; // stand-in for react/jsx-runtime
12}
13var Badge = function Badge(_ref) {
14 var _user$tags$length, _user$tags;
15 var user = _ref.user;
16 return jsx("span", {
17 title: (user === null || user === void 0 ? void 0 : user.name) != null ? user.name : "Guest",
18 children:
19 (_user$tags$length = user === null || user === void 0 ? void 0 : (_user$tags = user.tags) === null || _user$tags === void 0 ? void 0 : _user$tags.length) !== null && _user$tags$length !== void 0
20 ? _user$tags$length
21 : 0,
22 });
23};
24
25console.log(Badge({ user: { name: "Ada", tags: ["admin", "editor"] } })); // { type: "span", props: { title: "Ada", children: 2 } }
26console.log(Badge({})); // { type: "span", props: { title: "Guest", children: 0 } }
27
28// Typical commands:
29// npx tsc --noEmit -> type-check only
30// npx swc src -d dist -> fast transpile
31// npx esbuild src/app.ts --bundle --target=es2020 --outfile=dist/app.js

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.

Open in study app