-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.ts
More file actions
184 lines (173 loc) · 5.46 KB
/
vite.config.ts
File metadata and controls
184 lines (173 loc) · 5.46 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
import { defineConfig, loadEnv } from "vite";
import path from "path";
import react from "@vitejs/plugin-react";
import { exec } from "node:child_process";
import { VitePWA } from "vite-plugin-pwa";
import os from "node:os";
const stripAnsi = (str: string) =>
str.replace(
// eslint-disable-next-line no-control-regex -- Allow ANSI escape stripping
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
""
);
const LOG_MESSAGE_BOUNDARY = /\n(?=\[[A-Z][^\]]*\])/g;
const emitLog = (level: "info" | "warn" | "error", rawMessage: string) => {
const cleaned = stripAnsi(rawMessage).replace(/\r\n/g, "\n");
const parts = cleaned
.split(LOG_MESSAGE_BOUNDARY)
.map((part) => part.trimEnd())
.filter((part) => part.trim().length > 0);
if (parts.length === 0) {
console[level](cleaned.trimEnd());
return;
}
for (const part of parts) {
console[level](part);
}
};
// Create the custom logger for Vite
const customLogger = {
warnOnce: (msg: string) => emitLog("warn", msg),
info: (msg: string) => emitLog("info", msg),
warn: (msg: string) => emitLog("warn", msg),
error: (msg: string) => emitLog("error", msg),
hasErrorLogged: () => false,
clearScreen: () => { },
hasWarned: false,
};
function watchDependenciesPlugin() {
return {
// Plugin to clear caches when dependencies change
name: "watch-dependencies",
configureServer(server: any) {
const filesToWatch = [
path.resolve("package.json"),
path.resolve("bun.lock"),
];
server.watcher.add(filesToWatch);
server.watcher.on("change", (filePath: string) => {
if (filesToWatch.includes(filePath)) {
console.log(
`\n📦 Dependency file changed: ${path.basename(
filePath
)}. Clearing caches...`
);
// Run the cache-clearing command
exec(
"rm -f .eslintcache tsconfig.tsbuildinfo",
(err, stdout, stderr) => {
if (err) {
console.error("Failed to clear caches:", stderr);
return;
}
console.log("✅ Caches cleared successfully.\n");
}
);
}
});
},
};
}
// https://vite.dev/config/
export default ({ mode }: { mode: string }) => {
const env = loadEnv(mode, process.cwd());
return defineConfig({
plugins: [
react(),
watchDependenciesPlugin(),
VitePWA({
registerType: "autoUpdate",
manifestFilename: "site.webmanifest",
includeAssets: [
// include favicon or other static assets if needed
"focal-icon.svg",
],
workbox: {
navigateFallbackDenylist: [/^\/api\//],
maximumFileSizeToCacheInBytes: 10 * 1024 * 1024,
},
manifest: {
name: "Focal: AI Receipt Scanner",
short_name: "Focal",
description: "Scan receipts with AI and track expenses.",
theme_color: "#111827",
background_color: "#111827",
display: "standalone",
start_url: "/",
scope: "/",
categories: [
"productivity",
"finance",
],
icons: [
{ src: "/pwa-192x192.png", sizes: "192x192", type: "image/png" },
{ src: "/pwa-512x512.png", sizes: "512x512", type: "image/png" },
{ src: "/maskable-icon-512x512.png", sizes: "512x512", type: "image/png", purpose: "maskable" },
],
},
devOptions: {
enabled: true,
// store temp SW files in OS temp to avoid dev-dist glob warnings
resolveTempFolder: () => os.tmpdir(),
},
pwaAssets: {
image: "public/focal-icon.svg",
},
}),
],
build: {
minify: true,
sourcemap: "inline", // Use inline source maps for better error reporting
rollupOptions: {
output: {
sourcemapExcludeSources: false, // Include original source in source maps
},
},
},
customLogger: env.VITE_LOGGER_TYPE === 'json' ? customLogger : undefined,
// Enable source maps in development too
css: {
devSourcemap: true,
},
server: {
allowedHosts: true,
proxy: {
'/api': {
target: 'http://localhost:8787',
changeOrigin: true,
secure: false,
ws: true,
configure: (proxy, _options) => {
proxy.on('proxyRes', (proxyRes, req, res) => {
// Forward cookies from the backend to the frontend
const setCookieHeader = proxyRes.headers['set-cookie'];
if (setCookieHeader) {
// Ensure cookies work across the proxy
proxyRes.headers['set-cookie'] = setCookieHeader;
}
});
},
},
},
},
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
"@shared": path.resolve(__dirname, "./shared"),
},
},
optimizeDeps: {
// This is still crucial for reducing the time from when `bun run dev`
// is executed to when the server is actually ready.
include: ["react", "react-dom", "react-router-dom"],
exclude: ["agents"], // Exclude agents package from pre-bundling due to Node.js dependencies
force: true,
},
define: {
// Define Node.js globals for the agents package
global: "globalThis",
},
// Clear cache more aggressively
cacheDir: "node_modules/.vite",
});
};