-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
86 lines (81 loc) · 2.55 KB
/
Copy pathvite.config.ts
File metadata and controls
86 lines (81 loc) · 2.55 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
import { defineConfig, loadEnv, type Plugin } from 'vite';
import react from '@vitejs/plugin-react';
import { VitePWA } from 'vite-plugin-pwa';
import path from 'path';
/** Serves /api/ai during `vite dev` by delegating to the same handler used by
* the Vercel serverless function, so the AI proxy works without `vercel dev`. */
function apiDevMiddleware(): Plugin {
return {
name: 'dailylearn-api-dev-middleware',
configureServer(server) {
server.middlewares.use('/api/ai', async (req, res) => {
const { handleAiRequest } = await server.ssrLoadModule('/api/_lib/handleAiRequest.ts');
let body: unknown;
if (req.method === 'POST') {
const chunks: Buffer[] = [];
for await (const chunk of req) chunks.push(chunk as Buffer);
try {
body = JSON.parse(Buffer.concat(chunks).toString('utf-8'));
} catch {
body = undefined;
}
}
const { status, body: responseBody } = await handleAiRequest(
req.method ?? 'GET',
body,
req.headers.authorization,
);
res.statusCode = status;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(responseBody));
});
},
};
}
export default defineConfig(({ mode }) => {
// Load .env / .env.local etc. (including server-only vars without VITE_
// prefix, via the empty 3rd-arg prefix) so /api/ai works under `vite dev`.
const env = loadEnv(mode, process.cwd(), '');
for (const [key, value] of Object.entries(env)) {
if (process.env[key] === undefined) process.env[key] = value;
}
return {
plugins: [
react(),
apiDevMiddleware(),
VitePWA({
registerType: 'autoUpdate',
includeAssets: ['icons/icon.svg'],
manifest: {
name: 'DailyLearn',
short_name: 'DailyLearn',
description: 'Lifelong micro-learning, one small lesson a day.',
theme_color: '#0f172a',
background_color: '#0f172a',
display: 'standalone',
start_url: '/',
icons: [
{
src: '/icons/icon.svg',
sizes: 'any',
type: 'image/svg+xml',
purpose: 'any maskable',
},
],
},
workbox: {
globPatterns: ['**/*.{js,css,html,svg,png,ico,json}'],
navigateFallbackDenylist: [/^\/api/],
},
}),
],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
server: {
port: 5173,
},
};
});