FROM docker.io/node:18-alpine AS builder

# Use NODE_ENV=prod to exclude the `devDependencies` (e..g, vite)
# We require `vite` command in `npm run build` below
ARG NODE_ENV=dev

# Read args from build command
ARG VITE_APP_DOC_TITLE='My Heats'
ARG VITE_API_URI='http://127.0.0.1'
ARG VITE_API_PORT=8000
ARG VITE_SESSION_TTL=3600
ARG VITE_WS_URI='ws://127.0.0.1'
ARG VITE_WS_PORT=8000
ARG VITE_LOCALE='en-US'

# Configure the env with the args
ENV NODE_ENV="$NODE_ENV"
ENV VITE_APP_DOC_TITLE="$VITE_APP_DOC_TITLE"
ENV VITE_API_URI="$VITE_API_URI"
ENV VITE_API_PORT="$VITE_API_PORT"
ENV VITE_SESSION_TTL="$VITE_SESSION_TTL"
ENV VITE_WS_URI="$VITE_WS_URI"
ENV VITE_WS_PORT="$VITE_WS_PORT"
ENV VITE_LOCALE="$VITE_LOCALE"

WORKDIR /app
COPY . /app

# Install npm dependencies to node_modules
# https://docs.npmjs.com/cli/v10/commands/npm-ci
RUN npm ci
RUN npm run build

FROM docker.io/nginx:alpine

# Install the static files
RUN rm -rf /usr/share/nginx/html/*
COPY --from=builder /app/dist/ /usr/share/nginx/html/

# Install config file
RUN rm -rf /etc/nginx/conf.d/*
COPY dockerfiles/frontend/myheats.conf /etc/nginx/conf.d/

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