Files
persionalBrand/apps/taro/config/index.ts
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

100 lines
3.2 KiB
TypeScript

import { defineConfig } from "@tarojs/cli";
import path from "node:path";
import fs from "node:fs";
const root = path.resolve(__dirname, "../../..");
const previewAllTemplates = process.env.PREVIEW_ALL_TEMPLATES === "true";
const templates = fs
.readdirSync(path.join(root, "packages"))
.filter(
(directory) =>
directory.startsWith("template-") &&
fs.existsSync(path.join(root, "packages", directory, "taro/index.tsx")) &&
fs.existsSync(path.join(root, "packages", directory, "template.json")),
)
.map((directory) => directory.slice("template-".length))
.sort();
const key = process.env.TEMPLATE_KEY || templates[0];
if (!key) throw new Error("No Taro template package found");
if (!templates.includes(key)) throw new Error(`Unsupported Taro template: ${key}`);
process.env.TARO_APP_ID ||= process.env.WEAPP_APP_ID;
const template = path.join(root, "packages", `template-${key}`);
const manifest = JSON.parse(fs.readFileSync(path.join(template, "template.json"), "utf8")) as {
packageKey: string;
defaultBrandSlug?: string;
};
if (manifest.packageKey !== key) throw new Error(`Template key mismatch: ${key}`);
const brand = process.env.BRAND_SLUG || manifest.defaultBrandSlug || "default-brand";
fs.cpSync(path.join(template, "miniapp/assets"), path.resolve(__dirname, "../src/assets"), {
recursive: true,
});
export default defineConfig({
projectName: "personal-brand-miniapp",
date: "2026-10-08",
designWidth: 750,
deviceRatio: { 750: 1 },
sourceRoot: "src",
outputRoot: previewAllTemplates
? "dist/template-preview/h5"
: `dist/${key}/${process.env.TARO_ENV || "weapp"}`,
framework: "react",
compiler: { type: "vite" },
plugins: [],
alias: {
"@template": previewAllTemplates
? path.resolve(__dirname, "../src/runtime/template-preview-entry.tsx")
: path.join(template, "taro"),
},
defineConstants: {
TEMPLATE_KEY: JSON.stringify(key),
BRAND_SLUG: JSON.stringify(brand),
API_BASE_URL: JSON.stringify(process.env.API_BASE_URL || "http://localhost:3001"),
TEMPLATE_MANIFEST: JSON.stringify(manifest),
PREVIEW_ALL_TEMPLATES: JSON.stringify(previewAllTemplates),
},
copy: {
options: {},
patterns: [
...(previewAllTemplates
? templates.map((templateKey) => ({
from: path.join(root, "packages", `template-${templateKey}`, "miniapp/assets"),
to: `dist/template-preview/h5/assets/${templateKey}`,
}))
: []),
...(!previewAllTemplates
? [
{
from: path.join(template, "miniapp/assets"),
to: `dist/${key}/${process.env.TARO_ENV || "weapp"}/assets`,
},
]
: []),
],
},
mini: {
postcss: {
htmltransform: {
enable: false,
},
},
},
h5: {
publicPath: process.env.PREVIEW_PUBLIC_PATH || "/",
router: { mode: "hash" },
devServer: {
host: "0.0.0.0",
port: Number(process.env.PREVIEW_PORT || 3002),
proxy: {
"/media": {
target:
process.env.MEDIA_API_TARGET ||
(process.env.API_BASE_URL?.startsWith("http")
? new URL(process.env.API_BASE_URL).origin
: "http://localhost:3001"),
changeOrigin: true,
},
},
},
},
});