# ========== 阶段1：构建前端 ==========
FROM node:20-alpine AS builder

RUN npm install -g pnpm

WORKDIR /app

# 先拷贝依赖文件，利用 Docker 缓存层
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile

# 拷贝源码并构建
COPY . .
RUN pnpm run build

# ========== 阶段2：生产运行 ==========
FROM node:20-alpine AS runner

RUN npm install -g pnpm

WORKDIR /app

# 只拷贝生产需要的文件
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile --prod && pnpm store prune

# 拷贝 Express 服务代码
COPY server/ ./server/

# 从构建阶段拷贝前端产物
COPY --from=builder /app/dist ./dist

EXPOSE 5175 5173

ENV NODE_ENV=production
ENV VITE_SERVICE_PORT=5175

CMD ["node", "server/index.js"]