FROM node:14-alpine AS builder

WORKDIR /app

# 系统依赖配置
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
    && apk add --no-cache python3 make g++

# 只复制package.json（避免lockfile版本冲突）
COPY package.json ./

# 关键：先验证版本是否存在，不存在则切换官方源重试
RUN npm config set registry https://registry.npmmirror.com \
    && npm config set disturl https://npmmirror.com/dist \
    # 验证uni-app版本是否存在，不存在则切换官方源
    && (npm view @dcloudio/uni-app@2.0.0-30920220418001 version >/dev/null 2>&1 || npm config set registry https://registry.npmjs.org) \
    && npm install --legacy-peer-deps

# 复制源代码并构建
COPY . .
RUN npm run build:h5

# 生产环境
FROM nginx:1.21-alpine
COPY --from=builder /app/dist/build/h5 /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 8333
CMD ["nginx", "-g", "daemon off;"]
