Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
patternsdev avatar

Tree Shaking

  • 762 installs
  • 235 repo stars
  • Updated April 15, 2026
  • patternsdev/skills

tree-shaking is a patterns.dev agent skill (v1.1) that teaches dead-code elimination in JavaScript bundles so developers can remove unused exports and shrink production frontend payloads.

About

tree-shaking is a patterns.dev skill at version 1.1 that teaches agents how dead-code elimination works in modern JavaScript bundlers and when to apply it during the build step. The skill explains how unused exports and unreachable modules can silently inflate bundle size, then walks through ES module patterns, side-effect flags, and bundler configuration that enable Rollup, webpack, and Vite to strip unused code before deployment. It activates on **/*.js and **/*.ts paths and pairs with related patterns.dev skills module-pattern and singleton-pattern for export design that shakes cleanly. Developers reach for tree-shaking when Lighthouse or bundle analyzers show bloated chunks, when a utility library pulls entire packages into the client, or when refactoring imports after a dependency upgrade. The MIT-licensed guidance is authored by patterns.dev and fits SaaS SPAs and browser extensions where every kilobyte affects Time to Interactive.

  • Eliminates dead code from JavaScript and TypeScript bundles
  • Works with modern bundlers including Vite, Rollup, and esbuild
  • Reduces final bundle size by 15-40% on typical React and Vue apps
  • Agent skill that integrates directly into Cursor and Claude Code workflows
  • Produces optimized production builds with zero runtime overhead

Tree Shaking by the numbers

  • 762 all-time installs (skills.sh)
  • +46 installs in the week ending Aug 4, 2026 (Skillselion tracking)
  • Ranked #457 of 2,245 Frontend Development skills by installs in the Skillselion catalog
  • Data as of Aug 4, 2026 (Skillselion catalog sync)
npx skills add https://github.com/patternsdev/skills --skill tree-shaking

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs762
repo stars235
Last updatedApril 15, 2026
Repositorypatternsdev/skills

How do you remove unused JavaScript from bundles?

Automatically remove unused code, reduce bundle size, and optimize frontend JavaScript output before shipping.

Who is it for?

Frontend developers optimizing webpack, Rollup, or Vite builds where unused library exports inflate client bundle size.

Skip if: Backend-only Node services or projects with no client-side bundler where bundle size is irrelevant.

When should I use this skill?

Trigger when bundle analysis shows dead code, large vendor chunks, or questions about sideEffects and ES module exports arise.

What you get

Smaller production JS bundles with unused exports eliminated via correct ES module and bundler configuration.

  • optimized bundler config
  • ES module export refactors

By the numbers

  • patterns.dev tree-shaking skill version 1.1
  • Activates on **/*.js and **/*.ts file paths
  • Lists 2 related skills: module-pattern and singleton-pattern

Files

SKILL.mdMarkdownGitHub ↗

Tree Shaking

It can happen that we add code to our bundle that isn't used anywhere in our application. This piece of dead code can be eliminated in order to reduce the size of the bundle, and prevent unnecessarily loading more data! The process of eliminating dead code before adding it to our bundle, is called tree-shaking.

When to Use

  • Use this when your bundle includes unused code from imported modules
  • This is helpful for keeping JavaScript bundles lean and improving load performance

Instructions

  • Use ES2015 import/export syntax — only ES modules can be tree-shaken
  • Use named imports instead of importing entire modules to enable effective tree-shaking
  • Mark packages as side-effect-free in package.json when appropriate
  • Be aware that modules with side effects cannot be safely tree-shaken

Details

Concepts

Tree shaking is aimed at removing code that will never be used from a final JavaScript bundle. When done right, it can reduce the size of your JavaScript bundles and lower download, parse and (in some cases) execution time. For most modern JavaScript apps that use a module bundler (like webpack or Rollup), your bundler is what you would expect to automatically remove dead code.

Consider your application and its dependencies as an abstract syntax tree (we want to "shake" the syntax tree to optimize it). Each node in the tree is a dependency that gives your app functionality. In tree shaking, input files are treated as a graph. Each node in the graph is a top level statement which is called a "part" in the code. Tree shaking is a graph traversal which starts from the entry point and marks any traversed paths for inclusion.

Every component can declare symbols, reference symbols, and rely on other files. Even the "parts" are marked as having side effects or not. For example, the statement let firstName = 'Jane' has no side effects because the statement can be removed without any observed difference if nothing needs firstName. But the statement let firstName = getName() has side effects, because the call to getName() can not be removed without changing the meaning of the code, even if nothing needs firstName.

Imports

Only modules defined with the ES2015 module syntax (import and export) can be tree-shaken. The way you import modules specifies whether the module can be tree-shaken or not.

Tree shaking starts by visiting all parts of the entry point file with side effects, and proceeds to traverse the edges of the graph until new sections are reached. Once the traversal is completed, the JavaScript bundle includes only the parts that were reached during the traversal. The other pieces are left out.

Let's say we define the following utilities.js file:

export function read(props) {
  return props.book
}

export function nap(props) {
  return props.winks
}

Then we have the following index.js file:

import { read } from 'utilities';

eventHandler = (e) => {
  read({ book: e.target.value })
}

In this example, nap() isn't important and therefore won't be included in the bundle.

Side Effects

When we're importing an ES6 module, this module gets executed instantly. It could happen that although we're not referencing the module's exports anywhere in our code, the module itself affects the global scope while it's being executed (polyfills or global stylesheets, for example). This is called a side effect. Although we're not referencing the exports of the module itself, _if_ the module has exported values to begin with, the module cannot be tree-shaken due to the special behavior when it's being imported!

The Webpack documentation gives a clear explanation on tree-shaking and how to avoid breaking it.

Source

Related skills

How it compares

Use alongside module-pattern when export structure—not just bundler config—blocks effective dead-code elimination.

FAQ

What problem does tree-shaking solve?

The tree-shaking skill from patterns.dev teaches how bundlers eliminate unused exports and dead code from JavaScript bundles, reducing final payload size when **/*.js or **/*.ts files include symbols never called at runtime.

Which file types trigger the tree-shaking skill?

tree-shaking activates on **/*.js and **/*.ts paths per its SKILL.md manifest, and version 1.1 links to related patterns.dev skills module-pattern and singleton-pattern for export-friendly designs.

Frontend Developmentfrontendintegrations

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.