Skip to content
ClapelloNotes by Barath
ReactCh 02 · 3/6
Beginner3 min readUpdated

React / 06

What a bundler does

TL;DR

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 in node_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

index.htmlmain.jsxApp.jsxMenuCard.jsxApp.csslogo.pngnode_modules/react
npm run build

the bundler

Vite · Parcel · webpack

  • transpile JSX
  • bundle imports
  • tree-shake
  • split chunks
  • minify
  • hash names
  • optimise assets
writes

dist/ · ready to ship

index.htmlassets/index-B7xQ2kLm.jsassets/index-Dk3pR9aZ.cssassets/logo-C4nV8wYt.png
The bundler reads your source and node_modules, follows every import from the entry point, and writes a small set of optimised files to dist/.

Every job, explained

JobWhat it means
TranspileRewrite code the browser can't run (JSX, TypeScript, very new syntax) into plain JavaScript
BundleStart at the entry file, follow every import, and join the modules into a few files
Tree shakingDrop exports nobody imports (dead code elimination)
MinifyRemove spaces and comments, shorten variable names. Same behaviour, far fewer bytes
Code splittingPut parts loaded with import() into separate files (chunks) that load only when needed
Content hashingName files after their content (index-B7xQ2kLm.js) so browsers can cache them safely
Asset handlingProcess 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 it

math.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)

Turn on each optimisation and watch the output shrink. Simplified: real minifiers go further and might even work out add(2, 3) = 5 at build time.

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?

ToolWhere you meet it
ViteToday's default for React apps. Vite 8 bundles with Rolldown and compiles with Oxc, both written in Rust
ParcelZero-config: point it at index.html and go. Still a fine choice
webpackThe long-time standard. Powered Create React App, and many older projects still use it
TurbopackThe 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.