import type { BundleTarget } from "./bundle-size-scenarios.ts"; export interface BundleSize { target: BundleTarget; minified: number; gzip: number; } export interface BundleFailure { target: BundleTarget; error: string; } export interface SizeSnapshot { bundles: Record; failures: Record; } const WARN_THRESHOLD_PERCENT = 5; const TARGET_ORDER: Record = { server: 0, client: 1 }; const TABLE_HEADER = `| Status | Target | Scenario | Minified | Gzip | ฮ” Minified | ฮ” Gzip | |:------:|:-------|:---------|---------:|-----:|-----------:|-------:|`; const byteFormat = new Intl.NumberFormat("en", { maximumFractionDigits: 2 }); export const isRecord = (value: unknown): value is Record => typeof value === "object" && value !== null && !Array.isArray(value); const isTarget = (value: unknown): value is BundleTarget => value === "server" || value === "client"; function parseEntries(value: unknown, parse: (entry: Record) => T | null) { const result: Record = {}; if (!isRecord(value)) return result; for (const [name, entry] of Object.entries(value)) { const parsed = isRecord(entry) ? parse(entry) : null; if (parsed === null) throw new Error(`Malformed snapshot entry "${name}"`); result[name] = parsed; } return result; } export function parseSnapshot(value: unknown): SizeSnapshot { if (!isRecord(value) || !isRecord(value.bundles)) throw new Error("Snapshot has no bundles"); return { bundles: parseEntries(value.bundles, ({ target, minified, gzip }) => isTarget(target) && typeof minified === "number" && typeof gzip === "number" ? { target, minified, gzip } : null, ), failures: parseEntries(value.failures, ({ target, error }) => isTarget(target) && typeof error === "string" ? { target, error } : null, ), }; } export function formatBytes(bytes: number): string { if (Math.abs(bytes) < 1000) return `${bytes} B`; return `${byteFormat.format(bytes / 1000)} kB`; } function formatDelta(current: number, baseline: number | undefined): string { if (baseline === undefined) return "โ€”"; const delta = current - baseline; if (delta === 0) return "ยฑ0"; const sign = delta > 0 ? "+" : ""; const percent = baseline === 0 ? "" : ` (${sign}${((delta / baseline) * 100).toFixed(1)}%)`; return `${sign}${formatBytes(delta)}${percent}`; } function percentChange(current: number, baseline: number): number { return baseline === 0 ? 0 : ((current - baseline) / baseline) * 100; } function status(current: BundleSize, baseline: BundleSize | undefined): string { if (!baseline) return "๐Ÿ†•"; // Gzip is what ships; fall back to minified so a change never shows as โšช. const gzipPercent = percentChange(current.gzip, baseline.gzip); const percent = gzipPercent === 0 ? percentChange(current.minified, baseline.minified) : gzipPercent; if (percent >= WARN_THRESHOLD_PERCENT) return "๐Ÿ”ด"; if (percent > 0) return "๐ŸŸก"; if (percent < 0) return "๐ŸŸข"; return "โšช"; } const tableRow = (cells: string[]) => `| ${cells.join(" | ")} |`; function sizeRow(name: string, current: BundleSize, baseline: BundleSize | undefined): string { return tableRow([ status(current, baseline), current.target, name, formatBytes(current.minified), formatBytes(current.gzip), formatDelta(current.minified, baseline?.minified), formatDelta(current.gzip, baseline?.gzip), ]); } function failureRow(name: string, failure: BundleFailure, baseline: BundleSize | undefined) { const baseCell = baseline ? formatBytes(baseline.gzip) : "โ€”"; return tableRow(["โŒ", failure.target, name, "failed", "failed", "โ€”", `was ${baseCell}`]); } function sortByTarget(entries: [string, T][]) { return entries.toSorted( ([nameA, a], [nameB, b]) => TARGET_ORDER[a.target] - TARGET_ORDER[b.target] || nameA.localeCompare(nameB), ); } export function generateReport(head: SizeSnapshot, base: SizeSnapshot | null): string { const changed: string[] = []; const unchanged: string[] = []; for (const [name, current] of sortByTarget(Object.entries(head.bundles))) { const baseline = base?.bundles[name]; const isUnchanged = baseline?.minified === current.minified && baseline.gzip === current.gzip; (isUnchanged ? unchanged : changed).push(sizeRow(name, current, baseline)); } const failures = sortByTarget(Object.entries(head.failures)); changed.unshift( ...failures.map(([name, failure]) => failureRow(name, failure, base?.bundles[name])), ); // The first line identifies the sticky PR comment; keep it first. const parts = ["", "## ๐Ÿ“ฆ Bundle size report\n"]; parts.push( changed.length > 0 ? `${TABLE_HEADER}\n${changed.join("\n")}\n` : "_No bundle size changes._\n", ); if (failures.length > 0) { parts.push( "Some scenarios failed to bundle on this PR. Fix the export or update `scripts/bundle-size-scenarios.ts`:\n", ...failures.map( ([name, failure]) => `- **${name}**: \`${failure.error.replaceAll("`", "'")}\``, ), "", ); } if (unchanged.length > 0) { parts.push( `
\nUnchanged (${unchanged.length})\n\n${TABLE_HEADER}\n${unchanged.join("\n")}\n\n
\n`, ); } if (!base) { parts.push("> The base commit could not be measured, so there are no deltas.\n"); } parts.push(`
How this is measured Each scenario is bundled from the built package with Rolldown (tree-shaken, minified, dynamic imports inlined), then gzipped. Peer dependencies are external. Scenarios live in \`scripts/bundle-size-scenarios.ts\`. ๐ŸŸข smaller ยท ๐ŸŸก larger (< ${WARN_THRESHOLD_PERCENT}%) ยท ๐Ÿ”ด larger (โ‰ฅ ${WARN_THRESHOLD_PERCENT}%) ยท ๐Ÿ†• not in base ยท โŒ failed to bundle ยท โšช unchanged
`); return parts.join("\n"); }