327 lines
17 KiB
JavaScript
327 lines
17 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* 产物体检(阶段 8 · 第 6 项「UI chunk 优化」的分析工具):把一个 vite 构建产物
|
||
* 拆成「首屏必须下载什么」+「每个 chunk 里装的是谁的代码」。
|
||
*
|
||
* 为什么要它:`ui` chunk 1216 kB 一直在顶 chunkSizeWarningLimit,但只凭这一行
|
||
* 数字没法判断该不该动、动了有没有收益。要判断必须回答三个问题:
|
||
* 1) 首屏(index.html 的 <script> + modulepreload + 它们的静态 import 闭包)
|
||
* 到底拉了哪些文件——lazy 路由的 chunk 不算首屏,混在一起看会得出错误结论;
|
||
* 2) 这些文件里前十大来源是哪些依赖——不知道是谁占的就不知道能拆什么;
|
||
* 3) 同一个东西在 lazy chunk 里有多少是首屏其实不需要的。
|
||
*
|
||
* 大小口径:raw(磁盘字节)/ gzip / brotli 都给。传输看压缩,parse 看 raw,
|
||
* 两个都要,只看 raw 会高估、只看 gzip 会低估 CPU 成本。
|
||
* 归属口径:用同名 .map 的 mappings 把「产物列区间」映射回源文件,
|
||
* 再按 node_modules 包名 / src 路径聚合。列 ≈ 字节(产物是 ASCII 压缩代码),
|
||
* 这是近似值,但足够定位大头。没有 .map 时只出尺寸表。
|
||
*
|
||
* ⚠ 阶段 31 修正了这把尺本身的缺陷(详见 §3 注释与
|
||
* `reports/NEX阶段31-Bundle模块级结案与chunkinfo归属修正-2026-11-09.md`):
|
||
* 旧实现把「每行最后一个映射点之后的全部列」回填给那个源,在**预压缩 CJS 透传段**
|
||
* (`react` / `scheduler` / `@babel/runtime` 的 `*.production.min.js` 这类磁盘几 kB、
|
||
* 产物里只有一两行且 mappings 覆盖不全的源)上会凭空造出几百 kB。
|
||
* 现在空档超过 `GAP_LIMIT` 一律记为「无主字节」单列,并把每个源的归属字节
|
||
* 与它在磁盘上的真实大小对账,超出 `SOURCE_TOLERANCE` 倍直接点名。
|
||
* 想看「谁真的占了多少」的精确口径,用模块级的 `renderedLength`:
|
||
* `node reports/ui_evidence/nex-p31/eagergraph.mjs`。
|
||
*
|
||
* 用法:
|
||
* node scripts/ui/chunkinfo.mjs [dist 目录] # 默认 frontend/dist
|
||
* node scripts/ui/chunkinfo.mjs frontend/dist-sm # 带 --sourcemap 的产物做归属分析
|
||
*/
|
||
import fs from 'node:fs'
|
||
import path from 'node:path'
|
||
import zlib from 'node:zlib'
|
||
import { fileURLToPath } from 'node:url'
|
||
|
||
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
||
const DIST = path.resolve(process.argv[2] || path.join(HERE, '../../frontend/dist'))
|
||
const ASSETS = path.join(DIST, 'assets')
|
||
|
||
const kb = (n) => (n / 1024).toFixed(2).padStart(8)
|
||
const exists = (p) => fs.existsSync(p)
|
||
|
||
function sizes(file) {
|
||
const buf = fs.readFileSync(file)
|
||
return {
|
||
raw: buf.length,
|
||
gzip: zlib.gzipSync(buf, { level: 9 }).length,
|
||
brotli: zlib.brotliCompressSync(buf).length,
|
||
buf,
|
||
}
|
||
}
|
||
|
||
/* ---------- 1. 首屏闭包:index.html → 静态 import 图 ---------- */
|
||
const html = fs.readFileSync(path.join(DIST, 'index.html'), 'utf8')
|
||
const ref = /(\/assets\/[\w.-]+\.(?:js|css))/g
|
||
const eager = new Set()
|
||
let m
|
||
while ((m = ref.exec(html))) {
|
||
const f = path.join(ASSETS, path.basename(m[1]))
|
||
if (exists(f)) eager.add(path.basename(f))
|
||
}
|
||
/* 沿静态 import / export-from 递归(dynamic import 不算:那是换页时才拉的) */
|
||
const STATIC = /(?:^|[\s;}])import\s+(?:[\w*{}\s,]+\s+from\s+)?["'](\.\/[\w.-]+\.js)["']|(?:^|[\s;}])import\s+["'](\.\/[\w.-]+\.js)["']|(?:^|[\s;}])export\s+(?:[\w*{}\s,]+\s+)?from\s+["'](\.\/[\w.-]+\.js)["']/g
|
||
function closure(seed) {
|
||
const seen = new Set(seed)
|
||
const queue = [...seed]
|
||
while (queue.length) {
|
||
const name = queue.shift()
|
||
const f = path.join(ASSETS, name)
|
||
if (!exists(f) || !name.endsWith('.js')) continue
|
||
const code = fs.readFileSync(f, 'utf8')
|
||
let mm
|
||
while ((mm = STATIC.exec(code))) {
|
||
const dep = path.basename(mm[1] || mm[2] || mm[3])
|
||
if (exists(path.join(ASSETS, dep)) && !seen.has(dep)) { seen.add(dep); queue.push(dep) }
|
||
}
|
||
}
|
||
return seen
|
||
}
|
||
const eagerAll = closure(eager)
|
||
|
||
/* ---------- 2. 尺寸表 ---------- */
|
||
const files = fs.readdirSync(ASSETS).filter((f) => /\.(js|css)$/.test(f)).sort()
|
||
const rows = files.map((f) => ({ f, ...sizes(path.join(ASSETS, f)) }))
|
||
const total = rows.reduce((a, r) => ({ raw: a.raw + r.raw, gzip: a.gzip + r.gzip, brotli: a.brotli + r.brotli }), { raw: 0, gzip: 0, brotli: 0 })
|
||
const eagerRows = rows.filter((r) => eagerAll.has(r.f))
|
||
const eagerTotal = eagerRows.reduce((a, r) => ({ raw: a.raw + r.raw, gzip: a.gzip + r.gzip, brotli: a.brotli + r.brotli }), { raw: 0, gzip: 0, brotli: 0 })
|
||
|
||
console.log('dist:', DIST)
|
||
console.log('\n=== 首屏闭包(index.html 静态引用 + 静态 import 递归)===')
|
||
console.log('文件'.padEnd(44), 'raw kB', 'gzip kB', 'brotli kB')
|
||
for (const r of eagerRows.sort((a, b) => b.raw - a.raw)) {
|
||
console.log(r.f.padEnd(44), kb(r.raw), kb(r.gzip), kb(r.brotli))
|
||
}
|
||
console.log('-'.repeat(72))
|
||
console.log('首屏合计'.padEnd(42), kb(eagerTotal.raw), kb(eagerTotal.gzip), kb(eagerTotal.brotli))
|
||
console.log('全部产物'.padEnd(42), kb(total.raw), kb(total.gzip), kb(total.brotli))
|
||
console.log(`首屏占全部:raw ${(100 * eagerTotal.raw / total.raw).toFixed(1)}% · gzip ${(100 * eagerTotal.gzip / total.gzip).toFixed(1)}%`)
|
||
|
||
console.log('\n=== 全部 chunk(含换页才拉的)===')
|
||
console.log('文件'.padEnd(44), 'raw kB', 'gzip kB', 'brotli kB', '首屏')
|
||
for (const r of rows.sort((a, b) => b.raw - a.raw)) {
|
||
console.log(r.f.padEnd(44), kb(r.raw), kb(r.gzip), kb(r.brotli), eagerAll.has(r.f) ? 'YES' : ' ')
|
||
}
|
||
|
||
/* ---------- 3. 归属分析(需要 .map) ---------- */
|
||
/* source-map VLQ 解码:只需要 generated line / column → source index。
|
||
*
|
||
* ⚠ 阶段 31 修掉了这把尺子真正的病灶(不是行尾回填,是这个解码器):
|
||
* source map v3 里 **generated column 每行归零**,但
|
||
* **source index / source line / source column 是整串 mappings 累积的**。
|
||
* 旧实现 `decode(line)` 把 `sidx` 也放在行内归零,于是从第 2 行起
|
||
* 每一段都拿「本行累计的源下标」去查 `sources[]`,整张归属表被打乱:
|
||
* 实测同一份 `WarningOutlined-*.js.map`,错的解码器报
|
||
* `npm:react` 234.5 kB(该文件磁盘只有 6.8 kB)、
|
||
* 指向 react 的段有 51 990 个、源切换 5 万多次;
|
||
* 按规范累积的解码器报同一个包几个连续区间、全文件源切换只有 1 011 次
|
||
* (拼接产物的正常形态)。
|
||
* ⇒ 阶段 24 报告里「react 306 / @babel-runtime 217 / rc-util 170 kB」是伪像,
|
||
* 本轮更正;`renderedLength` 口径(`eagergraph.mjs`,不读 sourcemap)不受影响。
|
||
* 修法:跨行累积 `sidx`,`gcol` 每行归零;并保留行尾/空档不回填 + 磁盘对账两道自证。 */
|
||
const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
||
const VAL = new Int8Array(128).fill(-64)
|
||
CHARS.split('').forEach((c, i) => { VAL[c.charCodeAt(0)] = i })
|
||
|
||
/* 返回 Array<线>[generatedLine] = [[gcol, sourceIndex], …];sidx 跨行累积 */
|
||
function decodeMappings(mappings) {
|
||
const byLine = []
|
||
let sidx = 0
|
||
for (const line of String(mappings || '').split(';')) {
|
||
let gcol = 0
|
||
const segs = []
|
||
for (const seg of line.split(',')) {
|
||
if (!seg) continue
|
||
const fields = []
|
||
let i = 0
|
||
while (i < seg.length && fields.length < 5) {
|
||
let shift = 0, result = 0, cont = true
|
||
while (cont) {
|
||
const d = VAL[seg.charCodeAt(i++)]
|
||
if (d < 0) { cont = false; break }
|
||
cont = !!(d & 32)
|
||
result |= (d & 31) << shift
|
||
shift += 5
|
||
}
|
||
const neg = result & 1
|
||
result >>= 1
|
||
fields.push(neg ? -result : result)
|
||
}
|
||
gcol += fields[0] || 0
|
||
if (fields.length >= 4) { sidx += fields[1]; segs.push([gcol, sidx]) }
|
||
}
|
||
byLine.push(segs)
|
||
}
|
||
return byLine
|
||
}
|
||
|
||
|
||
const TOPN = Number(process.env.NEX_TOP || 15) /* 想看某包落在第几名时调大,不改默认口径 */
|
||
const GAP_LIMIT = Number(process.env.GAP_LIMIT || 2048)
|
||
const SOURCE_TOLERANCE = Number(process.env.SOURCE_TOLERANCE || 1.5)
|
||
const COV_MIN = Number(process.env.SOURCE_CHECK_MIN_COVERAGE || 60) /* 覆盖率低于此 ⇒ 自证空转,不许宣称「没有伪像」 */
|
||
const SOURCE_CHECK_STRICT = process.env.SOURCE_CHECK_STRICT === '1' /* 可选:把自证判定升级成退出码 */
|
||
const ORPHAN = '(无主字节:map 未覆盖的列区间)'
|
||
|
||
const withMaps = rows.filter((r) => r.f.endsWith('.js') && exists(path.join(ASSETS, r.f + '.map')))
|
||
if (!withMaps.length) {
|
||
console.log('\n(产物没有 .map:跳过归属分析。用 `npx vite build --sourcemap --outDir dist-sm` 再跑一次。)')
|
||
} else {
|
||
const agg = new Map()
|
||
const aggByChunk = new Map()
|
||
const orphanByChunk = new Map()
|
||
const orphans = []
|
||
const diskCache = new Map()
|
||
const add = (map, k, w) => map.set(k, (map.get(k) || 0) + w)
|
||
/* 源文件在磁盘上的真实大小:归属字节不可能显著超过它,超了就是尺子在造数。
|
||
⚠ 阶段 31 翻自己的案:旧实现只认「.map 所在目录」一个基准。产物在项目树内时
|
||
`dist/assets/../../node_modules/…` 恰好等于 `frontend/node_modules/…`,看着没毛病;
|
||
产物一旦复制到树外(`/tmp/nex-p31-dist-sm`)全部解析失败,对账覆盖率 0/1177,
|
||
判定行仍打印「(无)⇒ 没有伪像」⇒ 尺子的自证自己空转了(假绿)。
|
||
现在按顺序退让多个基准,并记录每个源命中哪个基准 + 给覆盖率设门槛:
|
||
覆盖率不达标时判定必须自称空转,不许冒充证据。 */
|
||
const PROJECT_ROOT = path.resolve(HERE, '../..')
|
||
const BASES = [...new Set([
|
||
ASSETS, /* 规范口径:sources 相对 .map 所在目录 */
|
||
DIST, PROJECT_ROOT, path.join(PROJECT_ROOT, 'frontend'), process.cwd(),
|
||
])].filter((p) => exists(p))
|
||
const BASE_LABELS = BASES.map((p) => {
|
||
const rel = path.relative(PROJECT_ROOT, p)
|
||
return rel && !rel.startsWith('..') ? rel || '.' : p
|
||
})
|
||
const isFile = (p) => { try { return fs.statSync(p).isFile() } catch { return false } }
|
||
const hits = BASES.map(() => 0)
|
||
let unresolved = 0
|
||
const diskOf = (mapDir, src) => {
|
||
const key = mapDir + '\u0000' + src
|
||
if (diskCache.has(key)) return diskCache.get(key)
|
||
let hit = { disk: -1, base: -1 }
|
||
try {
|
||
const clean = decodeURIComponent(src.split('?')[0].split('#')[0])
|
||
const stripped = clean.replace(/^(?:\.\.(?:\/|$)|\.(?:\/|$))+/, '')
|
||
const nm = clean.lastIndexOf('node_modules/')
|
||
const cands = []
|
||
if (path.isAbsolute(clean)) cands.push([0, clean])
|
||
else BASES.forEach((b, bi) => {
|
||
cands.push([bi, path.resolve(b, clean)])
|
||
if (stripped !== clean) cands.push([bi, path.join(b, stripped)])
|
||
if (nm >= 0) cands.push([bi, path.join(b, clean.slice(nm))]) /* 多处 node_modules 时兜住 */
|
||
})
|
||
for (const [bi, abs] of cands) {
|
||
if (!isFile(abs)) continue
|
||
hit = { disk: fs.statSync(abs).size, base: bi }
|
||
hits[bi]++
|
||
break
|
||
}
|
||
if (hit.base < 0) unresolved++
|
||
} catch { hit = { disk: -1, base: -1 } }
|
||
diskCache.set(key, hit)
|
||
return hit
|
||
}
|
||
|
||
for (const r of withMaps) {
|
||
const map = JSON.parse(fs.readFileSync(path.join(ASSETS, r.f + '.map'), 'utf8'))
|
||
const srcs = map.sources || []
|
||
const mapLines = decodeMappings(map.mappings)
|
||
const code = r.buf.toString('utf8').split('\n')
|
||
const mapDir = ASSETS /* sources 是相对 .map 所在目录的路径,基准必须是目录本身 */
|
||
const own = new Map()
|
||
let orphan = 0
|
||
/* 逐行:段间空档与行尾空档 > GAP_LIMIT 记无主;mappings 没覆盖到的行整行无主。
|
||
不回填给邻居 ⇒ 归属 Σ + 无主 === 文件大小(可自证)。 */
|
||
const nLines = Math.max(mapLines.length, code.length)
|
||
for (let li = 0; li < nLines; li++) {
|
||
const lineLen = Buffer.byteLength(code[li] ?? '', 'utf8') + 1
|
||
const segs = li < mapLines.length ? mapLines[li] : []
|
||
if (!segs.length) { orphan += lineLen; continue }
|
||
for (let si = 0; si + 1 < segs.length; si++) {
|
||
const w = segs[si + 1][0] - segs[si][0]
|
||
if (w <= 0) continue
|
||
if (w > GAP_LIMIT) { orphan += w; continue }
|
||
add(own, srcs[segs[si][1]] || '?', w)
|
||
}
|
||
const tail = lineLen - segs[segs.length - 1][0]
|
||
if (tail > 0) {
|
||
if (tail > GAP_LIMIT) orphan += tail
|
||
else add(own, srcs[segs[segs.length - 1][1]] || '?', tail)
|
||
}
|
||
}
|
||
const per = new Map()
|
||
for (const [src, w] of own) {
|
||
const rel = src.split('node_modules/').pop()
|
||
const key = src.includes('node_modules/')
|
||
? 'npm:' + rel.split('/').slice(0, rel.startsWith('@') ? 2 : 1).join('/')
|
||
: 'src:' + src.split('/src/').pop()
|
||
add(agg, key, w)
|
||
add(per, key, w)
|
||
orphans.push({ chunk: r.f, src, w, ...diskOf(mapDir, src) })
|
||
}
|
||
const accounted = [...own.values()].reduce((a, b) => a + b, 0)
|
||
orphanByChunk.set(r.f, { orphan, raw: r.raw, accounted })
|
||
aggByChunk.set(r.f, per)
|
||
}
|
||
|
||
const top = (map, n) => [...map.entries()].sort((a, b) => b[1] - a[1]).slice(0, n)
|
||
const mapRaw = withMaps.reduce((a, r) => a + r.raw, 0)
|
||
const orphanTotal = [...orphanByChunk.values()].reduce((a, v) => a + v.orphan, 0)
|
||
const ownedTotal = [...orphanByChunk.values()].reduce((a, v) => a + v.accounted, 0)
|
||
const mapHead = (label) => {
|
||
console.log('\n' + label)
|
||
console.log(` 对账:归属 ${kb(ownedTotal)} + 无主 ${kb(orphanTotal)} = ${kb(ownedTotal + orphanTotal)} kB,带 map 产物 ${kb(mapRaw)} kB` +
|
||
` ⇒ 无主占 ${(100 * orphanTotal / mapRaw).toFixed(1)} %(gap>${GAP_LIMIT}B 不回填)`)
|
||
}
|
||
|
||
mapHead(`=== 全站前 ${TOPN} 大来源(压缩产物字节近似归属 · map 空档单列不回填)===`)
|
||
for (const [k, v] of top(agg, TOPN)) console.log(kb(v), 'kB', k)
|
||
console.log(kb(orphanTotal), 'kB', ORPHAN)
|
||
|
||
for (const [chunk, per] of aggByChunk) {
|
||
if (!eagerAll.has(chunk)) continue
|
||
if (per.size < 8) continue
|
||
const st = orphanByChunk.get(chunk)
|
||
console.log(`\n=== 首屏 chunk ${chunk} 前 ${TOPN} 大来源 ===`)
|
||
console.log(` 对账:归属 ${kb(st.accounted)} + 无主 ${kb(st.orphan)} = ${kb(st.accounted + st.orphan)} kB,文件 ${kb(st.raw)} kB` +
|
||
` ⇒ 无主占 ${(100 * st.orphan / st.raw).toFixed(1)} %`)
|
||
for (const [k, v] of top(per, TOPN)) console.log(kb(v), 'kB', k)
|
||
if (st.orphan) console.log(kb(st.orphan), 'kB', ORPHAN)
|
||
}
|
||
|
||
/* 自证:归属字节显著超过源文件磁盘大小 ⇒ 尺子伪像(或该源在块内被复制)。
|
||
⚠ 判定有两个前提必须同时成立才有资格说「没有伪像」:
|
||
a) 覆盖率达标(源能在磁盘上找到,否则整条对账根本没在看东西);b) 点名 0 条。
|
||
缺 a 就是阶段 31 踩过的那条假绿。 */
|
||
const bad = orphans
|
||
.filter((o) => o.disk > 0 && o.w > o.disk * SOURCE_TOLERANCE)
|
||
.map((o) => ({ ...o, over: o.w - o.disk, ratio: o.w / o.disk }))
|
||
.sort((a, b) => b.over - a.over)
|
||
const resolved = orphans.filter((o) => o.disk > 0).length
|
||
const cov = orphans.length ? (100 * resolved) / orphans.length : 0
|
||
const idle = cov < COV_MIN
|
||
const verdict = idle ? 'IDLE' : bad.length ? 'SUSPECT' : 'OK'
|
||
console.log('\n=== 尺子自证:归属 > 磁盘源文件 × ' + SOURCE_TOLERANCE + ' 的源(应当为 0 条)===')
|
||
console.log(` 对账覆盖 ${resolved}/${orphans.length} 个源(${cov.toFixed(1)} %,其余是磁盘上不存在或无权限的虚拟源,不参与判定)`)
|
||
console.log(' 解析口径 ' + BASE_LABELS.map((l, i) => `${l}:${hits[i]}`).join(' ') + ` 未解析:${unresolved}` +
|
||
`(规范基准=mapDir;其余是产物被移到项目树外时的退让,命中越多说明 map 与磁盘越脱节)`)
|
||
console.log(`ATTRIB_COVERAGE=${resolved}/${orphans.length} (${cov.toFixed(1)}%)`)
|
||
console.log(`ATTRIB_SUSPECT=${bad.length}`)
|
||
console.log(`ATTRIB_VERDICT=${verdict}`)
|
||
if (idle) {
|
||
console.log(` !! 覆盖率 ${cov.toFixed(1)} % < 门槛 ${COV_MIN} % ⇒ **本条自证空转,不作证据**` +
|
||
`(多半是产物不在项目树内或 node_modules 不在原位)。把产物放回 frontend/dist-sm 重跑,别引用本轮归属读数。`)
|
||
} else if (!bad.length) {
|
||
console.log(' (无)⇒ 本轮归属读数没有「源文件装不下」的伪像')
|
||
} else {
|
||
for (const o of bad.slice(0, 10)) {
|
||
console.log(` ${kb(o.w)} kB 记给 ${o.src.split('node_modules/').pop()},磁盘只有 ${kb(o.disk)} kB(×${o.ratio.toFixed(1)})in ${o.chunk}〔磁盘基准 ${o.base >= 0 ? BASE_LABELS[o.base] : '?'}〕`)
|
||
}
|
||
console.log(` 共 ${bad.length} 条,合计虚高 ${kb(bad.reduce((a, o) => a + o.over, 0))} kB` +
|
||
` ⇒ 这些数字只能当「产物列区间」读,不能当「该依赖的体积」引用`)
|
||
}
|
||
if (SOURCE_CHECK_STRICT && (idle || bad.length)) {
|
||
console.log(`\n!! SOURCE_CHECK_STRICT=1:自证判定 ${verdict} ⇒ 退出码 1(不设该变量时本尺恒退出 0,见 nex.config.yaml)`)
|
||
process.exitCode = 1
|
||
}
|
||
}
|