import { defineConfig } from "@tarojs/cli"; import path from "node:path"; import fs from "node:fs"; const root = path.resolve(__dirname, "../../.."); const key = process.env.TEMPLATE_KEY || "acheng-campus-food"; 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)); if (!templates.includes(key)) throw new Error(`Unsupported Taro template: ${key}`); 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-taro", 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}`, })) : []), ...(process.env.TARO_ENV === "weapp" ? [ { from: path.resolve(__dirname, "../project.config.json"), to: `dist/${key}/weapp/project.config.json`, }, ] : []), ...(!previewAllTemplates ? [ { from: path.join(template, "miniapp/assets"), to: `dist/${key}/${process.env.TARO_ENV || "weapp"}/assets`, }, ] : []), ], }, mini: {}, h5: { publicPath: process.env.PREVIEW_PUBLIC_PATH || "/", router: { mode: "hash" }, devServer: { host: "0.0.0.0", port: Number(process.env.PREVIEW_PORT || 3002) }, }, });