Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

measure-bundle.mjs

43 lines1.2 KB

1import { readFile, readdir, stat } from 'node:fs/promises'
2import path from 'node:path'
3import { gzipSync } from 'node:zlib'
4import { fileURLToPath } from 'node:url'
5​
6const scriptDir = path.dirname(fileURLToPath(import.meta.url))
7const repoRoot = path.resolve(scriptDir, '..')
8const distDir = path.join(repoRoot, 'build', 'modern')
9const entryFile = path.join(distDir, 'index.js')
10​
11async function getDirSizeBytes(dirPath) {
12 let total = 0
13 const entries = await readdir(dirPath, { withFileTypes: true })
14 for (const entry of entries) {
15 const fullPath = path.join(dirPath, entry.name)
16 if (entry.isDirectory()) {
17 total += await getDirSizeBytes(fullPath)
18 } else if (entry.isFile()) {
19 total += (await stat(fullPath)).size
20 }
21 }
22 return total
24​
25async function run() {
26 const entry = await readFile(entryFile)
27 const entryGzip = gzipSync(entry)
28 const distBytes = await getDirSizeBytes(distDir)
29​
30 const output = {
31 measuredAt: new Date().toISOString(),
32 entryJsBytes: entry.length,
33 entryJsGzipBytes: entryGzip.length,
34 distTotalBytes: distBytes,
35 }
36​
37 console.log(JSON.stringify(output, null, 2))
39​
40run().catch((error) => {
41 console.error(error)
42 process.exit(1)
43})