Repository navigation
Expand file tree
/
Copy pathvite.config.js
More file actions
190 lines (167 loc) · 5.02 KB
/
Copy pathvite.config.js
File metadata and controls
190 lines (167 loc) · 5.02 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import path from 'node:path'
import react from '@vitejs/plugin-react'
import { defineConfig, loadEnv } from 'vite'
import analyzeHandler from './api/analyze.js'
import healthHandler from './api/health.js'
import analyzeVoiceHandler from './api/analyze-voice.js'
import blocklistLiteHandler from './api/blocklist-lite.js'
import tinyfishHandler from './api/tinyfish/investigate.js'
function createMockRequest(req, body) {
return {
method: req.method,
headers: req.headers,
body,
query: {},
url: req.url,
}
}
function createMockResponse(res) {
return {
statusCode: 200,
headers: {},
setHeader(key, value) {
this.headers[key] = value
res.setHeader(key, value)
},
status(code) {
this.statusCode = code
res.statusCode = code
return this
},
json(payload) {
if (!res.writableEnded) {
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(payload))
}
return this
},
send(payload) {
if (!res.writableEnded) {
if (typeof payload === 'object' && payload !== null) {
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(payload))
} else {
res.setHeader('Content-Type', 'text/plain')
res.end(String(payload))
}
}
return this
},
end(payload) {
if (!res.writableEnded) {
res.end(payload)
}
return this
},
}
}
async function readJsonBody(req) {
const chunks = []
for await (const chunk of req) {
chunks.push(chunk)
}
if (chunks.length === 0) return {}
try {
return JSON.parse(Buffer.concat(chunks).toString('utf8'))
} catch {
return {}
}
}
function localApiPlugin() {
return {
name: 'ghostnet-local-api',
configureServer(server) {
server.middlewares.use('/api/health', async (req, res) => {
const mockRes = createMockResponse(res)
await healthHandler(createMockRequest(req, {}), mockRes)
})
server.middlewares.use('/api/analyze', async (req, res) => {
if (req.method === 'OPTIONS') {
res.statusCode = 204
res.end()
return
}
const body = req.method === 'POST' ? await readJsonBody(req) : {}
const mockReq = createMockRequest(req, body)
const mockRes = createMockResponse(res)
await analyzeHandler(mockReq, mockRes)
})
server.middlewares.use('/api/analyze-voice', async (req, res) => {
if (req.method === 'OPTIONS') {
res.statusCode = 204
res.end()
return
}
const body = req.method === 'POST' ? await readJsonBody(req) : {}
const mockReq = createMockRequest(req, body)
const mockRes = createMockResponse(res)
await analyzeVoiceHandler(mockReq, mockRes)
})
server.middlewares.use('/api/blocklist-lite', async (req, res) => {
if (req.method === 'OPTIONS') {
res.statusCode = 204
res.end()
return
}
const mockReq = createMockRequest(req, {})
const mockRes = createMockResponse(res)
await blocklistLiteHandler(mockReq, mockRes)
})
server.middlewares.use('/api/tinyfish/investigate', async (req, res) => {
if (req.method === 'OPTIONS') {
res.statusCode = 204
res.end()
return
}
const body = req.method === 'POST' ? await readJsonBody(req) : {}
const mockReq = createMockRequest(req, body)
const mockRes = createMockResponse(res)
await tinyfishHandler(mockReq, mockRes)
})
},
}
}
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
for (const [key, value] of Object.entries(env)) {
if (process.env[key] === undefined) {
process.env[key] = value
}
}
return {
base: './',
logLevel: 'info',
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'@designcodeio/threeui/style.css': path.resolve(__dirname, './src/shaders/threeui.css'),
'@designcodeio/threeui': path.resolve(__dirname, './src/shaders/index.ts'),
},
},
plugins: [react(), localApiPlugin()],
esbuild: {
target: 'es2018',
},
build: {
target: ['chrome75', 'es2018'],
cssTarget: ['chrome75'],
chunkSizeWarningLimit: 2000,
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes('node_modules')) {
if (id.includes('three')) return 'three'
if (id.includes('framer-motion')) return 'motion'
if (id.includes('lucide-react')) return 'icons'
if (id.includes('@radix-ui')) return 'radix'
if (id.includes('recharts') || id.includes('d3-')) return 'charts'
if (id.includes('jspdf') || id.includes('html2canvas')) return 'pdf'
if (id.includes('@supabase') || id.includes('@tanstack')) return 'data'
return 'vendor'
}
},
},
},
},
}
})