Frontend System Design · Lesson 21 of 86 · 4 min
Module Bundling Basics
In an interview, explaining why we bundle shows you think about performance from first principles.
Your browser can't efficiently handle a thousand tiny JavaScript files. Each import could trigger a separate HTTP request, creating a network waterfall that kills your page load time. That’s the core problem.
Module bundling is the process of resolving all your import and export statements at build time, stitching the code together into a few optimized files—the "bundles"—that the browser can download efficiently.
From entry point to dependency graph
A bundler doesn't just guess which files to include. You give it a starting point, the "entry point," which is usually your main index.js or app.tsx.
From there, it reads the code and looks for import statements. For each one it finds, it follows the path to that module and adds it to a "dependency graph." It then inspects that module for its own imports and repeats the process recursively until every needed piece of code is mapped out.
This graph is the bundler's master plan. It represents every module your application uses—your own components, utility functions, and third-party libraries from node_modules.
In an interview, describing how a bundler builds this graph shows you understand the mechanics, not just the buzzwords. It's the foundation for every optimization that follows.
Transforming and optimizing the output
Once the dependency graph is built, the bundler processes each file and assembles the final output. This involves several key transformations.
Transpilation: Your source code might be TypeScript, JSX, or use modern JavaScript features not supported by all browsers. The bundler uses tools like Babel or tsc to transpile this code into a more compatible version, typically ES5 or ES6.
Loaders and Plugins: Bundlers can be configured to handle more than just JavaScript. "Loaders" teach the bundler how to process other file types. A CSS loader might handle @import rules, and an image loader might inline small images as Base64 strings or copy larger ones to the output directory.
Minification: The bundler removes all unnecessary characters from the code—whitespace, comments, newlines—to reduce file size without changing functionality. It also performs "uglification," renaming variables and functions to shorter names (e.g., calculateUserPermissions becomes a).
Tree Shaking: This is a critical optimization. The bundler analyzes the dependency graph to detect which exported functions or variables are actually imported and used. Any unused code is "shaken out" of the final bundle.
The trap: Tree shaking only works with ES Modules (import/export). If your project or a dependency uses CommonJS (require/module.exports), the bundler can't safely determine what's unused, and you may end up shipping dead code.
Here’s a simple example. You have two modules:
// src/greeting.js
export function greet(name) {
return `Hello, ${name}!`;
}
export function farewell(name) {
return `Goodbye, ${name}!`;
}
// src/app.js
import { greet } from './greeting'; // Only 'greet' is imported
const userName = 'Frontend Developer';
console.log(greet(userName));
The bundler sees that farewell is exported but never imported by app.js or any of its dependencies. The conceptual output would effectively be:
// dist/bundle.js (simplified)
function greet(name) {
return `Hello, ${name}!`;
}
const userName = 'Frontend Developer';
console.log(greet(userName));
The farewell function is gone. For a library like Lodash, this can save hundreds of kilobytes.
Splitting the bundle for faster loads
A single, large bundle is better than a thousand small files, but it's still not ideal. A user might download a 2MB app.bundle.js on their first visit, even if they only need 200KB of it for the landing page.
This is where code splitting comes in.
Instead of creating one giant bundle, you can configure the bundler to create multiple smaller ones. A common strategy is route-based splitting. The bundler creates a small "main" bundle with core, app-wide logic, and then separate bundles for each page or major feature (e.g., profile.js, dashboard.js, settings.js).
When the user navigates to their profile page, the application dynamically loads the profile.js bundle just in time. This dramatically improves the initial page load time, which is a critical performance metric.
When asked to design a large-scale frontend application, mentioning code splitting is non-negotiable. It demonstrates you're thinking about user experience and performance at scale, not just developer convenience.
Try it
Create a new project using Vite (npm create vite@latest). Make two JS files: one that exports two functions, and another that imports and uses only one of them. Run npm run build and inspect the output file in the dist directory. Search for the name of the unused function; you shouldn't find it.
Next: You'll learn how to measure the performance impact of your bundle with Core Web Vitals: LCP, INP and CLS.
Lock it in — apply this lesson under interview conditions.