feat: 遮蔽产物中的 Taro 指纹

H5 预览与小程序产物会带出 taro_tmpl、taro-*-core、taro.js、--taro-button-height、
project.config.json 的 projectname 等指纹,而 Taro 4.3 没有隐藏这些名称的配置。
改为在 build:h5、build:weapp、build:preview 之后按 apps/taro/config/mask.config.json
做构建后处理:

- 同一张替换表同时作用于文件名与文本内容,重命名后引用保持同步,长度 ≥ 40 的 base64/哈希片段原样保留;
- 遮蔽后仍命中 forbidden 词表时构建失败,并把 project.config.json 的 projectname/description 中性化;
- dev:h5、dev:preview 保持原始命名,便于本地调试;改架构名只改 mask.config.json;
- 产物契约测试先用 unmask() 还原再断言,浏览器测试选择器写成 :is(<源码名>, <遮蔽名>)。

取舍与迁移见 docs/adr/0025-build-output-stack-masking.md。
This commit is contained in:
que01 committed 2026-10-13 23:03:32 +08:00
1 parent 53713e5935
commit 9ad2776515
13 files changed
+382 -10

No files matched your search

+22
View File
@@ -0,0 +1,22 @@
import fs from "node:fs";
import path from "node:path";
const configPath = path.join(import.meta.dirname, "../config/mask.config.json");
export const maskConfig = JSON.parse(fs.readFileSync(configPath, "utf8"));
const entries = Object.entries(maskConfig.replacements).sort(([a], [b]) => b.length - a.length);
const reverseEntries = entries
.map(([from, to]) => [to, from])
.sort(([a], [b]) => b.length - a.length);
/** 按遮蔽配置推导名称,测试与构建共用同一张表,避免两边写死而不同步。 */
export function masked(name) {
if (!maskConfig.enabled) return name;
return entries.reduce((result, [from, to]) => result.split(from).join(to), name);
}
/** 把制品中的名称还原成源码名称,让产物契约断言不依赖产物是否已经遮蔽。 */
export function unmask(name) {
if (!maskConfig.enabled) return name;
return reverseEntries.reduce((result, [from, to]) => result.split(from).join(to), name);
}