FROM node:20-alpine AS builder

WORKDIR /app

# 配置 npm 淘宝镜像
RUN npm config set registry https://registry.npmmirror.com

# 先复制 package.json 和 package-lock.json，利用 Docker 层缓存
COPY frontend/package.json frontend/package-lock.json* ./

# 使用缓存卷安装依赖（不复制源代码时即可缓存依赖层）
RUN --mount=type=cache,target=/root/.npm \
    if [ -f package-lock.json ]; then npm ci; else npm install; fi

# 复制所有源代码
COPY frontend ./

# 构建（使用缓存的依赖）
RUN npm run build

FROM nginx:1.29 AS runner

COPY --from=builder /app/dist /opt/frontend
COPY deployment/docker/datamate/backend.conf /etc/nginx/conf.d/backend.conf

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && rm -f /etc/nginx/conf.d/default.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
