65 lines
2.3 KiB
JavaScript
65 lines
2.3 KiB
JavaScript
// The size budget: what does the framework itself cost on the wire?
|
|||
|
|
// Bundles @choojs/core + @choojs/html (browser condition) minified via
|
||
|
|
// the same Vite/Rolldown pipeline apps use, then reports min+gzip and
|
||
|
|
// min+brotli. CI fails if min+gzip exceeds the budget.
|
||
|
|
//
|
||
|
|
// npm run size
|
||
|
|
|
||
|
|
import { build } from 'vite'
|
||
|
|
import { readFile, readdir, rm, mkdtemp } from 'node:fs/promises'
|
||
|
|
import { join } from 'node:path'
|
||
|
|
import { tmpdir } from 'node:os'
|
||
|
|
import { gzipSync, brotliCompressSync, constants } from 'node:zlib'
|
||
|
|
|
||
|
|
// The promise the README makes; raise only with a paper trail.
|
||
|
|
// Context: v7's "4kb" was choo alone — nanohtml lived in the browserify
|
||
|
|
// transform. This number is the whole framework: core, html engine,
|
||
|
|
// morph, hydrate, router, bus. Currently ~7.97 kB gzip; the headroom is
|
||
|
|
// margin, not license.
|
||
|
|
const BUDGET_GZIP = 8.5 * 1024
|
||
|
|
|
||
|
|
const VIRTUAL = '\0size:entry'
|
||
|
|
|
||
|
|
const outDir = await mkdtemp(join(tmpdir(), 'choo-size-'))
|
||
|
|
await build({
|
||
|
|
appType: 'custom',
|
||
|
|
logLevel: 'error',
|
||
|
|
plugins: [{
|
||
|
|
name: 'size-entry',
|
||
|
|
resolveId: (id) => id === 'size:entry' ? VIRTUAL : undefined,
|
||
|
|
load: (id) => id === VIRTUAL
|
||
|
|
? "import choo, { Choo, lazy } from '@choojs/core'\n" +
|
||
|
|
"import html from '@choojs/html'\n" +
|
||
|
|
"import raw from '@choojs/html/raw'\n" +
|
||
|
|
'window.__keep = { choo, Choo, lazy, html, raw }\n'
|
||
|
|
: undefined
|
||
|
|
}],
|
||
|
|
build: {
|
||
|
|
outDir,
|
||
|
|
emptyOutDir: true,
|
||
|
|
rollupOptions: { input: { framework: 'size:entry' } }
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
const assets = await readdir(join(outDir, 'assets'))
|
||
|
|
const file = assets.find((name) => name.endsWith('.js'))
|
||
|
|
const code = await readFile(join(outDir, 'assets', file))
|
||
|
|
await rm(outDir, { recursive: true, force: true })
|
||
|
|
|
||
|
|
const gz = gzipSync(code, { level: constants.Z_BEST_COMPRESSION }).length
|
||
|
|
const br = brotliCompressSync(code, {
|
||
|
|
params: { [constants.BROTLI_PARAM_QUALITY]: constants.BROTLI_MAX_QUALITY }
|
||
|
|
}).length
|
||
|
|
|
||
|
|
const kb = (n) => (n / 1024).toFixed(2) + ' kB'
|
||
|
|
console.log(`@choojs/core + @choojs/html (browser, minified)`)
|
||
|
|
console.log(` raw: ${kb(code.length)}`)
|
||
|
|
console.log(` gzip: ${kb(gz)} (budget ${kb(BUDGET_GZIP)})`)
|
||
|
|
console.log(` brotli: ${kb(br)}`)
|
||
|
|
|
||
|
|
if (gz > BUDGET_GZIP) {
|
||
|
|
console.error(`\nsize budget exceeded: ${kb(gz)} > ${kb(BUDGET_GZIP)} min+gzip`)
|
||
|
|
process.exit(1)
|
||
|
|
}
|
||
|
|
console.log('\nwithin budget ✔')
|