gpt4 book ai didi

node.js - 如何在没有所有额外内容的情况下仅获取 Webpack 构建的包文件大小

转载 作者:行者123 更新时间:2023-12-04 07:16:45 25 4
gpt4 key购买 nike

我需要获取捆绑文件大小作为命令输出或将其写入文件。
我考虑过 webpack-bundle-analyzer ,但命令和 JSON 文件输出似乎做了很多与我的用例无关的事情。
我也考虑过 bundlesize 包,但它主要进行比较检查并将失败或成功状态报告为命令输出。
如果有人对哪些相关工具、命令或标志可以帮助实现这一点有任何想法。将不胜感激。
干杯

最佳答案

你好吗? ❤️希望如此。
如果您正在寻找非常具体的东西。你可以试试creating your own plugin代码来提取你需要的东西。

class PluginGetFileSize {
private file: string;

constructor(file: string) {
// receive file to get size
this.file = file;
}
apply(compiler: any) {
compiler.hooks.done.tap(
'Get File Size',
(stats: any) => {
// Get output file
const file = stats.compilation.assetsInfo.get(this.file);

// Verify if file exists
if (!file)
return console.log('File not exists');

// Get file size
const fileSizeInBytes = file.size;

// only used to convert
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];

// Get size type
const sizeType = parseInt(Math.floor(Math.log(fileSizeInBytes) / Math.log(1024)).toString());

// Get size of file using size type
const size = Math.round(fileSizeInBytes / Math.pow(1024, sizeType));

// Here you can log, create a file or anything else
console.log(`${this.file} has ${size} ${sizes[sizeType]}`);
}
);
}
}
对于这个简单的例子,我只需要将一个文件路径传递给 use the plugin但如果需要,您可以在此处传递更多文件。
module.exports = {
//...
plugins: [
new PluginGetFileSize('YOUR-OUTPUT-FILE.js'),
],
};
我相信一定有一些具有类似功能的包,例如:
  • size-plugin
  • webpack dashboard您可以了解更多信息 here
  • webpack analyse
  • bundlephobia以简单的方式可视化您的依赖项大小
  • webpack visualize
  • webpack-bundle-analyzer你自己带来的
  • lighthouse用于从不同方面分析您的整个应用程序

  • 我想就是这样,我希望我有帮助👍

    关于node.js - 如何在没有所有额外内容的情况下仅获取 Webpack 构建的包文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68716056/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com