Bundlers can be complicated, so to understand here's one in 200 lines.
/src
-> application code/compiler
-> our compiler code
npm run start
runs 3 commands:
npm clean
deletes the build bundle + manifest insidebuild
npm compile
runs our compiler which builds a bunde and manifest filenpm run start:server
starts our basic express server which uses the bundle hash from manifest.
- IIFE
- Dependency graphs
- Recursive functions
- Defining our own import/export system
- ESM
- AST code parsing
- AST code generation
- Hashing
- Pass by ref
Modules -> Compiler -> Assets
- Builds dependency graph from entry file - see
compiler/deps_graph.mjs
- Converts tree into a bundle, stores hash of contents - see
compiler/transform.mjs
. - Server file reads the resulting manifest for details of bundle hash and serves to browser - see
server.mjs
- We use ESM so it handles cyclic dependencies better than CJS (it does this due to its compile-time statis analysis feature).
- For simplicity we don't include non-js assets (e.g. images/css) or separate bundles (e.g. app/vendors).
This repo is designed to help understand the basics, there are many other features WP includes which this does not:
- non-js assets
- Dev and HMR
- Code splitting/cherry picking/tree shaking
- Dynamic imports
- Making it extensible or configurable
- Loaders, plugins or lifecycle