Why does an unbundled dev server pre-bundle third-party dependencies before serving them, and what makes it redo that work?
answer
- what the browser loader cannot run
- CommonJS and UMD packages
- hundreds of internal files, one request
- bare imports found before serving
- lockfile as the cache key
basics
~20 sPre-bundling converts CommonJS or UMD packages into ES modules the browser can import, and collapses packages built from hundreds of internal files into one module; the result is cached and rebuilt when its inputs, such as the lockfile, change.
solid answer
~40 sAn unbundled server relies on the browser's ES module loader, and dependencies break it in two ways. A package shipped as **CommonJS** or **UMD** cannot be imported as a module at all, so it has to be converted to ESM. And a package split into hundreds of internal ES modules would cost hundreds of requests on every cold load, so it is collapsed into **one module**. Before serving, the server scans the source for bare imports, bundles each dependency once, rewrites imports to point at the result and caches it. The cache is keyed on inputs such as the **lockfile**, the server's dependency configuration, patches to installed packages and the environment mode; it is rebuilt when one of them changes, never because you edited your own source.
go deeper
Recall the two reasons for the step: the browser cannot load CommonJS packages as modules, and many-file packages would cost hundreds of requests.
Explain the sequence of discovering bare imports, bundling each dependency once, rewriting imports and caching, and name the inputs that invalidate the cache.
Show you can tell a stale pre-bundle from a code bug, and know that editing source never refreshes it while changing installed packages does.
Weigh development-only steps like this one against the cost of a dev loop that behaves differently from the build, and decide what the team should standardise.
## Why dependencies are a special case An **unbundled dev server** serves your source as native ES modules, one file per request, and transforms each file on demand. That works well for code you edit all day. Third-party **dependencies**, the packages installed under `node_modules`, break both assumptions behind that design: many are not ES modules at all, and many ES-module packages are split into hundreds of internal files. **Dependency pre-bundling** is the step that fixes both before the browser ever asks for them. ## Reason 1: the module format The browser's module loader understands `import` and `export` and nothing else. A package published only as **CommonJS** (`require()` and `module.exports`) or as **UMD** cannot be loaded as a module, so the server must convert it into an ES module first. The conversion also has to present a CommonJS package's assigned exports as named ES exports, so that a line such as `import { parse } from 'some-cjs-package'` works. How CommonJS and ES modules interoperate is a language question; the pre-bundling step only has to apply that interop once per package instead of on every request. ## Reason 2: the number of requests Some packages ship valid ES modules as many small files that import one another. A single named import from such a package makes the browser walk the package's internal graph, one request per file. In the analytics dashboard example, the charting package's several hundred files would add several hundred requests to every cold page load. The server handles them easily; the browser's network queue does not, and the page loads noticeably slower. Pre-bundling collapses the package into **one module**, so the import costs one request. ## How the step runs 1. **Discover.** Before serving, the server scans the app's entries and source for **bare imports**: specifiers such as `charts-lib` that are resolved from installed packages rather than as relative paths. 2. **Bundle each dependency.** Each discovered dependency entry is bundled, together with its internal files, into a single ES module, converting CommonJS or UMD on the way. 3. **Rewrite.** When the server later transforms a source module, it rewrites the bare import into the URL of the pre-bundled file. 4. **Cache.** The output is written to a cache folder and reused on the next start. Only dependencies are treated this way. Source modules stay unbundled because they change on every save, and serving them one module at a time is what keeps a hot swap small. ## What makes it redo the work The cache is keyed on the inputs that decide what the pre-bundled output should contain. Typically these are: - the **lockfile**, which changes whenever a package is installed, removed or upgraded; - the dev server's own **configuration** for dependency handling; - **patches** applied to installed packages; - the **environment mode** the server runs in. If none of those changed, a restart reuses the cache and the server starts almost at once. Editing your own source never invalidates it, because source is not part of the pre-bundle. Dev servers that pre-bundle typically also offer a way to force a rebuild, for the case where you know the cache is stale for a reason its key cannot see. ## What pre-bundling is not | | Dependency pre-bundling | Production bundling | |---|---|---| | When it runs | development only | the build | | Input | each third-party dependency entry | source and dependencies together | | Output | one ES module per dependency entry | split, minified chunks | | Goal | make dependencies loadable and cheap to request | optimised shipped output | Two consequences follow from that table: - Reading the pre-bundled files as a preview of production chunk sizes is a mistake: they are a development convenience shaped for an unbundled loader. - A package that is already a compact ES module, made of a few files, gains little from pre-bundling; the step earns its keep on CommonJS packages and on many-file packages. A bundling dev server usually needs no separate step of this kind, because it already bundles dependencies together with source before it serves anything.
- Why are dependencies pre-bundled while the app's own source is not?Dependencies change rarely, only when the lockfile does, so converting them once and caching the result is cheap over a working day. Source modules change on every save; bundling them would bring back the rebuild cost that unbundled serving avoids, and a hot swap would have to replace a bundle instead of one module.
- Is the pre-bundled output a preview of what the production build ships?No. Pre-bundling is a development-only step that turns each dependency entry into one ES module the browser can import. The production build bundles source and dependencies together, splits chunks and minifies them. Judging production chunk sizes from the pre-bundled files leads to wrong conclusions.
- Pre-bundling exists to minify dependencies for the production build
- The app's own source modules are pre-bundled along with the packages
- Pre-bundling runs again on every save
- A dependency that already ships ES modules never benefits from pre-bundling