Commit db98e01
authored
🤖 Lazy-load IpcMain to reduce startup time & lowercase package name (#223)
## Problem
App startup took 6+ seconds for Electron users because `main.ts`
immediately loaded `IpcMain`, which transitively imports the entire AI
SDK stack (`ai`, `@ai-sdk/anthropic`, `ai-tokenizer`, etc.). These are
large modules that weren't needed until the window was created and the
user started interacting.
Additionally, the package was being built as "Cmux" instead of "cmux".
## Solution
### 1. Lazy-load IpcMain
Convert static imports to dynamic imports in `createWindow()`. Config,
IpcMain, and tokenizer modules now load on-demand when the window is
created, not at app startup.
**Before:**
```typescript
import { Config } from './config';
import { IpcMain } from './services/ipcMain';
const config = new Config();
const ipcMain = new IpcMain(config); // Heavy AI SDK loaded immediately
```
**After:**
```typescript
import type { Config } from './config';
import type { IpcMain } from './services/ipcMain';
let config: Config | null = null;
let ipcMain: IpcMain | null = null;
async function createWindow() {
if (!config || !ipcMain) {
// Load only when needed
const [{ Config: ConfigClass }, { IpcMain: IpcMainClass }] = await Promise.all([
import('./config'),
import('./services/ipcMain'),
]);
config = new ConfigClass();
ipcMain = new IpcMainClass(config);
}
// ... create window
}
```
### 2. Lowercase package name
Changed `productName` from "Cmux" to "cmux" in `package.json` build
config.
## Implementation Details
- Changed Config/IpcMain/loadTokenizerModules imports to type-only
imports
- Created module-level variables to cache loaded modules
- Made `createWindow()` async to await dynamic imports
- Moved tokenizer loading to after window creation
- Fixed e2e test userData path (can't use `config.rootDir` before config
loads)
- Added ESLint justification for dynamic imports
## Testing
- ✅ Unit tests pass (379 tests)
- ✅ Type checking passes
- ✅ ESLint passes
_Generated with `cmux`_1 parent acb2863 commit db98e01
2 files changed
+49
-17
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | | - | |
| 105 | + | |
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
| 8 | + | |
| 9 | + | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
44 | 47 | | |
45 | 48 | | |
46 | 49 | | |
47 | 50 | | |
48 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
49 | 56 | | |
50 | 57 | | |
51 | 58 | | |
| |||
175 | 182 | | |
176 | 183 | | |
177 | 184 | | |
178 | | - | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
179 | 209 | | |
180 | 210 | | |
181 | 211 | | |
| |||
235 | 265 | | |
236 | 266 | | |
237 | 267 | | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | 268 | | |
246 | 269 | | |
247 | 270 | | |
| |||
255 | 278 | | |
256 | 279 | | |
257 | 280 | | |
258 | | - | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
259 | 291 | | |
260 | 292 | | |
261 | 293 | | |
| |||
269 | 301 | | |
270 | 302 | | |
271 | 303 | | |
272 | | - | |
| 304 | + | |
273 | 305 | | |
274 | 306 | | |
275 | 307 | | |
0 commit comments