-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathvite.config.ts
More file actions
381 lines (352 loc) · 11.2 KB
/
Copy pathvite.config.ts
File metadata and controls
381 lines (352 loc) · 11.2 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import { defineConfig, type UserConfig } from "vite";
import solid from "vite-plugin-solid";
import tailwindcss from "@tailwindcss/vite";
import { visualizer } from "rollup-plugin-visualizer";
import path from "path";
// Check if we're running bundle analysis
const isAnalyze = process.env.ANALYZE === "true";
const host = process.env.TAURI_DEV_HOST;
/**
* Manual chunk configuration for optimal code splitting.
* Separates large dependencies into individual chunks for better caching
* and parallel loading.
*/
function createManualChunks(id: string): string | undefined {
// ===========================================================================
// SOURCE CODE SPLITTING - Split heavy app modules for lazy loading
// ===========================================================================
// Extension host system - ~590KB, loaded lazily when extensions are activated
if (id.includes("/extension-host/") && !id.includes("node_modules")) {
return "app-extension-host";
}
// Heavy context providers - defer loading to after first paint
if (id.includes("/context/DebugContext") && !id.includes("node_modules")) {
return "app-context-debug";
}
if (id.includes("/context/TasksContext") && !id.includes("node_modules")) {
return "app-context-tasks";
}
if (id.includes("/context/TerminalsContext") && !id.includes("node_modules")) {
return "app-context-terminals";
}
if (id.includes("/context/TestingContext") && !id.includes("node_modules")) {
return "app-context-testing";
}
if (id.includes("/context/LSPContext") && !id.includes("node_modules")) {
return "app-context-lsp";
}
if (id.includes("/context/ExtensionsContext") && !id.includes("node_modules")) {
return "app-context-extensions";
}
// ===========================================================================
// VENDOR SPLITTING - External dependencies
// ===========================================================================
// Monaco Editor - Large library, separate chunk for lazy loading
if (id.includes("monaco-editor") || id.includes("@monaco-editor")) {
return "vendor-monaco";
}
// Xterm terminal - Split for progressive loading
if (id.includes("@xterm/xterm")) {
return "vendor-xterm-core";
}
if (id.includes("@xterm/addon-webgl")) {
return "vendor-xterm-webgl";
}
if (id.includes("@xterm/addon")) {
return "vendor-xterm-addons";
}
// Shiki syntax highlighter - Split into core + languages for better lazy loading
if (id.includes("shiki")) {
// Shiki WASM engine - load separately (required for highlighting)
if (id.includes("onig.wasm") || id.includes("/wasm")) {
return "vendor-shiki-wasm";
}
// Shiki themes - only github-dark is used, but bundle includes others
if (id.includes("/themes/")) {
return "vendor-shiki-themes";
}
// Shiki languages - split by usage frequency
if (id.includes("/langs/")) {
// High priority: JS/TS ecosystem (most common in IDE)
if (id.match(/\/(javascript|typescript|jsx|tsx|json)\./)) {
return "vendor-shiki-lang-js";
}
// Web languages
if (id.match(/\/(html|css|scss|markdown|xml)\./)) {
return "vendor-shiki-lang-web";
}
// Scripting languages
if (id.match(/\/(python|ruby|php|bash|shell)\./)) {
return "vendor-shiki-lang-script";
}
// Systems languages
if (id.match(/\/(rust|go|c|cpp|java|kotlin|swift)\./)) {
return "vendor-shiki-lang-systems";
}
// Everything else - rarely used
return "vendor-shiki-lang-other";
}
// Core shiki engine
return "vendor-shiki-core";
}
// Marked markdown parser
if (id.includes("marked")) {
return "vendor-marked";
}
// Kobalte UI components
if (id.includes("@kobalte")) {
return "vendor-kobalte";
}
// Solid.js ecosystem (core + router + primitives)
if (
id.includes("solid-js") ||
id.includes("@solidjs/router") ||
id.includes("@solid-primitives")
) {
return "vendor-solid";
}
// Tauri plugins - Group all Tauri-related code
if (id.includes("@tauri-apps")) {
return "vendor-tauri";
}
// Diff library
if (id.includes("node_modules/diff")) {
return "vendor-diff";
}
// Generic node_modules fallback (small remaining dependencies)
if (id.includes("node_modules")) {
return "vendor-common";
}
return undefined;
}
export default defineConfig(async (): Promise<UserConfig> => ({
plugins: [
solid({
hot: true,
include: [
/\.tsx$/,
/\.jsx$/,
],
}),
tailwindcss(),
// Bundle analyzer - only in analyze mode
isAnalyze && visualizer({
open: true,
filename: "dist/bundle-stats.html",
gzipSize: true,
brotliSize: true,
template: "treemap",
}),
].filter(Boolean),
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
// Optimize module resolution
dedupe: ["solid-js", "@solidjs/router"],
},
// Dependency optimization for dev server
optimizeDeps: {
include: [
"solid-js",
"solid-js/store",
"solid-js/web",
"@tauri-apps/api/core",
"@tauri-apps/api/event",
"@solidjs/router",
"@tauri-apps/plugin-dialog",
"@tauri-apps/plugin-clipboard-manager",
"@tauri-apps/plugin-shell",
"@tauri-apps/plugin-os",
"marked",
"diff",
],
esbuildOptions: {
target: "es2021",
treeShaking: true,
// Minify pre-bundled deps for faster parsing
minify: true,
// Keep names for debugging
keepNames: true,
},
// Don't wait for first request - prebundle immediately
noDiscovery: false,
// Hold until deps are optimized
holdUntilCrawlEnd: true,
},
// Build configuration
build: {
// Use esbuild for faster minification (default in Vite 5+, but explicit)
minify: "esbuild",
target: "es2021",
// Enable source maps for production debugging (optional, remove for smaller builds)
sourcemap: false,
// CSS code splitting
cssCodeSplit: true,
// Increase chunk size warning limit (Monaco is large)
chunkSizeWarningLimit: 1000,
// CRITICAL: Disable modulepreload polyfill to prevent heavy chunks from being preloaded
// This allows lazy chunks (AppCore, contexts) to load AFTER first paint
modulePreload: {
// Only preload essential entry chunks, not lazy-loaded ones
resolveDependencies: (filename, deps) => {
// Don't preload heavy app chunks - they should load lazily
const heavyChunks = [
'app-context-debug',
'app-context-tasks',
'app-context-terminals',
'app-context-testing',
'app-context-lsp',
'app-context-extensions',
'app-extension-host',
'AppCore',
'vendor-monaco',
'vendor-shiki',
'vendor-xterm',
];
return deps.filter(dep =>
!heavyChunks.some(chunk => dep.includes(chunk))
);
},
},
// Rollup-specific options
rollupOptions: {
output: {
// Manual chunk splitting for optimal caching
manualChunks: createManualChunks,
// Use content hashing for cache busting
entryFileNames: "assets/[name]-[hash].js",
chunkFileNames: "assets/[name]-[hash].js",
assetFileNames: "assets/[name]-[hash][extname]",
// Optimize chunk loading
compact: true,
// Preserve module structure for better tree-shaking
preserveModules: false,
// Minimize side effects
hoistTransitiveImports: true,
},
// Tree-shake unused exports
treeshake: {
// Aggressive tree-shaking
moduleSideEffects: (id) => {
// CSS files have side effects
if (id.endsWith(".css")) return true;
// Tauri plugins may have side effects
if (id.includes("@tauri-apps")) return true;
// Everything else is tree-shakeable
return false;
},
// Remove unused property reads
propertyReadSideEffects: false,
// Remove annotations from output
annotations: true,
},
},
reportCompressedSize: false,
},
// CSS configuration
css: {
// CSS modules configuration
modules: {
// Generate scoped class names in production
generateScopedName: "[hash:base64:8]",
// Local scope by default
scopeBehaviour: "local",
},
// PostCSS/preprocessor options
devSourcemap: true,
},
// Esbuild configuration
esbuild: {
// Remove console.log and debugger in production
drop: process.env.NODE_ENV === "production" ? ["console", "debugger"] : [],
target: "es2021",
// Enable tree-shaking
treeShaking: true,
// Legal comments handling
legalComments: "none",
},
// Clear screen disabled for Tauri integration
clearScreen: false,
// Development server configuration
server: {
port: 1420,
strictPort: true,
host: host || false,
hmr: host
? {
protocol: "ws",
host,
port: 1421,
}
: undefined,
watch: {
ignored: ["**/src-tauri/**"],
},
// Warm up ALL critical files for instant navigation
warmup: {
// Pre-transform critical entry points and frequently used modules
clientFiles: [
// Entry points
"./src/index.tsx",
"./src/AppShell.tsx",
"./src/AppCore.tsx",
// Pages
"./src/pages/Home.tsx",
"./src/pages/Session.tsx",
// Core layout
"./src/components/MenuBar.tsx",
"./src/components/cortex/CortexDesktopLayout.tsx",
// All providers (critical for startup)
"./src/context/OptimizedProviders.tsx",
// Tier 1 providers
"./src/context/I18nContext.tsx",
"./src/context/ThemeContext.tsx",
"./src/context/CortexColorThemeContext.tsx",
"./src/context/ToastContext.tsx",
"./src/context/SettingsContext.tsx",
"./src/context/WindowsContext.tsx",
"./src/context/LayoutContext.tsx",
// Tier 2+ providers
"./src/context/SDKContext.tsx",
"./src/context/SessionContext.tsx",
"./src/context/EditorContext.tsx",
"./src/context/WorkspaceContext.tsx",
"./src/context/CommandContext.tsx",
"./src/context/KeymapContext.tsx",
// Design system
"./src/design-system/tokens/index.ts",
"./src/design-system/primitives/Flex.tsx",
],
},
// Pre-warm dependencies
preTransformRequests: true,
},
// Preview server (for testing production builds)
preview: {
port: 1421,
strictPort: true,
host: host || false,
},
// Worker configuration for web workers
worker: {
format: "es",
rollupOptions: {
output: {
entryFileNames: "assets/worker-[name]-[hash].js",
},
},
},
// JSON handling optimization
json: {
// Enable named exports for tree-shaking JSON imports
namedExports: true,
// Stringify JSON for smaller bundles
stringify: true,
},
// Define global constants (dead code elimination)
define: {
__DEV__: JSON.stringify(process.env.NODE_ENV !== "production"),
__VERSION__: JSON.stringify(process.env.npm_package_version || "0.1.0"),
},
}));