Guide
Configure Tree Shaking in Rollup to Remove Unused JavaScript Code
Learn how to enable Rollup’s tree shaking to drop unused ES‑module exports, verify the size reduction, and handle common pitfalls like side‑effects and dynamic imports.
Published by Tasadduq Burney
03 Sept 2025, 09:19 UTC
3 min106.3K views0

Desired outcome
Reduce the size of a JavaScript bundle by having Rollup drop unused ES‑module exports (tree shaking) while preserving the application’s runtime behavior.
Prerequisites
- Node.js ≥14 installed.
- A project that uses ES‑module syntax (import/export) for at least one module.
- Basic familiarity with a terminal and npm/yarn.
- Rollup installed as a dev dependency (
npm i -D rollup).
Procedure
- Create a minimal example (if you don’t already have code):
# src/utils.js export function used() { return 'kept'; } export function unused() { return 'removed'; } # src/main.js import { used } from './utils.js'; console.log(used()); - Add Rollup plugins (optional but common for resolving dependencies):
npm i -D @rollup/plugin-node-resolve @rollup/plugin-commonjs - Create or edit the Rollup configuration file (
rollup.config.js) in the project root:import { nodeResolve } from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; export default { input: 'src/main.js', output: { file: 'dist/bundle.js', format: 'iife', // immediately‑invoked function expression for browsers name: 'MyApp' }, plugins: [nodeResolve(), commonjs()], // Enable tree shaking – the default is true for ES modules, but we make it explicit treeshake: { // Preserve side‑effects for modules that may need them (e.g., polyfills) moduleSideEffects: 'no-external' } }; - Add a build script to
package.json:"scripts": { "build": "rollup -c" } - Run the build:
npm run build - Inspect the output (optional but recommended):
You should see that the# Show the bundle content cat dist/bundle.jsunusedfunction is not present in the bundle.
Expected checks
- File size reduction: compare
dist/bundle.jssize before and after enablingtreeshake. A noticeable drop (often 10‑50 % for small examples) indicates tree shaking worked. - Visual verification: install
rollup-plugin-visualizerand add it to the plugins array, then rebuild and open the generated report to confirm thatunused(or wholeutils.jsif fully unused) is absent. - Runtime test: run the application’s test suite or manually execute the bundle in a browser/Node environment to ensure the used functionality still works.
Limitations and cautions
- Dynamic imports (
import()) are not analyzed for tree shaking; they remain in the bundle unless manually marked with side‑effect flags. - Modules that rely on runtime‑evaluated property names (e.g.,
export [computed] = …) may be incorrectly considered unused and removed. - Non‑ESM CommonJS modules processed via
@rollup/plugin-commonjsexpose all exports unless you declare side effects inpackage.json("sideEffects": false) or mark individual files. - Circular dependencies can break the analysis; try to refactor to eliminate cycles or set
treeshake: { moduleSideEffects: false }as a guard for the problematic module. - When using
--watch, incremental builds may skip a full analysis, leaving dead code. Perform a full rebuild (rollup -c) after watching to verify.
Recovery options (rollback)
If the bundle size does not decrease or the application breaks after enabling tree shaking, revert the change:
- Remove or set
treeshake: falseinrollup.config.js. - Re‑run the build:
npm run build. - Confirm the bundle returns to its previous size and that the application works.
This rollback only affects the build output; source code remains unchanged.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.