What you'll learn: By the end of this lesson you will be able to name the stages of a front-end build (resolve, transform, chunk, hash, emit), explain why a dev server can serve one file per module while a production build should not, say what a content hash in a file name buys and what it costs when one file names another, choose what the build should and should not do (type checking, source maps, environment values), and predict which output files change when one source file is edited.
Think of a newspaper print shop. Journalists write many short articles (your modules). Before the paper reaches a reader, the print shop lays the articles out into a few sections, prints an issue number on every section, and sends the sections to newsstands. A reader who already holds Monday's sports section does not need it again on Tuesday, but only if the issue number tells them it is the same section. A build tool is that print shop, and the issue number is the hash in a file name.
This lesson is about the build as a system between your source and the browser's cache. How a bundler decides what to drop and where to cut is in Code Splitting, Lazy Loading and Tree Shaking, and what the module language itself guarantees is in JavaScript Modules and TypeScript at Scale. The next lesson, Module Formats, Package Exports and Browser Targets, covers what happens to the packages you import and to the browsers you target.
The core idea
- A build is a pipeline over the module graph: resolve each import to a file, load it, transform it, cut the graph into chunks, name the chunks, write them out.
- Development and production want opposite things. A dev server wants each edit visible at once, so it can serve files as they are. A production build wants few requests and long-lived caches.
- A file name that carries a hash of its content lets a cache keep a file forever, because a changed file gets a new name.
- Naming has a cost: if file A contains the name of file B, a change in B changes A. Which files change on a deploy is a design decision, not an accident.
- The build transforms code; it does not prove it right. Type checking, linting and tests are separate gates.
Tool versions move quickly. The Vite documentation read on 2026-12-01 describes Rolldown as the bundler behind its production build and dependency pre-bundling; other tools differ in names and defaults, so treat the configuration names below as examples and check your own tool's page. The lab ran on Node 26.13.1.
What a build does
Figure 1. The stages of a production build and where the output goes
How to read this: the top row is the build, the bottom row is delivery. The two highlighted stages decide what the browser's cache can reuse.
| Stage | Question it answers | A concrete example |
|---|---|---|
| Resolve and load | Which file does this import mean? | ./cart becomes a path; a package name becomes a file inside a package |
| Transform | What must change in this file? | Types are removed; a named constant is replaced with a value; syntax is rewritten for the targets |
| Chunk | Which modules travel together? | The entry's imports in one file; each lazy page in its own |
| Hash and emit | What is each file called, and what refers to it? | checkout-573a2ef3.js, plus a way to find it |
Vite's documentation says its TypeScript support is transpile-only: "Vite only performs transpilation on .ts files and does NOT perform type checking", and it tells you to set isolatedModules. So a build that passes can still contain a type error unless a separate tsc step runs in CI, which is the same point as in the modules lesson.
A define replaces a constant with a literal before bundling; Vite's define option says entries are "statically replaced during build". Environment values work the same way, and Vite exposes to client code only variables with the prefix in envPrefix (default VITE_). Anything that reaches client code is public: a build cannot keep a secret that the browser needs. Runtime configuration is a later lesson in this module.
Development is not production
A dev server can skip bundling. Vite's documentation says source code "is served on-demand over native ESM", so the browser loads only what the current page needs. The same page says why that is not enough for production: shipping unbundled modules "is still inefficient due to additional network round trips from nested imports".
Figure 2. Why unbundled loading takes many rounds
How to read this: a browser learns about an import only after it has the file that names it, so the rounds are sequential even when requests inside a round run in parallel.
Illustration 1, the minimal example: requests and one transform
The lab, build-pipeline-model, is a model, not a bundler: it reads import lists it is given and its minifier is a toy. It generates a small app (6 features of 5 leaf modules each, plus two shared utilities) and counts the files a first page view needs in two settings. It then shows the transform stage on one module whose body uses a build-time constant.
Expected output, captured from a real run (scenarios 1 and 2):
=== 1. First page view: dev server (one file per module) against a production build
generated app: 39 modules
dev server: 40 requests in 6 rounds
production build: 2 requests in 2 rounds
=== 2. The transform stage on one module (lib/env)
source:
// the build mode is a constant
export const mode = __MODE__;
development, not minified:
// the build mode is a constant
export const mode = "development";
production, minified:
export const mode = "production";
The numbers belong to the generated app, not to real tools. What carries over is the shape: dev cost grows with the number of modules, build cost with the number of chunks.
Dependencies are the sharpest case. Vite's pre-bundling page says that without it, importing from a package such as lodash-es could make "the browser fires off 600+ HTTP requests at the same time", and it also converts CommonJS and UMD packages to ESM. The result is cached in node_modules/.vite and rebuilt when the lockfile, patches, relevant config or NODE_ENV change. So a dev server is a small build of its own, for your dependencies.
Chunks and names
A production build cuts the graph into files. In the lab's model, a module goes to every root (the entry or a lazily imported page) that reaches it by static imports, and modules with the same set of roots share a chunk. Real tools add their own rules and let you adjust them, and Vite exposes this through build.rolldownOptions.output.codeSplitting; the model is a teaching picture.
Figure 3. The storefront's chunks
How to read this: dashed arrows are loaded on demand, solid arrows are loaded together with the file. Each shared chunk is named after the roots that use it.
Illustration 2, the realistic example: a storefront build
The fixture is an illustrative ShopStream storefront with eleven modules: a router in the entry, three lazy pages, two shared UI and format modules, and a lazily loaded chart module. Each file name carries the first eight hex characters of a SHA-256 of the generated text.
Expected output, captured from a real run (the chunk list of scenario 3):
admin: pages/admin, ui/table
file: assets/admin-1ecf9997.js (232 bytes)
imports first: shared-admin-checkout-product
loads on demand: charts
charts: charts
file: assets/charts-41cb81a1.js (59 bytes)
checkout: lib/validate, pages/checkout
file: assets/checkout-573a2ef3.js (238 bytes)
imports first: shared-admin-checkout-product, shared-checkout-product
main: app/router, lib/env, main
file: assets/main-2d4e154f.js (234 bytes)
loads on demand: admin, checkout, product
product: pages/product
file: assets/product-b3cef338.js (161 bytes)
imports first: shared-admin-checkout-product, shared-checkout-product
shared-admin-checkout-product: lib/format
file: assets/shared-admin-checkout-product-782fc9c1.js (58 bytes)
shared-checkout-product: ui/button
file: assets/shared-checkout-product-00e0af0c.js (61 bytes)
Each lazy chunk must name the shared chunks it needs.
Naming and the cost of naming
Now the question this lesson is really about. When lib/validate changes, which files get new names? In the model's default setting (called inline), a file that loads another lazily contains that file's final name, so the hash of the caller depends on the hash of the callee.
Expected output, captured from a real run (scenarios 4 and 5):
=== 4. Edit lib/validate (only the checkout chunk contains it)
edit (dynamicRefs=inline): 3 output files change of 8
checkout: assets/checkout-573a2ef3.js -> assets/checkout-f7ae617a.js
main: assets/main-2d4e154f.js -> assets/main-e8141646.js
index.html: content changes (it names the new script file)
edit (dynamicRefs=runtime-map): 3 output files change of 9
checkout: assets/checkout-573a2ef3.js -> assets/checkout-f7ae617a.js
index.html: content changes (it names the new script file)
runtime: assets/runtime-37611257.js -> assets/runtime-3efa20f9.js
=== 5. Edit lib/format (three lazy pages share its chunk)
edit (dynamicRefs=inline): 6 output files change of 8
admin: assets/admin-1ecf9997.js -> assets/admin-3883c265.js
checkout: assets/checkout-573a2ef3.js -> assets/checkout-4e90f951.js
main: assets/main-2d4e154f.js -> assets/main-41b41603.js
product: assets/product-b3cef338.js -> assets/product-fe56b9b0.js
shared-admin-checkout-product: assets/shared-admin-checkout-product-782fc9c1.js -> assets/shared-admin-checkout-product-f48547ad.js
index.html: content changes (it names the new script file)
edit (dynamicRefs=runtime-map): 6 output files change of 9
admin: assets/admin-36e6ad0b.js -> assets/admin-968b16ba.js
checkout: assets/checkout-573a2ef3.js -> assets/checkout-4e90f951.js
product: assets/product-b3cef338.js -> assets/product-fe56b9b0.js
shared-admin-checkout-product: assets/shared-admin-checkout-product-782fc9c1.js -> assets/shared-admin-checkout-product-f48547ad.js
index.html: content changes (it names the new script file)
runtime: assets/runtime-37611257.js -> assets/runtime-7fd8726c.js
Read three things from this. A change in a lazy chunk changes the entry too under inline, because the entry names it. Under runtime-map the entry stays the same, and only a tiny runtime file that holds the name-to-file map changes. And a change in a shared chunk changes every chunk that imports it statically, whatever the setting.
The webpack caching guide describes the same pressure from real tools: "webpack includes certain boilerplate, specifically the runtime and manifest, in the entry chunk", and optimization.runtimeChunk: 'single' splits it out; it also recommends a separate vendor chunk for libraries that change less often than your code. Vite can write a manifest (build.manifest, off by default) that maps source files to hashed outputs and lists each chunk's imports and dynamicImports, so a server can render links with hashed names. Test which behaviour your own tool has; do not assume it.
What hashed names buy
The [contenthash] substitution in webpack "will add a unique hash based on the content of an asset. When the asset's content changes, [contenthash] will change as well." MDN calls the resulting practice the cache-busting pattern: put a version or hash in the URL, never modify the file, and give it Cache-Control: max-age=31536000, immutable, while the HTML that points at it gets Cache-Control: no-cache.
Figure 4. A returning shopper after a deploy
How to read this: the only file the browser must ask about is the one that is never cached for long. Everything else is looked up by name.
Illustration 3, the failure: long cache, plain names
The lab's cache model keeps everything under assets/ forever and always fetches index.html. A shopper views the home and checkout pages, then the site deploys an edit to lib/validate, then the shopper returns. The model has no expiry and no revalidation; it shows what that policy does, not how a real cache works.
Expected output, captured from a real run (scenario 6):
=== 6. One returning shopper, a cache that keeps assets/ forever, and a deploy that edits lib/validate
--- hashNames=false
visit 1 (home, then the checkout page, old deploy):
assets/checkout.js: fetched
assets/main.js: fetched
assets/shared-admin-checkout-product.js: fetched
assets/shared-checkout-product.js: fetched
index.html: fetched
visit 2 (the same pages after the deploy):
assets/checkout.js: cache hit, STALE
assets/main.js: cache hit
assets/shared-admin-checkout-product.js: cache hit
assets/shared-checkout-product.js: cache hit
index.html: fetched
--- hashNames=true
visit 1 (home, then the checkout page, old deploy):
assets/checkout-573a2ef3.js: fetched
assets/main-2d4e154f.js: fetched
assets/shared-admin-checkout-product-782fc9c1.js: fetched
assets/shared-checkout-product-00e0af0c.js: fetched
index.html: fetched
visit 2 (the same pages after the deploy):
assets/checkout-f7ae617a.js: fetched
assets/main-e8141646.js: fetched
assets/shared-admin-checkout-product-782fc9c1.js: cache hit
assets/shared-checkout-product-00e0af0c.js: cache hit
index.html: fetched
With plain names the shopper keeps the old checkout code and no request is made to learn otherwise. With hashed names two files are fetched and two are reused. Notice that main is fetched too: that is the naming cost from the previous section.
Source maps, minification and targets
- Minification shortens the generated text. Vite's
build.minifydefaults to'oxc'for client builds. - Source maps map generated code back to your files. Vite's
build.sourcemapdefaults tofalse, andtruecreates "a separate sourcemap file". Whether to publish those files is a security and privacy decision; error reporting with source maps is its own lesson. - Targets decide how much syntax is rewritten. Vite's
build.targetdefaults tobaseline-widely-available, a fixed browser set per major release (the page lists Chrome and Edge 111, Firefox 114, Safari and iOS 16.4 for the version read). The next lesson covers targets in detail. - Inlining. Assets under
build.assetsInlineLimit(4096 bytes by default) are inlined as base64 URLs.
Trade-offs
| Choice | Gains | Costs |
|---|---|---|
| Unbundled dev server | Instant start, edits visible per module | Dev differs from production; dependencies need pre-bundling |
| Bundled production build | Few requests, shared chunks, minification | A separate step that can disagree with dev |
| Hashed names with long cache | Files reused until content changes | Old and new files must coexist during a deploy |
| Names written inline in importers | No extra file or request | A lazy change cascades up to the entry |
| Runtime file holding the name map | Entry stays stable | One more file, and the map must load first |
| Many small chunks | Fine-grained reuse | More requests and more names to keep consistent |
| Source maps published | Debuggable production errors | Source exposure unless access is restricted |
Build it: the build pipeline model
Goal. Run a model of the five stages over an illustrative storefront and watch which output files change for each kind of edit.
cd examples/frontend/labs/build-pipeline-model
pnpm test # 24 tests
pnpm typecheck
node src/demo.ts # prints the six scenarios shown above
This is a model, not a bundler. It does not parse JavaScript, its minifier only drops line comments and collapses whitespace, its chunk rule is the textbook picture, and sizes are the length of the generated text. No real bundler ran, so the lab says nothing about real build speed, output size or the exact rule any tool uses.
Break it. In test/build.test.ts, change the expectation of "a change in a lazy chunk also changes every chunk that names it" to ["checkout"] and the test fails, because main also changes. Restore it.
In practice: a checkout that breaks only for open tabs
This is a realistic scenario, not a case history.
The situation
The ShopStream storefront deploys several times a day. Support reports that some shoppers who left a tab open all morning see a blank panel when they press "Checkout", and a reload fixes it.
Starting architecture
A build with hashed file names; the CDN keeps only the files of the latest build, and the deploy job deletes the previous assets/ folder before uploading.
Options considered
Figure 5. Deciding what a deploy does with old files
How to read this: tabs outlive deploys, so the answer to the first question is almost always yes. The second step is the safety net.
| Option | Result | Risk |
|---|---|---|
| Keep old files for a set period | Old tabs keep working | Storage grows; the period is a guess |
| Delete at once, rely on reload | Simple | Open tabs break on their next lazy load |
| Never delete | Safest for tabs | Unbounded growth |
The decision
Keep the previous build's files for a period longer than a typical session, delete older ones, and listen for failures. Vite documents that it "emits vite:preloadError event when it fails to load dynamic imports", which is the hook for a "new version available, reload" message.
Rollout and verification
Change the deploy job to add files rather than replace them, and add a test that requests a chunk name from the previous build after a deploy and expects it to succeed. Check the index.html response for Cache-Control: no-cache.
The 2 a.m. failure
A cleanup job runs nightly and removes everything older than one build, not one day. After a busy evening of three deploys, an overnight tab asks for a checkout- file that no longer exists and gets a 404 where the page expected JavaScript. The dynamic import rejects and the panel stays blank. The rollback is to restore the deleted files from the artifact store; the lasting fix is retention measured in time, plus the failed-import handler.
Architect's notes
- Decide which files may change on a deploy and keep that set small.
- Keep the HTML short-lived and everything it names long-lived.
- Run type checks and tests as separate required gates.
- Treat everything the build puts in client code as public.
Common mistakes
- Long-caching files whose names do not change.
- Deleting the previous build's files at deploy time.
- Believing a passing build means a type-correct build.
- Putting a secret in a variable the build exposes to client code.
Key takeaways
- A build resolves, transforms, chunks, hashes and emits.
- Unbundled dev serving costs a round per level of imports; a bundle avoids it.
- A hash in a file name makes long caching safe; the HTML must stay revalidated.
- A file that names another file changes when that file changes.
- Transpiling is not type checking.
- Tabs outlive deploys: keep old hashed files for a while.
Practice exercises
Exercise 1, easy (20 minutes). Run the demo and predict, before reading, which files change when you edit ui/button. Check with a new scenario.
Exercise 2, intermediate (45 minutes). Add a vendor rule to the model that puts everything under lib/ into one chunk, and compare how many files change for edits to lib/validate and lib/format.
Exercise 3, intermediate (60 minutes). Build your own app, record the output file names, edit one module, build again, and write down which names changed and whether the entry changed.
Related reading
- Code Splitting, Lazy Loading and Tree Shaking: what to cut and what to drop.
- JavaScript Modules and TypeScript at Scale: the module language and its lab,
esm-module-semantics. - HTTP Caching: validators and freshness.
- CDN and Edge Caching: where the hashed files sit.
- Performance Budgets and Profiling: putting a limit on the output.
- Build performance and caching, runtime configuration, CI/CD for front ends, and blue-green, canary and rollbacks are later lessons in this module.
Official documentation
- Why Vite: native ESM in dev, why production still bundles.
- Dependency pre-bundling (Vite): CommonJS conversion, request counts, cache.
- Build options (Vite):
target,minify,sourcemap,manifest,cssCodeSplit. - Backend integration (Vite): the manifest fields.
- Caching (webpack):
contenthash, runtime chunk, vendor chunk. - Cache-Control (MDN):
immutableand cache busting.