@@ -81,7 +81,7 @@ export class BundlerContext {
8181 #optionsFactory: BundlerOptionsFactory < BuildOptions & { metafile : true ; write : false } > ;
8282 #shouldCacheResult: boolean ;
8383 #loadCache?: LoadResultCache ;
84- readonly #loadCacheKeys = new Set < string > ( ) ;
84+ #loadCacheKeys?: Set < string > ;
8585 #invalidationEpoch = 0 ;
8686 readonly watchFiles = new Set < string > ( ) ;
8787
@@ -94,9 +94,8 @@ export class BundlerContext {
9494 sharedLoadCache ?: LoadResultCache ,
9595 ) {
9696 this . #loadCache = sharedLoadCache ;
97- // In incremental mode, an option factory is needed to capture the full set of dependencies.
98- // In non-incremental mode, results can always be cached for the duration of the build.
99- this . #shouldCacheResult = ! incremental || typeof options === 'function' ;
97+ // To cache the results an option factory is needed to capture the full set of dependencies
98+ this . #shouldCacheResult = incremental && typeof options === 'function' ;
10099 this . #optionsFactory = ( ...args ) => {
101100 const baseOptions = typeof options === 'function' ? options ( ...args ) : options ;
102101
@@ -230,22 +229,25 @@ export class BundlerContext {
230229 this . #loadCache = new MemoryLoadResultCache ( ) ;
231230 }
232231 const loadCache = this . #loadCache;
233- const scopedLoadCache : LoadResultCache | undefined = loadCache && {
234- get : ( path ) => {
235- this . #loadCacheKeys. add ( path ) ;
236-
237- return loadCache . get ( path ) ;
238- } ,
239- put : ( path , result ) => {
240- this . #loadCacheKeys. add ( path ) ;
241-
242- return loadCache . put ( path , result ) ;
243- } ,
244- invalidate : ( path ) => loadCache . invalidate ( path ) ,
245- get watchFiles ( ) {
246- return loadCache . watchFiles ;
247- } ,
248- } ;
232+ const scopedLoadCache : LoadResultCache | undefined =
233+ this . incremental && loadCache
234+ ? {
235+ get : ( path ) => {
236+ ( this . #loadCacheKeys ??= new Set ( ) ) . add ( path ) ;
237+
238+ return loadCache . get ( path ) ;
239+ } ,
240+ put : ( path , result ) => {
241+ ( this . #loadCacheKeys ??= new Set ( ) ) . add ( path ) ;
242+
243+ return loadCache . put ( path , result ) ;
244+ } ,
245+ invalidate : ( path ) => loadCache . invalidate ( path ) ,
246+ get watchFiles ( ) {
247+ return loadCache . watchFiles ;
248+ } ,
249+ }
250+ : loadCache ;
249251 this . #esbuildOptions = this . #optionsFactory( scopedLoadCache ) ;
250252 }
251253
@@ -476,7 +478,7 @@ export class BundlerContext {
476478 }
477479
478480 async #addLoadCacheFilesToWatch( ) : Promise < void > {
479- if ( this . incremental && this . #loadCache) {
481+ if ( this . incremental && this . #loadCache && this . #loadCacheKeys ) {
480482 for ( const key of this . #loadCacheKeys) {
481483 const cachedResult = await this . #loadCache. get ( key ) ;
482484 if ( cachedResult ?. watchFiles ) {
@@ -585,7 +587,7 @@ export class BundlerContext {
585587 this . #esbuildResult = undefined ;
586588 this . #activeBundlePromise = undefined ;
587589 this . #loadCache = undefined ;
588- this . #loadCacheKeys. clear ( ) ;
590+ this . #loadCacheKeys = undefined ;
589591 await this . #esbuildContext?. dispose ( ) ;
590592 } finally {
591593 this . #esbuildContext = undefined ;
0 commit comments