Open History to see the last commit
1#!/usr/bin/env node
2/**
3 * Compare the shipped size of packages with their merge-base on a base branch.
4 *
5 * Code weight is what users download and parse, so this measures shipped
6 * bytes after gzip and brotli, not source lines: comments, names, and
7 * formatting do not count. It builds each package with the same
8 * `vite build --minify` that the Compressed size CI check uses. Then, as an
9 * app's production build would, it bundles each package's exported entries
10 * with `process.env.NODE_ENV` set to `production` and its dependencies
11 * external, minifies, and compresses the bundle. Development-only code, such
12 * as full error messages, drops out. `--dev` instead compares every minified
13 * `dist/esm` file as the CI check does, development code included.
14 *
15 * Usage:
16 * pnpm size:compare [--base origin/main] [--exact-base] [--dev]
17 * [--packages db,react-db,...] [--files] [--rebuild]
18 *
19 * The baseline is the merge-base with `--base` (as CI compares), built once in
20 * a cached worktree under the OS temp directory. The working tree is rebuilt
21 * in parallel, and a package whose sources did not change since its last
22 * build is not rebuilt. With `--files`, production mode lists each source
23 * module's minified bytes in the bundle, before compression.
24 */
25import { execFile, execSync } from 'node:child_process'
26import { createHash } from 'node:crypto'
27import {
28 existsSync,
29 readFileSync,
30 readdirSync,
31 statSync,
32 writeFileSync,
33} from 'node:fs'
34import { availableParallelism, tmpdir } from 'node:os'
35import { join, relative } from 'node:path'
36import { promisify } from 'node:util'
37import { brotliCompressSync, constants, gzipSync } from 'node:zlib'
38import { build as esbuild } from 'esbuild'
39
40const run = promisify(execFile)
41const args = process.argv.slice(2)
42const option = (name, fallback) => {
43 const index = args.indexOf(`--${name}`)
44 return index === -1 ? fallback : args[index + 1]
45}
46const flag = (name) => args.includes(`--${name}`)
47
48const DEFAULT_PACKAGES = [
49 `db`,
50 `react-db`,
51 `query-db-collection`,
52 `electric-db-collection`,
53 `powersync-db-collection`,
54 `trailbase-db-collection`,
55 `rxdb-db-collection`,
56 `db-sqlite-persistence-core`,
57]
58// Built first, in order: the other packages resolve their types while their
59// declarations build.
60const PREREQUISITES = [`db-ivm`, `db`]
61
62const root = execSync(`git rev-parse --show-toplevel`, {
63 encoding: `utf8`,
64}).trim()
65const baseRef = option(`base`, `origin/main`)
66const packages = option(`packages`, DEFAULT_PACKAGES.join(`,`)).split(`,`)
67const git = (command) =>
68 execSync(`git ${command}`, { cwd: root, encoding: `utf8` }).trim()
69const baseSha = flag(`exact-base`)
70 ? git(`rev-parse ${baseRef}`)
71 : git(`merge-base HEAD ${baseRef}`)
72const baseDir = join(
73 tmpdir(),
74 `tanstack-db-size-baseline-${baseSha.slice(0, 9)}`,
75)
76
77function walk(dir, keep, out = []) {
78 if (!existsSync(dir)) return out
79 for (const name of readdirSync(dir)) {
80 const path = join(dir, name)
81 if (statSync(path).isDirectory()) walk(path, keep, out)
82 else if (keep(name)) out.push(path)
83 }
84 return out
85}
86
87// A package's dist depends on its own sources and build config only: its
88// dependencies are external to the bundle.
89function sourceHash(dir, pkg) {
90 const pkgDir = join(dir, `packages`, pkg)
91 const hash = createHash(`sha256`)
92 const files = [
93 ...walk(join(pkgDir, `src`), () => true),
94 ...[`vite.config.ts`, `package.json`, `tsconfig.json`]
95 .map((name) => join(pkgDir, name))
96 .filter(existsSync),
97 ].sort()
98 for (const file of files) {
99 hash.update(relative(pkgDir, file))
100 hash.update(readFileSync(file))
101 }
102 return hash.digest(`hex`)
103}
104
105async function buildPackage(dir, pkg, force) {
106 const pkgDir = join(dir, `packages`, pkg)
107 if (!existsSync(pkgDir)) return `missing`
108 const stamp = join(pkgDir, `dist`, `.size-compare-hash`)
109 const hash = sourceHash(dir, pkg)
110 if (!force && existsSync(stamp) && readFileSync(stamp, `utf8`) === hash)
111 return `cached`
112 try {
113 await run(`npx`, [`vite`, `build`, `--minify`], {
114 cwd: pkgDir,
115 maxBuffer: 1 << 28,
116 })
117 } catch (error) {
118 const output = `${error.stderr ?? ``}${error.stdout ?? ``}`
119 throw new Error(
120 `vite build failed in packages/${pkg}:\n${output.split(`\n`).slice(0, 20).join(`\n`)}`,
121 )
122 }
123 writeFileSync(stamp, hash)
124 return `built`
125}
126
127async function build(dir, label, force) {
128 const started = Date.now()
129 const prerequisiteResults = {}
130 for (const pkg of PREREQUISITES)
131 prerequisiteResults[pkg] = await buildPackage(dir, pkg, force)
132 // Packages build independently, so run them in parallel.
133 const limit = Math.max(1, Math.floor(availableParallelism() / 2))
134 const queue = packages.filter((pkg) => !PREREQUISITES.includes(pkg))
135 const results = {}
136 await Promise.all(
137 Array.from({ length: limit }, async () => {
138 for (let pkg = queue.shift(); pkg; pkg = queue.shift())
139 results[pkg] = await buildPackage(dir, pkg, force)
140 }),
141 )
142 for (const pkg of packages.filter((pkg) => PREREQUISITES.includes(pkg)))
143 results[pkg] = prerequisiteResults[pkg]
144 const built = Object.values(results).filter((r) => r === `built`).length
145 process.stderr.write(
146 `${label}: built ${built} of ${packages.length} packages in ${(
147 (Date.now() - started) /
148 1000
149 ).toFixed(1)}s\n`,
150 )
151}
152
153if (!existsSync(baseDir)) {
154 process.stderr.write(`creating baseline worktree at ${baseSha.slice(0, 9)}\n`)
155 git(`worktree add --detach ${baseDir} ${baseSha}`)
156 execSync(`pnpm install --frozen-lockfile --prefer-offline`, {
157 cwd: baseDir,
158 stdio: `ignore`,
159 })
160}
161await build(baseDir, `baseline ${baseSha.slice(0, 9)}`, false)
162await build(root, `working tree`, flag(`rebuild`))
163
164const sizes = (buffer) => ({
165 gzip: gzipSync(buffer, { level: 9 }).length,
166 brotli: brotliCompressSync(buffer, {
167 params: { [constants.BROTLI_PARAM_QUALITY]: 11 },
168 }).length,
169})
170function measureDev(dir, pkg) {
171 const esm = join(dir, `packages`, pkg, `dist`, `esm`)
172 return new Map(
173 walk(esm, (name) => /\.m?js$/.test(name)).map((file) => [
174 relative(esm, file),
175 sizes(readFileSync(file)),
176 ]),
177 )
178}
179
180// The ESM files a package exports, as an app imports them.
181function exportedEntries(pkgDir) {
182 const manifest = JSON.parse(
183 readFileSync(join(pkgDir, `package.json`), `utf8`),
184 )
185 const entries = new Set()
186 const visit = (value) => {
187 if (typeof value === `string`) {
188 if (/\.m?js$/.test(value) && !value.includes(`/cjs/`)) entries.add(value)
189 } else if (value && typeof value === `object`) {
190 for (const [condition, target] of Object.entries(value))
191 if (condition !== `require` && condition !== `types`) visit(target)
192 }
193 }
194 visit(manifest.exports ?? manifest.module ?? manifest.main)
195 return [...entries].map((entry) => join(pkgDir, entry))
196}
197
198// One bundle per package, as an app's production build sees it. The total
199// is the compressed bundle; each file's entry is its minified bytes in it.
200async function measureProduction(dir, pkg) {
201 const pkgDir = join(dir, `packages`, pkg)
202 const entries = exportedEntries(pkgDir).filter(existsSync)
203 if (entries.length === 0) return new Map()
204 const result = await esbuild({
205 stdin: {
206 contents: entries
207 .map((entry) => `export * from ${JSON.stringify(entry)}`)
208 .join(`\n`),
209 resolveDir: pkgDir,
210 },
211 absWorkingDir: pkgDir,
212 bundle: true,
213 minify: true,
214 format: `esm`,
215 platform: `neutral`,
216 packages: `external`,
217 // Resolve imports as an app would: the repo's tsconfig paths would
218 // otherwise inline other workspace packages from their sources.
219 tsconfigRaw: `{}`,
220 write: false,
221 metafile: true,
222 logLevel: `error`,
223 define: { 'process.env.NODE_ENV': `"production"` },
224 })
225 const bundle = Buffer.from(result.outputFiles[0].contents)
226 const files = new Map([[`(bundle)`, sizes(bundle)]])
227 const output = Object.values(result.metafile.outputs)[0]
228 for (const [input, { bytesInOutput }] of Object.entries(output.inputs)) {
229 const name = relative(join(`dist`, `esm`), input)
230 files.set(name, { gzip: bytesInOutput, brotli: bytesInOutput, raw: true })
231 }
232 return files
233}
234
235const production = !flag(`dev`)
236const measure = (dir, pkg) =>
237 production
238 ? measureProduction(dir, pkg)
239 : Promise.resolve(measureDev(dir, pkg))
240
241const signed = (n) => (n > 0 ? `+${n}` : `${n}`)
242const percent = (delta, base) =>
243 base ? ` (${signed(Math.round((delta / base) * 1000) / 10)}%)` : ``
244// In production mode the bundle is the total; per-file entries are detail.
245const total = (map, key) =>
246 production
247 ? (map.get(`(bundle)`)?.[key] ?? 0)
248 : [...map.values()].reduce((sum, size) => sum + size[key], 0)
249
250console.log(
251 `merge-base ${baseSha.slice(0, 9)} (${baseRef}) vs working tree: ${
252 production
253 ? `production bundle per package (NODE_ENV=production, dependencies external)`
254 : `minified dist/esm files, development code included`
255 }, gzip -9 and brotli -11 bytes${
256 production && flag(`files`)
257 ? `; per-file rows are minified bytes before compression`
258 : ``
259 }\n`,
260)
261console.log(
262 [`package`, `base gzip`, `head gzip`, `Δ gzip`, `Δ brotli`].join(`\t`),
263)
264const sum = { base: 0, head: 0, baseBrotli: 0, headBrotli: 0 }
265for (const pkg of packages) {
266 const base = await measure(baseDir, pkg)
267 const head = await measure(root, pkg)
268 const baseGzip = total(base, `gzip`)
269 const headGzip = total(head, `gzip`)
270 sum.base += baseGzip
271 sum.head += headGzip
272 sum.baseBrotli += total(base, `brotli`)
273 sum.headBrotli += total(head, `brotli`)
274 console.log(
275 [
276 pkg,
277 baseGzip,
278 headGzip,
279 signed(headGzip - baseGzip) + percent(headGzip - baseGzip, baseGzip),
280 signed(total(head, `brotli`) - total(base, `brotli`)),
281 ].join(`\t`),
282 )
283 if (!flag(`files`)) continue
284 const changed = [...new Set([...base.keys(), ...head.keys()])]
285 .filter((file) => file !== `(bundle)`)
286 .map((file) => [
287 file,
288 (head.get(file)?.gzip ?? 0) - (base.get(file)?.gzip ?? 0),
289 ])
290 .filter(([, delta]) => delta !== 0)
291 .sort((a, b) => b[1] - a[1])
292 for (const [file, delta] of changed)
293 console.log(` ${file}\t\t\t${signed(delta)}`)
294}
295console.log(
296 [
297 `TOTAL`,
298 sum.base,
299 sum.head,
300 signed(sum.head - sum.base) + percent(sum.head - sum.base, sum.base),
301 signed(sum.headBrotli - sum.baseBrotli),
302 ].join(`\t`),
303)