-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.ts
More file actions
111 lines (102 loc) · 3.68 KB
/
vite.config.ts
File metadata and controls
111 lines (102 loc) · 3.68 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 { paraglideVitePlugin } from '@inlang/paraglide-js';
import tailwindcss from '@tailwindcss/vite';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig, type ViteDevServer } from 'vite';
import type { IncomingMessage } from 'node:http';
import type { Duplex } from 'node:stream';
import mkcert from 'vite-plugin-mkcert';
function wsPlugin() {
return {
name: 'ws-dev',
configureServer(server: ViteDevServer) {
server.httpServer?.on('upgrade', (req, socket, head) => {
const g = globalThis as typeof globalThis & {
__wssUpgrade?: (req: IncomingMessage, socket: Duplex, head: Buffer) => void;
};
if (g.__wssUpgrade) {
g.__wssUpgrade(req, socket, head);
}
});
}
};
}
function devAutoRestart() {
const RACE_CONDITION_PATTERNS = [
'has not been implemented', // Pothos ObjectRef race condition
'Class extends value undefined is not a constructor or null', // urql/svelte race condition
'Received multiple implementations for plugin' // Pothos plugin re-registration after Vite reload
];
return {
name: 'dev-auto-restart',
configureServer(server: ViteDevServer) {
let restarting = false;
const triggerRestart = (label: string) => {
if (restarting) return;
restarting = true;
console.warn(`\n⚠️ ${label}, restarting dev server...\n`);
server.restart();
};
const isRaceCondition = (message: string | undefined) =>
RACE_CONDITION_PATTERNS.some((pattern) => message?.includes(pattern));
const onUnhandledRejection = (reason: unknown) => {
if (reason instanceof Error && isRaceCondition(reason.message)) {
triggerRestart('Race condition detected');
}
};
process.on('unhandledRejection', onUnhandledRejection);
server.httpServer?.on('close', () => {
process.off('unhandledRejection', onUnhandledRejection);
});
const originalSsrFixStacktrace = server.ssrFixStacktrace;
server.ssrFixStacktrace = function (e: Error) {
originalSsrFixStacktrace.call(this, e);
if (isRaceCondition(e?.message)) {
triggerRestart('SSR race condition detected');
}
};
}
};
}
export default defineConfig({
plugins: [
mkcert(),
devAutoRestart(),
tailwindcss(),
paraglideVitePlugin({
project: './project.inlang',
outdir: './src/lib/paraglide',
strategy: ['cookie', 'preferredLanguage', 'baseLocale']
}),
sveltekit(),
wsPlugin()
],
// Yjs throws "Yjs was already imported" if two copies are loaded.
// Vite prebundles `y-websocket` + `y-protocols/*` and chunks the shared
// `yjs` they pull in. If we ALSO alias chase's direct `yjs` imports to
// `node_modules/yjs/dist/yjs.mjs` directly, that bypasses the chunk and
// loads a SECOND yjs module at runtime. Use `dedupe` (which makes
// resolution always walk up to the project's yjs) without the alias —
// then chase's import and the prebundled chunk converge on the same
// module instance.
resolve: {
dedupe: ['yjs', 'y-protocols']
},
optimizeDeps: {
include: ['y-protocols/sync', 'y-protocols/awareness', 'y-websocket'],
// EXCLUDE yjs from prebundling entirely. When yjs is prebundled,
// vite ends up with two yjs entries (one for chase's direct import,
// one as the chunk shared by y-protocols/y-websocket), each with
// its own module-init scope → "Yjs was already imported" warning.
// Excluded, every `import 'yjs'` resolves to the single source file
// at chase/node_modules/yjs/dist/yjs.mjs, ESM-cached once.
exclude: ['yjs']
},
server: {
allowedHosts: ['svelte-dev.munify.cloud'],
watch: {
// Ignore Claude Code worktrees and other internal directories so
// changes there don't force-reload the dev server.
ignored: ['**/.claude/**', '**/node_modules/**', '**/.svelte-kit/**']
}
}
});