Files
persionalBrand/apps/taro/test/build-contract.test.mjs
T
que01 9ad2776515 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。
2026-10-13 23:03:32 +08:00

45 lines
2.0 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { unmask } from "./mask-names.mjs";
const root = path.resolve(import.meta.dirname, "../../..");
const templateKeys = fs
.readdirSync(path.join(root, "packages"))
.filter((directory) => directory.startsWith("template-"))
.filter((directory) => fs.existsSync(path.join(root, "packages", directory, "template.json")))
.map((directory) => directory.slice("template-".length))
.sort();
for (const key of templateKeys)
test(`${key} 两端产物有真实入口及模板导航`, () => {
const manifest = JSON.parse(
fs.readFileSync(path.join(root, `packages/template-${key}/template.json`)),
);
const base = path.join(root, `apps/taro/dist/${key}`);
const config = JSON.parse(fs.readFileSync(path.join(base, "weapp/app.json")));
assert.equal(config.pages.length, 8);
assert.deepEqual(
config.tabBar.list.map((item) => item.text),
manifest.navigation.map((item) => item.label),
);
for (const page of config.pages)
for (const ext of ["js", "wxml", "json"])
assert.ok(fs.existsSync(path.join(base, "weapp", `${page}.${ext}`)), `${page}.${ext}`);
if (fs.existsSync(path.join(base, "h5/index.html")))
assert.ok(
fs.readFileSync(path.join(base, "h5/index.html"), "utf8").includes('type="module"'),
);
assert.ok(fs.existsSync(path.join(base, "weapp/project.config.json")));
const weappStyles = fs.readFileSync(path.join(base, "weapp/common.wxss"), "utf8");
const appStyles = unmask(fs.readFileSync(path.join(base, "weapp/app-origin.wxss"), "utf8"));
assert.ok(weappStyles.length > 1000);
assert.match(appStyles, /(^|})button\{/);
assert.match(appStyles, /--taro-button-height:\s*88rpx/);
assert.match(
appStyles,
/display:flex;align-items:center;justify-content:center;width:100%;min-height:var\(--taro-button-height\)/,
);
assert.doesNotMatch(appStyles, /(^|})\.h5-button\{/);
});