JavaScript · Senior · Question 58 of 75
Why might a bundler fail to tree-shake an unused export from a library?
- ✓Correct answer: The code isn't ESM, or the module has (or is assumed to have) side effects, e.g. no
"sideEffects": falseinpackage.json - 2Tree shaking works only in development
- 3Named exports can't be tree-shaken
Explanation
Static import/export lets bundlers prove a binding is unused. CommonJS require and top-level side effects (polyfills, global registration) force the module to be kept.