|
| 1 | +import { Buffer } from 'node:buffer' |
| 2 | +import { mkdtemp, readdir, readFile, rm, writeFile } from 'node:fs/promises' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { fileURLToPath } from 'node:url' |
| 6 | +import { gzipSync } from 'node:zlib' |
| 7 | +import { build } from 'tsdown' |
| 8 | +import buildConfig from '../tsdown.config.ts' |
| 9 | + |
| 10 | +const root = fileURLToPath(new URL('../', import.meta.url)) |
| 11 | +const readmePath = join(root, 'README.md') |
| 12 | +const readme = await readFile(readmePath, 'utf8') |
| 13 | +const section = /^- ✨ Minimal footprint\r?\n(?: {2}[^\r\n]*\r?\n)*/m |
| 14 | +if (!section.test(readme)) { |
| 15 | + throw new Error('Cannot find the Minimal footprint section in README.md') |
| 16 | +} |
| 17 | + |
| 18 | +const outDir = await mkdtemp(join(tmpdir(), 'obug-bundle-size-')) |
| 19 | +const sizes: { name: string; bytes: number; gzipBytes: number }[] = [] |
| 20 | +try { |
| 21 | + for (const name of ['plain', 'browser', 'ansi']) { |
| 22 | + const entryDir = join(outDir, name) |
| 23 | + await build({ |
| 24 | + ...buildConfig[0], |
| 25 | + cwd: root, |
| 26 | + config: false, |
| 27 | + entry: { [name]: join(root, 'src', `${name}.ts`) }, |
| 28 | + outDir: entryDir, |
| 29 | + dts: false, |
| 30 | + minify: true, |
| 31 | + report: false, |
| 32 | + logLevel: 'silent', |
| 33 | + }) |
| 34 | + const files = await readdir(entryDir) |
| 35 | + if (files.length !== 1 || files[0] !== `${name}.js`) { |
| 36 | + throw new Error(`Expected a single complete bundle for ${name}: ${files}`) |
| 37 | + } |
| 38 | + const code = await readFile(join(entryDir, `${name}.js`)) |
| 39 | + const gzipBytes = gzipSync(code, { level: 9 }).byteLength |
| 40 | + sizes.push({ name, bytes: Buffer.byteLength(code), gzipBytes }) |
| 41 | + } |
| 42 | +} finally { |
| 43 | + await rm(outDir, { recursive: true, force: true }) |
| 44 | +} |
| 45 | + |
| 46 | +const newline = readme.includes('\r\n') ? '\r\n' : '\n' |
| 47 | +const footprint = |
| 48 | + [ |
| 49 | + '- ✨ Minimal footprint', |
| 50 | + ...sizes.map( |
| 51 | + ({ name, gzipBytes }) => |
| 52 | + ` - ${(gzipBytes / 1000).toFixed(2)} kB minified + gzipped (${name})`, |
| 53 | + ), |
| 54 | + ].join(newline) + newline |
| 55 | +const updated = readme.replace(section, footprint) |
| 56 | +if (updated !== readme) { |
| 57 | + await writeFile(readmePath, updated) |
| 58 | +} |
| 59 | +console.table(sizes) |
| 60 | +console.log( |
| 61 | + updated === readme ? 'README.md is up to date' : 'Updated README.md', |
| 62 | +) |
0 commit comments