Full answer
A bundler turns many source files into a few files that the browser downloads. The main steps:
- Resolve. Starting at the entry point, it reads each
importand finds the file, including packages innode_modules. - Transform. Loaders convert TypeScript, JSX and modern syntax to what the target browsers run, and can import CSS, images and JSON.
- Build the module graph. Every file and its dependencies form a graph. The bundler knows which code is reachable.
- Optimise. It removes unused exports (tree shaking), merges modules, minifies names and whitespace, and can split the graph into chunks.
- Emit. It writes output files with content hashes in the names, and a manifest or HTML that points to them.
Why not just use native ES modules? They do work, and for small projects or fast local development they are fine. Reasons to bundle for production:
- A page that imports a deep tree of modules makes a chain of requests, because the browser finds the next import only after it loads the previous file. Bundling reduces round trips. HTTP/2 helps, but it does not remove the discovery chain.
- Packages are published in many formats, and a bundler normalises them.
- Dead code removal, minification and chunking need to see the whole graph.
- Non-JavaScript assets and environment variables need processing.
Popular tools are esbuild, Rollup, webpack, Vite (a native-module dev server with its own production build step) and others. The names change over time, so describe the ideas and say you would pick based on the framework and the team.
The Node script below writes two small files into a temporary folder and bundles them with esbuild. The result is a single minified file. The function that main.js never calls is not in the output, and the bundle runs and prints the greeting.
import { build } from 'esbuild';
import { mkdtempSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { execFileSync } from 'node:child_process';
const dir = mkdtempSync(join(tmpdir(), 'bundle-'));
writeFileSync(join(dir, 'greet.js'), "export const greet = (n) => 'Hello, ' + n;\nexport const unused = () => 'never called';\n");
writeFileSync(join(dir, 'main.js'), "import { greet } from './greet.js';\nconsole.log(greet('Asha'));\n");
const result = await build({
entryPoints: [join(dir, 'main.js')], bundle: true, minify: true,
format: 'iife', write: false, metafile: true,
});
const code = result.outputFiles[0].text;
console.log('output files:', result.outputFiles.length);
console.log('unused function in bundle:', code.includes('never called'));
console.log('inputs:', Object.keys(result.metafile.inputs).map((p) => p.split('/').pop()).sort().join(', '));
console.log('bundle prints:', execFileSync('node', ['-e', code]).toString().trim());Run with Node 24.22.1. The output matched the text in the answer in two runs. Run with Node 24 and esbuild 0.28. The output was identical in two runs. Only esbuild was run, not webpack, Rollup or Vite.
Key points
- A bundler follows imports to build a module graph from an entry point.
- It transforms, tree shakes, minifies, splits and emits hashed files.
- Native modules work, but deep import chains cost round trips.
- Bundlers normalise package formats and non-JS assets.
- Choose a tool by framework and team, not by trend.
Common mistakes
- Saying a bundler is only for minification.
- Believing HTTP/2 makes bundling pointless in every case.
- Bundling everything into one huge file and ignoring code splitting.
- Naming a tool without describing what it does.
Follow-ups
- What is the difference between a bundler and a transpiler?
- How does a dev server differ from a production build?
- What is a module graph used for besides bundling?
Tips
- List the five steps in order.
- Give one honest reason to skip a bundler for a small project.