A module graph represents modules as nodes and their import dependencies as directed edges. Bundlers analyze this graph to decide what code to remove, combine, or split into chunks. section 13.3 section 13.2.2
How the graph affects JavaScript builds and execution:
- Static ESM imports let tooling resolve dependencies before execution, making it practical to analyze the graph and prune unused code. section 13.0 section 13.3
- CommonJS require() can run conditionally or construct its path at runtime, so the dependency graph cannot be determined in the same static way. section 13.0 section 13.2.2
- Top-level await makes ESM modules asynchronous: modules waiting on async dependencies can change execution order, while other modules proceed. section 13.2.2
One graph, several chunks
A website can place shared libraries such as React in one chunk and each page’s content in a separate chunk. The bundler uses module dependencies to organize those outputs. section 13.3
// Require 1.js, 2.js, or 3.js at random
require(`./${Math.ceil(Math.random() * 3)}.js`);This runtime-built require() path illustrates why a CommonJS dependency may not be visible to static graph analysis. section 13.2.2
