chore: add optional bun frontend build

This commit is contained in:
boos
2026-05-25 22:05:03 +08:00
parent 3f9f9f7665
commit 6869504119
2 changed files with 45 additions and 4 deletions
+13
View File
@@ -360,6 +360,19 @@ npm run build
# 输出到 dist/web/,Rust 后端自动 serve # 输出到 dist/web/,Rust 后端自动 serve
``` ```
可选使用 Bun 加速主前端构建:
```bash
# 一次性使用 Bun 构建主前端
./build-and-run.sh --bun
# 或设置环境变量,auto 会优先使用可用的 bun,缺省回退 npm/npx
BOOSAPI_FRONTEND_PM=bun ./build-and-run.sh --no-run
BOOSAPI_FRONTEND_PM=auto ./build-and-run.sh
```
> Workflow UI 子模块仍按官方 `pnpm-lock.yaml` 使用 pnpm 构建,避免 ComfyUI 前端依赖兼容问题。
> 生产环境不需要 Node.js 进程。`npm run build` 后,Rust 后端直接 serve 静态文件。 > 生产环境不需要 Node.js 进程。`npm run build` 后,Rust 后端直接 serve 静态文件。
> 前端 API 请求通过 Vite 代理(开发)或同域访问(生产)指向 `localhost:4000`。 > 前端 API 请求通过 Vite 代理(开发)或同域访问(生产)指向 `localhost:4000`。
+32 -4
View File
@@ -21,6 +21,7 @@ NO_RUN=0
FORCE_FRONTEND=0 FORCE_FRONTEND=0
SKIP_FRONTEND=0 SKIP_FRONTEND=0
CARGO_FEATURES="${BOOSAPI_CARGO_FEATURES:-}" CARGO_FEATURES="${BOOSAPI_CARGO_FEATURES:-}"
FRONTEND_PM="${BOOSAPI_FRONTEND_PM:-auto}"
usage() { usage() {
cat <<'EOF' cat <<'EOF'
@@ -35,14 +36,17 @@ usage() {
--backend-only 只构建后端 --backend-only 只构建后端
--force-frontend 强制重建前端,忽略缓存 --force-frontend 强制重建前端,忽略缓存
--skip-frontend 跳过前端构建(沿用现有 dist/web) --skip-frontend 跳过前端构建(沿用现有 dist/web)
--bun 主前端使用 Bun 执行 Vite 构建(Workflow UI 子模块仍使用 pnpm)
--no-run 构建后不启动服务 --no-run 构建后不启动服务
BOOSAPI_BUILD_PROFILE 可设置默认 profile(dev / release / fast-release) BOOSAPI_BUILD_PROFILE 可设置默认 profile(dev / release / fast-release)
BOOSAPI_CARGO_FEATURES 可覆盖后端 Cargo features(默认:空) BOOSAPI_CARGO_FEATURES 可覆盖后端 Cargo features(默认:空)
BOOSAPI_FRONTEND_PM 可设置主前端构建器:auto / npm / bun(默认:auto)
-h, --help 显示帮助 -h, --help 显示帮助
示例: 示例:
./build-and-run.sh --release ./build-and-run.sh --release
./build-and-run.sh --fast-release ./build-and-run.sh --fast-release
./build-and-run.sh --bun
BOOSAPI_BUILD_PROFILE=release ./build-and-run.sh BOOSAPI_BUILD_PROFILE=release ./build-and-run.sh
EOF EOF
} }
@@ -61,6 +65,7 @@ while [ $# -gt 0 ]; do
--backend-only) BACKEND_ONLY=1 ;; --backend-only) BACKEND_ONLY=1 ;;
--force-frontend) FORCE_FRONTEND=1 ;; --force-frontend) FORCE_FRONTEND=1 ;;
--skip-frontend) SKIP_FRONTEND=1 ;; --skip-frontend) SKIP_FRONTEND=1 ;;
--bun) FRONTEND_PM="bun" ;;
--no-run) NO_RUN=1 ;; --no-run) NO_RUN=1 ;;
--rebuild-image) log "--rebuild-image 已废弃:内置 Docker ComfyUI 工作台已移除,忽略该参数" ;; --rebuild-image) log "--rebuild-image 已废弃:内置 Docker ComfyUI 工作台已移除,忽略该参数" ;;
-h|--help) usage; exit 0 ;; -h|--help) usage; exit 0 ;;
@@ -167,8 +172,24 @@ build_frontend() {
return 0 return 0
fi fi
if ! command -v npx >/dev/null 2>&1; then local frontend_pm="$FRONTEND_PM"
fail "未找到 npx,请先安装 Node.js" case "$frontend_pm" in
auto)
if command -v bun >/dev/null 2>&1; then
frontend_pm="bun"
else
frontend_pm="npm"
fi
;;
npm|bun) ;;
*) fail "未知 BOOSAPI_FRONTEND_PM: $frontend_pm(可选:auto / npm / bun)" ;;
esac
if [ "$frontend_pm" = "bun" ]; then
command -v bun >/dev/null 2>&1 || fail "未找到 bun,请安装 Bun 或移除 --bun"
else
command -v node >/dev/null 2>&1 || fail "未找到 node,请先安装 Node.js"
command -v npx >/dev/null 2>&1 || fail "未找到 npx,请先安装 Node.js"
fi fi
local fingerprint_file="$ROOT_DIR/dist/web/.boosapi-build-fingerprint" local fingerprint_file="$ROOT_DIR/dist/web/.boosapi-build-fingerprint"
@@ -176,10 +197,13 @@ build_frontend() {
fingerprint="$( fingerprint="$(
{ {
printf 'build-script-v2\n' printf 'build-script-v2\n'
printf 'frontend_pm=%s\n' "$frontend_pm"
printf 'node=' printf 'node='
node --version 2>/dev/null || true node --version 2>/dev/null || true
printf 'npx=' printf 'npx='
npx --version 2>/dev/null || true npx --version 2>/dev/null || true
printf 'bun='
bun --version 2>/dev/null || true
for path in package.json pnpm-lock.yaml package-lock.json yarn.lock vite.config.ts tsconfig.json tsconfig.web.json tsconfig.transpile.json src/web src/shared; do for path in package.json pnpm-lock.yaml package-lock.json yarn.lock vite.config.ts tsconfig.json tsconfig.web.json tsconfig.transpile.json src/web src/shared; do
[ -e "$ROOT_DIR/$path" ] && find "$ROOT_DIR/$path" -type f -print [ -e "$ROOT_DIR/$path" ] && find "$ROOT_DIR/$path" -type f -print
done | sort | xargs -r sha256sum done | sort | xargs -r sha256sum
@@ -194,9 +218,13 @@ build_frontend() {
return 0 return 0
fi fi
log "构建前端 (Vite)..." log "构建前端 (Vite via ${frontend_pm})..."
# 直接调用 vite build,跳过 desktop:icons(脚本文件不存在) # 直接调用 vite build,跳过 desktop:icons(脚本文件不存在)
npx vite build if [ "$frontend_pm" = "bun" ]; then
bunx --bun vite build
else
npx vite build
fi
mkdir -p "$ROOT_DIR/dist/web" mkdir -p "$ROOT_DIR/dist/web"
printf '%s\n' "$fingerprint" > "$fingerprint_file" printf '%s\n' "$fingerprint" > "$fingerprint_file"
ok "前端构建完成 → dist/web/" ok "前端构建完成 → dist/web/"