Q49: What is Tree Shaking and how does static analysis make it possible?

Tree Shaking is a term for dead-code elimination in JavaScript bundlers (Webpack, Rollup). Static analysis of ES Module import/export statements enables bundlers to detect and remove un-referenced exports from final production bundles.

Key Takeaways
  • Eliminates dead, unused JavaScript code from distribution bundles.
  • Requires ES Module static structure; fails with dynamic CommonJS require().
Knowledge Check

Why does dynamic `require()` syntax in CommonJS prevent effective tree shaking?