Files
goodbuddy/src/main/lazy-require.ts
T
mesalogo 010aafb6f4 perf(startup): load canvas, node-pty, ssh2, html-to-text and the OpenCode SDK on first use
Main module evaluation drops from ~420 ms to ~330 ms. Types stay as import type; the DeepSeek Harness chunks stay static because the protocol chunk shares code with Main startup paths.
2026-10-03 17:15:13 +08:00

15 lines
494 B
TypeScript

import { createRequire } from 'node:module'
/**
* Synchronously loads a dependency on first use instead of at module
* evaluation (P6). Main is ESM, but some test fixtures bundle the same code as
* CommonJS, where `import.meta.url` is empty and `__filename` exists instead.
*/
export function createLazyRequire<T>(specifier: string): () => T {
let loaded: T | undefined
return () => {
loaded ??= createRequire(import.meta.url || __filename)(specifier) as T
return loaded
}
}