Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
102 lines (95 loc) · 3.08 KB
/
Copy pathvite.config.ts
File metadata and controls
102 lines (95 loc) · 3.08 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { defineConfig, Plugin, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
import tsconfigPaths from "vite-tsconfig-paths";
import { tanstackStart } from "@tanstack/react-start/plugin/vite";
import { nitro } from "nitro/vite";
const apiMockPlugin = (): Plugin => ({
name: 'api-mock-plugin',
configureServer(server) {
server.middlewares.use(async (req: any, res: any, next) => {
if (req.url?.startsWith('/api/')) {
const url = new URL(req.url, `http://${req.headers.host}`);
const filePath = `/api${url.pathname.replace('/api', '')}.ts`;
req.query = Object.fromEntries(url.searchParams);
try {
const module = await server.ssrLoadModule(filePath);
const chunks: Buffer[] = [];
req.on('data', (chunk: any) => {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
});
req.on('end', async () => {
const rawBuffer = Buffer.concat(chunks);
if (rawBuffer.length > 0) {
try {
req.body = JSON.parse(rawBuffer.toString('utf8'));
} catch(e) {
req.body = rawBuffer;
}
}
res.status = (code: number) => {
res.statusCode = code;
return res;
};
res.json = (data: any) => {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(data));
};
try {
await module.default(req, res);
} catch (err: any) {
console.error("API Error:", err);
if (!res.writableEnded) {
res.status(500).json({ error: err.message || "Internal Server Error" });
}
}
});
return;
} catch (e) {
console.error(`Error loading API route ${filePath}:`, e);
if (!res.writableEnded) {
res.statusCode = 500;
res.end(JSON.stringify({ error: "Failed to load API route." }));
}
return;
}
}
next();
});
}
});
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd(), '');
process.env = { ...process.env, ...env };
const isDev = command === 'serve' && process.env.npm_lifecycle_event === 'dev';
return {
plugins: [
apiMockPlugin(),
tanstackStart(),
command === 'build' ? nitro({ preset: 'vercel' }) : undefined,
react(),
tailwindcss(),
tsconfigPaths(),
].filter(Boolean) as Plugin[],
resolve: {
alias: {
"@": "/src",
},
dedupe: ["react", "react-dom", "@tanstack/react-router"],
},
server: {
port: 8080,
host: true,
strictPort: true,
},
build: {
rollupOptions: {
output: {
manualChunks(id) {
// Disabled manual chunks to prevent circular dependencies in production
}
}
}
}
};
});