Sub topic 18
Why Static Structure Enables Tree Shaking
This chapter assumes you know named exports, re-exports, and the module lifecycle from earlier chapters. You should also know what a bundler is: a tool such as Rollup, webpack, or esbuild that combines many modules into a few output files.
What tree shaking is
Tree shaking removes exports that no module ever imports. The bundler treats your program as a graph of modules and keeps only the parts that are reachable from the entry point. Everything else is dropped from the output file, so users download less code.
This only works because ES module structure is static. A tool can read the imports and exports by parsing the source, without running any of it:
importandexportdeclarations may only appear at the top level of a module. They cannot sit inside anifor a function.- Specifiers are string literals, so the tool always knows which file is meant.
- Imported names are fixed in the source. A module cannot invent a new export at runtime.
A worked example
// math.js
export const PI = 3.14159;
export function area(radius) {
return PI * radius * radius;
}
export function perimeter(radius) {
return 2 * PI * radius;
}
// main.js
import { area } from './math.js';
console.log(area(2));The bundler sees that main.js asks only for area, and that area uses PI. Nothing references perimeter, so it is left out of the bundle.
Compare this with CommonJS:
// main.cjs
const name = process.argv[2] || 'area';
const math = require('./math.cjs');
console.log(math[name](2));require is an ordinary function call and math is an ordinary object. The property being read is only known at runtime, so the bundler has to keep every export.
What defeats tree shaking
| Pattern | Why it blocks removal |
|---|---|
| `require()` and `module.exports` | Exports are a mutable object built at runtime |
| `math[name]` on a namespace import | The tool cannot tell which property is used |
| Top-level code with side effects | Removing the module might change behavior |
| `import('./math.js')` with a computed path | The target module is unknown at build time |
A namespace import such as import * as math can still be shaken when you only use static property names like math.area. Computed access forces the tool to keep everything.
Barrel modules and tree shaking
A barrel file that uses export { area } from './math.js' or export * from './polygons.js' is still static, so bundlers can trace each name back to its source. The risk is side effects. If any module behind the barrel runs top-level code, importing one name through the barrel can pull in the whole set.
Exercise
A module exports format and parse. Your app runs import * as utils from './utils.js' and later calls utils[action](), where action comes from user input. Can the bundler remove parse? Why or why not?
Show answer
No. The property is chosen at runtime through computed access, so the bundler cannot prove that parse is unused. It keeps both exports. Using static calls such as utils.format(), or named imports, would let it drop the unused one.
Exercise
Why can a bundler analyze import { area } from './math.js' without executing any code?
Show answer
Import declarations must appear at the top level and their specifiers must be string literals. The bundler can therefore read both the target module and the imported names by parsing the source alone.