Files
UHD_Media_Tool/apps/web/vite.config.ts
T
rootandClaude Opus 5.5 11ca2f01ff chore: 一键检查、发布脚本与开发环境约定
- deno task test:all(scripts/test-all.sh):逐包跑完再汇总,连不上测试库直接失败,
  web 用 Node 跑 vitest;deno task ci 改用它
- deno task release(scripts/release.sh):工作区不干净、CHANGELOG 无本版条目即拒绝;
  打 tag、构建、只替换 app、核对健康接口版本
- deno task dev:up:起开发库并迁移到最新;开发 API 端口改 4390(4380 留给本机部署版)
- CLAUDE.md:命令、提交与发布约定、文档放哪

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-02 16:08:09 +08:00

68 lines
2.2 KiB
TypeScript

import fs from "node:fs";
import path from "node:path";
import { reactRouter } from "@react-router/dev/vite";
import tailwindcss from "@tailwindcss/vite";
import { defineConfig, loadEnv, type Plugin } from "vite";
/** 读取 git 短哈希(不依赖 git 命令) */
function readGitShortSha(cwd: string): string | null {
try {
const gitDir = path.join(cwd, ".git");
const head = fs.readFileSync(path.join(gitDir, "HEAD"), "utf8").trim();
let sha = head;
if (head.startsWith("ref: ")) {
const ref = head.slice(5);
const refFile = path.join(gitDir, ref);
if (fs.existsSync(refFile)) {
sha = fs.readFileSync(refFile, "utf8").trim();
} else {
const packed = fs.readFileSync(path.join(gitDir, "packed-refs"), "utf8");
const line = packed.split("\n").find((l) => l.endsWith(` ${ref}`));
if (!line) return null;
sha = line.split(" ")[0] ?? "";
}
}
return /^[0-9a-f]{40}$/.test(sha) ? sha.slice(0, 7) : null;
} catch {
return null;
}
}
/** 构建时注入 __APP_VERSION__ 并输出 version.json;优先 APP_VERSION,其次 git 短哈希,最后时间戳 */
function appVersion(): Plugin {
const repoRoot = path.resolve(process.cwd(), "../..");
const version = process.env.APP_VERSION?.trim() || readGitShortSha(repoRoot) || `build-${Date.now().toString(36)}`;
return {
name: "app-version",
config() {
return { define: { __APP_VERSION__: JSON.stringify(version) } };
},
generateBundle() {
this.emitFile({
type: "asset",
fileName: "version.json",
source: JSON.stringify({ version, builtAt: new Date().toISOString() }),
});
},
};
}
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), "");
const apiTarget = env.VITE_API_TARGET || "http://localhost:4390";
return {
plugins: [tailwindcss(), reactRouter(), appVersion()],
resolve: { tsconfigPaths: true },
server: {
port: 5180,
strictPort: true,
// 依赖装在仓库根 node_modules/.deno(deno workspace),vite 找不到 npm workspace 根,需显式放行
fs: { allow: [path.resolve(process.cwd(), "../..")] },
proxy: {
"/api": { target: apiTarget, changeOrigin: true },
},
},
};
});