diff --git a/packages/desktop/electron.vite.config.ts b/packages/desktop/electron.vite.config.ts index 6dd68067f33..2db653a6f38 100644 --- a/packages/desktop/electron.vite.config.ts +++ b/packages/desktop/electron.vite.config.ts @@ -28,6 +28,29 @@ const sentry = }) : false +// Every module the entry reaches through static imports lands in one chunk. Automatic splitting +// otherwise fragments the initial graph into ~50 files shared with lazy routes, and each file costs +// the renderer a main-thread request round trip through the main process before first paint. +type ChunkingContext = { getModuleInfo(id: string): { isEntry: boolean; importers: readonly string[] } | null } +const initialGraph = new WeakMap>() +function inInitialGraph(id: string, ctx: ChunkingContext) { + const memo = initialGraph.get(ctx) ?? new Map() + initialGraph.set(ctx, memo) + const visit = (id: string, path: Set): boolean => { + const known = memo.get(id) + if (known !== undefined) return known + if (path.has(id)) return false + const info = ctx.getModuleInfo(id) + if (!info) return false + path.add(id) + const result = info.isEntry || info.importers.some((importer) => visit(importer, path)) + path.delete(id) + memo.set(id, result) + return result + } + return visit(id, new Set()) +} + export default defineConfig(({ command }) => ({ main: { resolve: { @@ -110,6 +133,11 @@ const require = __cjs_mod__.createRequire(import.meta.url); input: { main: "src/renderer/index.html", }, + output: { + codeSplitting: { + groups: [{ name: (id, ctx) => (inInitialGraph(id, ctx) ? "app" : null), priority: 10 }], + }, + }, }, }, },