forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-bundle-budget.mjs
More file actions
64 lines (52 loc) · 1.63 KB
/
Copy pathcheck-bundle-budget.mjs
File metadata and controls
64 lines (52 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { existsSync, readFileSync, readdirSync } from "node:fs";
import { join } from "node:path";
import { gzipSync } from "node:zlib";
const budgets = {
javascriptGzipBytes: 760 * 1024,
cssGzipBytes: 20 * 1024,
totalGzipBytes: 780 * 1024,
};
const roots = [join(".next", "static")];
const totals = { js: 0, css: 0 };
function walk(dir) {
if (!existsSync(dir)) {
return;
}
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const path = join(dir, entry.name);
if (entry.isDirectory()) {
walk(path);
continue;
}
if (!entry.isFile() || !/\.(js|css)$/.test(entry.name)) {
continue;
}
const bytes = gzipSync(readFileSync(path)).byteLength;
if (entry.name.endsWith(".js")) {
totals.js += bytes;
} else {
totals.css += bytes;
}
}
}
for (const root of roots) {
walk(root);
}
const total = totals.js + totals.css;
const failures = [
["JavaScript gzip", totals.js, budgets.javascriptGzipBytes],
["CSS gzip", totals.css, budgets.cssGzipBytes],
["Total gzip", total, budgets.totalGzipBytes],
].filter(([, actual, budget]) => actual > budget);
function format(bytes) {
return `${(bytes / 1024).toFixed(1)} KiB`;
}
console.log(`JavaScript gzip: ${format(totals.js)} / ${format(budgets.javascriptGzipBytes)}`);
console.log(`CSS gzip: ${format(totals.css)} / ${format(budgets.cssGzipBytes)}`);
console.log(`Total gzip: ${format(total)} / ${format(budgets.totalGzipBytes)}`);
if (failures.length > 0) {
for (const [label, actual, budget] of failures) {
console.error(`${label} budget exceeded: ${format(actual)} > ${format(budget)}`);
}
process.exit(1);
}