-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
61 lines (56 loc) · 1.62 KB
/
Copy pathvite.config.ts
File metadata and controls
61 lines (56 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
import path from "node:path";
import { defineConfig, loadEnv } from "vite";
export default defineConfig(({ mode }) => {
const env = { ...loadEnv(mode, process.cwd(), ""), ...process.env };
const webPort = readPort(env, ["WEB_PORT"], 5173);
const apiPort = readPort(env, ["API_PORT", "PORT"], 4141);
const webHost = env.WEB_HOST ?? "0.0.0.0";
const apiHost = normalizeProxyHost(env.API_HOST ?? env.HOST ?? "127.0.0.1");
const apiTarget = `http://${formatHttpHost(apiHost)}:${apiPort}`;
return {
plugins: [react(), tailwindcss()],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
server: {
host: webHost,
port: webPort,
strictPort: true,
proxy: {
"/api": apiTarget,
},
},
preview: {
host: webHost,
port: webPort,
strictPort: true,
},
};
});
function readPort(env: Record<string, string | undefined>, names: string[], fallback: number) {
for (const name of names) {
const raw = env[name]?.trim();
if (!raw) {
continue;
}
const port = Number(raw);
if (Number.isInteger(port) && port >= 1 && port <= 65535) {
return port;
}
throw new Error(`${name} must be a port number between 1 and 65535.`);
}
return fallback;
}
function normalizeProxyHost(host: string) {
return host === "0.0.0.0" || host === "::" ? "127.0.0.1" : host;
}
function formatHttpHost(host: string) {
if (host.startsWith("[") && host.endsWith("]")) {
return host;
}
return host.includes(":") ? `[${host}]` : host;
}