From f208beb92f0f7f4d62a5f6bf19121ae1d6d35bb7 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Sun, 1 Nov 2026 13:49:34 -0500 Subject: [PATCH] feat: generate bundle summary --- .github/workflows/ci.yml | 11 +++++++++++ scripts/build.js | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 33fec51..2d70593 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,6 +14,17 @@ concurrency: cancel-in-progress: true jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-node@v7 + with: + node-version: latest + - run: npm ci + - name: Build and summarize bundle size + run: npm run build -- --summary + test: runs-on: ubuntu-latest steps: diff --git a/scripts/build.js b/scripts/build.js index c30529c..03f77e5 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -1,12 +1,14 @@ 'use strict'; const path = require('node:path'); +const { appendFileSync } = require('node:fs'); const { buildSync } = require('esbuild'); -buildSync({ +const { metafile } = buildSync({ entryPoints: [path.join(__dirname, '..', 'index.js')], outfile: path.join(__dirname, '..', 'dist', 'microtel.js'), bundle: true, + metafile: true, platform: 'node', format: 'cjs', // Function and class names stay observable, as in the unbundled source. @@ -16,3 +18,34 @@ buildSync({ charset: 'ascii', logLevel: 'warning', }); + +if (process.argv.includes('--summary')) { + const formatSize = (bytes) => { + if (bytes < 1024) return `${bytes} B`; + if (bytes < 1024 ** 2) return `${(bytes / 1024).toFixed(2)} KiB`; + return `${(bytes / 1024 ** 2).toFixed(2)} MiB`; + }; + const summary = ['## Build size', '']; + for (const [file, output] of Object.entries(metafile.outputs)) { + summary.push( + `### ${file}: ${formatSize(output.bytes)}`, + '', + '| Input | Size in bundle | Share of bundle |', + '| --- | ---: | ---: |', + ); + for (const [input, { bytesInOutput }] of Object.entries(output.inputs).sort( + (a, b) => b[1].bytesInOutput - a[1].bytesInOutput, + )) { + summary.push( + `| ${input} | ${formatSize(bytesInOutput)} | ${((bytesInOutput / output.bytes) * 100).toFixed(2)}% |`, + ); + } + summary.push(''); + } + const report = summary.join('\n'); + if (process.env.GITHUB_STEP_SUMMARY) { + appendFileSync(process.env.GITHUB_STEP_SUMMARY, report); + } else { + process.stdout.write(report); + } +}