vite.config.ts 3.2 KB

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