Rollup ESM bundle retains unused exports despite tree-shaking
0 reputation · 29 Nov 2020, 06:22 UTC
0 reputation · 29 Nov 2020, 06:22 UTC
This build configures Rollup to tree-shake an ES-module bundle for a low-traffic site. The goal is to confirm that unused exports are removed based on side-effect analysis, and to understand whether retained code is expected behavior or a configuration gap.
Rollup documentation indicates tree-shaking depends on accurate side-effect detection for imported files, and behavior may differ between Rollup 3.x and 4.x because of changes to side-effect flag handling. It is unclear whether a specific import is being treated as side-effectful, preventing elimination of an unused export.
Plugin ordering, particularly minification plugins, could preserve dead code before the bundle is finalized.
Which Rollup version governs side-effect flag handling in this build? Is an imported module being treated as side-effectful, and what evidence appears in the bundle output? Does minifier placement change whether unused exports survive tree-shaking?
35275 reputation · 29 Nov 2020, 07:04 UTC
The Rollup version that controls side‑effect flag handling is the version actually used in the build (check with rollup --version or the devDependencies entry). If you are on Rollup 4.x, side‑effect detection is stricter; on Rollup 3.x it is more permissive.
Likely explanation: an imported module is being treated as side‑effectful, so Rollup cannot drop its unused export. This happens when the module’s package.json lacks "sideEffects": false or contains top‑level code that Rollup cannot prove pure (e.g., a function call, class definition, or side‑effecting import).
Confirmed facts (observable in the bundle):
--plugin rollup-plugin-visualizer --visualizer.filename=stats.html) to see which modules are marked as having side effects.input.plugin, it runs before Rollup’s tree‑shaking pass and can obscure pure‑ness checks, causing dead code to be retained.rollup --version or inspect package.json.package.json for "sideEffects": false. If missing, add it (or set treeshake.moduleSideEffects: 'no-external' in config).input.plugins to output.plugins so it runs after bundling.Ask for one missing diagnostic detail only when it changes the recommendation: we need to know the exact Rollup version (or the name of the imported module) to confirm whether the stricter 4.x side‑effect rules apply.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.