# syntax=docker/dockerfile:1
# =============================================================================
# PERLER LAB — 前端 Dockerfile（多阶段构建）
# 构建阶段：node:20-alpine + Vite 构建 React 应用 → dist/
# 运行阶段：nginx:alpine 提供静态资源服务（SPA 路由回退由 nginx.conf 处理）
# =============================================================================

# ── 第一阶段：构建 ──
FROM node:20-alpine AS build

WORKDIR /app

# 复制依赖清单（有 package-lock.json，使用 npm ci 保证可复现）
COPY package.json package-lock.json ./
RUN npm ci --no-audit --no-fund

# 复制 Vite/TS 配置与 HTML 入口
COPY tsconfig.json vite.config.ts index.html ./

# 复制源码与静态资源（public/ 由 Vite 在构建时合并进 dist/）
COPY src ./src
COPY public ./public

# 构建 React 应用 → dist/（与 package.json 的 build 脚本一致：vite build）
RUN npm run build

# ── 第二阶段：Nginx 运行 ──
FROM nginx:1.27-alpine

# 复制 nginx 配置
COPY nginx.conf /etc/nginx/conf.d/default.conf

# 复制构建产物（dist/ 含 index.html + assets/）到 nginx 根目录
COPY --from=build /app/dist /usr/share/nginx/html

# 健康检查
HEALTHCHECK --interval=30s --timeout=5s --retries=3 --start-period=10s \
    CMD wget -qO- http://localhost:80/healthz || exit 1

EXPOSE 80
