Tree Shaking

patternsdev/skills/javascript/tree-shaking

作者 patternsdev48bf58a488cdMIT253 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5个月前更新

Teaches tree shaking for dead code elimination in JavaScript bundles. Use when your bundle includes unused exports and you want to reduce the final bundle size during the build step.

AI 生成的概览

讲解通过 tree shaking 移除未使用的 JavaScript 导出,以在构建阶段减小打包体积。

功能
该技能提供关于 tree shaking 的教学指导,即在 JavaScript 打包产物中消除死代码的过程。内容涵盖 ES2015 import/export 语法要求、具名导入、将包标记为无副作用,以及打包工具如何遍历模块图并只保留可达部分。它产出的是说明文字和代码示例,而不是文件或脚本。
适用场景
当 JavaScript 或 TypeScript 打包产物包含来自导入模块的未使用导出,并希望减小最终打包体积时使用。它也适用于分析会阻碍安全 tree shaking 的副作用问题。
运行要求
不需要脚本或工具,仅为说明性内容。假定读者熟悉 webpack 或 Rollup 等 JavaScript 模块打包工具。

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:

js
export function read(props) {  return props.book}
export function nap(props) {  return props.winks}

Then we have the following index.js file:

js
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

来源与署名

来源:patternsdev/skills位于javascript/tree-shaking提交48bf58a

许可证: MIT

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架