Overview
Tree shaking relies on the static structure of ES modules (import and export) to determine which parts of a library are actually being used. Unused functions or classes are 'shaken off' and excluded from the production build.
Requirements
- ES Modules: Code must use
importandexportrather than CommonJSrequire. - Static Analysis: The bundler must be able to determine usage at build time.
- Side-Effect Free: Code should not have hidden side effects that prevent it from being safely removed.
Benefits
Significantly reduces the size of the final JavaScript bundle, especially when using large utility libraries like Lodash or UI frameworks.