-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvite.config.ts
More file actions
91 lines (88 loc) 路 3.41 KB
/
Copy pathvite.config.ts
File metadata and controls
91 lines (88 loc) 路 3.41 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
import { defineConfig, Plugin } from 'vite'
import react from '@vitejs/plugin-react'
import { readFileSync, cpSync, existsSync } from 'fs'
import { resolve } from 'path'
// Extract resolved versions from package.json at build time
const pkg = JSON.parse(readFileSync('./package.json', 'utf-8'));
const cleanVer = (v: string) => v.replace(/^[\^~>=<]+/, '');
/**
* Copy Monaco Editor AMD assets to dist/vs at build time,
* and serve them from node_modules in dev mode.
* The AMD loader files are in IIFE format: workers load without ESM issues.
*/
function copyMonacoAssets(): Plugin {
const monacoPath = resolve(__dirname, 'node_modules/monaco-editor/min');
return {
name: 'copy-monaco-assets',
configureServer(server) {
// Serve /vs/* from node_modules in dev mode
const monacoVsPath = resolve(monacoPath, 'vs');
server.middlewares.use('/vs', (req, res, next) => {
const relativePath = (req.url || '').replace(/^\//, '').split('?')[0];
const filePath = resolve(monacoVsPath, relativePath);
// H30: Validate resolved path stays within Monaco directory (prevent path traversal)
if (!filePath.startsWith(monacoVsPath)) {
res.statusCode = 403;
res.end('Forbidden');
return;
}
if (existsSync(filePath)) {
const ext = filePath.split('.').pop() || '';
const mime: Record<string, string> = { js: 'application/javascript', css: 'text/css', ttf: 'font/ttf', svg: 'image/svg+xml' };
res.setHeader('Content-Type', mime[ext] || 'application/octet-stream');
res.end(readFileSync(filePath));
} else {
next();
}
});
// Return 204 for /min-maps/* requests: Monaco references source maps that don't exist
// in the npm package. Without this, Vite returns HTML (index fallback) causing JSON parse errors.
server.middlewares.use('/min-maps', (_req, res) => {
res.statusCode = 204;
res.end();
});
},
writeBundle() {
const src = resolve(monacoPath, 'vs');
const dest = resolve(__dirname, 'dist/vs');
if (existsSync(src)) {
cpSync(src, dest, { recursive: true });
}
},
};
}
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), copyMonacoAssets()],
build: {
rollupOptions: {
// Multi-page: the main app plus the dedicated lightweight `extract` window
// (Deliverable G). Each HTML entry gets its own bundle so extract.html does
// not pull in the full App.
input: {
main: resolve(__dirname, 'index.html'),
extract: resolve(__dirname, 'extract.html'),
},
},
},
server: {
host: '127.0.0.1',
port: 5173,
strictPort: true,
// Don't watch the Rust build tree: src-tauri/target holds thousands of
// build artifacts that otherwise exhaust the kernel's inotify watch limit
// and crash the dev server's file watcher.
watch: {
ignored: ['**/src-tauri/**'],
},
},
define: {
__FRONTEND_VERSIONS__: JSON.stringify({
react: cleanVer(pkg.dependencies?.['react'] ?? '?'),
typescript: cleanVer(pkg.devDependencies?.['typescript'] ?? '?'),
tailwindcss: cleanVer(pkg.devDependencies?.['tailwindcss'] ?? '?'),
monaco: cleanVer(pkg.dependencies?.['monaco-editor'] ?? cleanVer(pkg.dependencies?.['@monaco-editor/react'] ?? '?')),
vite: cleanVer(pkg.devDependencies?.['vite'] ?? '?'),
}),
},
})