|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | + |
| 4 | +const projectRoot = process.cwd(); |
| 5 | +const nextDir = path.join(projectRoot, ".next"); |
| 6 | +const chunksDir = path.join(nextDir, "static", "chunks"); |
| 7 | +const buildManifestPath = path.join(nextDir, "build-manifest.json"); |
| 8 | + |
| 9 | +const totalBudgetKb = Number(process.env.TOTAL_JS_BUDGET_KB || 650); |
| 10 | +const chunkBudgetKb = Number(process.env.MAX_CHUNK_BUDGET_KB || 220); |
| 11 | + |
| 12 | +if (!fs.existsSync(chunksDir)) { |
| 13 | + console.error("Missing .next/static/chunks. Run `next build` first."); |
| 14 | + process.exit(1); |
| 15 | +} |
| 16 | + |
| 17 | +const allChunkFiles = []; |
| 18 | +const stack = [chunksDir]; |
| 19 | +while (stack.length > 0) { |
| 20 | + const current = stack.pop(); |
| 21 | + for (const item of fs.readdirSync(current, { withFileTypes: true })) { |
| 22 | + const fullPath = path.join(current, item.name); |
| 23 | + if (item.isDirectory()) { |
| 24 | + stack.push(fullPath); |
| 25 | + } else if (item.isFile() && item.name.endsWith(".js")) { |
| 26 | + allChunkFiles.push(fullPath); |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +const manifest = fs.existsSync(buildManifestPath) |
| 32 | + ? JSON.parse(fs.readFileSync(buildManifestPath, "utf8")) |
| 33 | + : null; |
| 34 | + |
| 35 | +const initialFiles = new Set(); |
| 36 | +if (manifest) { |
| 37 | + for (const file of [...(manifest.polyfillFiles || []), ...(manifest.rootMainFiles || [])]) { |
| 38 | + if (typeof file === "string" && file.endsWith(".js")) { |
| 39 | + initialFiles.add(file); |
| 40 | + } |
| 41 | + } |
| 42 | + for (const pageFiles of Object.values(manifest.pages || {})) { |
| 43 | + for (const file of pageFiles || []) { |
| 44 | + if (typeof file === "string" && file.endsWith(".js")) { |
| 45 | + initialFiles.add(file); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +let allBytes = 0; |
| 52 | +for (const filePath of allChunkFiles) { |
| 53 | + const size = fs.statSync(filePath).size; |
| 54 | + allBytes += size; |
| 55 | +} |
| 56 | + |
| 57 | +let initialBytes = 0; |
| 58 | +const offenders = []; |
| 59 | +for (const relativePath of initialFiles) { |
| 60 | + const filePath = path.join(nextDir, relativePath); |
| 61 | + if (!fs.existsSync(filePath)) continue; |
| 62 | + const size = fs.statSync(filePath).size; |
| 63 | + initialBytes += size; |
| 64 | + if (size > chunkBudgetKb * 1024) { |
| 65 | + offenders.push({ |
| 66 | + file: relativePath, |
| 67 | + kb: (size / 1024).toFixed(1), |
| 68 | + }); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +const totalKb = initialBytes / 1024; |
| 73 | +console.log(`Initial JS size (build manifest): ${totalKb.toFixed(1)} KB`); |
| 74 | +console.log(`All chunk JS size: ${(allBytes / 1024).toFixed(1)} KB`); |
| 75 | +console.log(`Budget: ${totalBudgetKb} KB`); |
| 76 | + |
| 77 | +let hasError = false; |
| 78 | +if (totalKb > totalBudgetKb) { |
| 79 | + console.error(`Total JS budget exceeded by ${(totalKb - totalBudgetKb).toFixed(1)} KB`); |
| 80 | + hasError = true; |
| 81 | +} |
| 82 | + |
| 83 | +if (offenders.length > 0) { |
| 84 | + console.error(`Chunks above ${chunkBudgetKb} KB:`); |
| 85 | + for (const item of offenders) { |
| 86 | + console.error(`- ${item.file}: ${item.kb} KB`); |
| 87 | + } |
| 88 | + hasError = true; |
| 89 | +} |
| 90 | + |
| 91 | +if (hasError) { |
| 92 | + process.exit(1); |
| 93 | +} |
| 94 | + |
| 95 | +console.log("Performance budgets passed."); |
0 commit comments