# 使用 Node 14 (兼容 Vue 2) - 切换到buster版本以支持Python2
FROM docker.m.daocloud.io/library/node:14-buster AS build

# 使用清华源替代阿里云源（支持Buster版本）
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
    sed -i 's|security.debian.org/debian-security|mirrors.tuna.tsinghua.edu.cn/debian-security|g' /etc/apt/sources.list && \
    apt-get update && \
    apt-get install -y python2 && \
    ln -s /usr/bin/python2 /usr/bin/python

# 环境变量与系统依赖
ENV npm_config_canvas_binary_host_mirror="https://npmmirror.com/mirrors/canvas" \
    npm_config_sharp_binary_host="https://npmmirror.com/mirrors/sharp" \
    npm_config_sharp_libvips_binary_host="https://npmmirror.com/mirrors/sharp-libvips" \
    ESBUILD_BINARY_HOST="https://npmmirror.com/mirrors/esbuild"

# 安装构建依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential pkg-config \
    libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev libvips-dev \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY package.json package-lock.json ./

# 优化npm安装流程
RUN npm set registry https://registry.npmmirror.com && \
    npm config set sharp_binary_host https://npmmirror.com/mirrors/sharp && \
    npm config set sharp_libvips_binary_host https://npmmirror.com/mirrors/sharp-libvips && \
    npm cache clean --force && \
    npm install --legacy-peer-deps --verbose

COPY . .
RUN npm run build:test

# 最终阶段 - Nginx
FROM docker.m.daocloud.io/library/nginx:alpine

# 使用清华源替代阿里云源（Alpine镜像）
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
    apk update && apk add --no-cache tzdata && \
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone

COPY --from=build /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 8333
CMD ["nginx", "-g", "daemon off;"]
