mirror of
https://github.com/gadgethd/ukmesh.git
synced 2026-12-06 19:24:41 +00:00
79 lines
2.6 KiB
JavaScript
79 lines
2.6 KiB
JavaScript
import { gzipSync } from 'node:zlib';
|
|
import { readdir, readFile } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
|
|
const KIB = 1024;
|
|
const outputDir = path.resolve('dist/assets');
|
|
const files = await readdir(outputDir);
|
|
const { target = 'combined' } = JSON.parse(await readFile(path.resolve('dist/build-target.json'), 'utf8'));
|
|
const websiteTarget = target === 'public-website' || target === 'dev-website';
|
|
|
|
async function measure(matcher) {
|
|
const selected = files.filter(matcher);
|
|
const buffers = await Promise.all(selected.map((file) => readFile(path.join(outputDir, file))));
|
|
return {
|
|
files: selected,
|
|
raw: buffers.reduce((total, buffer) => total + buffer.byteLength, 0),
|
|
gzip: buffers.reduce((total, buffer) => total + gzipSync(buffer).byteLength, 0),
|
|
};
|
|
}
|
|
|
|
const budgets = [
|
|
{
|
|
name: 'main App chunk',
|
|
measure: await measure((file) => /^App-[^.]+\.js$/.test(file)),
|
|
raw: 900 * KIB,
|
|
gzip: 260 * KIB,
|
|
exactlyOne: !websiteTarget,
|
|
forbidden: websiteTarget,
|
|
},
|
|
{
|
|
name: 'MapLibre renderer chunks',
|
|
measure: await measure((file) => /^maplibre-[^.]+\.js$/.test(file)
|
|
&& !file.startsWith('maplibre-gl-worker-')),
|
|
raw: 820 * KIB,
|
|
gzip: 225 * KIB,
|
|
atLeastOne: true,
|
|
},
|
|
{
|
|
name: 'Recharts chunk',
|
|
measure: await measure((file) => /^recharts-[^.]+\.js$/.test(file)),
|
|
raw: 400 * KIB,
|
|
gzip: 110 * KIB,
|
|
exactlyOne: true,
|
|
},
|
|
{
|
|
name: 'all JavaScript',
|
|
measure: await measure((file) => file.endsWith('.js')),
|
|
raw: 2_900 * KIB,
|
|
gzip: 850 * KIB,
|
|
},
|
|
{
|
|
name: 'all CSS',
|
|
measure: await measure((file) => file.endsWith('.css')),
|
|
raw: 230 * KIB,
|
|
gzip: 42 * KIB,
|
|
},
|
|
];
|
|
|
|
let failed = false;
|
|
for (const budget of budgets) {
|
|
const rawKib = budget.measure.raw / KIB;
|
|
const gzipKib = budget.measure.gzip / KIB;
|
|
const missing = (budget.exactlyOne && budget.measure.files.length !== 1)
|
|
|| (budget.atLeastOne && budget.measure.files.length === 0)
|
|
|| (budget.forbidden && budget.measure.files.length > 0);
|
|
const exceeds = budget.measure.raw > budget.raw || budget.measure.gzip > budget.gzip;
|
|
const status = missing || exceeds ? 'FAIL' : 'PASS';
|
|
console.log(
|
|
`${status} ${budget.name}: ${rawKib.toFixed(1)} KiB raw / ${gzipKib.toFixed(1)} KiB gzip`
|
|
+ ` (limits ${(budget.raw / KIB).toFixed(0)} / ${(budget.gzip / KIB).toFixed(0)})`,
|
|
);
|
|
if (missing) {
|
|
console.error(`Expected ${budget.exactlyOne ? 'exactly one' : 'at least one'} matching asset, found: ${budget.measure.files.join(', ') || 'none'}`);
|
|
}
|
|
failed ||= missing || exceeds;
|
|
}
|
|
|
|
if (failed) process.exitCode = 1;
|