React / 06
What a bundler does
A bundler turns your many source files and packages into a few small, fast files a browser can run. Here's every job it does.
The problem in one list
If you pointed a browser at your src folder, it would hit three walls:
- JSX isn't JavaScript. The browser throws a syntax error on
<h1>. - Bare imports don't resolve.
import { useState } from "react"means "find it innode_modules". Browsers don't know how to do that on their own. - Too many, too big. Hundreds of files means hundreds of requests, all full of whitespace, comments and code you never use.
your source · dozens of files
the bundler
Vite · Parcel · webpack
- transpile JSX
- bundle imports
- tree-shake
- split chunks
- minify
- hash names
- optimise assets
dist/ · ready to ship
Every job, explained
| Job | What it means |
|---|---|
| Transpile | Rewrite code the browser can't run (JSX, TypeScript, very new syntax) into plain JavaScript |
| Bundle | Start at the entry file, follow every import, and join the modules into a few files |
| Tree shaking | Drop exports nobody imports (dead code elimination) |
| Minify | Remove spaces and comments, shorten variable names. Same behaviour, far fewer bytes |
| Code splitting | Put parts loaded with import() into separate files (chunks) that load only when needed |
| Content hashing | Name files after their content (index-B7xQ2kLm.js) so browsers can cache them safely |
| Asset handling | Process CSS, copy and optimise images and fonts, and give them hashed names too |
Compression (gzip or Brotli) usually happens later, on the server or CDN that sends your files. Some bundlers can pre-compress with a plugin.
See tree shaking and minifying
Shrink the bundle
Try itmath.js
export function add(a, b) {
return a + b;
}
export function multiply(a, b) {
return a * b;
}app.js
import { add } from "./math.js";
console.log(add(2, 3));dist/bundle.js
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
console.log(add(2, 3));106 characters (100% of the original)
multiply is exported but never imported, so tree shaking deletes it. Minifying then squeezes what's left. On a real app, these two together often cut the JavaScript by more than half.
Code splitting in one line
import { lazy } from "react";
const AdminPage = lazy(() => import("./AdminPage.jsx"));import() (with brackets) tells the bundler: put AdminPage and its imports in a separate chunk and fetch it only when it's needed. Most visitors never download the admin code. You'll use this with React.lazy in the lazy-loading chapter.
Which bundler?
| Tool | Where you meet it |
|---|---|
| Vite | Today's default for React apps. Vite 8 bundles with Rolldown and compiles with Oxc, both written in Rust |
| Parcel | Zero-config: point it at index.html and go. Still a fine choice |
| webpack | The long-time standard. Powered Create React App, and many older projects still use it |
| Turbopack | The bundler built into Next.js |
They all do the same jobs from the table. What changes is speed and how much you configure.
Gotchas
Recall
Can you answer these?
0/4
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
