문제: 초기 JS가 예산을 넘으면 check:bundle은 "Bundle budget or manifest integrity failed: " 한 줄만 쓰고 콜론 뒤가 비어 있었다. 측정값과 한도는 artifacts/performance/bundle.json에만 있어 예산 실패인지 manifest 무결성 실패인지, 몇 바이트 넘었는지 알 수 없었다. 또 초기 JS 파일 수에는 상한이 없어 08-08처럼 파일을 16개에서 25개로 늘리는 변경도 모든 게이트를 통과했다. 템플릿은 HTTP/2를 전제로 초기 청크를 나눠 두었는데, 파일 수를 늘리는 결정이 어디에도 드러나지 않았다. 고른 방법: 예산 실패와 무결성 실패를 다른 문장으로 쓴다. 예산 실패는 넘은 한도마다 측정값·한도·초과량을 한 줄로 쓴다 (describeBundleBudgetFailures). config/performance/budgets.json에 initialJsFiles(지금 25)를 두고 check:bundle이 entry를 포함한 초기 파일 수를 센다. bundle.json 스키마는 측정값·한도·결과와 자가 검사 fixture를 하나씩 더 가진다. 두 변경은 같은 함수와 스키마를 고치므로 한 커밋에 넣었다. 버린 대안: 파일 수를 문서에만 적기. 08-08이 그 상태에서 파일을 늘렸다. 위험·한계: 초기 파일을 하나라도 늘리는 변경은 이제 budgets.json을 같이 고쳐야 통과한다. 그것이 목적이다. HTTP/2 확인 자체는 이 커밋에 없다 (13-02에서 probe에 넣는다). 검증: tests/unit/check-bundle-output.test.ts가 손으로 만든 빌드 출력에 check-bundle.ts를 돌려 통과·초기 바이트 초과·lazy 초과·파일 수 초과· 무결성 실패의 출력을 본다(고치기 전 4건 실패). bundle-artifact-contract, performance-budgets 통과. 실제 빌드에서 check:bundle PASS(25 / 25 files). Refs: docs/reviews/2026-11-03-maturity-review/15-performance.md 15-03, 15-05
149 lines
4.2 KiB
TypeScript
149 lines
4.2 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
|
|
import {
|
|
describeBundleBudgetFailures,
|
|
evaluateBundleBudget,
|
|
} from "./lib/performance-budgets.ts";
|
|
import {
|
|
bundleOutputInventoryArtifactSchema,
|
|
bundlePerformanceArtifactSchema,
|
|
} from "./contracts/release-artifacts.ts";
|
|
import { classifyViteJavascript } from "./lib/classify-vite-bundle.ts";
|
|
import { writeValidatedJsonArtifact } from "./lib/validated-json-artifact.ts";
|
|
|
|
type ViteManifest = Record<
|
|
string,
|
|
{ file: string; isEntry?: boolean; imports?: string[] }
|
|
>;
|
|
type BundleBudgets = {
|
|
initialJsGzipBytes: number;
|
|
initialJsFiles: number;
|
|
lazyChunkGzipBytes: number;
|
|
};
|
|
|
|
const report = bundleOutputInventoryArtifactSchema.parse(
|
|
JSON.parse(await readFile("artifacts/performance/bundle.json", "utf8")) as unknown,
|
|
);
|
|
const viteManifest = JSON.parse(
|
|
await readFile("dist/.vite/manifest.json", "utf8"),
|
|
) as ViteManifest;
|
|
const budgets = JSON.parse(
|
|
await readFile("config/performance/budgets.json", "utf8"),
|
|
).bundle as BundleBudgets;
|
|
|
|
const outputByPath = new Map(
|
|
report.outputs.map((output) => [output.path.replace(/^dist\//, ""), output]),
|
|
);
|
|
const classification = classifyViteJavascript(viteManifest);
|
|
const initialJsGzipBytes = classification.initialFiles.reduce(
|
|
(total, file) => total + (outputByPath.get(file)?.gzipBytes ?? 0),
|
|
0,
|
|
);
|
|
const lazyChunks = classification.lazyFiles.map((file) => ({
|
|
path: file,
|
|
gzipBytes: outputByPath.get(file)?.gzipBytes ?? 0,
|
|
}));
|
|
const missingOutputs = [
|
|
...classification.initialFiles,
|
|
...classification.lazyFiles,
|
|
].filter((file) => !outputByPath.has(file));
|
|
const measurements = {
|
|
initialJsGzipBytes,
|
|
initialJsFiles: classification.initialFiles.length,
|
|
lazyChunks,
|
|
};
|
|
const result = evaluateBundleBudget(measurements, budgets);
|
|
const fixtures = [
|
|
{
|
|
name: "initial-js-over-budget",
|
|
passed:
|
|
!evaluateBundleBudget(
|
|
{
|
|
initialJsGzipBytes: budgets.initialJsGzipBytes + 1,
|
|
initialJsFiles: 0,
|
|
lazyChunks: [],
|
|
},
|
|
budgets,
|
|
).passed,
|
|
},
|
|
{
|
|
name: "lazy-chunk-over-budget",
|
|
passed:
|
|
!evaluateBundleBudget(
|
|
{
|
|
initialJsGzipBytes: 0,
|
|
initialJsFiles: 0,
|
|
lazyChunks: [
|
|
{
|
|
path: "fixture.js",
|
|
gzipBytes: budgets.lazyChunkGzipBytes + 1,
|
|
},
|
|
],
|
|
},
|
|
budgets,
|
|
).passed,
|
|
},
|
|
{
|
|
name: "initial-files-over-budget",
|
|
passed:
|
|
!evaluateBundleBudget(
|
|
{
|
|
initialJsGzipBytes: 0,
|
|
initialJsFiles: budgets.initialJsFiles + 1,
|
|
lazyChunks: [],
|
|
},
|
|
budgets,
|
|
).passed,
|
|
},
|
|
];
|
|
const passed =
|
|
result.passed &&
|
|
fixtures.every((fixture) => fixture.passed) &&
|
|
classification.missingImports.length === 0 &&
|
|
missingOutputs.length === 0;
|
|
const completedReport = {
|
|
...report,
|
|
measurements,
|
|
classification,
|
|
missingOutputs,
|
|
thresholds: budgets,
|
|
results: result,
|
|
fixtures,
|
|
passed,
|
|
};
|
|
|
|
await writeValidatedJsonArtifact({
|
|
path: "artifacts/performance/bundle.json",
|
|
schema: bundlePerformanceArtifactSchema,
|
|
value: completedReport,
|
|
});
|
|
// 1103/15-03. The budget and the manifest integrity fail for different
|
|
// reasons, so each says what failed and by how much.
|
|
if (!passed) {
|
|
const budgetFailures = describeBundleBudgetFailures(measurements, budgets);
|
|
if (budgetFailures.length > 0) {
|
|
process.stderr.write(
|
|
`Bundle budget failed (config/performance/budgets.json):\n${budgetFailures
|
|
.map((line) => ` ${line}\n`)
|
|
.join("")}`,
|
|
);
|
|
}
|
|
const integrityFailures = [
|
|
...classification.missingImports.map((key) => `manifest import without an entry: ${key}`),
|
|
...missingOutputs.map((file) => `output missing from the inventory: ${file}`),
|
|
...fixtures
|
|
.filter((fixture) => !fixture.passed)
|
|
.map((fixture) => `budget self-check did not fail: ${fixture.name}`),
|
|
];
|
|
if (integrityFailures.length > 0) {
|
|
process.stderr.write(
|
|
`Bundle manifest integrity failed:\n${integrityFailures.map((line) => ` ${line}\n`).join("")}`,
|
|
);
|
|
}
|
|
process.exit(1);
|
|
}
|
|
process.stdout.write(
|
|
`Bundle budget: PASS (initial JS ${initialJsGzipBytes} / ${budgets.initialJsGzipBytes} gzip bytes, ` +
|
|
`${measurements.initialJsFiles} / ${budgets.initialJsFiles} files)\n`,
|
|
);
|