@@ -303,6 +303,7 @@ let printedWatchWarning = false;
303
303
* @param {string } options.srcEntrypoint
304
304
* @param {string } options.builtEntrypoint
305
305
* @param {string } options.output
306
+ * @param {boolean } [options.enableCompileCache]
306
307
* @param {Task[] } [options.mainDeps]
307
308
* @param {BundlerTaskOptions } [options.bundlerOptions]
308
309
*/
@@ -313,7 +314,37 @@ function entrypointBuildTask(options) {
313
314
run : ( ) => buildProject ( options . project ) ,
314
315
} ) ;
315
316
316
- const bundler = createBundler ( options . srcEntrypoint , options . output , options . bundlerOptions ) ;
317
+ const mainDeps = options . mainDeps ?. slice ( 0 ) ?? [ ] ;
318
+
319
+ let output = options . output ;
320
+ if ( options . enableCompileCache ) {
321
+ const originalOutput = output ;
322
+ output = path . join ( path . dirname ( output ) , "_" + path . basename ( output ) ) ;
323
+
324
+ const compileCacheShim = task ( {
325
+ name : `shim-compile-cache-${ options . name } ` ,
326
+ run : async ( ) => {
327
+ const outDir = path . dirname ( originalOutput ) ;
328
+ await fs . promises . mkdir ( outDir , { recursive : true } ) ;
329
+ const moduleSpecifier = path . relative ( outDir , output ) ;
330
+ const lines = [
331
+ `// This file is a shim which defers loading the real module until the compile cache is enabled.` ,
332
+ `try {` ,
333
+ ` const { enableCompileCache } = require("node:module");` ,
334
+ ` if (enableCompileCache) {` ,
335
+ ` enableCompileCache();` ,
336
+ ` }` ,
337
+ `} catch {}` ,
338
+ `module.exports = require("./${ moduleSpecifier . replace ( / [ \\ / ] / g, "/" ) } ");` ,
339
+ ] ;
340
+ await fs . promises . writeFile ( originalOutput , lines . join ( "\n" ) + "\n" ) ;
341
+ } ,
342
+ } ) ;
343
+
344
+ mainDeps . push ( compileCacheShim ) ;
345
+ }
346
+
347
+ const bundler = createBundler ( options . srcEntrypoint , output , options . bundlerOptions ) ;
317
348
318
349
// If we ever need to bundle our own output, change this to depend on build
319
350
// and run esbuild on builtEntrypoint.
@@ -336,14 +367,13 @@ function entrypointBuildTask(options) {
336
367
const shim = task ( {
337
368
name : `shim-${ options . name } ` ,
338
369
run : async ( ) => {
339
- const outDir = path . dirname ( options . output ) ;
370
+ const outDir = path . dirname ( output ) ;
340
371
await fs . promises . mkdir ( outDir , { recursive : true } ) ;
341
372
const moduleSpecifier = path . relative ( outDir , options . builtEntrypoint ) ;
342
- await fs . promises . writeFile ( options . output , `module.exports = require("./${ moduleSpecifier . replace ( / [ \\ / ] / g, "/" ) } ")` ) ;
373
+ await fs . promises . writeFile ( output , `module.exports = require("./${ moduleSpecifier . replace ( / [ \\ / ] / g, "/" ) } ")` ) ;
343
374
} ,
344
375
} ) ;
345
376
346
- const mainDeps = options . mainDeps ?. slice ( 0 ) ?? [ ] ;
347
377
if ( cmdLineOptions . bundle ) {
348
378
mainDeps . push ( bundle ) ;
349
379
if ( cmdLineOptions . typecheck ) {
@@ -392,6 +422,7 @@ const { main: tsc, watch: watchTsc } = entrypointBuildTask({
392
422
builtEntrypoint : "./built/local/tsc/tsc.js" ,
393
423
output : "./built/local/tsc.js" ,
394
424
mainDeps : [ generateLibs ] ,
425
+ enableCompileCache : true ,
395
426
} ) ;
396
427
export { tsc , watchTsc } ;
397
428
@@ -429,6 +460,7 @@ const { main: tsserver, watch: watchTsserver } = entrypointBuildTask({
429
460
output : "./built/local/tsserver.js" ,
430
461
mainDeps : [ generateLibs , services ] ,
431
462
bundlerOptions : { usePublicAPI : true } ,
463
+ enableCompileCache : true ,
432
464
} ) ;
433
465
export { tsserver , watchTsserver } ;
434
466
@@ -589,6 +621,7 @@ const { main: typingsInstaller, watch: watchTypingsInstaller } = entrypointBuild
589
621
output : "./built/local/typingsInstaller.js" ,
590
622
mainDeps : [ services ] ,
591
623
bundlerOptions : { usePublicAPI : true } ,
624
+ enableCompileCache : true ,
592
625
} ) ;
593
626
594
627
const { main : watchGuard , watch : watchWatchGuard } = entrypointBuildTask ( {
@@ -885,12 +918,15 @@ export const produceLKG = task({
885
918
const expectedFiles = [
886
919
"built/local/cancellationToken.js" ,
887
920
"built/local/tsc.js" ,
921
+ "built/local/_tsc.js" ,
888
922
"built/local/tsserver.js" ,
923
+ "built/local/_tsserver.js" ,
889
924
"built/local/tsserverlibrary.js" ,
890
925
"built/local/tsserverlibrary.d.ts" ,
891
926
"built/local/typescript.js" ,
892
927
"built/local/typescript.d.ts" ,
893
928
"built/local/typingsInstaller.js" ,
929
+ "built/local/_typingsInstaller.js" ,
894
930
"built/local/watchGuard.js" ,
895
931
] . concat ( libs ( ) . map ( lib => lib . target ) ) ;
896
932
const missingFiles = expectedFiles
0 commit comments