|
| 1 | +#!/usr/bin/env tsx |
| 2 | +// Bundles the built package plus its runtime dependencies through Vite/Rollup and |
| 3 | +// checks the gzipped size against bundle-size.budget.json to catch regressions (#389). |
| 4 | +// Also emits a dependency treemap at bundle-size/stats.html. |
| 5 | +// Run `yarn size` to check, or `yarn size --update` to rewrite the budget. |
| 6 | + |
| 7 | +import { build } from 'vite'; |
| 8 | +import { visualizer } from 'rollup-plugin-visualizer'; |
| 9 | +import { gzipSync } from 'node:zlib'; |
| 10 | +import { readFileSync, writeFileSync, mkdirSync, rmSync } from 'node:fs'; |
| 11 | +import { fileURLToPath } from 'node:url'; |
| 12 | +import { dirname, resolve, join } from 'node:path'; |
| 13 | + |
| 14 | +interface SizeResult { |
| 15 | + minified: number; |
| 16 | + gzip: number; |
| 17 | +} |
| 18 | + |
| 19 | +interface Budget { |
| 20 | + tolerance?: number; |
| 21 | + scenarios?: Record<string, { maxGzip: number }>; |
| 22 | +} |
| 23 | + |
| 24 | +const scriptDir = dirname(fileURLToPath(import.meta.url)); |
| 25 | +const pkgDir = resolve(scriptDir, '..'); |
| 26 | +const workDir = join(pkgDir, 'bundle-size'); |
| 27 | +const budgetFile = join(pkgDir, 'bundle-size.budget.json'); |
| 28 | + |
| 29 | +// `full` references the whole namespace so nothing tree-shakes (like bundlephobia). |
| 30 | +// `typical` is a realistic app that builds and sends a transaction. |
| 31 | +const scenarios: Record<string, string> = { |
| 32 | + full: `import * as cashscript from '../dist/index.js'; |
| 33 | +globalThis.__keepAlive = cashscript;`, |
| 34 | + typical: `import { Contract, TransactionBuilder, ElectrumNetworkProvider, SignatureTemplate } from '../dist/index.js'; |
| 35 | +console.log(Contract, TransactionBuilder, ElectrumNetworkProvider, SignatureTemplate);`, |
| 36 | +}; |
| 37 | + |
| 38 | +// Build in app mode (not lib mode) so Vite bundles every dependency instead of |
| 39 | +// externalizing them. |
| 40 | +async function measure(name: string, contents: string): Promise<SizeResult> { |
| 41 | + const entryFile = join(workDir, `entry-${name}.js`); |
| 42 | + const outDir = join(workDir, `out-${name}`); |
| 43 | + writeFileSync(entryFile, contents); |
| 44 | + |
| 45 | + await build({ |
| 46 | + configFile: false, |
| 47 | + logLevel: 'silent', |
| 48 | + root: workDir, |
| 49 | + build: { |
| 50 | + outDir, |
| 51 | + emptyOutDir: true, |
| 52 | + target: 'esnext', // libauth instantiates its wasm crypto via top-level await |
| 53 | + minify: 'esbuild', |
| 54 | + modulePreload: false, |
| 55 | + reportCompressedSize: false, |
| 56 | + rollupOptions: { |
| 57 | + input: entryFile, |
| 58 | + output: { entryFileNames: 'bundle.js', format: 'es' }, |
| 59 | + plugins: name === 'full' |
| 60 | + ? [visualizer({ filename: join(workDir, 'stats.html'), gzipSize: true, brotliSize: true })] |
| 61 | + : [], |
| 62 | + }, |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + const code = readFileSync(join(outDir, 'bundle.js')); |
| 67 | + return { minified: code.length, gzip: gzipSync(code, { level: 9 }).length }; |
| 68 | +} |
| 69 | + |
| 70 | +const kb = (bytes: number): string => `${(bytes / 1024).toFixed(1)} kB`; |
| 71 | + |
| 72 | +mkdirSync(workDir, { recursive: true }); |
| 73 | + |
| 74 | +const update = process.argv.includes('--update'); |
| 75 | +const budget: Budget = update ? {} : JSON.parse(readFileSync(budgetFile, 'utf8')); |
| 76 | + |
| 77 | +const measured: Record<string, SizeResult> = {}; |
| 78 | +for (const [name, contents] of Object.entries(scenarios)) { |
| 79 | + measured[name] = await measure(name, contents); |
| 80 | +} |
| 81 | + |
| 82 | +// Clean up the transient build outputs, keeping the treemap. |
| 83 | +for (const name of Object.keys(scenarios)) { |
| 84 | + rmSync(join(workDir, `out-${name}`), { recursive: true, force: true }); |
| 85 | + rmSync(join(workDir, `entry-${name}.js`), { force: true }); |
| 86 | +} |
| 87 | + |
| 88 | +if (update) { |
| 89 | + const next = { |
| 90 | + _comment: 'Gzipped-byte budgets for the cashscript consumer bundle. Regenerate with `yarn size --update`. See issue #389.', |
| 91 | + tolerance: 0.05, |
| 92 | + scenarios: Object.fromEntries( |
| 93 | + Object.entries(measured).map(([name, m]) => [name, { maxGzip: m.gzip }]), |
| 94 | + ), |
| 95 | + }; |
| 96 | + writeFileSync(budgetFile, `${JSON.stringify(next, null, 2)}\n`); |
| 97 | + console.log(`Updated ${budgetFile}`); |
| 98 | + for (const [name, m] of Object.entries(measured)) { |
| 99 | + console.log(` ${name.padEnd(8)} ${kb(m.gzip)} gzip (${kb(m.minified)} min)`); |
| 100 | + } |
| 101 | + process.exit(0); |
| 102 | +} |
| 103 | + |
| 104 | +const tolerance = budget.tolerance ?? 0.05; |
| 105 | +let failed = false; |
| 106 | + |
| 107 | +console.log(`Bundle size check (budget tolerance +${(tolerance * 100).toFixed(0)}%)\n`); |
| 108 | +console.log('scenario gzip min budget status'); |
| 109 | +for (const [name, m] of Object.entries(measured)) { |
| 110 | + const max = budget.scenarios?.[name]?.maxGzip; |
| 111 | + if (max == null) { |
| 112 | + console.log(`${name.padEnd(10)} ${kb(m.gzip).padEnd(11)} ${kb(m.minified).padEnd(11)} (no budget) MISSING`); |
| 113 | + failed = true; |
| 114 | + continue; |
| 115 | + } |
| 116 | + const limit = max * (1 + tolerance); |
| 117 | + const ok = m.gzip <= limit; |
| 118 | + failed = failed || !ok; |
| 119 | + console.log(`${name.padEnd(10)} ${kb(m.gzip).padEnd(11)} ${kb(m.minified).padEnd(11)} ${kb(max).padEnd(11)} ${ok ? 'ok' : 'OVER BUDGET'}`); |
| 120 | +} |
| 121 | + |
| 122 | +if (failed) { |
| 123 | + console.error('\nBundle size exceeded budget. If this is intentional, run `yarn size --update` and commit the new budget.'); |
| 124 | + process.exit(1); |
| 125 | +} |
| 126 | +console.log('\nAll scenarios within budget.'); |
0 commit comments