Why a bundler, if the browser understands import?
What Vite and webpack do with your modules, and why they didn't disappear when browsers learned import.
import for years. Yet every project still has Vite or webpack, and npm run build glues my twenty tidy files back into one. Did I clean out the closet for nothing?A bit of history. JavaScript itself had no modules until 2015. Node.js invented its own — CommonJS with require. For the browser there were home-made systems and bundlers: programs that read your files with require, figure out who loads whom, and glue everything into one big file. In 2015 the standard added import/export, and by 2018 every major browser supported them. The bundlers stayed.
How does a bundler turn many modules into one file? The idea is simpler than it seems: each file becomes a function, export becomes an entry in an exports object, and import becomes a call to a function that runs the needed module and returns its exports. That's exactly what the Tasky sandbox does when you write code in several tabs:
// Two files after bundling (simplified)
const modules = {
"math.js": function (exports, load) {
// was: export function sum(a, b) { … }
exports.sum = function sum(a, b) {
return a + b;
};
},
"main.js": function (exports, load) {
// was: import { sum } from "./math.js";
const { sum } = load("math.js");
console.log(sum(2, 3));
},
};
const cache = {};
function load(name) {
if (cache[name]) return cache[name];
console.log("Running " + name);
const exports = (cache[name] = {});
modules[name](exports, load);
return exports;
}
load("main.js");
load("math.js"); // the second time — from the cacheSee? Both rules from the last lesson aren't magic. Its own scope — because the file's code lives inside a function. Runs once — because load checks the cache first. Real webpack works almost the same way, just more carefully.
So why all this, if the browser can import by itself? Several reasons:
- A waterfall of requests. The browser learns that
main.jsimportscart.jsonly after downloadingmain.js. Then it learns aboutformat.jsonly after downloadingcart.js. Hundreds of files — hundreds of requests in a chain. One bundled file arrives in one go; - Packages.
import { format } from "date-fns"— the browser doesn't know wheredate-fnslives. The bundler finds it in node_modules; - Dead code. The bundler throws out what nobody imports (more on that below) and squeezes names and whitespace (minification);
- Not-JavaScript. TypeScript, JSX, CSS modules, images — the bundler turns them into what the browser understands.
Tree shaking: out of a library of a hundred functions, only the three you imported end up in the bundle. It's possible precisely because import/export are static. They appear only at the top level of a file, the path is a string literal, the names are right there in the text. A bundler can read them without running the code. With require that's impossible: require(cond ? "a" : "b") can be called anywhere with any computed path — there's no knowing in advance what will be needed.
Modern Vite uses both worlds. While you develop, it serves real ES modules to the browser one by one — an edit to one file reloads just that file, instantly. For production it still builds a bundle: your users care more about the site opening fast than about how many files your project has.
Myth or fact?
Decide whether the statement is true. Got it wrong? Try again; once you're right, Byte explains what's going on.