chore: simplify frontend verification
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { statSync } from "node:fs";
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
|
||||
@@ -64,41 +64,8 @@ function assertBundleBudget(initialBytes) {
|
||||
}
|
||||
}
|
||||
|
||||
function getLazyRouteAssetSizes(manifest, getSize, baseline) {
|
||||
const routeEntries = manifestEntries(manifest).filter(
|
||||
([, entry]) =>
|
||||
entry?.isDynamicEntry === true && typeof entry.src === "string",
|
||||
);
|
||||
if (routeEntries.length === 0) {
|
||||
throw new Error("Vite manifest must contain lazy route chunks");
|
||||
}
|
||||
|
||||
const routeSizes = Object.fromEntries(
|
||||
routeEntries
|
||||
.map(([entryName, entry]) => [
|
||||
entry.src,
|
||||
getSize(assetFile(entry, entryName)),
|
||||
])
|
||||
.sort(([left], [right]) => left.localeCompare(right)),
|
||||
);
|
||||
if (!baseline) return routeSizes;
|
||||
|
||||
for (const [route, size] of Object.entries(routeSizes)) {
|
||||
if (!Number.isSafeInteger(baseline[route])) {
|
||||
throw new Error(`Missing baseline for lazy route chunk '${route}'`);
|
||||
}
|
||||
if (size > baseline[route]) {
|
||||
throw new Error(
|
||||
`Lazy route chunk '${route}' is ${size} bytes; baseline is ${baseline[route]} bytes`,
|
||||
);
|
||||
}
|
||||
}
|
||||
return routeSizes;
|
||||
}
|
||||
|
||||
const distributionDirectory = resolve("dist");
|
||||
const manifestPath = resolve(distributionDirectory, "bundle-manifest.json");
|
||||
const baselinePath = resolve("scripts/lazy-route-bundle-baseline.json");
|
||||
|
||||
async function loadJson(path) {
|
||||
return JSON.parse(await readFile(path, "utf8"));
|
||||
@@ -108,27 +75,13 @@ function getAssetSize(file) {
|
||||
return statSync(resolve(distributionDirectory, file)).size;
|
||||
}
|
||||
|
||||
async function run(mode) {
|
||||
async function run() {
|
||||
const manifest = await loadJson(manifestPath);
|
||||
const initialBytes = measureAssets(
|
||||
getInitialAssetFiles(manifest),
|
||||
getAssetSize,
|
||||
);
|
||||
assertBundleBudget(initialBytes);
|
||||
const baseline =
|
||||
mode === "baseline" ? undefined : await loadJson(baselinePath);
|
||||
const lazyRouteSizes = getLazyRouteAssetSizes(
|
||||
manifest,
|
||||
getAssetSize,
|
||||
baseline,
|
||||
);
|
||||
|
||||
if (mode === "baseline") {
|
||||
await writeFile(
|
||||
baselinePath,
|
||||
`${JSON.stringify(lazyRouteSizes, null, "\t")}\n`,
|
||||
);
|
||||
}
|
||||
console.log(`Initial JavaScript payload: ${initialBytes} bytes`);
|
||||
}
|
||||
|
||||
@@ -136,17 +89,12 @@ if (
|
||||
process.argv[1] &&
|
||||
import.meta.url === pathToFileURL(process.argv[1]).href
|
||||
) {
|
||||
const mode = process.argv[2] ?? "check";
|
||||
if (mode !== "check" && mode !== "baseline") {
|
||||
throw new Error("Usage: node scripts/bundle-budget.mjs [check|baseline]");
|
||||
}
|
||||
await run(mode);
|
||||
await run();
|
||||
}
|
||||
|
||||
export {
|
||||
assertBundleBudget,
|
||||
getInitialAssetFiles,
|
||||
getLazyRouteAssetSizes,
|
||||
INITIAL_BUNDLE_BUDGET_BYTES,
|
||||
measureAssets,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user