#!/usr/bin/env node /** * 构建 web/ 下的 Vue 控制台,并把 JS/CSS 内联成单个 HTML 供二进制内嵌。 * * 前置:bun install --cwd web(仅开发机需要;生成物随 git 提交,CI 不需要) * 用法:bun run web:build */ import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs"; import { dirname, join, resolve } from "node:path"; import { execFileSync } from "node:child_process"; const root = resolve(import.meta.dirname, "../.."); const webDir = join(root, "web"); const outDir = join(root, ".webui-dist"); // 后缀用 .txt 而非 .html:Bun 对 .html 的 text import 会推断成 HTMLBundle, // 导致 tsc 报错;内容仍是完整 HTML,由 HTTP 层按 text/html 返回。 const target = join(root, "src", "packaging", "generated", "webui.txt"); if (!existsSync(join(webDir, "node_modules"))) { console.error("[build:webui] web/ 依赖未安装,请先执行: bun install --cwd web"); process.exit(1); } console.log("[build:webui] building Vue app..."); execFileSync("bun", ["run", "build"], { cwd: webDir, stdio: "inherit" }); const indexPath = join(outDir, "index.html"); if (!existsSync(indexPath)) { console.error(`[build:webui] 找不到构建产物: ${indexPath}`); process.exit(1); } let html = readFileSync(indexPath, "utf8"); const files = readdirSync(outDir); for (const file of files.filter((name) => name.endsWith(".css"))) { const css = readFileSync(join(outDir, file), "utf8"); html = html.replace(/]+href="[^"]*\.css"[^>]*>/g, () => ``); } for (const file of files.filter((name) => name.endsWith(".js"))) { const js = readFileSync(join(outDir, file), "utf8").replaceAll("", "<\\/script>"); html = html.replace(/]+src="[^"]*\.js"[^>]*><\/script>/g, () => ``); } if (/]+src=/.test(html) || /]+href="[^"]*\.css"/.test(html)) { console.warn("[build:webui] 警告: 仍有外链资源未被内联,页面在纯本地环境可能加载失败"); } mkdirSync(dirname(target), { recursive: true }); writeFileSync(target, html, "utf8"); console.log(`[build:webui] ✓ ${target} (${(html.length / 1024).toFixed(1)} KiB)`);