-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathvite.config.ts
More file actions
383 lines (351 loc) · 11.1 KB
/
vite.config.ts
File metadata and controls
383 lines (351 loc) · 11.1 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
381
382
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.
*
* Chunk strategy:
* - Heavy vendor libs (monaco, xterm, shiki) get their own chunks for lazy loading
* - Framework code (solid, tauri) is split for long-term caching
* - App source contexts are split so they load after first paint
* - Small remaining node_modules fall into vendor-common
*/
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 (~2.6MB), separate chunk for on-demand loading
// Loaded lazily via dynamic import() in monacoManager.ts
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";
}
// Emmet abbreviation engine (~1.1MB source) - only needed when editing HTML/JSX
if (id.includes("node_modules/emmet") || id.includes("node_modules/@emmetio")) {
return "vendor-emmet";
}
// Marked markdown parser
if (id.includes("marked")) {
return "vendor-marked";
}
// Kobalte UI components (headless component library)
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";
}
// Zustand state management + Immer
if (
id.includes("node_modules/zustand") ||
id.includes("node_modules/solid-zustand") ||
id.includes("node_modules/immer")
) {
return "vendor-zustand";
}
// 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 ({ command }): Promise<UserConfig> => {
const isProd = command === "build";
return {
plugins: [
solid({
hot: true,
ssr: false,
include: [
/\.tsx$/,
/\.jsx$/,
],
solid: {
omitNestedClosingTags: true,
delegateEvents: true,
wrapConditionals: true,
generate: "dom",
hydratable: false,
},
}),
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: [
// SolidJS core
"solid-js",
"solid-js/store",
"solid-js/web",
"@solidjs/router",
// Tauri IPC (used on every page)
"@tauri-apps/api/core",
"@tauri-apps/api/event",
"@tauri-apps/plugin-dialog",
"@tauri-apps/plugin-clipboard-manager",
"@tauri-apps/plugin-shell",
"@tauri-apps/plugin-os",
// Large libs that benefit from pre-bundling (many internal modules)
"monaco-editor",
"shiki",
"emmet",
"@xterm/xterm",
// Utilities
"marked",
"diff",
],
esbuildOptions: {
target: "es2022",
treeShaking: true,
minify: true,
keepNames: true,
},
noDiscovery: false,
holdUntilCrawlEnd: true,
},
// Build configuration
build: {
minify: "esbuild",
// Tauri v2 WebViews: macOS Safari 15+, Windows Edge/Chromium 91+, Linux WebKitGTK 2.36+
target: "es2022",
// Source maps only during development builds; disabled for production
sourcemap: isProd ? false : "inline",
cssCodeSplit: true,
// Monaco + workers are inherently large; suppress noise
chunkSizeWarningLimit: 1500,
// Prevent heavy lazy chunks from being preloaded before first paint
modulePreload: {
resolveDependencies: (_filename, deps) => {
const heavyChunks = [
'app-context-debug',
'app-context-tasks',
'app-context-terminals',
'app-context-testing',
'app-context-lsp',
'app-context-extensions',
'app-extension-host',
'AppCore',
'EditorPanel',
'vendor-monaco',
'vendor-emmet',
'vendor-shiki',
'vendor-xterm',
];
return deps.filter(dep =>
!heavyChunks.some(chunk => dep.includes(chunk))
);
},
},
rollupOptions: {
output: {
manualChunks: createManualChunks,
entryFileNames: "assets/[name]-[hash].js",
chunkFileNames: "assets/[name]-[hash].js",
assetFileNames: "assets/[name]-[hash][extname]",
compact: true,
preserveModules: false,
hoistTransitiveImports: true,
},
treeshake: {
moduleSideEffects: (id) => {
if (id.endsWith(".css")) return true;
if (id.includes("@tauri-apps")) return true;
return false;
},
propertyReadSideEffects: false,
annotations: true,
},
},
reportCompressedSize: false,
},
// CSS configuration
css: {
modules: {
generateScopedName: "[hash:base64:8]",
scopeBehaviour: "local",
},
devSourcemap: true,
},
// Esbuild configuration
esbuild: {
drop: isProd ? ["console", "debugger"] : [],
target: "es2022",
treeShaking: true,
legalComments: "none",
},
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/**"],
},
warmup: {
clientFiles: [
"./src/index.tsx",
"./src/AppShell.tsx",
"./src/AppCore.tsx",
"./src/pages/Home.tsx",
"./src/pages/Session.tsx",
"./src/components/MenuBar.tsx",
"./src/components/cortex/CortexDesktopLayout.tsx",
"./src/context/OptimizedProviders.tsx",
"./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",
"./src/context/SDKContext.tsx",
"./src/context/SessionContext.tsx",
"./src/context/EditorContext.tsx",
"./src/context/WorkspaceContext.tsx",
"./src/context/CommandContext.tsx",
"./src/context/KeymapContext.tsx",
"./src/design-system/tokens/index.ts",
"./src/design-system/primitives/Flex.tsx",
],
},
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: {
namedExports: true,
stringify: true,
},
// Define global constants (dead code elimination)
define: {
__DEV__: JSON.stringify(!isProd),
__VERSION__: JSON.stringify(process.env.npm_package_version || "0.1.0"),
},
};
});