DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

What Is Tree Shaking? How Bundlers Remove Unused JavaScript

Tree shaking removes unused JavaScript during bundling. Here is how webpack and Rollup decide what to drop, and how sideEffects settings can break it.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Tree shaking is dead-code elimination during JavaScript bundling. A bundler reads your imports and exports, works out which code is never used, and leaves that code out of the production output. MDN’s glossary defines it as “the removal of dead code”, with bundlers using module imports and exports to decide what is used. It happens at build time, not in the browser, and it only works well when your code and your dependencies cooperate.

How tree shaking works

A bundler starts at your application’s entry points and follows the dependency graph. When modules use ES2015 import and export syntax, the bundler can often tell which exported bindings are actually used. It marks the unused ones, and the production minimizer then removes the statements it can prove are safe to drop. webpack’s guide demonstrates this with an unused exported function that disappears from the minified bundle. The example output is only “a few bytes smaller”, so treat it as an illustration, not a typical saving.

Tree shaking is not a browser feature. Tools such as webpack and Rollup do the work while generating output, so it does not clean up an arbitrary script at runtime.

Why ES module syntax matters

Static import/export statements let the bundler analyze the code without running it. If a compiler transforms those statements into CommonJS before the bundler sees them, the bundler has less static information and may keep more code. According to webpack’s guide, you should preserve ES module syntax through your toolchain up to the bundling step.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Unused exports versus side effects

webpack treats two things separately, as its tree shaking guide explains:

  • Used-export analysis (usedExports): marks exports nobody uses. The code is actually removed only if the minimizer can prove the statements are safe to delete.
  • The sideEffects flag: declares whether importing a file does anything beyond providing exports. If a file is correctly marked side-effect-free and nothing from it is used, webpack can skip the whole module and its dependency subtree.

What counts as a side effect

Some modules matter even if the app uses none of their exports:

  • CSS imports
  • Polyfills
  • Global registrations and event listeners
  • Changes to prototypes

Setting "sideEffects": false in package.json is a correctness claim that importing any file in the package has no meaningful behavior apart from its exports. If the claim is too broad, the bundler may drop styles or setup code, which is the usual cause of “my CSS disappeared after tree shaking”. Instead, list the files that do have effects, including CSS, using patterns. webpack’s guide describes this approach.

Rollup’s equivalent controls

Rollup uses treeshake.moduleSideEffects. According to the Rollup configuration documentation, the default is true. Setting it to false makes Rollup assume that modules from which nothing is imported have no other effects. Rollup warns that this can remove setup modules, polyfills or styles. Rollup core does not read a package’s sideEffects field itself. The node-resolve plugin can read it and set per-module behavior. Check your Rollup version and plugin setup before relying on these details.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A practical workflow

  1. Measure first. MDN recommends using the browser’s network and performance tools to find real bottlenecks before optimizing.
  2. Keep ES module syntax intact until the bundler runs.
  3. Build in production mode so the minimizer runs.
  4. Declare side effects honestly: list CSS, polyfills and initialization files rather than flagging the whole package.
  5. Test a minimal production build that imports one component. Inspect the generated bundle, then confirm that styles and behavior still work.

Tree shaking versus related techniques

Technique What it targets When it happens
Tree shaking / dead-code elimination Unused exports, modules or statements that can be proven unnecessary Build
Minification The characters in the code that remains Build
Compression (gzip, Brotli) Bytes sent over the network Transfer
Code splitting / deferred loading When separate pieces of JavaScript load; nothing is deleted Runtime loading

These are distinct and are normally combined, as MDN’s JavaScript performance guide explains.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What to expect from the results

Removing unused JavaScript can reduce the code transferred, parsed and executed. None of the documentation reviewed gives a general percentage or guaranteed speedup, so the benefit depends on how much of your dependencies you actually use. Verify it by comparing bundle size and load behavior before and after.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.