This commit is contained in:
vet
2026-04-21 12:24:04 +07:00
parent 67ab5f5ba5
commit 6eb89ad2a9
15 changed files with 421 additions and 170 deletions

View File

@@ -1,20 +1,19 @@
#!/usr/bin/env bash
# =============================================================================
# 07-start-frontend.sh — 启动前端开发服务器
# 07-start-frontend.sh — 启动前端开发服务器(开发态)
#
# 用法:
# ./07-start-frontend.sh # 启动全部前端项目
# ./07-start-frontend.sh <project> # 只启动指定项目
#
# 项目与端口:
# pc → 默认 yarn dev:web :5173无 Electron本机要 Electron 时设 PC_ELECTRON=1 使用 yarn dev
# 后端地址:在 .env.deploy-test 设 PC_BACKEND_ORIGIN启动时注入 VITE_*(不改 pc 目录)
# 域名入口由 Nginx 代理到 127.0.0.1:5173
# meetingh5 → React + Vite :5188
# meetingh5 → Vue + Vite :5188
# h5 → Vue + Vite :3003
# cms → UMI Max :8001
# build-cms → UMI Max :8002
# build-down → UMI v3 :8003
#
#
# 注意:
# pc / cms / build-cms / build-down 已改为静态构建 + Nginx 域名访问:
# ./deploy-test/08-build-static-frontend.sh
#
# 日志文件: .local-dev/logs/fe-<project>.log
# PID 文件: .local-dev/pids/fe-<project>.pid
@@ -41,66 +40,35 @@ header "启动前端开发服务器"
# ── 前端服务配置 ──────────────────────────────────────────────────────────────
# name → (目录, 包管理器, 启动命令, 环境变量, 端口描述)
declare -A FE_DIR=(
[pc]="pc"
[meetingh5]="meetingh5"
[h5]="h5"
[cms]="cms"
[build-cms]="build-cms"
[build-down]="build-down"
)
declare -A FE_PM=(
[pc]="yarn"
[meetingh5]="npm"
[h5]="npm"
[cms]="pnpm"
[build-cms]="pnpm"
[build-down]="npm"
)
declare -A FE_CMD=(
[pc]="yarn dev:web"
[meetingh5]="npm run dev"
[h5]="npm run dev"
[cms]="./node_modules/.bin/max dev --host 0.0.0.0 --port 8001"
[build-cms]="./node_modules/.bin/max dev --host 0.0.0.0 --port 8002"
[build-down]="./node_modules/.bin/umi dev --host 0.0.0.0 --port 8003"
)
# cms/build-cms/build-down 不配置端口时默认都是 8000需手动指定
declare -A FE_ENV=(
[pc]=""
[meetingh5]=""
[h5]=""
[cms]="REACT_APP_ENV=dev MOCK=none UMI_ENV=dev HOST=0.0.0.0 PORT=8001"
[build-cms]="MOCK=none UMI_ENV=dev HOST=0.0.0.0 PORT=8002"
[build-down]="NODE_OPTIONS=--openssl-legacy-provider HOST=0.0.0.0 PORT=8003"
)
declare -A FE_PORT=(
[pc]=":5173 (yarn dev:web)"
[meetingh5]=":5188"
[h5]=":3003"
[cms]=":8001"
[build-cms]=":8002"
[build-down]=":8003"
)
declare -A FE_PORT_NUM=(
[pc]="5173"
[meetingh5]="5188"
[h5]="3003"
[cms]="8001"
[build-cms]="8002"
[build-down]="8003"
)
# 本机需 Electron 桌面客户端时PC_ELECTRON=1 → yarn dev需 GUI / libatk 等)
if [[ "${PC_ELECTRON:-}" == "1" ]] || [[ "${PC_ELECTRON:-}" == "true" ]]; then
FE_CMD[pc]="yarn dev"
FE_PORT[pc]=":5173 (yarn dev + Electron)"
fi
_check_fe_port_free() {
local name="$1"
local port="${FE_PORT_NUM[$name]}"
@@ -164,11 +132,6 @@ _start_fe() {
# 已在运行
if [[ -f "$pidfile" ]] && kill -0 "$(cat "$pidfile")" 2>/dev/null; then
warn "$name 已在运行 (端口=$port, PID=$(cat "$pidfile")),跳过"
if [[ "$name" == "pc" ]]; then
warn " Vite 环境变量只在启动时读取;若仍请求旧地址,请执行: ./deploy-test/stop-frontend.sh pc && ./deploy-test/07-start-frontend.sh pc"
pc_export_vite_backend_env
pc_print_vite_backend_env
fi
return 0
fi
@@ -191,13 +154,6 @@ _start_fe() {
fi
info "启动 ${BOLD}$name${NC} (端口=$port) ..."
if [[ "$name" == "pc" ]]; then
pc_ensure_wasm_assets
pc_export_vite_backend_env
pc_print_vite_backend_env
pc_check_nginx_gateway
fi
# 写日志分隔符
{
echo ""
@@ -207,9 +163,6 @@ _start_fe() {
# 后台启动(带环境变量前缀)
(
cd "$dir"
if [[ "$name" == "pc" ]]; then
pc_export_vite_backend_env
fi
if [[ -n "$env_prefix" ]]; then
# shellcheck disable=SC2086
nohup env $env_prefix $cmd >> "$logfile" 2>&1 &
@@ -235,10 +188,6 @@ _start_fe() {
return 1
fi
success "$name (端口=$port, PID=$(cat "$pidfile")) ${FE_PORT[$name]}$logfile"
if [[ "$name" == "pc" ]]; then
pc_check_wasm_assets "${PC_BACKEND_ORIGIN}"
info " PC 浏览器入口建议使用: ${PC_BACKEND_ORIGIN%/}/"
fi
else
error "$name 启动失败 (端口=$port),查看日志:"
tail -20 "$logfile" 2>/dev/null || true
@@ -248,7 +197,8 @@ _start_fe() {
# ── 入口 ─────────────────────────────────────────────────────────────────────
TARGET="${1:-all}"
FE_PROJECTS=(pc meetingh5 h5 cms build-cms build-down)
FE_PROJECTS=(meetingh5 h5)
STATIC_FRONTENDS=(pc cms build-cms build-down)
_all_valid() {
for p in "${FE_PROJECTS[@]}"; do
@@ -279,11 +229,7 @@ if [[ "$TARGET" == "all" ]]; then
echo ""
echo -e "${BOLD}访问地址:${NC}"
echo " PC: ${PC_BACKEND_ORIGIN%/}/ (外部 HTTPS → 本机 Nginx :80 → pc dev:web :5173)"
echo " PC 直连调试: http://${DEPLOY_TEST_IP}:5173 (绕过 NginxElectron 用 PC_ELECTRON=1 或 pc 目录内 yarn dev)"
echo " H5: http://${DEPLOY_TEST_IP}:3003"
echo " CMS: http://${DEPLOY_TEST_IP}:8001"
echo " Build CMS: http://${DEPLOY_TEST_IP}:8002"
echo " Build Download: http://${DEPLOY_TEST_IP}:8003"
echo ""
echo -e "${BOLD}MeetingH5 访问地址(后端 URL 由 .env.local 默认设置,也可通过 URL 参数覆盖):${NC}"
@@ -298,9 +244,18 @@ if [[ "$TARGET" == "all" ]]; then
echo " 可能原因: node_modules 未安装,执行: ./deploy-test/06-install-frontend.sh"
fi
else
for static_fe in "${STATIC_FRONTENDS[@]}"; do
if [[ "$TARGET" == "$static_fe" ]]; then
error "$TARGET 已改为静态构建,不再通过 07-start-frontend.sh 启动"
echo "请执行: ./deploy-test/08-build-static-frontend.sh $TARGET"
echo "然后执行: sudo ./deploy-test/00-init-tools.sh nginx"
exit 1
fi
done
if ! _all_valid "$TARGET"; then
error "未知项目: $TARGET"
echo "可用: ${FE_PROJECTS[*]}"
echo "开发态前端可用: ${FE_PROJECTS[*]}"
echo "静态前端请用: ./deploy-test/08-build-static-frontend.sh [pc|cms|build-cms|build-down]"
exit 1
fi
step "启动前端项目: $TARGET"