首页 技术 正文
技术 2022年11月15日
0 收藏 385 点赞 3,875 浏览 833 个字

webpack_bundle_analyzer 是什么?

这是webpack官方出品的,对项目中模块依赖关系及体积的分析插件,其界面如下:

探寻 webpack_bundle_analyzer 原理

问题来了,这是如何来进行统计的?

这里提供一个插件的代码,可以观察到里面的 json 结构。

var fs = require('fs');
var path = require('path');function FunctionFinder() {};FunctionFinder.prototype.apply = function (compiler) {
compiler.plugin('emit', function (compilation) {
compiler.plugin('done', function(stats){ let json = stats.toJson({
timings: false,
cached: false,
errors: false,
errorDetails: false,
warning: false
}); fs.writeFile(path.resolve(`./1.json`) , JSON.stringify(json.chunks[1]), function (err) {
if (err) {
console.log(err);
} else {
console.log('ok.');
}
});
});
});
};module.exports = FunctionFinder;

代码中的 stats 是一个巨大的对象,里面包含 chunks,也就是 webpack 分块之后的文件模块,如果项目比较大的话,直接输出 json 可能会报内存溢出,此时可以分批写入文件或者写入不同文件。

上面代码中,只是将 chunks 数组里下标为1的 chunk 到 1.json 中,最终格式化后,可以看到:

探寻 webpack_bundle_analyzer 原理

基本就是类似这样的 json 结构,从里面可以看到依赖关系,文件尺寸等。

看到这里,是否已经清楚 webpack_bundle_analyzer 的原理了呢?

更为深入的部分,可以直接去看源代码,这里只做简单介绍。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848