-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
54 lines (52 loc) · 1.61 KB
/
vite.config.ts
File metadata and controls
54 lines (52 loc) · 1.61 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
import * as https from "node:https";
import * as path from "node:path";
import tailwindcss from "@tailwindcss/vite";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
import svgr from "vite-plugin-svgr";
const API_TARGET = "api.whylog.site";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
{
name: "api-proxy",
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (!req.url?.startsWith("/api")) return next();
const {
connection,
"keep-alive": _ka,
"transfer-encoding": _te,
origin: _origin,
referer: _referer,
...safeHeaders
} = req.headers;
const options: https.RequestOptions = {
hostname: API_TARGET,
port: 443,
path: req.url,
method: req.method,
headers: { ...safeHeaders, host: API_TARGET },
};
const proxyReq = https.request(options, (proxyRes) => {
const { "transfer-encoding": _tte, ...safeResHeaders } =
proxyRes.headers;
res.writeHead(proxyRes.statusCode ?? 500, safeResHeaders);
proxyRes.pipe(res, { end: true });
});
proxyReq.on("error", (err) => {
console.error("[api-proxy] upstream error:", err.message);
next();
});
req.pipe(proxyReq, { end: true });
});
},
},
svgr(),
react(),
tailwindcss(),
],
resolve: {
alias: [{ find: "@", replacement: path.resolve(__dirname, "src") }],
},
});