FROM node:20-alpine as builder

WORKDIR /app

# 设置阿里云源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# 设置 yarn 阿里云源并安装依赖
COPY web/package*.json web/yarn.lock* ./
RUN yarn config set registry https://registry.npmmirror.com && \
    yarn install

# 复制源代码
COPY web/ .

# 设置构建时环境变量
ARG VITE_API_BASE_URL=/api
ENV VITE_API_BASE_URL=$VITE_API_BASE_URL

# 构建应用
RUN yarn build

# 使用nginx提供静态文件
FROM nginx:alpine

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

EXPOSE 80 443

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