-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.config.js
More file actions
172 lines (156 loc) · 5.15 KB
/
Copy pathvite.config.js
File metadata and controls
172 lines (156 loc) · 5.15 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
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const IS_VITEST = process.env.VITEST === 'true';
// Scan plugins for additionalDependencies to mark as external
function collectAdditionalDependencies() {
const deps = new Set();
const pluginDirs = ['src/plugins', 'extensions'];
function scanDir(dir) {
if (!fs.existsSync(dir)) return;
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const configPath = path.join(dir, entry.name, 'config.js');
if (fs.existsSync(configPath)) {
try {
const content = fs.readFileSync(configPath, 'utf8');
const match = content.match(/additionalDependencies\s*:\s*\[([^\]]*)\]/);
if (match) {
match[1].split(',')
.map(s => s.trim().replace(/['"]/g, ''))
.filter(s => s.length > 0)
.forEach(dep => deps.add(dep));
}
} catch (e) { /* ignore */ }
}
// Recurse into subdirectories (nested plugin structure)
scanDir(path.join(dir, entry.name));
}
}
pluginDirs.forEach(d => scanDir(path.join(__dirname, d)));
return Array.from(deps);
}
const externalDeps = collectAdditionalDependencies();
// Optional dependencies that may not be installed
const optionalDeps = ['puppeteer-core'];
// Combine with plugin additional dependencies
const allExternalDeps = [...externalDeps, ...optionalDeps];
if (allExternalDeps.length > 0) {
console.log(`[Vite] External dependencies: ${allExternalDeps.join(', ')}`);
}
// Startup banner is shown by hooks.server.js instead to avoid duplication
export default defineConfig({
plugins: [
sveltekit(),
...(!IS_VITEST
? [{
name: 'websocket-server',
configureServer(server) {
// Import and initialize WebSocket server using tracker-core
Promise.all([
import('@owlcms/tracker-core'),
import('@owlcms/tracker-core/websocket')
]).then(([{ competitionHub }, { attachWebSocketToServer }]) => {
attachWebSocketToServer({
server: server.httpServer,
path: '/ws',
hub: competitionHub,
localFilesDir: path.join(__dirname, 'local'),
localUrlPrefix: '/local',
onConnect: () => console.log('[WebSocket] OWLCMS connected'),
onDisconnect: () => console.log('[WebSocket] OWLCMS disconnected')
});
});
// Add OWLCMS reverse proxy for Vaadin pages
import('./src/lib/server/owlcms-proxy.js').then(({ attachProxyToViteServer }) => {
attachProxyToViteServer(server);
}).catch(err => {
console.warn('[OWLCMS Proxy] Failed to attach:', err.message);
});
// Serve /local directory (flags, pictures, styles)
server.middlewares.use('/local', (req, res, next) => {
// Decode URL-encoded paths (e.g., "AK%20Bj%C3%B8rgvin.png" → "AK Bjørgvin.png")
const decodedUrl = decodeURIComponent(req.url);
const filePath = path.join(__dirname, 'local', decodedUrl);
// Security: prevent directory traversal
if (!filePath.startsWith(path.join(__dirname, 'local'))) {
res.statusCode = 403;
res.end('Forbidden');
return;
}
// Try to serve the file
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
res.setHeader('Content-Type', getMimeType(filePath));
res.end(fs.readFileSync(filePath));
} else {
res.statusCode = 404;
res.end('Not found');
}
});
// Serve pagedjs from node_modules (pinned to 0.4.3)
server.middlewares.use('/node_modules/pagedjs', (req, res, next) => {
const filePath = path.join(__dirname, 'node_modules', 'pagedjs', req.url);
// Security: prevent directory traversal
if (!filePath.startsWith(path.join(__dirname, 'node_modules', 'pagedjs'))) {
res.statusCode = 403;
res.end('Forbidden');
return;
}
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
res.setHeader('Content-Type', getMimeType(filePath));
res.end(fs.readFileSync(filePath));
} else {
res.statusCode = 404;
res.end('Not found');
}
});
}
}]
: [])
],
server: {
port: 8096,
host: true
},
preview: {
port: 8096,
host: true
},
ssr: {
// Don't bundle plugin additionalDependencies - loaded dynamically at runtime
external: allExternalDeps,
noExternal: []
},
build: {
rollupOptions: {
// Don't bundle plugin additionalDependencies - loaded dynamically at runtime
external: allExternalDeps
}
},
test: {
environment: 'node',
include: [
'tests/**/*.{test,spec}.{js,ts}',
'src/plugins/**/*.{test,spec}.{js,ts}',
'extensions/**/*.{test,spec}.{js,ts}'
]
}
});
function getMimeType(filePath) {
const ext = path.extname(filePath).toLowerCase();
const mimeTypes = {
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.webp': 'image/webp',
'.css': 'text/css',
'.js': 'application/javascript'
};
return mimeTypes[ext] || 'application/octet-stream';
}