vite.config.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. VITE_WATCH_USE_POLLING = "false",
  13. } = loadEnv(mode, process.cwd());
  14. const USE_TLS = VITE_USE_TLS === "true";
  15. const INSECURE_SKIP_VERIFY = VITE_INSECURE_SKIP_VERIFY === "true";
  16. const PROTOCOL = USE_TLS ? "https" : "http";
  17. const WS_PROTOCOL = USE_TLS ? "wss" : "ws";
  18. const API_URL = `${PROTOCOL}://${VITE_BACKEND_HOST}/`;
  19. const WS_URL = `${WS_PROTOCOL}://${VITE_BACKEND_HOST}/`;
  20. const FE_PORT = Number.parseInt(VITE_FRONTEND_PORT, 10);
  21. // check BACKEND_HOST is something like "example.com"
  22. if (!VITE_BACKEND_HOST.match(/^([\w\d-]+(\.[\w\d-]+)+(:\d+)?)/)) {
  23. throw new Error(
  24. `Invalid BACKEND_HOST ${VITE_BACKEND_HOST}, example BACKEND_HOST 127.0.0.1:3000`,
  25. );
  26. }
  27. return {
  28. // depending on your application, base can also be "/"
  29. base: "",
  30. plugins: [react(), viteTsconfigPaths()],
  31. clearScreen: false,
  32. server: {
  33. watch: {
  34. usePolling: VITE_WATCH_USE_POLLING === "true",
  35. },
  36. port: FE_PORT,
  37. proxy: {
  38. "/api": {
  39. target: API_URL,
  40. changeOrigin: true,
  41. secure: !INSECURE_SKIP_VERIFY,
  42. },
  43. "/ws": {
  44. target: WS_URL,
  45. ws: true,
  46. changeOrigin: true,
  47. secure: !INSECURE_SKIP_VERIFY,
  48. },
  49. },
  50. },
  51. test: {
  52. environment: "jsdom",
  53. globals: true,
  54. setupFiles: ["vitest.setup.ts"],
  55. },
  56. };
  57. });