|
1 | 1 | import { createHash } from 'node:crypto'; |
2 | 2 | import fs from 'node:fs'; |
3 | 3 | import fsp from 'node:fs/promises'; |
| 4 | +import module from 'node:module'; |
4 | 5 | import path from 'node:path'; |
5 | 6 | import { debuglog } from 'node:util'; |
6 | 7 |
|
@@ -240,6 +241,48 @@ export class ManifestStore { |
240 | 241 | } catch (err: any) { |
241 | 242 | if (err.code !== 'ENOENT') throw err; |
242 | 243 | } |
| 244 | + ManifestStore.cleanCompileCache(baseDir); |
| 245 | + } |
| 246 | + |
| 247 | + // --- Compile Cache --- |
| 248 | + |
| 249 | + /** |
| 250 | + * Enable Node.js module compile cache for the current process. |
| 251 | + * Sets NODE_COMPILE_CACHE and NODE_COMPILE_CACHE_PORTABLE env vars |
| 252 | + * so forked child processes also inherit compile cache. |
| 253 | + */ |
| 254 | + static enableCompileCache(baseDir: string): void { |
| 255 | + if (process.env.NODE_COMPILE_CACHE || process.env.NODE_DISABLE_COMPILE_CACHE) return; |
| 256 | + const cacheDir = path.join(baseDir, '.egg', 'compile-cache'); |
| 257 | + process.env.NODE_COMPILE_CACHE = cacheDir; |
| 258 | + process.env.NODE_COMPILE_CACHE_PORTABLE = '1'; |
| 259 | + try { |
| 260 | + const result = module.enableCompileCache?.(cacheDir); |
| 261 | + debug('compile cache enabled: %o', result); |
| 262 | + } catch (err) { |
| 263 | + debug('compile cache enable failed: %o', err); |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + /** |
| 268 | + * Flush accumulated compile cache entries to disk. |
| 269 | + */ |
| 270 | + static flushCompileCache(): void { |
| 271 | + try { |
| 272 | + module.flushCompileCache?.(); |
| 273 | + debug('compile cache flushed'); |
| 274 | + } catch (err) { |
| 275 | + debug('compile cache flush failed: %o', err); |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + /** |
| 280 | + * Remove the compile cache directory. |
| 281 | + */ |
| 282 | + static cleanCompileCache(baseDir: string): void { |
| 283 | + const compileCacheDir = path.join(baseDir, '.egg', 'compile-cache'); |
| 284 | + fs.rmSync(compileCacheDir, { recursive: true, force: true }); |
| 285 | + debug('compile cache removed: %s', compileCacheDir); |
243 | 286 | } |
244 | 287 |
|
245 | 288 | // --- Path Utilities --- |
|
0 commit comments