# =====================================================================
# 前端镜像：pnpm 构建静态资源 -> Nginx 托管（同时反代 /api 到后端）
# 构建：docker build -t lab-frontend ./frontend
#
# 国内加速：npm/pnpm 使用 npmmirror（淘宝镜像）
# 如需走官方源：docker build --build-arg NPM_REGISTRY=https://registry.npmjs.org -t lab-frontend ./frontend
# =====================================================================

# ---------- 构建阶段 ----------
FROM node:24-alpine AS build
WORKDIR /app

# 使用 npmmirror 加速 npm 全局安装与依赖拉取
ARG NPM_REGISTRY=https://registry.npmmirror.com
RUN npm config set registry "$NPM_REGISTRY" \
    && npm install -g pnpm@11.3.0 \
    && pnpm config set registry "$NPM_REGISTRY"

# 先装依赖，充分利用层缓存
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
RUN pnpm install --frozen-lockfile --registry="$NPM_REGISTRY"

COPY . .
RUN pnpm build

# ---------- 运行阶段 ----------
FROM nginx:1.27-alpine

COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/dist /usr/share/nginx/html

EXPOSE 80

HEALTHCHECK --interval=15s --timeout=3s --start-period=10s --retries=3 \
    CMD wget -q -O /dev/null http://127.0.0.1/ || exit 1
