mirror of
https://github.com/YuleBest/FontMM.git
synced 2026-12-04 05:44:50 +08:00
打包耗时几乎全在压缩, 其中 system/fonts/ (131MB 原始 / 71MB 压缩) 占整包 压缩量的约 70%, 而这部分只在发布新字体时才变化。现将该子树预先压缩并缓存, 后续打包直接搬运已压缩数据 (zip.js passThrough, 不重新 deflate)。 本地构建 28s -> 13s (~2.2x), 产物不变。 实现: - dev/lib/zipcache.mjs: 缓存片段读写。指纹为「文件名 + 大小 + mtime + 权限位」, 任一项变化即失效; 缓存目录 dev/.cache/ 不入库 - dev/lib/zip.mjs: packDir 支持 passThrough 复用缓存条目。此处有个易错点 —— zip.js 把压缩级别编码进 zip 通用标志位 (bit 1-2), passThrough 时若不传 level, 写出的标志位与现场压缩不同, 会导致整包哈希漂移 (已修复并验证) - dev/pack.mjs: 命中则复用, 未命中现场压缩后自动建立缓存; 新增 --no-cache - dev/cache.mjs: status / clean / test 三个管理命令 - CI 构建加 --no-cache (一次性构建无收益, 也避免引入与构建机相关的状态) 缓存是纯本地的构建加速, 不影响产物也不参与分发: - 命中缓存与不使用缓存的 zip 逐字节相同 (以 SOURCE_DATE_EPOCH 固定时间戳验证) - 缓存缺失 / 索引损坏 / 数据文件截断 / 格式版本不符, 四种情况均回退现场压缩 (已逐一测试), 不会导致构建失败 dev/cache.mjs test 可校验缓存内容与源文件逐一相符。
77 lines
2.6 KiB
JavaScript
77 lines
2.6 KiB
JavaScript
#!/usr/bin/env node
|
|
// dev/cache.mjs — 预压缩缓存管理
|
|
//
|
|
// 用法:
|
|
// node dev/cache.mjs status 查看缓存状态 (片段/大小/文件数/建立时间)
|
|
// node dev/cache.mjs clean 清空缓存
|
|
// node dev/cache.mjs test 校验缓存可用性: 用缓存重建一遍并与源文件逐条比对
|
|
//
|
|
// 缓存由 dev/pack.mjs 在打包时自动建立与复用; 本脚本仅用于查看/清理/校验。
|
|
import fsp from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import { log, die } from './lib/log.mjs';
|
|
import { ROOT, SRC_DIR } from './lib/paths.mjs';
|
|
import { indexZip, hashFile } from './lib/zip.mjs';
|
|
import { cacheStatus, clearCache, CACHE_DIR } from './lib/zipcache.mjs';
|
|
|
|
const cmd = process.argv[2] ?? 'status';
|
|
const mb = (n) => `${(n / 1048576).toFixed(1)}MB`;
|
|
|
|
if (cmd === 'status') {
|
|
const list = await cacheStatus();
|
|
if (list.length === 0) {
|
|
log.info(`无缓存 (${path.relative(ROOT, CACHE_DIR)} 不存在或为空)`);
|
|
log.detail('下次运行 pnpm run pack 会自动建立');
|
|
process.exit(0);
|
|
}
|
|
log.step(`缓存目录: ${path.relative(ROOT, CACHE_DIR)}`);
|
|
for (const c of list) {
|
|
const flag = c.usable ? '✓ 可用' : '✗ 不可用 (将回退现场压缩)';
|
|
log.detail(`[${c.segment}] ${flag}`);
|
|
log.detail(` 大小 ${mb(c.size)} 含 ${c.files} 个文件 建立于 ${c.createdAt ?? '未知'}`);
|
|
}
|
|
process.exit(0);
|
|
}
|
|
|
|
if (cmd === 'clean') {
|
|
await clearCache();
|
|
log.ok('缓存已清空');
|
|
process.exit(0);
|
|
}
|
|
|
|
if (cmd === 'test') {
|
|
// 校验: 把缓存里的每个条目解压出来, 与源文件比对内容
|
|
const list = await cacheStatus();
|
|
if (list.length === 0) die('无缓存可校验');
|
|
let failures = 0;
|
|
for (const c of list.filter((x) => x.usable)) {
|
|
const segZip = path.join(CACHE_DIR, `${c.segment}.zip`);
|
|
log.step(`校验片段 ${c.segment} (${mb(c.size)})...`);
|
|
const index = await indexZip(segZip);
|
|
let checked = 0;
|
|
for (const [rel, info] of index) {
|
|
if (info.isDir) continue;
|
|
const src = path.join(SRC_DIR, rel);
|
|
let expected;
|
|
try {
|
|
expected = hashFile(src);
|
|
} catch {
|
|
log.fail(`源文件不存在: ${rel}`);
|
|
failures++;
|
|
continue;
|
|
}
|
|
if (expected !== info.sha256) {
|
|
log.fail(`内容不一致: ${rel}`);
|
|
failures++;
|
|
}
|
|
checked++;
|
|
}
|
|
log.ok(` ${c.segment}: ${checked} 个文件全部与源文件一致`);
|
|
}
|
|
if (failures > 0) die(`${failures} 项校验失败`);
|
|
log.ok('缓存校验通过');
|
|
process.exit(0);
|
|
}
|
|
|
|
die(`未知命令: ${cmd} (可用: status / clean / test)`);
|