-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
111 lines (108 loc) · 3.9 KB
/
vite.config.js
File metadata and controls
111 lines (108 loc) · 3.9 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
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";
import fs from "node:fs";
import path from "node:path";
/**
* Vite plugin that fixes stale dep hash 504s when serving through cloudflared.
*
* Problem: Vite optimizes deps and tags them with a browser hash (?v=abc123).
* When the hash changes (e.g. after a restart or re-optimization), browsers
* with cached modules still request the OLD hash. Vite returns 504 for hash
* mismatches, which breaks hydration entirely — no client-side JS works.
*
* Fix: Intercept requests for optimized dep files and strip the ?v= parameter
* so Vite always serves the current version regardless of what hash the browser
* is requesting. The browser will get the right content and on next navigation
* SvelteKit's version check will force a full reload if needed.
*/
/**
* Vite plugin that stubs out native Node.js modules during production builds.
*
* Problem: mcp-client.js dynamically imports better-sqlite3 for local gateway
* usage. Even though the import is dynamic and gated behind runtime conditions,
* Vite still bundles it — pulling in fs, path, util, and native bindings that
* cause Wrangler's esbuild to fail with "Could not resolve" errors.
*
* Fix: During builds only, intercept the better-sqlite3 import and replace it
* with a stub module. The code paths that use it are never reached on Workers.
*/
function excludeNativeModules() {
return {
name: 'exclude-native-modules',
apply: 'build',
resolveId(source) {
if (source === 'better-sqlite3') {
return '\0virtual:better-sqlite3-stub';
}
},
load(id) {
if (id === '\0virtual:better-sqlite3-stub') {
return 'export default class Database { constructor() { throw new Error("better-sqlite3 is not available in Cloudflare Workers"); } }';
}
}
};
}
function staleDepsFix() {
return {
name: 'stale-deps-fix',
configureServer(server) {
server.middlewares.use((req, res, next) => {
// Only intercept requests for Vite's optimized deps
if (req.url?.includes('/node_modules/.vite/deps/') && req.url.includes('?v=')) {
// Strip the ?v= hash so Vite serves whatever version it currently has
req.url = req.url.replace(/\?v=[a-f0-9]+/, '');
}
next();
});
},
};
}
export default defineConfig({
plugins: [excludeNativeModules(), staleDepsFix(), sveltekit()],
test: {
environment: 'node',
},
optimizeDeps: {
// Pre-bundle Svelte runtime deps at startup so they're ready instantly.
// Without this, Vite discovers and optimizes lazily on first request,
// which causes 504 Gateway Timeouts through the cloudflared tunnel.
include: [
'svelte',
'svelte/store',
'svelte/transition',
'svelte/animate',
'svelte/easing',
'svelte/internal/client',
'svelte/internal/disclose-version',
'svelte/internal/flags/legacy',
],
},
server: {
// Pre-transform client files at startup so they're served instantly.
// Without this, the first page load triggers on-demand transforms that
// can 504 through the cloudflared tunnel.
warmup: {
clientFiles: [
'./src/routes/**/*.svelte',
'./src/lib/components/**/*.svelte',
],
},
port: 4269,
host: true, // Listen on all interfaces for tunnel access
allowedHosts: ["spacebot-dev.starspace.group", "spacebot.starspace.group", "localhost"],
// Disable HMR completely to prevent WebSocket connection issues over tunnels
// This prevents the site from hanging when the HMR WebSocket fails to connect
// For local development, you can set VITE_HMR=true to re-enable HMR
hmr: process.env.VITE_HMR === 'true' ? {
timeout: 5000,
} : false,
// Prevent browsers from caching modules and CSS with 304 responses.
// Without HMR the only way to pick up changes is a full reload,
// so stale 304s cause "flash then revert" styling bugs.
headers: {
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
'Pragma': 'no-cache',
'Expires': '0',
},
},
});