From e39998a09d070886050a21bc563cc01fcd60a9ce Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Tue, 13 May 2025 01:46:10 +0300 Subject: [PATCH] Refactor build process and runtime setup for dynamic base path handling --- frontend/.env.production | 2 +- frontend/.gitignore | 1 + frontend/dockerfile | 10 ++++++++-- frontend/runtimeSetup.js | 17 ++++++++--------- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/frontend/.env.production b/frontend/.env.production index 82e1f2d..c32226b 100644 --- a/frontend/.env.production +++ b/frontend/.env.production @@ -1,4 +1,4 @@ -VITE_APP_BASE_URL=/@RUNTIME_BASE_URL@/ +VITE_APP_BASE_URL= VITE_APP_TUITIO_URL=https:// VITE_APP_NETWORK_RESURRECTOR_API_URL=https:// diff --git a/frontend/.gitignore b/frontend/.gitignore index a627bb5..ae72d74 100644 --- a/frontend/.gitignore +++ b/frontend/.gitignore @@ -10,6 +10,7 @@ # production /build +/build2 # misc .DS_Store diff --git a/frontend/dockerfile b/frontend/dockerfile index bd04b8b..198098c 100644 --- a/frontend/dockerfile +++ b/frontend/dockerfile @@ -2,6 +2,8 @@ FROM node:23-slim AS builder WORKDIR /app +ARG APP_SUBFOLDER="" + COPY .npmrc .npmrc COPY package*.json ./ RUN npm install @@ -10,13 +12,16 @@ RUN rm -f .npmrc COPY . ./ # build the react app -RUN npm run build +RUN if [ -z "$APP_SUBFOLDER" ]; then npm run build; else VITE_APP_BASE_URL=/${APP_SUBFOLDER}/ npm run build; fi # PRODUCTION ENVIRONMENT FROM node:23-slim +ARG APP_SUBFOLDER="" + RUN printf '\n\n- Copy application files\n' -COPY --from=builder /app/build ./application +COPY --from=builder /app/build ./application/${APP_SUBFOLDER} +COPY --from=builder /app/build/index.html ./application/ COPY --from=builder /app/runtimeSetup.js ./application/runtimeSetup.js #install static server @@ -25,6 +30,7 @@ RUN npm install -g serve # environment variables ENV AUTHOR="Tudor Stanciu" ENV APP_NAME="Network resurrector UI" +ENV VITE_APP_BASE_URL=/${APP_SUBFOLDER}/ ARG APP_VERSION=0.0.0 ENV APP_VERSION=${APP_VERSION} diff --git a/frontend/runtimeSetup.js b/frontend/runtimeSetup.js index e656e4b..4a2d493 100644 --- a/frontend/runtimeSetup.js +++ b/frontend/runtimeSetup.js @@ -4,9 +4,7 @@ const path = require("path"); const crypto = require("crypto"); const prefix = "VITE_APP_"; -const RUNTIME_BASE_URL_PLACEHOLDER = "/@RUNTIME_BASE_URL@"; const APP_DIR = "./application"; -const ENV_JS_DIR = APP_DIR; function generateScriptContent() { const prefixRegex = new RegExp(`^${prefix}`); @@ -35,7 +33,7 @@ function updateIndexHtml(envFileName, basePath) { let indexContent = fs.readFileSync(indexPath, "utf8"); // Replace base path placeholder with actual value - indexContent = indexContent.replace(new RegExp(RUNTIME_BASE_URL_PLACEHOLDER, "g"), basePath || "/"); + // indexContent = indexContent.replace(new RegExp(RUNTIME_BASE_URL_PLACEHOLDER, "g"), basePath || "/"); // Replace any existing env script with the new one const envScriptRegex = /