vite.config.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /// <reference types="vitest" />
  2. import { defineConfig, loadEnv } from "vite";
  3. import react from "@vitejs/plugin-react";
  4. import viteTsconfigPaths from "vite-tsconfig-paths";
  5. import path from "path";
  6. export default defineConfig(({ mode }) => {
  7. const {
  8. VITE_BACKEND_HOST = "127.0.0.1:3000",
  9. VITE_USE_TLS = "false",
  10. VITE_FRONTEND_PORT = "3001",
  11. VITE_INSECURE_SKIP_VERIFY = "false",
  12. } = loadEnv(mode, process.cwd());
  13. const USE_TLS = VITE_USE_TLS === "true";
  14. const INSECURE_SKIP_VERIFY = VITE_INSECURE_SKIP_VERIFY === "true";
  15. const PROTOCOL = USE_TLS ? "https" : "http";
  16. const WS_PROTOCOL = USE_TLS ? "wss" : "ws";
  17. const API_URL = `${PROTOCOL}://${VITE_BACKEND_HOST}/`;
  18. const WS_URL = `${WS_PROTOCOL}://${VITE_BACKEND_HOST}/`;
  19. const FE_PORT = Number.parseInt(VITE_FRONTEND_PORT, 10);
  20. // check BACKEND_HOST is something like "example.com"
  21. if (!VITE_BACKEND_HOST.match(/^([\w\d-]+(\.[\w\d-]+)+(:\d+)?)/)) {
  22. throw new Error(
  23. `Invalid BACKEND_HOST ${VITE_BACKEND_HOST}, example BACKEND_HOST 127.0.0.1:3000`,
  24. );
  25. }
  26. return {
  27. // depending on your application, base can also be "/"
  28. base: "",
  29. plugins: [react(), viteTsconfigPaths()],
  30. clearScreen: false,
  31. server: {
  32. port: FE_PORT,
  33. proxy: {
  34. "/api": {
  35. target: API_URL,
  36. changeOrigin: true,
  37. secure: !INSECURE_SKIP_VERIFY,
  38. },
  39. "/ws": {
  40. target: WS_URL,
  41. ws: true,
  42. changeOrigin: true,
  43. secure: !INSECURE_SKIP_VERIFY,
  44. },
  45. },
  46. },
  47. test: {
  48. environment: "jsdom",
  49. globals: true,
  50. setupFiles: ["vitest.setup.ts"],
  51. },
  52. };
  53. });