fix: 缺少 npm 构建产物时模板产物校验只告警

This commit is contained in:
que01 committed 2026-10-21 12:16:00 +08:00
1 parent 2325214078
commit 784b9cbac8
2 files changed
+45 -3

No files matched your search

+7 -3
View File
@@ -299,7 +299,7 @@ function resolveRequire(output, fromFile, reference) {
}
/** 校验产物可以被微信开发者工具直接打开:页面、组件、require、图标和模板隔离。 */
function validateOutput(output, appJson, packageKey) {
export function validateOutput(output, appJson, packageKey) {
const errors = [];
const warnings = [];
for (const page of appJson.pages) {
@@ -325,11 +325,15 @@ function validateOutput(output, appJson, packageKey) {
if (file.endsWith(".json") && (file.startsWith("pages/") || file.startsWith("components/"))) {
const usingComponents = JSON.parse(source).usingComponents || {};
for (const [name, reference] of Object.entries(usingComponents)) {
if (reference.startsWith("/miniprogram_npm/") && !hasNpm) {
if (reference.startsWith("plugin://")) continue;
// miniprogram_npm 由微信开发者工具生成且不入库;缺失时只能提示,不能把干净检出判为失败。
const needsNpm =
reference.startsWith("/miniprogram_npm/") ||
(!reference.startsWith("/") && !reference.startsWith("."));
if (needsNpm && !hasNpm) {
warnings.push(`${file}: ${name} needs miniprogram_npm (build npm in apps/miniapp first)`);
continue;
}
if (reference.startsWith("plugin://")) continue;
if (!resolveComponent(output, file, reference))
errors.push(`${file}: component ${name} cannot be resolved (${reference})`);
}
@@ -5,6 +5,7 @@ import path from "node:path";
import { execFileSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import test from "node:test";
import { validateOutput } from "../src/cli.mjs";
const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const workspaceRoot = path.resolve(packageRoot, "../..");
@@ -59,6 +60,43 @@ test("compiler generates an isolated Acheng runtime manifest", () => {
}
});
test("干净检出缺少 miniprogram_npm 时 npm 组件引用只提示不报错", () => {
const output = fs.mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-npm-"));
try {
fs.mkdirSync(path.join(output, "pages/index"), { recursive: true });
fs.mkdirSync(path.join(output, "components/ui/brand-button"), { recursive: true });
for (const extension of [".js", ".json", ".wxml", ".wxss"]) {
fs.writeFileSync(
path.join(output, "pages/index/index" + extension),
extension === ".json" ? "{}" : "",
);
}
fs.writeFileSync(
path.join(output, "components/ui/brand-button/brand-button.json"),
JSON.stringify({ usingComponents: { "t-button": "tdesign-miniprogram/button/button" } }),
);
const app = {
pages: ["pages/index/index"],
tabBar: { list: [{ pagePath: "pages/index/index" }] },
};
const withoutNpm = validateOutput(output, app, "fixture-template");
assert.deepEqual(withoutNpm.errors, []);
assert.ok(
withoutNpm.warnings.some((warning) => warning.includes("t-button needs miniprogram_npm")),
);
// 微信开发者工具构建出 miniprogram_npm 后,仍必须严格校验组件是否真实存在。
fs.mkdirSync(path.join(output, "miniprogram_npm/tdesign-miniprogram/button"), {
recursive: true,
});
const withNpm = validateOutput(output, app, "fixture-template");
assert.ok(withNpm.errors.some((error) => error.includes("tdesign-miniprogram/button/button")));
} finally {
fs.rmSync(output, { recursive: true, force: true });
}
});
test("compiler generates the Linjing navigation contract", () => {
const output = fs.mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-"));
execFileSync(process.execPath, [compiler, "--template", "linjing-editorial", "--out", output], {