-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
130 lines (116 loc) · 3.66 KB
/
Copy pathvite.config.ts
File metadata and controls
130 lines (116 loc) · 3.66 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
import { defineConfig, loadEnv } from "vite";
import path from "path";
import react from "@vitejs/plugin-react";
import { exec } from "node:child_process";
import pino from "pino";
import { cloudflare } from "@cloudflare/vite-plugin";
const logger = pino();
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) {
logger[level](cleaned.trimEnd());
return;
}
for (const part of parts) {
logger[level](part);
}
};
// 3. Create the custom logger for Vite
const customLogger = {
warnOnce: (msg: string) => emitLog("warn", msg),
// Use Pino's methods, passing the cleaned message
info: (msg: string) => emitLog("info", msg),
warn: (msg: string) => emitLog("warn", msg),
error: (msg: string) => emitLog("error", msg),
hasErrorLogged: () => false,
// Keep these as-is
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(), cloudflare(), watchDependenciesPlugin()],
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,
},
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",
});
};