-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathvite.config.ts
More file actions
151 lines (136 loc) · 5.1 KB
/
vite.config.ts
File metadata and controls
151 lines (136 loc) · 5.1 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
import path from 'path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import { defineConfig, loadEnv } from 'vite';
import tailwindcss from '@tailwindcss/vite';
import react from '@vitejs/plugin-react';
import vike from 'vike/plugin';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const normalizeBaseUrl = (url: string): string => url.replace(/\/+$/g, '');
const normalizeBasePath = (value: string): string => {
const trimmed = (value || '').trim();
if (!trimmed) return '/';
const ensuredLeading = trimmed.startsWith('/') ? trimmed : `/${trimmed}`;
const withoutTrailing = ensuredLeading.replace(/\/+$/g, '');
if (withoutTrailing === '/') return '/';
return `${withoutTrailing}/`;
};
const stripTrailingApiPath = (url: string): string => {
const normalized = normalizeBaseUrl(url);
try {
const u = new URL(normalized);
if (u.pathname === '/api') {
u.pathname = '';
return normalizeBaseUrl(u.toString());
}
return normalized;
} catch {
return normalized.replace(/\/api$/g, '');
}
};
const serveFaviconIcoPlugin = () => {
const rewriteFaviconIco = (middlewares: any) => {
middlewares.use((req: any, _res: any, next: any) => {
const rawUrl = typeof req.url === 'string' ? req.url : '';
const url = rawUrl.split('?')[0]?.split('#')[0] ?? '';
if (url !== '/favicon.ico') return next();
// Browsers often request /favicon.ico regardless of <link rel="icon">.
// Keep a dedicated .ico asset for best compatibility (including crawlers).
req.url = '/UI/favicon.ico';
return next();
});
};
return {
name: 'serve-favicon-ico',
apply: 'serve' as const,
configureServer(server: any) {
rewriteFaviconIco(server.middlewares);
},
configurePreviewServer(server: any) {
rewriteFaviconIco(server.middlewares);
},
};
};
const servePublicIndexHtmlPlugin = () => {
return {
name: 'serve-public-index-html',
apply: 'serve' as const,
configureServer(server: any) {
const publicDir = path.resolve(__dirname, 'frontend/public');
const vikeOwnedRoutes = new Set(['how-it-works', 'features']);
server.middlewares.use((req: any, _res: any, next: any) => {
const rawUrl = typeof req.url === 'string' ? req.url : '';
const url = rawUrl.split('?')[0]?.split('#')[0] ?? '';
if (!url || url === '/' || !url.startsWith('/')) return next();
// Only consider paths that look like directory routes (no file extension)
if (path.posix.extname(url)) return next();
const normalized = url.endsWith('/') ? url : `${url}/`;
const relativeDir = normalized.replace(/^\/+/, '');
// Allow Vike to handle routes that will be pre-rendered/served by React pages.
const firstSegment = relativeDir.split('/')[0] || '';
if (vikeOwnedRoutes.has(firstSegment)) return next();
const candidateIndexPath = path.join(publicDir, relativeDir, 'index.html');
// If the public folder contains <route>/index.html, serve it at the clean route.
// This keeps marketing/SEO pages working on localhost without the SPA taking over.
if (fs.existsSync(candidateIndexPath)) {
req.url = `${normalized}index.html`;
}
return next();
});
},
};
};
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, '.', '');
const backendUrl = stripTrailingApiPath(env.VITE_BACKEND_URL || 'http://localhost:5000');
return {
base: normalizeBasePath(env.VITE_BASE_PATH || '/'),
root: path.resolve(__dirname, 'frontend'),
envDir: __dirname,
server: {
port: 3000,
host: '0.0.0.0',
proxy: {
'/api': {
target: backendUrl,
changeOrigin: true,
secure: false,
configure: (proxy) => {
proxy.on('proxyReq', (proxyReq) => {
// The frontend calls /api via same-origin. Stripping Origin avoids backend CORS
// allowlist issues when you open Vite via LAN IP (e.g. from a phone).
proxyReq.removeHeader('origin');
});
},
},
},
},
plugins: [serveFaviconIcoPlugin(), servePublicIndexHtmlPlugin(), tailwindcss(), react(), vike()],
resolve: {
alias: {
'@': path.resolve(__dirname, 'frontend'),
}
},
build: {
// Keep Netlify publish directory stable (repo-root dist/)
outDir: path.resolve(__dirname, 'dist'),
emptyOutDir: true,
// Production optimizations
minify: 'terser',
target: 'esnext',
sourcemap: false,
rollupOptions: {
output: {
// Code splitting for better caching (Vike requires manualChunks to be a function)
manualChunks: (id: string) => {
if (!id.includes('node_modules')) return;
if (id.includes('/recharts/')) return 'vendor-charts';
if (id.includes('/date-fns/') || id.includes('/lucide-react/')) return 'vendor-utils';
// Let Vite/Rollup decide chunking for React core packages to avoid circular chunk imports.
return;
}
}
}
}
};
});