当前位置:网站首页>What is the principle of tree shaking?
What is the principle of tree shaking?
2022-07-18 02:03:00 【Front end students】
What is? tree-shaking
Don't call it shaking tree , The first is the Rollup Realization , It is a technology that optimizes the volume of code by deleting unnecessary additional code
tree-shaking During the packaging process Static analysis Import and export between modules , Determine which module export values are not marked with a pie , And delete it , Thus, the optimization of packaging products is realized .
tree-shaking The foundation that can be achieved
Before CommonJs、AMD、CMD In the modular scheme of , This import and export is dynamic Of , Unpredictable , So in the packaging phase , It is impossible to analyze which modules are used , for example :
if(someTrue){
require('./bar');
exports.foo = 'foo';
}
and ES static state module Under the plan , The dependencies between modules are highly deterministic and static , It has nothing to do with the running state , Reliable static analysis , So the whole dependency tree can be statically derived from the parse syntax tree , You can do it at compile time analysis ESM Module , You can infer from the literal amount of code which modules are not used , This is it. tree-shaking Necessary conditions for realization .
tree-shaking Examples of the role of
You can see in the example ,bar.js We have derived bar、foo, And only bar stay index.js Used in ,foo Never used , after tree-shaking After processing ,foo Variables will be deleted as useless code .
// index.js
import {bar} from './bar';
console.log(bar);
// bar.js
export const bar = 'bar';
export const foo = 'foo';
tree shaking What is the principle of ?
After reading the above analysis , I believe here you can easily get the answer to the question :
- ES6 Module Introduce static analysis , So when compiling, you should correctly judge which modules are loaded
- Static analysis program flow , Determine which modules and variables are not used or referenced , And then delete the corresponding code
边栏推荐
- 【第018问 Unity中对Quaternion.AngleAxis的理解?】
- 第四十四期:高效团队养成
- Use scroll view to scroll the content list vertically
- 51 single chip microcomputer serial port baud rate (keep it and don't look everywhere)
- Machine learning preparatory knowledge: classification and regression
- Determine whether the currently requested network is internal or external
- 第四十二期:所谓的管理团队
- WinForm控件属性大全
- redis持久化之aof
- Analyse de la « collaboration » du Bureau collaboratif pourquoi, le Bureau numérique et comment facile « résoudre le problème »?
猜你喜欢

百家号排名会丢吗,不稳定怎么办?

对比一下优质【测试报告】模板与你自己的有何不同?

已备案域名,严格管控买卖,你知道吗?

iotop命令(监视磁盘io状况)

针对采集丢权益,企业站,还值得深耕内容吗?

许式伟:Go+ 演进之路

What are the problems we need to pay attention to in setting the standard of Baidu search basic information?

百度搜索基础信息设置规范,有哪些我们需要注意的问题呢?

保持电气化时代的交通安全“零伤亡”,沃尔沃底气何来?

How to do Zhihu SEO and how to improve Zhihu SEO ranking?
随机推荐
VDD,VCC,VSS,GND,地之间有何区别?
Isbackground attribute of C thread
Inner class
软件测试-基础篇
[practice C] xiaolele walks up the steps
Issue 41: a project is failing
matlab 疑问
蛋糕可以一刀切,研发需求可以吗? | 敏捷实践
接口开发不用写Controller、Service、Dao、Mapper、XML、VO,全自动生成!
Officially released vs Code 1.69
Use scroll view to scroll the content list vertically
Send your code into space and develop "the greatest work" with Huawei cloud
“OpenCvSharp.Mat“的类型初始值设定项引发异常
第三十六期:近期文档计划
全球No.1港航人工智能AI企业中集飞瞳,港航人工智能产品成熟化标准化大规模应用,为港口大幅提效降本,提升港区效能,优化港区次序
[Verilog] 32-bit single precision floating-point number comparison size
2020-10-26
2020-10-26
Issue 42: the so-called management team
[HCIA] data communication network foundation