71 lines
2.2 KiB
JavaScript
71 lines
2.2 KiB
JavaScript
#!/usr/bin/env node
|
|
import { gzipSync } from "node:zlib";
|
|
import { readdir, readFile, stat } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
const distAssetsDir = path.join(rootDir, "dist", "assets");
|
|
const maxChunkGzipBytes = Number(process.env.MAX_BUNDLE_CHUNK_GZIP_KB ?? "170") * 1024;
|
|
const maxTotalGzipBytes = Number(process.env.MAX_BUNDLE_TOTAL_GZIP_KB ?? "230") * 1024;
|
|
|
|
function check(id, ok, detail) {
|
|
return { id, state: ok ? "pass" : "fail", detail };
|
|
}
|
|
|
|
function formatKiB(bytes) {
|
|
return `${(bytes / 1024).toFixed(2)} KiB`;
|
|
}
|
|
|
|
const entries = await readdir(distAssetsDir);
|
|
const jsFiles = entries.filter((name) => name.endsWith(".js")).sort();
|
|
const chunks = [];
|
|
|
|
for (const name of jsFiles) {
|
|
const filePath = path.join(distAssetsDir, name);
|
|
const [content, fileStat] = await Promise.all([readFile(filePath), stat(filePath)]);
|
|
chunks.push({
|
|
name,
|
|
bytes: fileStat.size,
|
|
gzipBytes: gzipSync(content).byteLength,
|
|
});
|
|
}
|
|
|
|
const maxChunk = chunks.reduce((largest, chunk) => (
|
|
!largest || chunk.gzipBytes > largest.gzipBytes ? chunk : largest
|
|
), undefined);
|
|
const totalGzipBytes = chunks.reduce((sum, chunk) => sum + chunk.gzipBytes, 0);
|
|
const checks = [
|
|
check("bundle-js-files", chunks.length > 0, `${chunks.length} JavaScript chunks`),
|
|
check(
|
|
"bundle-max-js-gzip",
|
|
Boolean(maxChunk) && maxChunk.gzipBytes <= maxChunkGzipBytes,
|
|
maxChunk ? `${maxChunk.name} ${formatKiB(maxChunk.gzipBytes)} <= ${formatKiB(maxChunkGzipBytes)}` : "no JavaScript chunks",
|
|
),
|
|
check(
|
|
"bundle-total-js-gzip",
|
|
totalGzipBytes <= maxTotalGzipBytes,
|
|
`${formatKiB(totalGzipBytes)} <= ${formatKiB(maxTotalGzipBytes)}`,
|
|
),
|
|
];
|
|
const failed = checks.filter((item) => item.state === "fail").length;
|
|
const report = {
|
|
schema: "motrixsim-web.bundle-size.v1",
|
|
distAssetsDir,
|
|
thresholds: {
|
|
maxChunkGzipBytes,
|
|
maxTotalGzipBytes,
|
|
},
|
|
summary: {
|
|
result: failed > 0 ? "fail" : "pass",
|
|
passed: checks.length - failed,
|
|
failed,
|
|
total: checks.length,
|
|
},
|
|
chunks,
|
|
checks,
|
|
};
|
|
|
|
console.log(JSON.stringify(report, null, 2));
|
|
process.exitCode = failed > 0 ? 1 : 0;
|