mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-16 14:27:26 +00:00
The studio shipped one eager entry chunk of 1,126,473 B gzip. CodeMirror and the markdown renderer were the two largest blocks on that path and neither is needed to paint the shell. - React.lazy the source editor (@codemirror/*, @lezer/*) and the storyboard source view (marked, dompurify), copying the deferred-dependency shape of the mediabunny dynamic import in player/lib/mediaProbe.ts. - Add LazyPanel: one Suspense + error boundary whose fallback fills the panel's slot (h-full w-full flex-1) so opening the editor cannot shift the layout, and whose rejected-chunk state renders a visible retry instead of a blank panel. - manualChunks splits the React runtime and the remaining vendor code out of the app chunk so app-only deploys stop invalidating them for repeat visitors. - tests/e2e/studio-bundle-budget.mjs gates the built artifact with no browser. It measures the entry module plus its modulepreloaded chunks, not the entry file alone: the entry file alone gzips to well under half the eager total and would report a false pass while the browser still downloads all of it. The gate enforces a ratchet just above what we ship today and reports the gap to the 600 KiB target every run. Closing that gap needs the Node-only AST stack (@babel/parser, esprima, acorn, recast, ast-types) out of the browser bundle, where it arrives through @hyperframes/sdk — outside this unit.
87 lines
3.4 KiB
TypeScript
87 lines
3.4 KiB
TypeScript
import { randomBytes } from "node:crypto";
|
|
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
// @ts-expect-error -- plain .mjs gate script, no type declarations
|
|
import {
|
|
checkEagerBundleBudget,
|
|
EAGER_ENTRY_CHUNK_GZIP_RATCHET_BYTES,
|
|
findEagerAssets,
|
|
} from "./studio-bundle-budget.mjs";
|
|
|
|
const ENTRY_TAG = '<script type="module" crossorigin src="/assets/index.js"></script>';
|
|
const PRELOAD_TAG = '<link rel="modulepreload" crossorigin href="/assets/vendor.js">';
|
|
const STYLE_TAG = '<link rel="stylesheet" href="/assets/index.css">';
|
|
const BUDGET: number = EAGER_ENTRY_CHUNK_GZIP_RATCHET_BYTES;
|
|
|
|
const temporaryDirectories: string[] = [];
|
|
|
|
afterEach(() => {
|
|
for (const directory of temporaryDirectories.splice(0)) {
|
|
rmSync(directory, { force: true, recursive: true });
|
|
}
|
|
});
|
|
|
|
/** Incompressible bytes, so each fixture's gzip size tracks its declared size. */
|
|
function fixtureDist(entryBytes: number, preloadBytes?: number): string {
|
|
const distDir = mkdtempSync(join(tmpdir(), "studio-bundle-budget-"));
|
|
temporaryDirectories.push(distDir);
|
|
mkdirSync(join(distDir, "assets"));
|
|
const preload = preloadBytes == null ? "" : PRELOAD_TAG;
|
|
writeFileSync(join(distDir, "index.html"), `${ENTRY_TAG}${preload}${STYLE_TAG}`);
|
|
writeFileSync(join(distDir, "assets/index.js"), randomBytes(entryBytes));
|
|
if (preloadBytes != null) {
|
|
writeFileSync(join(distDir, "assets/vendor.js"), randomBytes(preloadBytes));
|
|
}
|
|
return distDir;
|
|
}
|
|
|
|
describe("studio bundle budget", () => {
|
|
it("reads exactly one eagerly-loaded entry module plus its modulepreloads", () => {
|
|
const { entries, preloads } = findEagerAssets(`${ENTRY_TAG}${PRELOAD_TAG}${STYLE_TAG}`);
|
|
expect(entries).toEqual(["/assets/index.js"]);
|
|
// The stylesheet link must not be mistaken for a preloaded module.
|
|
expect(preloads).toEqual(["/assets/vendor.js"]);
|
|
});
|
|
|
|
it("fails when index.html has more than one eager entry module", () => {
|
|
const distDir = fixtureDist(64);
|
|
writeFileSync(join(distDir, "index.html"), `${ENTRY_TAG}${ENTRY_TAG}`);
|
|
|
|
const { passed, report } = checkEagerBundleBudget(distDir, BUDGET);
|
|
|
|
expect(passed).toBe(false);
|
|
expect(report).toContain("FAIL eagerEntryModuleCount measured=2 budget=1");
|
|
});
|
|
|
|
it("fails, naming the measured and budgeted values, on an over-budget artifact", () => {
|
|
const { passed, report, gzipBytes } = checkEagerBundleBudget(
|
|
fixtureDist(BUDGET + 64 * 1024),
|
|
BUDGET,
|
|
);
|
|
|
|
expect(passed).toBe(false);
|
|
expect(report).toContain(
|
|
`FAIL eagerEntryChunkGzipBytes measured=${gzipBytes}B budget=${BUDGET}B`,
|
|
);
|
|
});
|
|
|
|
it("counts modulepreloaded chunks, so a manualChunks split cannot game the budget", () => {
|
|
const total = BUDGET + 64 * 1024;
|
|
const whole = checkEagerBundleBudget(fixtureDist(total), BUDGET);
|
|
const split = checkEagerBundleBudget(fixtureDist(total / 2, total / 2), BUDGET);
|
|
|
|
// Same eager payload, two chunks instead of one — the verdict must not change.
|
|
expect(split.gzipBytes).toBeGreaterThan(whole.gzipBytes * 0.99);
|
|
expect(split.passed).toBe(false);
|
|
});
|
|
|
|
it("passes when the whole eager graph fits the budget", () => {
|
|
const { passed, report } = checkEagerBundleBudget(fixtureDist(1_024, 1_024), 5_000_000);
|
|
|
|
expect(passed).toBe(true);
|
|
expect(report).toContain("PASS eagerEntryChunkGzipBytes");
|
|
});
|
|
});
|