nex_animation/reports/ui_evidence/nex-p11/antdbytes.py

58 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""按 antd 子目录聚合产物字节(mappings 精确归属),找出「src 里没人用却坐在首屏」的那部分。"""
import base64, json, os, re, sys, collections
exec(open("reports/ui_evidence/nex-p11/chunkbytes.py").read().split("def analyze")[0].split('"""')[2])
js = "frontend/dist-sm/assets/ui-DeqJpVov.js"
m = json.load(open(js + ".map"))
srcs = m["sources"]
code = open(js, encoding="utf-8").read()
lines = code.split("\n")
raw = {}
def key(s):
mm = re.search(r"antd/es/([^/]+)/", s)
if mm: return "antd/" + mm.group(1)
if "antd/" in s: return "antd/other"
if "rc-picker" in s: return "rc-picker"
if "rc-tree" in s: return "rc-tree"
if "rc-cascader" in s: return "rc-cascader"
if "rc-color-picker" in s or "rc-picker" in s: return "rc-picker"
if "dayjs" in s: return "dayjs"
if "rc-table" in s: return "rc-table"
if "node_modules/" in s:
t = s.split("node_modules/", 1)[1].split("/")
return ("@".join(t[:2]) if t[0].startswith("@") else t[0])
return "src"
for li, line in enumerate(m["mappings"].split(";")):
if not line: continue
segs = []; gc = si = sl = sc = 0
for part in line.split(","):
if not part: continue
f = vlq(part); gc += f[0]
if len(f) >= 2: si += f[1]
if len(f) >= 3: sl += f[2]
if len(f) >= 4: sc += f[3]
if len(f) >= 2: segs.append((gc, si))
text = lines[li] if li < len(lines) else ""
for i, (g, s) in enumerate(segs):
end = segs[i+1][0] if i+1 < len(segs) else len(text)
raw.setdefault(srcs[s] if s < len(srcs) else "?", 0)
raw[srcs[s] if s < len(srcs) else "?"] += max(0, end - g)
agg = collections.Counter()
for s, b in raw.items(): agg[key(s)] += b
tot = sum(agg.values())
print(f"chunk raw={os.path.getsize(js)/1024:.1f} KiB attributable={tot/1024:.1f} KiB")
UNUSED = ["antd/tree","rc-tree","antd/date-picker","rc-picker","antd/calendar","antd/time-picker",
"antd/color-picker","antd/watermark","antd/popover","antd/descriptions","antd/statistic",
"antd/segmented","dayjs"]
for k, v in agg.most_common(40):
mark = " ← src 未使用" if k in ("antd/tree","rc-tree","antd/date-picker","rc-picker","antd/calendar","antd/time-picker","antd/color-picker","antd/watermark","dayjs") else ""
print(f"{v/1024:8.1f} KiB {k}{mark}")
print("\n== 疑似死重合计 ==")
dead = sum(v for k,v in agg.items() if k in ("antd/tree","rc-tree","antd/date-picker","rc-picker","antd/calendar","antd/time-picker","antd/color-picker","antd/watermark","dayjs"))
print(f"{dead/1024:.1f} KiB = {dead/tot*100:.1f} % of chunk")