Mini Bundler
Build a small JavaScript bundler: find imports with a real parser, build the module graph, rewrite ES modules into functions and emit one script that runs.
What you will build
You will write a small webpack-like bundler in plain JavaScript. It takes a virtual file system ({ "/src/index.js": "…" }), parses every module with acorn, follows the imports from the entry to build a dependency graph, rewrites import/export into module functions with getter-based exports, and emits one script with a tiny runtime that runs the app.
Then you make circular imports behave the way ES modules do, teach the bundler JSON modules, and bundle a real multi-file app with your own bundler and run it in the preview.
What you will learn
- Why bundlers find imports with a parser (an AST), not with regular expressions
- How module resolution and a dependency graph with stable ids work
- How ESM is rewritten into module functions, and why exports are getters (live bindings)
- What a bundle runtime does: the module map, require and the module cache
- How circular imports behave in CommonJS, in ESM and in your bundler
- How a bundler turns non-JS files such as JSON into modules
Steps
- 01Find the importsParse a module with acorn and list its static imports with their positions.
- 02Dependency graphResolve specifiers to files and walk the imports from the entry, giving every module a stable id.
- 03From ESM to a module functionRewrite import and export syntax into require() calls and getter-based exports.
- 04One script that runsEmit a module map and a tiny runtime with require() and a module cache.
- 05Circular importsFind import cycles in the graph and make cycles run the way ES modules do.
- 06Run it: bundle an appAdd JSON modules to your bundler and run a real multi-file app in the preview.
Practise this
Other hands-on work on the same topics.
Incident Simulator
System Design
Weekly Challenges
See it in real code
Guided tours through the source of real libraries that rely on it.