Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 38 ยท Getting Started

How a Bundler Works

Build a tiny module bundler in Node.js to see exactly how Webpack works: dependency graph, module wrapper, the require runtime, circular imports and a tree shaking sketch.

Webpack feels like magic until you build a small bundler yourself. The core is surprisingly short, and once you see it, configuration options stop being mysterious. In this lesson you write a stand-in bundler in plain Node.js. It is not Webpack and skips almost everything real (parsing, source maps, caching), but it follows the same three steps.

flowchart LR
  A["1. Parse<br/>find imports"] --> B["2. Build the graph<br/>resolve and visit"]
  B --> C["3. Emit<br/>wrap modules + runtime"]

Parse, graph, emit: the same pipeline in every bundler.

Step 1: turn imports into a runtime call

Browsers cannot run import between concatenated files, so a bundler rewrites each module into a function. Inside, import { add } from './math.js' becomes const { add } = __require('./math.js'), and export becomes an assignment to exports.

Webpack does this with an AST (a real parse tree). We use two regular expressions, which is enough for our tiny syntax and never good enough for real JavaScript.

Node.js Stand-in: rewrite import and export

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Step 2: the graph and the bundle

Now combine the parts: visit the entry, transform it, resolve each dependency, repeat. Then emit a single script holding a map from module id to wrapper function, a cache, and a __require that runs a module once.

Node.js Stand-in: a complete mini bundler

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Compare this with what you saw in Install Webpack and Build Your First Bundle: the module map, the cache and the require function are the same skeleton Webpack emits, only with more care.

Why the cache matters

Each module should run once, no matter how many files import it. In the demo, consts.js is imported by two modules but runs one time. The cache is also what makes circular imports survive: __require stores the (still incomplete) exports object before running the module, so a cycle gets whatever has been exported so far instead of looping forever. Webpack behaves the same way, and it is why a circular import can give you undefined for a value that the other module has not defined yet.

Tree shaking in one picture

A bundler that understands import { add } and export syntax statically can see which exports are never used and leave them out. This works because ES module imports and exports cannot change at runtime, unlike require calls inside if statements. That is also why tree shaking needs ES modules (import/export) and not CommonJS.

Node.js Stand-in: find unused exports

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Real tree shaking has an extra step: the minifier must also confirm that dropping the code has no side effects. Chapter 6 covers the sideEffects flag that tells Webpack when files are safe to skip entirely.

What the real Webpack adds

  • A real parser, so strings and comments containing import do not confuse it.
  • Resolvers that follow Node rules (node_modules, package.json exports, extensions): see Resolve and Aliases.
  • Loaders so any file type becomes a JavaScript module: Loaders Basics.
  • Plugins on dozens of hooks: Writing a Plugin.
  • Chunks and a runtime that downloads them on demand.
  • A persistent cache and source maps.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a bundler turn each module into?

    Each module becomes a wrapper function, and a small runtime executes them on demand.
  2. 2. What does the module cache guarantee?

    The cache stores a module's exports after the first require, so later imports reuse it.
  3. 3. Why does tree shaking require ES modules?

    Static syntax lets the bundler analyze usage at build time.
  4. 4. Why can a circular import give undefined values?

    A module is registered in the cache before it executes, so a cycle sees partial exports.

Summary

  • A bundler parses imports, builds the dependency graph and emits a runtime plus module wrappers.
  • The module map, cache and __require function are the skeleton of every Webpack bundle.
  • The cache makes modules run once and lets cycles terminate, with possibly partial exports.
  • Tree shaking relies on static import/export to find unused code.
  • Real Webpack adds a parser, resolvers, loaders, plugins, chunks and caching.

Next: Entry Points