# 国内默认走镜像；海外可构建时传 --build-arg REGISTRY_MIRROR= 直连
ARG REGISTRY_MIRROR=docker.m.daocloud.io/library/
FROM ${REGISTRY_MIRROR}node:20-alpine AS builder
WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm ci --legacy-peer-deps 2>/dev/null || npm install --legacy-peer-deps
COPY . .
RUN chmod -R +x node_modules/.bin 2>/dev/null || true
RUN npm run build

ARG REGISTRY_MIRROR=docker.m.daocloud.io/library/
FROM ${REGISTRY_MIRROR}nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
RUN printf '%s\n' \
'server {' \
'  listen 80;' \
'  root /usr/share/nginx/html;' \
'  index index.html;' \
'' \
'  # 根路径下的验证文件走热加载目录，不需要重建镜像' \
'  location ~ ^/([A-Za-z0-9._-]+\.(txt|html|xml))$ {' \
'    alias /verify-root/$1;' \
'  }' \
'' \
'  location ^~ /assets/ {' \
'    try_files $uri =404;' \
'    expires 7d;' \
'    add_header Cache-Control "public, immutable";' \
'  }' \
'' \
'  location = / {' \
'    try_files /index.html =404;' \
'  }' \
'  location / {' \
'    try_files $uri $uri/ /index.html;' \
'  }' \
'}' > /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
