-
Notifications
You must be signed in to change notification settings - Fork 384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
webpack打包bundle.js依赖分析 #64
Comments
Hello,少峰。麻烦您添加一下最后一个章节 |
|
闭包没看懂 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
问题
用了webpack有一段时间了,其实对webpack打包出来的bundle.js里面到底是什么,怎么实现的不了解,只是知道里面有一坨东西。恰好有同事研究到了这个,觉得非常有意思,所以自己也来研究一下。
一个入口,一个文件
分析:
1、 整个bundle.js其实是一个自执行表达式,传入参数是一个数组,数组的第一项是一个function,function里面是原先index.js里面真正的内容。
2、 IIFE里面有闭包,__webpack_require_是模块加载函数,接收模块id(对,webpack中每个模块都会有一个独一无二的id,其实也就是在IIFE传参数组中的索引值(0,1,2.....)
3、真正执行module index里面语句的是个调用
我们下面来看看webpack这么做是什么意思。
一个入口,两个文件,A依赖B
分析:
4.我们再来分析一下
使用call是因为为了确保每个module中的this指向的是module本身。然后给它传__webpack_require函数是想让module有加载其他module的能力。
两个入口,两个出口
下面我们讨论:A包含B和C,D包含C。
我们看看最后build出来的两个bundle.js有什么不同。
分析:
问题:这种情况下有个问题,那就是module c在两个bundle.js被重复包含了,怎么提取出来呢?
// 未完待续
The text was updated successfully, but these errors were encountered: