-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathvite.config.ts
More file actions
36 lines (34 loc) · 1.23 KB
/
vite.config.ts
File metadata and controls
36 lines (34 loc) · 1.23 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
import { createRequest, sendResponse } from '@remix-run/node-fetch-server'
import { defineConfig, loadEnv } from 'vite'
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
Object.assign(process.env, env)
return {
plugins: [
{
name: 'api',
async configureServer(server) {
const { handler } = await import('./src/server.ts')
// oxlint-disable-next-line no-async-endpoint-handlers
server.middlewares.use(async (req, res, next) => {
const request = createRequest(req, res)
const response = await handler(request)
if (response) await sendResponse(res, response)
else next()
})
server.httpServer?.once('listening', () => {
const addr = server.httpServer!.address()
const host =
typeof addr === 'object' && addr ? `localhost:${addr.port}` : 'localhost:5173'
const pm = process.env.npm_config_user_agent?.split('/')[0] ?? 'npx'
setTimeout(
() =>
console.log(`\n ${pm === 'npm' ? 'npx' : pm} mppx http://${host}/api/fortune\n`),
100,
)
})
},
},
],
}
})