# 第一阶段：构建项目
FROM node:16-alpine AS builder

# 设置工作目录
WORKDIR /app

# 替换国内镜像源（加速依赖安装）
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# 复制依赖文件（优先复制，利用 Docker 缓存）
COPY package.json package-lock.json ./

# 安装依赖（使用淘宝镜像加速）
RUN npm config set registry https://registry.npmmirror.com \
    && npm install --legacy-peer-deps

# 复制源代码
COPY . .

# 构建生产产物
RUN npm run build

# 第二阶段：部署到 Nginx
FROM nginx:1.21-alpine

# 复制构建产物到 Nginx 静态目录
COPY --from=builder /app/dist /usr/share/nginx/html

# 复制自定义 Nginx 配置（解决单页应用路由问题）
COPY nginx.conf /etc/nginx/conf.d/default.conf

# 暴露端口
EXPOSE 8333

# 启动 Nginx
CMD ["nginx", "-g", "daemon off;"]
