|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Post-build script to remove unnecessary files from Vercel serverless function |
| 4 | + * Reduces function size from 364MB to under 250MB limit |
| 5 | + */ |
| 6 | + |
| 7 | +import { rm } from "fs/promises" |
| 8 | +import { existsSync } from "fs" |
| 9 | +import { execSync } from "child_process" |
| 10 | + |
| 11 | +const FUNCTION_DIR = ".vercel/output/functions/_render.func" |
| 12 | + |
| 13 | +async function cleanup() { |
| 14 | + if (!existsSync(FUNCTION_DIR)) { |
| 15 | + console.error(`❌ Function directory not found: ${FUNCTION_DIR}`) |
| 16 | + process.exit(1) |
| 17 | + } |
| 18 | + |
| 19 | + console.log("🧹 Cleaning up serverless function bundle...") |
| 20 | + |
| 21 | + const sizeBefore = execSync(`du -sh ${FUNCTION_DIR}`, { encoding: "utf-8" }).trim() |
| 22 | + console.log(`📦 Size before: ${sizeBefore}`) |
| 23 | + |
| 24 | + // Remove large files/directories that are served statically by CDN |
| 25 | + const itemsToRemove = [ |
| 26 | + `${FUNCTION_DIR}/public/images`, |
| 27 | + `${FUNCTION_DIR}/public/search-index.json`, |
| 28 | + `${FUNCTION_DIR}/public/files`, |
| 29 | + `${FUNCTION_DIR}/public/default-og-image.png`, |
| 30 | + `${FUNCTION_DIR}/public/samples`, |
| 31 | + `${FUNCTION_DIR}/public/changelog.json`, |
| 32 | + ] |
| 33 | + |
| 34 | + for (const item of itemsToRemove) { |
| 35 | + if (existsSync(item)) { |
| 36 | + await rm(item, { recursive: true, force: true }) |
| 37 | + console.log(` ✓ Removed: ${item.replace(FUNCTION_DIR + "/", "")}`) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + const sizeAfter = execSync(`du -sh ${FUNCTION_DIR}`, { encoding: "utf-8" }).trim() |
| 42 | + console.log(`📦 Size after: ${sizeAfter}`) |
| 43 | + console.log("✨ Cleanup complete!") |
| 44 | +} |
| 45 | + |
| 46 | +cleanup().catch((error) => { |
| 47 | + console.error("❌ Cleanup failed:", error) |
| 48 | + process.exit(1) |
| 49 | +}) |
0 commit comments