vite-plugin-size
Version:
A simple vite plugin that will log your bundle size.
47 lines (46 loc) • 1.85 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const bytes = require("bytes");
const chalk = require("chalk");
const fs = require("fs-extra");
const gzipSize = require("gzip-size");
const fileStats = async (pathName) => {
return await fs.promises.stat(pathName);
};
const formatStats = async (pathName) => {
const stats = await fs.promises.stat(pathName);
stats.gzip = gzipSize.gzipSizeFromFileSync(pathName);
return stats;
};
const computeFileStats = async (pathName) => {
const stats = await formatStats(pathName);
return [stats.size, stats.gzip];
};
const computeDirectoryStats = async (pathName, nested = false) => {
const entries = await fs.promises.readdir(pathName);
const stats = await Promise.all(
entries.map(async (entry) => {
const stats2 = await fileStats(pathName + "/" + entry);
return stats2.isDirectory() ? await computeDirectoryStats(pathName + "/" + entry, true) : formatStats(pathName + "/" + entry);
})
);
if (nested) return stats.flat();
const size = stats.flat().reduce((a, b) => a + b.size, 0);
const gzip = stats.flat().reduce((a, b) => a + b.gzip, 0);
return [size, gzip];
};
const runSize = (pathName = "dist") => {
return {
name: "run-size",
closeBundle: async () => {
const file = await fileStats(pathName);
const [size, gzip] = file.isDirectory() ? await computeDirectoryStats(pathName) : computeFileStats(pathName);
const formattedSize = bytes.format(size, { unitSeparator: " " });
const formattedGZip = bytes.format(gzip, { unitSeparator: " " });
const decoratedTitle = chalk.blue(`bundle size (${pathName}): `);
const decoratedStats = chalk.dim(`${formattedSize} bundle | ${formattedGZip} gzip`);
console.log(decoratedTitle, decoratedStats);
}
};
};
exports.runSize = runSize;
;