Repository navigation
Expand file tree
/
Copy pathcheck-performance-budgets.mjs
More file actions
76 lines (70 loc) · 2.39 KB
/
Copy pathcheck-performance-budgets.mjs
File metadata and controls
76 lines (70 loc) · 2.39 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
65
66
67
68
69
70
71
72
73
74
75
76
import { gzipSync } from "node:zlib";
import { readFileSync } from "node:fs";
import { spawnSync } from "node:child_process";
const budgets = JSON.parse(readFileSync("performance-budgets.json", "utf8"));
const failures = [];
const measurements = [];
for (const [path, budget] of Object.entries(budgets.artifacts)) {
let contents;
try {
contents = readFileSync(path);
} catch {
failures.push(
`${path}: missing; run npm run build before checking budgets`,
);
continue;
}
const bytes = contents.byteLength;
const gzipBytes = gzipSync(contents).byteLength;
measurements.push(`${path}: ${bytes} bytes, ${gzipBytes} bytes gzip`);
if (bytes > budget.maximumBytes) {
failures.push(
`${path}: ${bytes} bytes exceeds ${budget.maximumBytes}-byte budget`,
);
}
if (gzipBytes > budget.maximumGzipBytes) {
failures.push(
`${path}: ${gzipBytes} gzip bytes exceeds ${budget.maximumGzipBytes}-byte budget`,
);
}
}
const pack = spawnSync(
process.platform === "win32" ? "npm.cmd" : "npm",
["pack", "--dry-run", "--ignore-scripts", "--json"],
{ encoding: "utf8" },
);
if (pack.status !== 0) {
failures.push(`npm pack --dry-run failed:\n${pack.stderr || pack.stdout}`);
} else {
const [result] = JSON.parse(pack.stdout);
const packageMeasurements = {
files: result.files.length,
packedBytes: result.size,
unpackedBytes: result.unpackedSize,
};
measurements.push(
`package: ${packageMeasurements.packedBytes} packed bytes, ${packageMeasurements.unpackedBytes} unpacked bytes, ${packageMeasurements.files} files`,
);
if (packageMeasurements.files > budgets.package.maximumFiles) {
failures.push(
`package: ${packageMeasurements.files} files exceeds ${budgets.package.maximumFiles}-file budget`,
);
}
if (packageMeasurements.packedBytes > budgets.package.maximumPackedBytes) {
failures.push(
`package: ${packageMeasurements.packedBytes} packed bytes exceeds ${budgets.package.maximumPackedBytes}-byte budget`,
);
}
if (
packageMeasurements.unpackedBytes > budgets.package.maximumUnpackedBytes
) {
failures.push(
`package: ${packageMeasurements.unpackedBytes} unpacked bytes exceeds ${budgets.package.maximumUnpackedBytes}-byte budget`,
);
}
}
if (failures.length > 0) {
console.error(failures.join("\n"));
process.exit(1);
}
console.log(`Performance budgets passed:\n${measurements.join("\n")}`);