@e.fe/filesize
Version:
File size calculation tool
103 lines (97 loc) • 4.15 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
// src/bin.ts
var import_node_process2 = __toESM(require("process"));
var import_picocolors2 = __toESM(require("picocolors"));
// src/analyze.ts
var import_promises2 = __toESM(require("fs/promises"));
var import_node_path = __toESM(require("path"));
var import_node_process = __toESM(require("process"));
var import_filesize = require("filesize");
var import_glob = require("glob");
var import_picocolors = __toESM(require("picocolors"));
// src/gzip.ts
var import_promises = __toESM(require("fs/promises"));
async function getGzipSize(filePath) {
const content = await import_promises.default.readFile(filePath);
return new Promise((resolve) => {
import("zlib").then(({ gzip }) => {
gzip(content, (err, result) => {
if (err)
resolve(0);
else resolve(result.length);
});
});
});
}
// src/analyze.ts
async function analyze(target = "dist", {
pattern = "*.@(js|mjs|cjs)"
} = {}) {
let patternPath;
const stats = await import_promises2.default.stat(target).catch(() => null);
if (stats && stats.isDirectory()) {
patternPath = `${target}/**/${pattern}`;
} else {
patternPath = target;
}
console.log(import_picocolors.default.cyan(`Using pattern: ${patternPath}`));
const files = await (0, import_glob.glob)(patternPath, { nodir: true });
if (files.length === 0) {
console.log(import_picocolors.default.yellow("Not found any files"));
return;
}
const filesWithSize = await Promise.all(
files.map(async (file) => {
const stats2 = await import_promises2.default.stat(file);
return {
path: file,
size: stats2.size,
gzip: await getGzipSize(file)
};
})
);
filesWithSize.sort((a, b) => b.size - a.size);
const totalSize = filesWithSize.reduce((acc, file) => acc + file.size, 0);
const totalGzip = filesWithSize.reduce((acc, file) => acc + file.gzip, 0);
console.log(import_picocolors.default.bold("\n\u{1F4E6} File size analysis report:\n"));
filesWithSize.forEach((file) => {
const relativePath = import_node_path.default.relative(import_node_process.default.cwd(), file.path);
const percentage = (file.size / totalSize * 100).toFixed(1);
console.log(
import_picocolors.default.blue(relativePath.padEnd(50)),
import_picocolors.default.yellow((0, import_filesize.filesize)(file.size).padEnd(15)),
import_picocolors.default.green(`(gzip: ${(0, import_filesize.filesize)(file.gzip)})`),
import_picocolors.default.gray(`${percentage}%`)
);
});
console.log(`
${import_picocolors.default.bold("Total:")}`);
console.log(import_picocolors.default.yellow(`Raw: ${(0, import_filesize.filesize)(totalSize)}`));
console.log(import_picocolors.default.green(`Gzip: ${(0, import_filesize.filesize)(totalGzip)}`));
}
// src/bin.ts
var inputPath = import_node_process2.default.argv[2] || "dist";
console.log(import_picocolors2.default.cyan(`Analyze path: ${inputPath}`));
analyze(inputPath);