Bundling concept

Why bundle

Browsers load pages fastest with few files, but developers think best in many small modules. Bundling resolves that tension by merging the module graph into a small set of output files.

The bundler follows imports from the entry, drops unused branches where possible, and emits plain JavaScript the browser can run without knowing about the original file layout.

One entry file

Every bundle starts from one entry file that describes the whole app. The entry imports screens, helpers, and styles by name, so the full dependency tree is explicit and traceable.

Keep the entry thin: import and start, nothing more. Feature logic belongs in nearby modules where it is easy to find and test in isolation.

import { startApp } from "./app"; import { routes } from "./routes"; startApp(routes);

The two modules belong to the reader's project, so they are not in this folder and the block is not run here; the entry is the whole point of the listing.

Split vendor sketch

App code changes daily while third party code changes rarely. Conceptually you can emit two outputs: one for your screens and one for stable vendor helpers shared across pages.

The browser then caches the vendor file longer and downloads only the changed app file. No tool setup is needed to grasp the idea, only the habit of separating own code from outside code.

// app chunk: screens that change often import { renderHome } from "./home"; // vendor chunk: stable helpers reused everywhere import { formatDate } from "./format"; renderHome(formatDate("2026-10-06"));

These modules also belong to the reader's project, so this sketch is not run here either; it shows which import belongs to which chunk.

Measure output

A bundle is a budget, not just a build artifact. Check the emitted file names and byte sizes after each change, then ask whether a new import earned its weight.

Large jumps usually point at an accidental heavy import. Smaller focused modules keep downloads fast and make future splits almost trivial.

wc -c dist/*

49152 dist/app.js
124928 dist/vendor.js
174080 total

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Why does a bundler merge many modules into a few files?

    Answer

    To keep the number of requests small while the code stays in modules

    Developers read small modules and browsers load few files, and the bundle resolves that tension.

  2. What does the bundler follow from the entry file?

    Answer

    The whole dependency tree, module by module, from its imports

    The bundler walks the imports from the entry, which is why an unused file never reaches the output.

  3. Why split the vendor code from the app code?

    Answer

    The browser can cache the stable vendor file while the app file changes

    Third-party code changes rarely, so a separate file keeps its long cache while the app is rebuilt often.

  4. What does a sudden jump in the bundle size usually mean?

    Answer

    A new import pulled in a heavy dependency

    The size is a budget: a heavy import that nobody noticed is the usual reason for a jump.

  5. What does this program print? function total(items: number[]): number { return items.reduce((a, b) => a + b, 0); } console.log(total([10, 20, 30]));

    Answer

    60

    The reducer adds the three numbers, starting from zero.

  6. What does this program print? const sizes: number[] = [48, 122]; console.log(sizes.reduce((a, b) => a + b, 0));

    Answer

    170

    The two sizes are added, and the total is 170, the two numbers of the example in kilobytes.

  7. What does this program print? const app = 48; const vendor = 122; console.log(app + vendor > 150);

    Answer

    true

    The sum is 170, which is larger than 150, so the comparison is true.

  8. What does the compiler say about this program? const sizes: number[] = [48, 122]; const total: string = sizes.reduce((a, b) => a + b, 0); console.log(total);

    Answer

    error TS2322: Type 'number' is not assignable to type 'string'.

    The reducer gives a number, and the annotation asks for a string.

  9. What does the compiler say about this program? const sizes = [48, 122]; const first: string = sizes[0]; console.log(first);

    Answer

    error TS2322: Type 'number' is not assignable to type 'string'.

    The array holds numbers, so its first element cannot be stored in a string.

  10. What is the cheapest way to keep the bundle honest?

    Answer

    Measure the output after each change

    A size read after every change shows the heavy import while it is still easy to remove.

Article author: Arthur Isaev