Lesson 1 of 9 · 18m
What a Bundler Actually Does
Parse, resolve, transform, link, and emit — the five-stage mental model that makes every bundler decision intelligible.
A bundler is usually introduced as “the thing that makes dist/”. That is like calling a compiler “the thing that makes .o files” — true, but useless when the output is wrong. The useful mental model is a pipeline: parse → resolve → transform → link → emit. Every mysterious message, cache poison, or bloated artifact belongs to one of those stages.
The pipeline, in one picture
entry.js
│ parse JavaScript/TS/JSX → ASTs and import records
▼
import graph
│ resolve "./x" or "react" → real files, following package rules
▼
resolved modules
│ transform TS→JS, JSX→createElement, syntax lowering, CSS extraction
▼
linked chunks
│ link split/merge modules, treeshake, execute wrappers
▼
dist/assets/*.js + manifestWhen “the import works in dev but not build”, the first question is never “why is JavaScript cursed?” It is: did resolution differ, did a transform differ, or did linking remove something?
One graph, many module kinds
Modern projects contain executable JavaScript, TypeScript types, CSS modules, images, workers, JSON, WASM, and sometimes assets referenced only by URL. Bundlers normalize all of those into modules in the graph:
import logoUrl from "./logo.svg?url";
import workerUrl from "./ocr.worker.ts?worker&url";
import data from "./countries.json";The ?url and ?worker suffixes are not browser standards; they are bundler-level resource requests. Knowing that prevents the common mistake of expecting those imports to work in Node, tests, or another bundler without an equivalent plugin.
Treeshaking is linking, not magic
Treeshaking works when the linker can prove a binding is unused. Static ESM is friendly; dynamic property access and side effects are not:
// linkable: named import is statically visible
import { formatMoney } from "./money";
// not statically provable: the whole module usually survives
import * as money from "./money";
const fn = money[someRuntimeName];Field notes
- “Bundle” is sloppy shorthand. Production output is often several chunks plus assets and a manifest.
- A missing import is a resolver failure; a wrong compiled syntax is a transform failure; missing exports are usually linking failures.
- Side-effectful modules (
import "./polyfill") are part of the graph even when they export nothing. - Read the first error line slowly. Bundlers chain errors and the last line is often the least informative.
Was this lesson helpful?