vite.config.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* eslint-disable import/no-extraneous-dependencies */
  2. /// <reference types="vitest" />
  3. /// <reference types="vite-plugin-svgr/client" />
  4. import { defineConfig, loadEnv } from "vite";
  5. import viteTsconfigPaths from "vite-tsconfig-paths";
  6. import svgr from "vite-plugin-svgr";
  7. import { vitePlugin as remix } from "@remix-run/dev";
  8. export default defineConfig(({ mode }) => {
  9. const {
  10. VITE_BACKEND_HOST = "127.0.0.1:3000",
  11. VITE_USE_TLS = "false",
  12. VITE_FRONTEND_PORT = "3001",
  13. VITE_INSECURE_SKIP_VERIFY = "false",
  14. } = loadEnv(mode, process.cwd());
  15. const USE_TLS = VITE_USE_TLS === "true";
  16. const INSECURE_SKIP_VERIFY = VITE_INSECURE_SKIP_VERIFY === "true";
  17. const PROTOCOL = USE_TLS ? "https" : "http";
  18. const WS_PROTOCOL = USE_TLS ? "wss" : "ws";
  19. const API_URL = `${PROTOCOL}://${VITE_BACKEND_HOST}/`;
  20. const WS_URL = `${WS_PROTOCOL}://${VITE_BACKEND_HOST}/`;
  21. const FE_PORT = Number.parseInt(VITE_FRONTEND_PORT, 10);
  22. /**
  23. * This script is used to unpack the client directory from the frontend build directory.
  24. * Remix SPA mode builds the client directory into the build directory. This function
  25. * moves the contents of the client directory to the build directory and then removes the
  26. * client directory.
  27. *
  28. * This script is used in the buildEnd function of the Vite config.
  29. */
  30. const unpackClientDirectory = async () => {
  31. const fs = await import("fs");
  32. const path = await import("path");
  33. const buildDir = path.resolve(__dirname, "build");
  34. const clientDir = path.resolve(buildDir, "client");
  35. const files = await fs.promises.readdir(clientDir);
  36. await Promise.all(
  37. files.map((file) =>
  38. fs.promises.rename(
  39. path.resolve(clientDir, file),
  40. path.resolve(buildDir, file),
  41. ),
  42. ),
  43. );
  44. await fs.promises.rmdir(clientDir);
  45. };
  46. return {
  47. plugins: [
  48. !process.env.VITEST &&
  49. remix({
  50. future: {
  51. v3_fetcherPersist: true,
  52. v3_relativeSplatPath: true,
  53. v3_throwAbortReason: true,
  54. },
  55. appDirectory: "src",
  56. buildEnd: unpackClientDirectory,
  57. ssr: false,
  58. }),
  59. viteTsconfigPaths(),
  60. svgr(),
  61. ],
  62. server: {
  63. port: FE_PORT,
  64. proxy: {
  65. "/api": {
  66. target: API_URL,
  67. changeOrigin: true,
  68. secure: !INSECURE_SKIP_VERIFY,
  69. },
  70. "/ws": {
  71. target: WS_URL,
  72. ws: true,
  73. changeOrigin: true,
  74. secure: !INSECURE_SKIP_VERIFY,
  75. },
  76. },
  77. },
  78. ssr: {
  79. noExternal: ["react-syntax-highlighter"],
  80. },
  81. clearScreen: false,
  82. test: {
  83. environment: "jsdom",
  84. setupFiles: ["vitest.setup.ts"],
  85. coverage: {
  86. reporter: ["text", "json", "html", "lcov", "text-summary"],
  87. reportsDirectory: "coverage",
  88. include: ["src/**/*.{ts,tsx}"],
  89. },
  90. },
  91. };
  92. });