Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move to LoadEnv in vite config #3825

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions src/frontend/vite.config.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import react from "@vitejs/plugin-react-swc";
import dotenv from "dotenv";
import path from "path";
import { defineConfig } from "vite";
import { defineConfig, loadEnv } from "vite";
import svgr from "vite-plugin-svgr";
import tsconfigPaths from "vite-tsconfig-paths";
import {
Expand All @@ -12,16 +10,14 @@ import {
} from "./src/customization/config-constants";

export default defineConfig(({ mode }) => {
dotenv.config({ path: path.resolve(__dirname, "../../.env") });
const env = loadEnv(mode, process.cwd(), "");

const apiRoutes = API_ROUTES || ["^/api/v1/", "/health"];

// Use environment variable to determine the target.
const target =
process.env.VITE_PROXY_TARGET || PROXY_TARGET || "http://127.0.0.1:7860";
env.VITE_PROXY_TARGET || PROXY_TARGET || "http://127.0.0.1:7860";

// Use environment variable to determine the UI server port
const port = Number(process.env.VITE_PORT) || PORT || 3000;
const port = Number(env.VITE_PORT) || PORT || 3000;

const proxyTargets = apiRoutes.reduce((proxyObj, route) => {
proxyObj[route] = {
Expand All @@ -34,16 +30,14 @@ export default defineConfig(({ mode }) => {
}, {});

return {
basename: BASENAME || "",
base: BASENAME || "",
build: {
outDir: "build",
},
define: {
"process.env.BACKEND_URL": JSON.stringify(process.env.BACKEND_URL),
"process.env.ACCESS_TOKEN_EXPIRE_SECONDS": JSON.stringify(
process.env.ACCESS_TOKEN_EXPIRE_SECONDS,
),
"process.env.CI": JSON.stringify(process.env.CI),
"process.env.BACKEND_URL": JSON.stringify(env.BACKEND_URL),
"process.env.ACCESS_TOKEN_EXPIRE_SECONDS": JSON.stringify(env.ACCESS_TOKEN_EXPIRE_SECONDS),
"process.env.CI": JSON.stringify(env.CI),
},
plugins: [react(), svgr(), tsconfigPaths()],
server: {
Expand All @@ -53,4 +47,4 @@ export default defineConfig(({ mode }) => {
},
},
};
});
});