refactor: 统一模板契约并修复预览版本

This commit is contained in:
que01 committed 2026-10-18 17:09:09 +08:00
1 parent cd2992c00a
commit 50b87e48de
56 files changed
+7849 -5756

No files matched your search

+6 -3
View File
@@ -1,9 +1,12 @@
export function resolvePreviewRenderer<T>(
modules: Record<string, T>,
config: unknown,
workspace: unknown,
): T | undefined {
if (!config || typeof config !== "object" || Array.isArray(config)) return undefined;
const key = (config as Record<string, unknown>).visualTemplateKey;
if (!workspace || typeof workspace !== "object" || Array.isArray(workspace)) return undefined;
const visualTemplate = (workspace as Record<string, unknown>).visualTemplate;
if (!visualTemplate || typeof visualTemplate !== "object" || Array.isArray(visualTemplate))
return undefined;
const key = (visualTemplate as Record<string, unknown>).key;
if (typeof key !== "string" || !key) return undefined;
const suffix = `/template-${key}/taro/index.tsx`;
return Object.entries(modules).find(([path]) => path.endsWith(suffix))?.[1];
@@ -9,7 +9,7 @@ const renderers = import.meta.glob<{
}>("../../../../packages/template-*/taro/index.tsx", { eager: true });
export function TemplatePage(props: TemplateProps) {
const rendererModule = resolvePreviewRenderer(renderers, props.workspace.brand.config);
const rendererModule = resolvePreviewRenderer(renderers, props.workspace);
if (!rendererModule)
return <div className="runtime-state">当前模板没有可用的 H5 预览 renderer</div>;
const Renderer = rendererModule.TemplatePage;
@@ -20,7 +20,6 @@ const brand = {
contactPhone: "13800138000",
contactWechat: "food-example",
config: {
visualTemplateKey: "acheng-campus-food",
founder: "测试主理人",
title: "私房菜主理人",
heroImage: "",
@@ -56,7 +55,11 @@ const runtimeWorkspace = {
founder: brand.config.founder,
title: brand.config.title,
visualPresentation: {},
visualTemplateKey: brand.config.visualTemplateKey,
},
visualTemplate: {
key: "acheng-campus-food",
version: "1.0.2",
name: "阿成校园餐",
},
templateData: {},
navigation: { tabs: [] },
+10 -7
View File
@@ -15,24 +15,27 @@ const modules = Object.fromEntries(
]),
);
test("多模板预览随当前品牌配置切换所有真实模板,重复切换不保留旧 renderer", () => {
test("多模板预览随 Runtime 模板声明切换所有真实 renderer", () => {
for (const manifest of [...manifests, ...manifests.reverse()]) {
assert.equal(
resolvePreviewRenderer(modules, { visualTemplateKey: manifest.packageKey })?.key,
resolvePreviewRenderer(modules, {
visualTemplate: { key: manifest.visualTemplateKey },
})?.key,
manifest.packageKey,
);
}
});
test("缺少或未知模板时不回落到首个模板", () => {
for (const config of [
test("缺少或未知 Runtime 模板声明时不回落到首个模板", () => {
for (const workspace of [
undefined,
null,
[],
{},
{ visualTemplateKey: "missing" },
{ visualTemplateKey: 1 },
{ brand: { config: { visualTemplateKey: "acheng-campus-food" } } },
{ visualTemplate: { key: "missing" } },
{ visualTemplate: { key: 1 } },
]) {
assert.equal(resolvePreviewRenderer(modules, config), undefined);
assert.equal(resolvePreviewRenderer(modules, workspace), undefined);
}
});