main-package-size.mjs
3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
export const DEFAULT_MAIN_PACKAGE_BUDGET_BYTES = 2_000_000
function normalizeRelative(value) {
return value.replaceAll('\\', '/').replace(/^\.\//, '').replace(/^\/+|\/+$/g, '')
}
function readJson(filePath) {
return JSON.parse(fs.readFileSync(filePath, 'utf8'))
}
function isInside(relativePath, rootPath) {
return relativePath === rootPath || relativePath.startsWith(`${rootPath}/`)
}
function collectUploadExclusions(projectConfig) {
const ignoredFolders = new Set(['node_modules'])
const ignoredFiles = new Set()
for (const item of projectConfig.packOptions?.ignore ?? []) {
const value = normalizeRelative(item.value ?? '')
if (!value) continue
if (item.type === 'folder') ignoredFolders.add(value)
if (item.type === 'file') ignoredFiles.add(value)
}
return {
ignoredFolders: [...ignoredFolders].sort(),
ignoredFiles: [...ignoredFiles].sort(),
}
}
function walkFiles(rootDir, shouldExclude) {
const files = []
function visit(relativeDir = '') {
const absoluteDir = path.join(rootDir, relativeDir)
const entries = fs.readdirSync(absoluteDir, { withFileTypes: true }).sort((a, b) =>
a.name.localeCompare(b.name, 'en'),
)
for (const entry of entries) {
const relativePath = normalizeRelative(path.join(relativeDir, entry.name))
if (shouldExclude(relativePath)) continue
if (entry.isDirectory()) visit(relativePath)
else if (entry.isFile()) files.push(relativePath)
}
}
visit()
return files
}
export function analyzeMainPackage({
rootDir = process.cwd(),
budgetBytes = DEFAULT_MAIN_PACKAGE_BUDGET_BYTES,
} = {}) {
const absoluteRoot = path.resolve(rootDir)
const appConfig = readJson(path.join(absoluteRoot, 'app.json'))
const projectConfig = readJson(path.join(absoluteRoot, 'project.config.json'))
const subpackageRoots = (appConfig.subpackages ?? appConfig.subPackages ?? [])
.map(item => normalizeRelative(item.root ?? ''))
.filter(Boolean)
.sort()
const { ignoredFolders, ignoredFiles } = collectUploadExclusions(projectConfig)
const shouldExclude = relativePath =>
subpackageRoots.some(root => isInside(relativePath, root)) ||
ignoredFolders.some(root => isInside(relativePath, root)) ||
ignoredFiles.includes(relativePath)
const files = walkFiles(absoluteRoot, shouldExclude)
const sizes = files.map(relativePath => ({
path: relativePath,
bytes: fs.statSync(path.join(absoluteRoot, relativePath)).size,
}))
const totalBytes = sizes.reduce((sum, item) => sum + item.bytes, 0)
const rootTotals = new Map()
for (const item of sizes) {
const root = item.path.includes('/') ? item.path.slice(0, item.path.indexOf('/')) : '(root)'
rootTotals.set(root, (rootTotals.get(root) ?? 0) + item.bytes)
}
const largestRoots = [...rootTotals.entries()]
.map(([root, bytes]) => ({ root, bytes }))
.sort((a, b) => b.bytes - a.bytes || a.root.localeCompare(b.root, 'en'))
return {
budgetBytes,
totalBytes,
totalFiles: files.length,
remainingBytes: budgetBytes - totalBytes,
withinBudget: totalBytes <= budgetBytes,
subpackageRoots,
largestRoots,
}
}
const currentFile = fileURLToPath(import.meta.url)
if (process.argv[1] && path.resolve(process.argv[1]) === path.resolve(currentFile)) {
const report = analyzeMainPackage()
console.log(JSON.stringify(report, null, 2))
if (!report.withinBudget) process.exitCode = 1
}