-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathvite.config.mts
More file actions
149 lines (139 loc) · 4.71 KB
/
vite.config.mts
File metadata and controls
149 lines (139 loc) · 4.71 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
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import { readFileSync } from 'fs';
import { execSync } from 'child_process';
// Read version from package.json as fallback
const packageJson = JSON.parse(readFileSync(path.join(__dirname, 'package.json'), 'utf-8'));
// Use VITE_APP_VERSION env var if set (during CI builds), otherwise use package.json
const appVersion = process.env.VITE_APP_VERSION || packageJson.version;
// Get the first 8 chars of git commit hash for dev mode
function getCommitHash(): string {
try {
// Note: execSync is safe here - no user input, static git command
return execSync('git rev-parse HEAD', { encoding: 'utf-8' }).trim().slice(0, 8);
} catch {
return '';
}
}
const disableHmr = process.env.DISABLE_HMR === '1';
export default defineConfig(({ mode }) => ({
plugins: [
react({ fastRefresh: !disableHmr }),
// In dev mode, relax CSP to allow Vite's inline HMR/React Refresh scripts
mode === 'development' && {
name: 'dev-csp-relaxation',
transformIndexHtml(html: string) {
return html.replace(
"script-src 'self'",
"script-src 'self' 'unsafe-inline' http://localhost:*"
);
},
},
].filter(Boolean),
root: path.join(__dirname, 'src/renderer'),
base: './',
define: {
__APP_VERSION__: JSON.stringify(appVersion),
// Show commit hash only in development mode
__COMMIT_HASH__: JSON.stringify(mode === 'development' ? getCommitHash() : ''),
// Explicitly define NODE_ENV for React and related packages
'process.env.NODE_ENV': JSON.stringify(mode),
},
resolve: {
alias: {
// In development, use wdyr.dev.ts which loads why-did-you-render
// In production, use wdyr.ts which is empty (prevents bundling the library)
'./wdyr':
mode === 'development'
? path.join(__dirname, 'src/renderer/wdyr.dev.ts')
: path.join(__dirname, 'src/renderer/wdyr.ts'),
},
},
esbuild: {
// Strip console.* and debugger in production builds
drop: mode === 'production' ? ['console', 'debugger'] : [],
},
build: {
outDir: path.join(__dirname, 'dist/renderer'),
emptyOutDir: true,
rollupOptions: {
// Prevent esbuild from re-minifying xterm's pre-minified code.
// Double-minification corrupts variable scoping in requestMode(),
// causing "ReferenceError: e is not defined" at runtime.
plugins: [
{
name: 'skip-xterm-minify',
renderChunk(code, chunk) {
if (chunk.name === 'vendor-xterm') {
return { code, map: null };
}
return null;
},
},
],
output: {
// Manual chunking for better caching and code splitting
manualChunks: (id) => {
// React core in its own chunk for optimal caching
if (id.includes('node_modules/react-dom')) {
return 'vendor-react';
}
if (id.includes('node_modules/react/') || id.includes('node_modules/react-is')) {
return 'vendor-react';
}
if (id.includes('node_modules/scheduler')) {
return 'vendor-react';
}
// Terminal (xterm) in its own chunk - large and not immediately needed
if (id.includes('node_modules/@xterm') || id.includes('node_modules/xterm')) {
return 'vendor-xterm';
}
// Markdown processing libraries
if (
id.includes('node_modules/react-markdown') ||
id.includes('node_modules/remark-') ||
id.includes('node_modules/rehype-') ||
id.includes('node_modules/unified') ||
id.includes('node_modules/unist-') ||
id.includes('node_modules/mdast-') ||
id.includes('node_modules/hast-') ||
id.includes('node_modules/micromark')
) {
return 'vendor-markdown';
}
// Syntax highlighting (large)
if (
id.includes('node_modules/react-syntax-highlighter') ||
id.includes('node_modules/prismjs') ||
id.includes('node_modules/refractor')
) {
return 'vendor-syntax';
}
// Heavy visualization libraries (lazy-loaded components)
if (id.includes('node_modules/mermaid')) {
return 'vendor-mermaid';
}
if (id.includes('node_modules/recharts') || id.includes('node_modules/d3-')) {
return 'vendor-charts';
}
if (id.includes('node_modules/reactflow') || id.includes('node_modules/@reactflow')) {
return 'vendor-flow';
}
// Diff viewer
if (id.includes('node_modules/react-diff-view') || id.includes('node_modules/diff')) {
return 'vendor-diff';
}
// Return undefined to let Rollup handle other modules automatically
return undefined;
},
},
},
},
server: {
port: process.env.VITE_PORT ? parseInt(process.env.VITE_PORT, 10) : 5173,
hmr: !disableHmr,
// Disable file watching entirely when HMR is disabled to prevent any reloads
watch: disableHmr ? null : undefined,
},
}));