65 lines
2.0 KiB
JavaScript
65 lines
2.0 KiB
JavaScript
import { readFileSync, readdirSync, existsSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
import { gzipSync } from 'node:zlib'
|
|
|
|
const inputDir = process.argv[2]
|
|
const candidates = [
|
|
inputDir,
|
|
'dist-local/assets',
|
|
'dist/assets',
|
|
].filter(Boolean)
|
|
|
|
const assetsDir = candidates.find((dir) => existsSync(dir))
|
|
if (!assetsDir) {
|
|
console.error('[bundle-budget] No assets directory found. Tried:', candidates.join(', '))
|
|
process.exit(1)
|
|
}
|
|
|
|
const files = readdirSync(assetsDir)
|
|
const jsFiles = files.filter((name) => name.endsWith('.js'))
|
|
|
|
if (jsFiles.length === 0) {
|
|
console.error(`[bundle-budget] No JS assets found in ${assetsDir}`)
|
|
process.exit(1)
|
|
}
|
|
|
|
const maxSingleGzip = Number(process.env.BUNDLE_MAX_SINGLE_GZIP_BYTES || 180 * 1024)
|
|
// Budget raised from 520 KB → 720 KB (2026-04-13) to accommodate Three.js
|
|
// (~132 KB gzip) added as a lazy-loaded chunk for the Spirit visualizer.
|
|
const maxTotalGzip = Number(process.env.BUNDLE_MAX_TOTAL_GZIP_BYTES || 720 * 1024)
|
|
|
|
const stats = jsFiles.map((name) => {
|
|
const abs = join(assetsDir, name)
|
|
const raw = readFileSync(abs)
|
|
return {
|
|
name,
|
|
rawBytes: raw.length,
|
|
gzipBytes: gzipSync(raw).length,
|
|
}
|
|
})
|
|
|
|
stats.sort((a, b) => b.gzipBytes - a.gzipBytes)
|
|
|
|
const largest = stats[0]
|
|
const totalGzip = stats.reduce((sum, s) => sum + s.gzipBytes, 0)
|
|
|
|
console.log(`[bundle-budget] assets dir: ${assetsDir}`)
|
|
console.log(`[bundle-budget] largest js gzip: ${largest.name} (${largest.gzipBytes} bytes)`)
|
|
console.log(`[bundle-budget] total js gzip: ${totalGzip} bytes across ${stats.length} files`)
|
|
|
|
const overLargest = largest.gzipBytes > maxSingleGzip
|
|
const overTotal = totalGzip > maxTotalGzip
|
|
|
|
if (overLargest || overTotal) {
|
|
console.error('[bundle-budget] FAILED')
|
|
if (overLargest) {
|
|
console.error(` largest js gzip ${largest.gzipBytes} > limit ${maxSingleGzip}`)
|
|
}
|
|
if (overTotal) {
|
|
console.error(` total js gzip ${totalGzip} > limit ${maxTotalGzip}`)
|
|
}
|
|
process.exit(1)
|
|
}
|
|
|
|
console.log('[bundle-budget] PASS')
|