Files

104 lines
3.5 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 || "touristappid";
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 templateApp = JSON.parse(
fs.readFileSync(path.join(template, "miniapp/app.json"), "utf8"),
) as { tabBar?: { color?: string; selectedColor?: string; backgroundColor?: string } };
const brand = process.env.BRAND_SLUG || manifest.defaultBrandSlug || "default-brand";
const buildOutputRoot =
process.env.MINIAPP_OUTPUT_ROOT || `dist/${key}/${process.env.TARO_ENV || "weapp"}`;
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" : buildOutputRoot,
framework: "react",
compiler: { type: "vite" },
plugins: [],
alias: {
"@template": previewAllTemplates
? path.resolve(__dirname, "../src/runtime/template-preview-entry.tsx")
: path.join(template, "taro"),
},
defineConstants: {
TEMPLATE_TAB_STYLE: JSON.stringify(templateApp.tabBar || {}),
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: `${buildOutputRoot}/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,
},
},
},
},
});