diff --git a/.env.declaration b/.env similarity index 100% rename from .env.declaration rename to .env diff --git a/Dockerfile b/Dockerfile index 774c9d90..fe905adf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,13 @@ -# build environment -FROM node:20-alpine AS build -WORKDIR /app +FROM node:20-alpine AS builder +WORKDIR /usr/local/app + COPY . . RUN npm ci && npm run build -# production environment FROM node:20-alpine -WORKDIR /app -COPY --from=build /app/dist . + +COPY --from=builder /usr/local/app/dist /usr/local/app/dist +WORKDIR /usr/local/app COPY package*.json . COPY server.js . @@ -17,4 +17,4 @@ RUN npm i --save-exact express vite-express ENV PORT 8080 EXPOSE 8080 -ENTRYPOINT sh -c "./vite-envs.sh && npm run prod" \ No newline at end of file +ENTRYPOINT sh -c "./dist/vite-envs.sh && npm run prod" \ No newline at end of file diff --git a/server.js b/server.js index ebe5e4ab..89e4e7a8 100644 --- a/server.js +++ b/server.js @@ -7,7 +7,7 @@ import proxy from 'express-http-proxy' const app = express() const PORT = process.env.PORT || 3000 // use cluster URL if available -const DAPLA_TEAM_API_URL = process.env.DAPLA_TEAM_API_CLUSTER_URL || "https://dapla-team-api-v2.staging-bip-app.ssb.no" +const DAPLA_TEAM_API_URL = process.env.DAPLA_TEAM_API_CLUSTER_URL || 'https://dapla-team-api-v2.staging-bip-app.ssb.no' // Proxy, note this middleware must be place before all else.. THIS TOOK ME 3 HOURS TO FIGURE OUT! TODO: Remove comment app.use( diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index bacb42a0..d3641997 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -6,6 +6,8 @@ type ImportMetaEnv = { MODE: string DEV: boolean PROD: boolean + BUILD_TIME: number + VERSION: string DAPLA_TEAM_API_URL: string DAPLA_TEAM_API_CLUSTER_URL: string PORT: string diff --git a/vite.config.ts b/vite.config.ts index c224cadf..e7b1567d 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -7,7 +7,42 @@ export default defineConfig({ plugins: [ react(), viteEnvs({ - declarationFile: '.env.declaration', + /* + * Uncomment the following line if `.env` is gitignored in your project. + * This enables you to use another file for declaring your variables. + */ + // declarationFile: '.env.declaration', + + /* + * This is completely optional. + * It enables you to define environment + * variables that are computed at build time. + */ + computedEnv: async ({ resolvedConfig /*declaredEnv, localEnv*/ }) => { + const path = await import('path') + const fs = await import('fs/promises') + + const packageJson = JSON.parse(await fs.readFile(path.join(resolvedConfig.root, 'package.json'), 'utf-8')) + + const DAPLA_TEAM_API_URL = process.env.DAPLA_TEAM_API_URL + const DAPLA_TEAM_API_CLUSTER_URL = process.env.DAPLA_TEAM_API_CLUSTER_URL + const PORT = process.env.PORT || 8080 + /* + * Here you can define any arbitrary value they will be available + * in `import.meta.env` and it's type definitions. + * You can also compute defaults for variable declared in `.env` files. + */ + return { + BUILD_TIME: Date.now(), + VERSION: packageJson.version, + DAPLA_TEAM_API_URL: DAPLA_TEAM_API_URL, + DAPLA_TEAM_API_CLUSTER_URL: DAPLA_TEAM_API_CLUSTER_URL, + PORT: PORT, + } + }, }), ], + build: { + sourcemap: true, + }, })