vite.config.ts 887 B

123456789101112131415161718192021222324252627282930
  1. import { defineConfig } from "vite";
  2. import react from "@vitejs/plugin-react";
  3. import viteTsconfigPaths from "vite-tsconfig-paths";
  4. const BACKEND_HOST = process.env.BACKEND_HOST || "localhost:3000";
  5. // check BACKEND_HOST is something like "localhost:3000" or "example.com"
  6. if (!BACKEND_HOST.match(/^(localhost|[\w\d-]+(\.[\w\d-]+)+(:\d+)?)/)) {
  7. throw new Error(`Invalid BACKEND_HOST ${BACKEND_HOST}, example BACKEND_HOST localhost:3000`);
  8. }
  9. export default defineConfig({
  10. // depending on your application, base can also be "/"
  11. base: "",
  12. plugins: [react(), viteTsconfigPaths()],
  13. server: {
  14. port: 3001,
  15. proxy: {
  16. "/api": {
  17. target: `http://${BACKEND_HOST}/`,
  18. changeOrigin: true,
  19. rewrite: (path: string) => path.replace(/^\/api/, ""),
  20. },
  21. "/ws": {
  22. target: `ws://${BACKEND_HOST}/`,
  23. ws: true,
  24. },
  25. },
  26. },
  27. });