From b2fc2e61078e49fae8c7a349ffdd1e99bb6c2128 Mon Sep 17 00:00:00 2001 From: Jaiyeola Akinjide Date: Fri, 24 Jul 2026 00:31:47 +0100 Subject: [PATCH] CI: bundle size budget guard --- apps/web/package.json | 2 +- apps/web/scripts/check-bundle-size.ts | 46 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 apps/web/scripts/check-bundle-size.ts diff --git a/apps/web/package.json b/apps/web/package.json index c5d41d5..608bc35 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -5,7 +5,7 @@ "private": true, "scripts": { "dev": "vite dev --port 3000", - "build": "vite build", + "build": "vite build && bun run scripts/check-bundle-size.ts", "preview": "vite preview", "lint": "eslint", "format": "prettier --write \"**/*.{ts,tsx}\"", diff --git a/apps/web/scripts/check-bundle-size.ts b/apps/web/scripts/check-bundle-size.ts new file mode 100644 index 0000000..ad638bd --- /dev/null +++ b/apps/web/scripts/check-bundle-size.ts @@ -0,0 +1,46 @@ +import { readdirSync, statSync } from "node:fs"; +import { join } from "node:path"; + +// Budget in bytes (e.g., 500 KB per chunk) +// If you need to update the budget, increase this value deliberately +// to ensure you are aware of the bundle size increase. +const MAX_CHUNK_SIZE_BYTES = 500 * 1024; // 500 KB + +const ASSETS_DIR = join(process.cwd(), ".output", "public", "assets"); + +function checkBundleSize() { + console.log(`Checking bundle sizes against budget of ${MAX_CHUNK_SIZE_BYTES / 1024} KB...`); + + let hasError = false; + let files; + + try { + files = readdirSync(ASSETS_DIR); + } catch (err) { + console.error(`Failed to read directory ${ASSETS_DIR}. Did the build succeed?`); + process.exit(1); + } + + const jsFiles = files.filter(f => f.endsWith(".js")); + + for (const file of jsFiles) { + const filePath = join(ASSETS_DIR, file); + const stats = statSync(filePath); + + if (stats.size > MAX_CHUNK_SIZE_BYTES) { + console.error(`❌ ERROR: Chunk ${file} exceeds the budget! Size: ${(stats.size / 1024).toFixed(2)} KB, Budget: ${MAX_CHUNK_SIZE_BYTES / 1024} KB`); + console.error(`If this increase is expected, update MAX_CHUNK_SIZE_BYTES in scripts/check-bundle-size.ts.`); + hasError = true; + } else { + console.log(`✅ ${file} is within budget (${(stats.size / 1024).toFixed(2)} KB)`); + } + } + + if (hasError) { + process.exit(1); + } + + console.log("Bundle size check passed."); +} + +checkBundleSize();