@@ -82,28 +82,8 @@ const getRequestKey = (dependency) => {
8282} ;
8383
8484/**
85- * Returns the module requests of the import entries.
86- * @param {Dependency[] } dependencies dependencies of the barrel
87- * @returns {Set<string> } import entry requests
88- */
89- const getImportEntryRequests = ( dependencies ) => {
90- /** @type {Set<string> } */
91- const requests = new Set ( ) ;
92- for ( const dep of dependencies ) {
93- if (
94- getLazyUntil ( dep ) !== null ||
95- dep . category !== Dependency . ESM_CATEGORY
96- ) {
97- continue ;
98- }
99- const resourceIdent = dep . getResourceIdentifier ( ) ;
100- if ( resourceIdent !== null ) requests . add ( resourceIdent ) ;
101- }
102- return requests ;
103- } ;
104-
105- /**
106- * Returns whether import declarations can be deferred: only names not provided locally were requested.
85+ * Returns true if names were requested and none of them is a local export.
86+ * Only local exports use the import declarations, so the barrel can defer them until a local export is requested.
10787 * @param {Set<string> | true | undefined } forwardedIds names requested before classification
10888 * @param {Set<string> } localIds locally provided export names
10989 * @returns {boolean } true, when import declarations can be deferred
@@ -424,11 +404,9 @@ class LazyBarrels {
424404 let info ;
425405 /** @type {Set<string> } */
426406 const localIds = new Set ( ) ;
427- /** @type {Set<string> } */
428- const reexportRequests = new Set ( ) ;
429407 let hasFallbackStarReexport = false ;
430408
431- // re-exports
409+ // re-export starts deferred and this compilation's requests activate the ones it needs
432410 for ( const dep of dependencies ) {
433411 const until = getLazyUntil ( dep ) ;
434412 if ( until === Dependency . LAZY_UNTIL_LOCAL ) {
@@ -441,50 +419,39 @@ class LazyBarrels {
441419 if ( factory === undefined ) continue ;
442420 const requestKey = getRequestKey ( dep ) ;
443421 if ( info === undefined ) info = new LazyBarrelInfo ( ) ;
422+ dep . setLazy ( true ) ;
444423 if ( dep . isLazy ( ) ) info . addLazy ( requestKey , dep , factory ) ;
445424 if ( until === Dependency . LAZY_UNTIL_ID ) {
446425 info . addForwardId (
447426 /** @type {string } */ ( dep . getLazyName ( ) ) ,
448427 requestKey
449428 ) ;
450- reexportRequests . add ( requestKey ) ;
451429 } else if ( until === Dependency . LAZY_UNTIL_FALLBACK ) {
452430 info . addFallback ( requestKey ) ;
453- reexportRequests . add ( requestKey ) ;
454431 hasFallbackStarReexport = true ;
455432 }
456433 }
457434
458435 // import declarations
436+ const deferImports = canDeferImports ( forwardedIds , localIds ) ;
459437 /** @type {Set<string> | undefined } */
460438 let importRequests ;
461- if ( canDeferImports ( forwardedIds , localIds ) ) {
462- const importEntryRequests = getImportEntryRequests ( dependencies ) ;
463- for ( const dep of dependencies ) {
464- const until = getLazyUntil ( dep ) ;
465- // Only for ESM specifiers (null) and ESM side-effect ("@")
466- if (
467- ( until !== null && until !== Dependency . LAZY_UNTIL_REQUEST ) ||
468- dep . category !== Dependency . ESM_CATEGORY
469- ) {
470- continue ;
471- }
472- const factory = this . _getDepFactory ( dep ) ;
473- if ( factory === undefined || dep . isLazy ( ) === false ) continue ;
474- const requestKey = getRequestKey ( dep ) ;
475- // a re-export's own side-effect import stays in the re-export group
476- if (
477- reexportRequests . has ( requestKey ) &&
478- ! importEntryRequests . has ( requestKey )
479- ) {
480- continue ;
481- }
482- if ( info === undefined ) info = new LazyBarrelInfo ( ) ;
483- dep . setLazy ( true ) ;
484- info . addLazy ( requestKey , dep , factory ) ;
485- if ( importRequests === undefined ) importRequests = new Set ( ) ;
486- importRequests . add ( requestKey ) ;
439+ for ( const dep of dependencies ) {
440+ if (
441+ getLazyUntil ( dep ) !== null ||
442+ dep . category !== Dependency . ESM_CATEGORY
443+ ) {
444+ continue ;
487445 }
446+ const factory = this . _getDepFactory ( dep ) ;
447+ if ( factory === undefined ) continue ;
448+ dep . setLazy ( deferImports ) ;
449+ if ( ! deferImports ) continue ;
450+ const requestKey = getRequestKey ( dep ) ;
451+ if ( info === undefined ) info = new LazyBarrelInfo ( ) ;
452+ if ( dep . isLazy ( ) ) info . addLazy ( requestKey , dep , factory ) ;
453+ if ( importRequests === undefined ) importRequests = new Set ( ) ;
454+ importRequests . add ( requestKey ) ;
488455 }
489456
490457 if ( info === undefined ) return ;
0 commit comments