|
|
@@ -1,40 +1,57 @@
|
|
|
/// <reference types="vitest" />
|
|
|
-import { defineConfig } from "vite";
|
|
|
+import { defineConfig, loadEnv } from "vite";
|
|
|
import react from "@vitejs/plugin-react";
|
|
|
import viteTsconfigPaths from "vite-tsconfig-paths";
|
|
|
|
|
|
-const BACKEND_HOST = process.env.BACKEND_HOST || "127.0.0.1:3000";
|
|
|
+export default defineConfig(({ mode }) => {
|
|
|
+ const {
|
|
|
+ VITE_BACKEND_HOST = "127.0.0.1:3000",
|
|
|
+ VITE_USE_TLS = "false",
|
|
|
+ VITE_FRONTEND_PORT = "3001",
|
|
|
+ VITE_INSECURE_SKIP_VERIFY = "false",
|
|
|
+ } = loadEnv(mode, process.cwd());
|
|
|
|
|
|
-// check BACKEND_HOST is something like "example.com"
|
|
|
-if (!BACKEND_HOST.match(/^([\w\d-]+(\.[\w\d-]+)+(:\d+)?)/)) {
|
|
|
- throw new Error(
|
|
|
- `Invalid BACKEND_HOST ${BACKEND_HOST}, example BACKEND_HOST 127.0.0.1:3000`,
|
|
|
- );
|
|
|
-}
|
|
|
+ const USE_TLS = VITE_USE_TLS === "true";
|
|
|
+ const INSECURE_SKIP_VERIFY = VITE_INSECURE_SKIP_VERIFY === "true";
|
|
|
+ const PROTOCOL = USE_TLS ? "https" : "http";
|
|
|
+ const WS_PROTOCOL = USE_TLS ? "wss" : "ws";
|
|
|
|
|
|
-export default defineConfig({
|
|
|
- // depending on your application, base can also be "/"
|
|
|
- base: "",
|
|
|
- plugins: [react(), viteTsconfigPaths()],
|
|
|
- clearScreen: false,
|
|
|
- server: {
|
|
|
- port: process.env.FRONTEND_PORT
|
|
|
- ? Number.parseInt(process.env.FRONTEND_PORT, 10)
|
|
|
- : 3001,
|
|
|
- proxy: {
|
|
|
- "/api": {
|
|
|
- target: `http://${BACKEND_HOST}/`,
|
|
|
- changeOrigin: true,
|
|
|
- },
|
|
|
- "/ws": {
|
|
|
- target: `ws://${BACKEND_HOST}/`,
|
|
|
- ws: true,
|
|
|
+ const API_URL = `${PROTOCOL}://${VITE_BACKEND_HOST}/`;
|
|
|
+ const WS_URL = `${WS_PROTOCOL}://${VITE_BACKEND_HOST}/`;
|
|
|
+ const FE_PORT = Number.parseInt(VITE_FRONTEND_PORT, 10);
|
|
|
+
|
|
|
+ // check BACKEND_HOST is something like "example.com"
|
|
|
+ if (!VITE_BACKEND_HOST.match(/^([\w\d-]+(\.[\w\d-]+)+(:\d+)?)/)) {
|
|
|
+ throw new Error(
|
|
|
+ `Invalid BACKEND_HOST ${VITE_BACKEND_HOST}, example BACKEND_HOST 127.0.0.1:3000`,
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ // depending on your application, base can also be "/"
|
|
|
+ base: "",
|
|
|
+ plugins: [react(), viteTsconfigPaths()],
|
|
|
+ clearScreen: false,
|
|
|
+ server: {
|
|
|
+ port: FE_PORT,
|
|
|
+ proxy: {
|
|
|
+ "/api": {
|
|
|
+ target: API_URL,
|
|
|
+ changeOrigin: true,
|
|
|
+ secure: !INSECURE_SKIP_VERIFY,
|
|
|
+ },
|
|
|
+ "/ws": {
|
|
|
+ target: WS_URL,
|
|
|
+ ws: true,
|
|
|
+ changeOrigin: true,
|
|
|
+ secure: !INSECURE_SKIP_VERIFY,
|
|
|
+ },
|
|
|
},
|
|
|
},
|
|
|
- },
|
|
|
- test: {
|
|
|
- environment: "jsdom",
|
|
|
- globals: true,
|
|
|
- setupFiles: ["vitest.setup.ts"],
|
|
|
- },
|
|
|
+ test: {
|
|
|
+ environment: "jsdom",
|
|
|
+ globals: true,
|
|
|
+ setupFiles: ["vitest.setup.ts"],
|
|
|
+ },
|
|
|
+ };
|
|
|
});
|