简体   繁体   English

什么是摇树,我为什么需要它?

[英]What is Tree Shaking and Why Would I Need It?

I've started learning about Angular 2 and have come across this term "tree shaking" and I haven't been able to find any good explanation of it from a beginners' perspective.我已经开始学习 Angular 2 并遇到过“摇树”这个术语,但我无法从初学者的角度找到任何好的解释。

I have two questions here:我在这里有两个问题:

  1. What is tree shaking and why would I need it?什么是摇树,我为什么需要它?
  2. How do I use it?我如何使用它?

I see you have three questions here;我看到你在这里有三个问题; 1. What is tree shaking? 1. 什么是摇树? 2. What's the need of it? 2. 需要什么? 3. And, how do you use it? 3. 还有,你如何使用它?

1. What's tree shaking? 1. 什么是摇树?

Tree shaking refers to dead code elimination .摇树是指消除死代码 It means that unused modules will not be included in the bundle during the build process.这意味着在构建过程中未使用的模块不会包含在包中。

When we import and export modules in JavaScript, most of the time there is unused code floating around.当我们在 JavaScript 中importexport模块时,大部分时间都会有未使用的代码漂浮。 Excluding that unused code (also referred as dead code) is called tree shaking .排除未使用的代码(也称为死代码)称为摇树

Utilizing the tree shaking and dead code elimination can significantly reduce the code size we have in our application.利用摇树和死代码消除可以显着减少我们应用程序中的代码大小。 The less code we send over the wire the more performant the application will be.我们通过网络发送的代码越少,应用程序的性能就越高。

在此处输入图片说明

在此处输入图片说明

2. What's the need of tree shaking? 2. 摇树需要什么?

Tree Shaking helps us to reduce the weight of the application. Tree Shaking 帮助我们减轻应用程序的重量。 For example, if we just want to create a “Hello World” Application in AngularJs 2 then it will take around 2.5MB, but by tree shaking we can bring down the size to just few hundred KBs, or maybe a few MBs.例如,如果我们只想在AngularJs 2 中创建一个“Hello World”应用程序,那么它大约需要 2.5MB,但是通过 treeShaking 我们可以将大小降低到几百 KB,或者可能只有几 MB。

3. How to use / implement tree shaking? 3. 如何使用/实现摇树?

Tools like webpack will detect dead code and mark it as “unused module” but it won't remove the code.webpack这样的工具会检测死代码并将其标记为“未使用的模块”,但不会删除代码。 Webpack relies on minifiers to cleanup dead code, one of them is UglifyJS plugin, which will eliminate the dead code from the bundle. Webpack 依赖于 minifiers 来清理死代码,其中之一是UglifyJS插件,它将从包中消除死代码。

// modules.js
export function drive(props) {
 return props.gas
}

export function fly(props) {
 return props.miles 
}

// main.js
import { drive } from modules;

/// some code
eventHandler = (event) => {
  event.preventDefault()
  drive({ gas: event.target.value })
}
/// some code

// fly() was never importent and won't be included in our bundle

It only works with import and export .它仅适用于importexport It won't work with CommonJS require syntax.它不适用于 CommonJS require语法。

Same applies to npm dependencies.这同样适用于 npm 依赖项。 great example is lodash, just import pick from 'lodash/pick' and your bundle will only include one small module instead of entire lodash library.一个很好的例子是 lodash,只需import pick from 'lodash/pick' ,你的包将只包含一个小模块而不是整个 lodash 库。

It just means that code that is in you project but not used/referenced anywhere will be dropped.这只是意味着您项目中但未在任何地方使用/引用的代码将被删除。 Like if you import a full library just to use 1 function in it.就像你导入一个完整的库只是为了在其中使用 1 个函数。 It reduces compile code size.它减少了编译代码的大小。

🌲 The Tree Shaking process reduces the download size of an application 🌲 Tree Shaking 过程减少了应用程序的下载大小
🌲 Tree shaking not exporting the modules that are not needed by our application in the bundle file, it is not going to remove the unused code from the bundle. 🌲 摇树不会在包文件中导出我们的应用程序不需要的模块,它不会从包中删除未使用的代码
🌲 Webpack removes the links and UglifyJs Plugin removes the code 🌲 Webpack移除链接UglifyJs Plugin 移除代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM