Module Bundlers & Build Tools
Why bundlers exist, what tools like Vite and Webpack actually do, and how Vite's dev server and build step work in practice.
Module Bundlers & Build Tools
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Browsers couldn't natively resolve module imports across hundreds of files for years, so bundlers evolved to merge a whole dependency graph into optimized output files.
- A bundler's core jobs are transpiling newer syntax to older JavaScript, combining modules into fewer files, and tree-shaking unused code out of the final bundle.
- Vite's dev server serves ES modules natively over HTTP and compiles files on demand, which is why it starts almost instantly even on large projects.
- Hot module replacement swaps just the changed module in a running page without a full reload, preserving state like form input or scroll position.
- For production, Vite switches strategies entirely, using Rollup to produce a small number of optimized, cacheable files instead of serving modules one by one.
The problem: JavaScript wasn’t built for large projects
JavaScript started as a language for small scripts sprinkled onto web pages — a few lines to validate a form, a hover effect. It had no built-in notion of “modules” for most of its early life: no clean way to split code across files and import one piece into another. As apps grew, developers worked around this with global variables and careful <script> tag ordering, which became unmanageable fast — one typo in tag order and Cannot read property of undefined appears with no clear cause.
ES modules (import/export) eventually arrived in the language itself, letting you cleanly import one file’s exports into another. But even once browsers added native support, a real app often imports hundreds of small modules, and each import can mean a separate network request. Loading an app that way is slow, and it still leaves no way to use newer JavaScript syntax or JSX before every browser supports it. Bundlers exist to close both gaps.
What a bundler actually does
A bundler like Webpack, Rollup, or Parcel starts at your app’s entry file, follows every import statement to build a complete dependency graph, and then does several things to that graph before handing you output:
Transpiling. Code written in newer JavaScript syntax, or in JSX/TypeScript, gets converted into JavaScript every target browser can run, usually via a tool like Babel or esbuild working alongside the bundler.
Bundling. Instead of hundreds of separate files and requests, the whole dependency graph gets combined into a small number of output files — maybe just one or two for a modest app — so the browser downloads far fewer things.
Tree-shaking. If you import one function from a large library, the bundler can statically determine that the rest of that library’s exports are never used, and exclude them from the final bundle. This only works reliably with ES modules, because their imports and exports are static and analyzable — unlike the older CommonJS require() pattern, which a bundler can’t always reason about the same way.
Code-splitting and asset handling. A bundler can also split output into chunks loaded on demand (so a settings page’s code isn’t downloaded until someone visits it) and process non-JS assets like CSS and images into the final output too.
Where the dev server fits in
All of that bundling work is for production. While you’re actively developing, re-running a full bundle on every file save would be painfully slow on anything but a tiny project. This is where a dev server comes in: a local server, included with tools like Vite or Webpack’s dev server, that rebuilds only what changed and serves the result to your browser, often pushing updates without a full page reload.
Vite, concretely
Vite is the clearest example of rethinking this for speed. Instead of bundling the whole app before you can see anything — the older approach — Vite’s dev server leans on the fact that modern browsers natively understand import statements. It serves your source files as native ES modules directly over HTTP, transforming each file (stripping TypeScript types, compiling JSX, etc.) only the first time the browser actually requests it. Startup time barely depends on how many files your project has, because nothing gets bundled upfront — the browser just asks for files as it encounters imports, the same way it would for any other HTTP resource.
This is also what makes hot module replacement (HMR) fast. When you save a file, Vite pushes just that one updated module to the browser over a WebSocket connection, and the running app swaps it in place — your component re-renders with the new code, but application state like form input, open menus, or scroll position survives, because the whole page never reloaded.
Crucially, Vite doesn’t use this same on-demand strategy for production. Shipping hundreds of individual file requests to a real user’s browser would be slower than one optimized bundle, cache invalidation aside. So when you run a production build, Vite switches to Rollup under the hood, which does the traditional bundling, minification, and tree-shaking pass to produce the small number of highly optimized, cacheable files you actually deploy. Understanding that split — on-demand native modules for development speed, a full Rollup bundle for production correctness and performance — is the key insight behind why Vite feels like a different category of tool from older all-in-one bundlers, while still producing comparable output at the end.
Check yourself
Why couldn't browsers just load a project's JavaScript modules directly, unbundled, for most of the last decade?
Older browsers had no native support for the import/export syntax at all, and even once they gained it, loading hundreds of separate files as individual network requests was slow compared to a few combined, optimized files — so bundlers were needed both for compatibility and performance.
What is tree-shaking, concretely?
It's the bundler analyzing which exports from a module are actually imported and used, and leaving the rest out of the final bundle entirely — so importing one function from a large utility library doesn't ship the whole library to the browser.
Why is Vite's dev server fast even on a large codebase, when older bundlers felt slow?
Vite doesn't bundle the whole app upfront for development. It serves native ES modules over HTTP and transforms each file on demand, the first time the browser requests it, so startup time stays roughly constant regardless of project size.
Does Vite use the same strategy for development and for production builds?
No. Development uses the fast, on-demand native-ESM dev server; production builds switch to Rollup under the hood, which bundles and optimizes everything into a small set of files suited for real-world deployment and caching.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.