Common chunks#52
Conversation
|
注:
|
|
同一个 page 有多个 entries: [ 如果 则最后会在 但是,通过实际运行服务发现,由于页面预先加载了 |
33c311d to
71d180c
Compare
|
~ |
|
|
||
| - vendor | ||
|
|
||
| 公用固定依赖集合,使用字段信息如 entries ,但是不支持数组 |
There was a problem hiding this comment.
这个说明不太清晰啊,没看明白 vendor 的值需要是什么格式的;
使用字段信息如 entries
这里的 entries 不好确定说的是最外层的 entries 还是 pages 中的 entries。
另外为什么 vendor 需要在每个 page 下指定呢,我感觉,vendor 跟 page 好像没啥关系?它跟 entries 有关系,是用来抽取 entries 间的公共部分的,所以好像整个项目只需要一处 vendor 的配置?
| const _ = require('lodash') | ||
| const webpack = require('webpack') | ||
| const update = require('immutability-helper') | ||
| const { abs } = require('../../utils/paths') |
There was a problem hiding this comment.
| return { | ||
| fixedName: fixPageName(name), | ||
| vendor: page.vendor, | ||
| entries: Array.isArray(page.entries) ? page.entries : [page.entries] |
There was a problem hiding this comment.
| entries.forEach(entry => { | ||
| vendorPlugins.push( | ||
| new webpack.optimize.CommonsChunkPlugin({ | ||
| name: vendor, |
There was a problem hiding this comment.
要不要指定一个带 hash 的 filename 啊?
| vendorPlugins.push( | ||
| new webpack.optimize.CommonsChunkPlugin({ | ||
| name: vendor, | ||
| chunks: [ |
There was a problem hiding this comment.
这是对 page 上的每个 entry,都创建一个它跟该页 vendor 的 common chunk?这事儿好像没啥收益啊
如果是按 page 配 vendor,为啥不是该页所有的 entry 与该 vendor 去创建一个 common chunk?
There was a problem hiding this comment.
因为 entry 和 entry 之间可能有相同部分,如果 vendor + entry1 + entry2 去抽取的话,会把 vendor 里没有 entry1, entry2 里没有的抽进 vendor 里。所以按照 vendor + entry1, vendor + entry2 这样抽
There was a problem hiding this comment.
如果 vendor + entry1 + entry2 去抽取的话,会把 vendor 里没有 entry1, entry2 里没有的抽进 vendor 里
没太理解
There was a problem hiding this comment.
会把 vendor 里没有 entry1, entry2 里有的抽进 vendor 里。多打了一个字
| // 确保 vendor 在内容不变的情况下不会因为对 entries 的依赖改变而导致 hash 发生变化 | ||
| ...fixedPages.map(({ fixedName, entries, vendor }) => { | ||
| if (vendor) { | ||
| return new webpack.optimize.CommonsChunkPlugin({ |
There was a problem hiding this comment.
这边 name 没有对应的 chunk,minChunks 又设置的 Infinity,所以其实会创建空的 common chunk?为啥这个可以:
确保 vendor 在内容不变的情况下不会因为对 entries 的依赖改变而导致 hash 发生变化
There was a problem hiding this comment.
因为在 页面进行改动的情况下,内部的 requireid 可能会重新分配,如果没有这个的话,会导致 vendor 里面的 requireid 也跟着变化。 有了这个文件后,vendor 里的不会变化,变化的是这个接近空的文件。 https://webpack.js.org/plugins/commons-chunk-plugin/#manifest-file
There was a problem hiding this comment.
明白,为啥我们不按惯例叫 "manifest",这边要防止名字冲突的话可以带上类似 fec 的前缀,__ 感觉不是特别好看 🤣
There was a problem hiding this comment.
我老是 把 manifest 和 manifest.json 联系在一起,再加上那个 ssr 也生成了一个 manifest
| plugins: { $push: [ | ||
| // async common-chunks | ||
| // 这里指定的是全部 pages 因为 chunks 和 children 不能同时设定 | ||
| new webpack.optimize.CommonsChunkPlugin({ |
There was a problem hiding this comment.
There was a problem hiding this comment.
对,这个是针对 code-splite 做的,就是 async-chunk 的那些也有公共部分
| }), | ||
| // 确保 vendor 在内容不变的情况下不会因为对 entries 的依赖改变而导致 hash 发生变化 | ||
| ...fixedPages.map(({ fixedName, entries, vendor }) => { | ||
| if (vendor) { |
There was a problem hiding this comment.
我看这里有根据 vendor 进行筛,上边 vendorPlugins 也有做这个事儿,可以提前把 fixedPages
根据是否有 vendor filter一下,得到 fixedPagesWithVendor,然后这两处用 fixedPagesWithVendor map 得到就好了
| return null | ||
| }) | ||
| .filter(item => item !== null), | ||
| // 确保 entries 中所有在 vendor 中引入的依赖不会再被重复引入 |
There was a problem hiding this comment.
确保 entries 中所有在 vendor 中引入的依赖不会再被重复引入
这个是怎么确保的。。
There was a problem hiding this comment.
这个问题好呀,我猜是他内部发现前面内存里已经分配了对应的 requireid , 所以这里遇到就不会重复做了,这个注释写得不好
There was a problem hiding this comment.
嗯,我主要是不太确定具体是哪部分代码的行为,做了这行注释说的事情,因为这行注释后边的代码是:
...vendorPlugins,没看出来“确保”的努力。。
There was a problem hiding this comment.
应该是我先写的注释,然后把代码挪走了,哈哈
| } | ||
|
|
||
| /** | ||
| * @desc 在设置 common-chunks 是使用, 调整 pageName, 使得 chunks 里不含 / |
There was a problem hiding this comment.
我看这个结果都是拿去拼 common chunk plugin 的 name 的,这里边含 / 是会有啥后果嘛?
There was a problem hiding this comment.
没啥影响,只是 static 里面多一层目录,感觉不好看
|
整体感觉是,针对每个 page 配好像收益不大,但是理解配置的难度会上升一些。如果不针对每个 page,而是对整个项目的 entries 做,逻辑会不会简单一点? |
|
这样其实差不多,都要配置三个 common-chunks, 而且也是需要指定 chunks 的,因为 vendor 不能按全部 chunks 抽离 |
其实是四个 1. manifest 2. async 3. vendor 4. common 对吧? 1、2 是不用用户配的,3 需要用户配,4 也不需要用户配(我们现在是每个页面一个 common);这样的话,我的想法是:
这是什么原理,不是不指定 |
|
|
早上想到,抽 common 这个事情最好也是有个开关的,比如 大概就是这样了: {
"optimization": {
"extract-common": true,
"extract-vendor": "vendor"
}
}你看咋样 |
|
没啥问题 |
f86ff59 to
3cf87c8
Compare
|
我发现 common 可以身兼 manifest 的功能 |
| if (optCommon || optVendor) { | ||
| plugins.push( | ||
| new webpack.optimize.CommonsChunkPlugin({ | ||
| name: chunks.common |
There was a problem hiding this comment.
这里不用指定个 minChunks 嘛?比如先用 2 或 entries 的数量?没查到不指定的话会发生什么。。
There was a problem hiding this comment.
| import 'lodash' | ||
| // ... | ||
| ``` | ||
| 注:如果设置了改自动,则 extract-common 也开启 |
There was a problem hiding this comment.
我觉得这个绑定的行为不太合理啊,我指定要抽 vendor,不代表我想抽 common 吧?
好像这里绑定起来主要是为了利用 common 作为 manifest?那是不是也开了 extract-common 的时候就利用它,没开的时候就创建一个 manifest?
There was a problem hiding this comment.
common 这个设置为不可更改,就是都开启?
There was a problem hiding this comment.
不是啊,开启 common 的时候就搞一个 common chunk,顺便可以当 vendor 的 manifest 使;没开启 common 的时候,如果开了 vendor,vendor 就自己去搞个 manifest 的 chunk?
There was a problem hiding this comment.
我是说 common 不开启没好处啊
There was a problem hiding this comment.
common 不开启就享受不到抽 common 的好处,这个是预期的呀;如果开了 vendor,不开 common,那我们帮他搞个 manifest + vendor,抽 vendor 的好处不是还是有的嘛?
There was a problem hiding this comment.
手动关了 common,享受不到 common 的好处,不是预期的嘛。。如果手动关了 common,因为手动开了 vendor,导致 common 的行为被打开了,这个才不符合预期呀
There was a problem hiding this comment.
所以我想把 common 从 optimization 里删掉啊
There was a problem hiding this comment.
不要删掉吧,虽然说抽 common 有好处,但也有不好的点,我觉得还是要给关的机会的
|
|
||
| 优化项 | ||
|
|
||
| - extract-common |
There was a problem hiding this comment.
这里是我的锅,我突然发现 build config 里其他的字段名都是驼峰的,这里是不是也驼峰好一点
|
|
||
| - extract-common | ||
|
|
||
| 是否处理 common-chunks |
There was a problem hiding this comment.
“是否抽取 entries 间的公共内容到单独的文件中”?
118bca5 to
2b25bf6
Compare
先合并
#41