Rollup tree shaking behavior with sideEffects property in nested dependencies
0 reputation · 03 Aug 2022, 01:30 UTC
When bundling a JavaScript library using Rollup, the effectiveness of dead-code elimination depends heavily on the sideEffects property within the package.json. While Rollup performs static analysis on ES modules to identify unused exports, the presence of global mutations or imported CSS can cause the bundler to retain unnecessary code.
The uncertainty arises when a nested dependency declares "sideEffects": false, but contains modules that perform essential initialization tasks, such as registering polyfills or modifying global state. If the primary library only imports a specific function from that dependency, Rollup may omit the side-effect producing modules if they are not explicitly referenced in the dependency graph.
How does Rollup prioritize the sideEffects flag when the module graph contains dynamic imports that reference the same dependency? Can the use of /*@__PURE__*/ annotations override a global sideEffects: false declaration in a deep dependency?