@@ -69,6 +69,8 @@ class LazyBarrelInfo {
6969 this . _terminalIds = undefined ;
7070 /** @type {Set<string> | undefined } requests of star re-exports */
7171 this . _fallbackRequests = undefined ;
72+ /** @type {Set<string> | undefined } ordinary import request keys */
73+ this . _ordinaryRequests = undefined ;
7274 }
7375
7476 /**
@@ -84,7 +86,9 @@ class LazyBarrelInfo {
8486 group = { factory, dependencies : [ ] , context } ;
8587 this . _requestToDepGroup . set ( requestKey , group ) ;
8688 }
87- group . dependencies . push ( dependency ) ;
89+ if ( ! group . dependencies . includes ( dependency ) ) {
90+ group . dependencies . push ( dependency ) ;
91+ }
8892 }
8993
9094 /**
@@ -116,6 +120,17 @@ class LazyBarrelInfo {
116120 this . _fallbackRequests . add ( requestKey ) ;
117121 }
118122
123+ /**
124+ * Records an ordinary import that is safe to defer while inspecting exports.
125+ * @param {string } requestKey request key
126+ */
127+ addOrdinary ( requestKey ) {
128+ if ( this . _ordinaryRequests === undefined ) {
129+ this . _ordinaryRequests = new Set ( ) ;
130+ }
131+ this . _ordinaryRequests . add ( requestKey ) ;
132+ }
133+
119134 /**
120135 * Returns whether nothing is deferred.
121136 * @returns {boolean } true, when no dependency is deferred
@@ -140,6 +155,7 @@ class LazyBarrelInfo {
140155 this . _forwardIdToRequest . clear ( ) ;
141156 this . _terminalIds = undefined ;
142157 this . _fallbackRequests = undefined ;
158+ this . _ordinaryRequests = undefined ;
143159 }
144160
145161 /**
@@ -165,6 +181,15 @@ class LazyBarrelInfo {
165181 this . _release ( ) ;
166182 return result ;
167183 }
184+ let requestOrdinary = false ;
185+ if ( this . _terminalIds !== undefined ) {
186+ for ( const id of forwardedIds ) {
187+ if ( this . _terminalIds . has ( id ) ) {
188+ requestOrdinary = true ;
189+ break ;
190+ }
191+ }
192+ }
168193 /**
169194 * @param {string } requestKey request key to take
170195 */
@@ -186,6 +211,9 @@ class LazyBarrelInfo {
186211 for ( const fallbackKey of this . _fallbackRequests ) take ( fallbackKey ) ;
187212 }
188213 }
214+ if ( requestOrdinary && this . _ordinaryRequests !== undefined ) {
215+ for ( const requestKey of this . _ordinaryRequests ) take ( requestKey ) ;
216+ }
189217 if ( this . _requestToDepGroup . size === 0 ) this . _release ( ) ;
190218 return result ;
191219 }
@@ -249,9 +277,35 @@ class LazyBarrelController {
249277 }
250278 const dependencies = module . dependencies ;
251279 const dependencyFactories = this . _compilation . dependencyFactories ;
280+ const state = modules . get ( module ) ;
281+ const forwardedIds = state && state . forwardedIds ;
282+ let inspectOnly =
283+ forwardedIds !== undefined &&
284+ forwardedIds !== true &&
285+ forwardedIds . size > 0 ;
286+ /** @type {Set<string> } */
287+ const terminalIds = new Set ( ) ;
288+ for ( const dep of dependencies ) {
289+ if (
290+ "getLazyUntil" in dep &&
291+ dep . getLazyUntil ( ) === Dependency . LAZY_UNTIL_LOCAL
292+ ) {
293+ const name = dep . getLazyName ( ) ;
294+ if ( typeof name === "string" ) terminalIds . add ( name ) ;
295+ }
296+ }
297+ if ( inspectOnly && forwardedIds !== undefined && forwardedIds !== true ) {
298+ for ( const id of forwardedIds ) {
299+ if ( terminalIds . has ( id ) ) {
300+ inspectOnly = false ;
301+ break ;
302+ }
303+ }
304+ }
252305 /** @type {LazyBarrelInfo | undefined } */
253306 let info ;
254307 let hasFallback = false ;
308+ let hasOrdinary = false ;
255309 for ( const dep of dependencies ) {
256310 // TODO in the next major release: remove
257311 // It may be missing on custom dependency types not extending the base Dependency
@@ -289,15 +343,41 @@ class LazyBarrelController {
289343 hasFallback = true ;
290344 }
291345 }
292- const state = modules . get ( module ) ;
346+ // Ordinary ESM imports are only deferred when this module is being built to
347+ // inspect a named star export and that name is not locally provided.
348+ if ( inspectOnly ) {
349+ for ( const dep of dependencies ) {
350+ if (
351+ "getLazyUntil" in dep &&
352+ ( dep . getLazyUntil ( ) === null ||
353+ dep . getLazyUntil ( ) === Dependency . LAZY_UNTIL_REQUEST ) &&
354+ dep . category === esmDependencyCategory &&
355+ "setLazy" in dep &&
356+ dep . getResourceIdentifier ( ) !== null
357+ ) {
358+ const factory = dependencyFactories . get (
359+ /** @type {DependencyConstructor } */ ( dep . constructor )
360+ ) ;
361+ if ( factory !== undefined ) {
362+ const resourceIdent = dep . getResourceIdentifier ( ) ;
363+ const requestKey = /** @type {string } */ ( resourceIdent ) ;
364+ if ( info === undefined ) info = new LazyBarrelInfo ( ) ;
365+ dep . setLazy ( true ) ;
366+ info . addLazy ( requestKey , dep , factory , dep . getContext ( ) ) ;
367+ info . addOrdinary ( requestKey ) ;
368+ hasOrdinary = true ;
369+ }
370+ }
371+ }
372+ }
293373 // info is only created once a deferrable target exists, so it is never empty here
294374 if ( info === undefined ) {
295375 if ( state !== undefined ) modules . delete ( module ) ;
296376 return false ;
297377 }
298378 // terminals only matter together with a star re-export (to avoid building it
299379 // for a locally-provided name); skip the extra pass otherwise
300- if ( hasFallback ) {
380+ if ( hasFallback || hasOrdinary ) {
301381 for ( const dep of dependencies ) {
302382 if (
303383 "getLazyUntil" in dep &&
@@ -341,8 +421,28 @@ class LazyBarrelController {
341421 const modules = this . _modules ;
342422 const state = modules . get ( module ) ;
343423
424+ const getRequestedIds = ( ) => {
425+ const directIds = getForwardedIds ( dependencies ) ;
426+ if ( directIds !== true && directIds . size !== 0 ) return directIds ;
427+ for ( const dependency of dependencies ) {
428+ if (
429+ "getLazyUntil" in dependency &&
430+ dependency . getLazyUntil ( ) === Dependency . LAZY_UNTIL_FALLBACK
431+ ) {
432+ const parent =
433+ this . _compilation . moduleGraph . getParentModule ( dependency ) ;
434+ const parentState =
435+ parent === undefined ? undefined : modules . get ( parent ) ;
436+ if ( parentState !== undefined ) {
437+ return parentState . forwardedIds ;
438+ }
439+ }
440+ }
441+ if ( directIds === true || directIds . size !== 0 ) return directIds ;
442+ return directIds ;
443+ } ;
344444 if ( state === undefined ) {
345- const forwardedIds = getForwardedIds ( dependencies ) ;
445+ const forwardedIds = getRequestedIds ( ) ;
346446 if ( forwardedIds !== true && forwardedIds . size === 0 ) return ;
347447 modules . set ( module , {
348448 forwardedIds,
@@ -351,7 +451,7 @@ class LazyBarrelController {
351451 return ;
352452 }
353453
354- const forwardedIds = getForwardedIds ( dependencies ) ;
454+ const forwardedIds = getRequestedIds ( ) ;
355455 if ( forwardedIds !== true && forwardedIds . size === 0 ) return ;
356456 if ( state . forwardedIds !== true ) {
357457 if ( forwardedIds === true ) state . forwardedIds = true ;
0 commit comments