@@ -1513,12 +1513,13 @@ class ConcatenatedModule extends Module {
15131513 } ;
15141514
15151515 /**
1516+ * Lists a module reached by a connection, except a concatenated module's own entry, which follows its imports.
15161517 * @param {ModuleGraphConnection } connection graph connection
15171518 * @param {RuntimeSpec | true } runtimeCondition runtime condition
15181519 * @param {NonDeferAccess } nonDeferAccess non-defer access
1519- * @returns {void }
1520+ * @returns {Iterator<{ connection: ModuleGraphConnection, runtimeCondition: RuntimeSpec | true, nonDeferAccess: NonDeferAccess }> | undefined } the imports still to enter for a concatenated module
15201521 */
1521- const enterModule = ( connection , runtimeCondition , nonDeferAccess ) => {
1522+ const enterModuleOnce = ( connection , runtimeCondition , nonDeferAccess ) => {
15221523 const module = connection . module ;
15231524 if ( ! module ) return ;
15241525 const existingEntry = existingEntries . get ( module ) ;
@@ -1546,92 +1547,119 @@ class ConcatenatedModule extends Module {
15461547 `Cannot deferred concatenate a module (${ module . identifier ( ) } in ${ this . rootModule . identifier ( ) } . This should not happen.`
15471548 ) ;
15481549 }
1549- const imports = getConcatenatedImports ( module ) ;
1550- for ( const {
1551- connection,
1550+ return getConcatenatedImports ( module ) [ Symbol . iterator ] ( ) ;
1551+ }
1552+ /** @type {RuntimeSpec | boolean } */
1553+ let reducedRuntimeCondition ;
1554+ /** @type {NonDeferAccess } */
1555+ let reducedNonDeferAccess ;
1556+ if ( existingEntry !== undefined ) {
1557+ reducedRuntimeCondition = subtractRuntimeCondition (
15521558 runtimeCondition ,
1553- nonDeferAccess
1554- } of imports ) {
1555- enterModule ( connection , runtimeCondition , nonDeferAccess ) ;
1559+ existingEntry . runtimeCondition ,
1560+ runtime
1561+ ) ;
1562+ reducedNonDeferAccess = subtractNonDeferAccess (
1563+ nonDeferAccess ,
1564+ existingEntry . nonDeferAccess
1565+ ) ;
1566+ if (
1567+ reducedRuntimeCondition === false &&
1568+ reducedNonDeferAccess === false
1569+ ) {
1570+ return ;
15561571 }
1557- list . push ( {
1558- type : "concatenated" ,
1559- wrapped : needWrapped ( module ) ,
1560- module : connection . module ,
1572+ if ( reducedRuntimeCondition !== false ) {
1573+ existingEntry . runtimeCondition = mergeRuntimeConditionNonFalse (
1574+ existingEntry . runtimeCondition ,
1575+ reducedRuntimeCondition ,
1576+ runtime
1577+ ) ;
1578+ }
1579+ if ( reducedNonDeferAccess !== false ) {
1580+ existingEntry . nonDeferAccess = mergeNonDeferAccess (
1581+ existingEntry . nonDeferAccess ,
1582+ reducedNonDeferAccess
1583+ ) ;
1584+ }
1585+ } else {
1586+ reducedRuntimeCondition = runtimeCondition ;
1587+ reducedNonDeferAccess = nonDeferAccess ;
1588+ existingEntries . set ( connection . module , {
15611589 runtimeCondition,
15621590 nonDeferAccess
15631591 } ) ;
1564- } else {
1565- /** @type {RuntimeSpec | boolean } */
1566- let reducedRuntimeCondition ;
1567- /** @type {NonDeferAccess } */
1568- let reducedNonDeferAccess ;
1569- if ( existingEntry !== undefined ) {
1570- reducedRuntimeCondition = subtractRuntimeCondition (
1571- runtimeCondition ,
1572- existingEntry . runtimeCondition ,
1592+ }
1593+ if ( list . length > 0 ) {
1594+ const lastItem = list [ list . length - 1 ] ;
1595+ if (
1596+ lastItem . type === "external" &&
1597+ lastItem . module === connection . module
1598+ ) {
1599+ lastItem . runtimeCondition = mergeRuntimeCondition (
1600+ lastItem . runtimeCondition ,
1601+ reducedRuntimeCondition ,
15731602 runtime
15741603 ) ;
1575- reducedNonDeferAccess = subtractNonDeferAccess (
1576- nonDeferAccess ,
1577- existingEntry . nonDeferAccess
1604+ lastItem . nonDeferAccess = mergeNonDeferAccess (
1605+ lastItem . nonDeferAccess ,
1606+ reducedNonDeferAccess
15781607 ) ;
1579- if (
1580- reducedRuntimeCondition === false &&
1581- reducedNonDeferAccess === false
1582- ) {
1583- return ;
1584- }
1585- if ( reducedRuntimeCondition !== false ) {
1586- existingEntry . runtimeCondition = mergeRuntimeConditionNonFalse (
1587- existingEntry . runtimeCondition ,
1588- reducedRuntimeCondition ,
1589- runtime
1590- ) ;
1591- }
1592- if ( reducedNonDeferAccess !== false ) {
1593- existingEntry . nonDeferAccess = mergeNonDeferAccess (
1594- existingEntry . nonDeferAccess ,
1595- reducedNonDeferAccess
1596- ) ;
1597- }
1598- } else {
1599- reducedRuntimeCondition = runtimeCondition ;
1600- reducedNonDeferAccess = nonDeferAccess ;
1601- existingEntries . set ( connection . module , {
1602- runtimeCondition,
1603- nonDeferAccess
1604- } ) ;
1608+ return ;
16051609 }
1606- if ( list . length > 0 ) {
1607- const lastItem = list [ list . length - 1 ] ;
1608- if (
1609- lastItem . type === "external" &&
1610- lastItem . module === connection . module
1611- ) {
1612- lastItem . runtimeCondition = mergeRuntimeCondition (
1613- lastItem . runtimeCondition ,
1614- reducedRuntimeCondition ,
1615- runtime
1616- ) ;
1617- lastItem . nonDeferAccess = mergeNonDeferAccess (
1618- lastItem . nonDeferAccess ,
1619- reducedNonDeferAccess
1620- ) ;
1621- return ;
1622- }
1610+ }
1611+ list . push ( {
1612+ type : "external" ,
1613+ wrapped : wrappedModules . has ( connection . module ) ,
1614+ get module ( ) {
1615+ // We need to use a getter here, because the module in the dependency
1616+ // could be replaced by some other process (i. e. also replaced with a
1617+ // concatenated module)
1618+ return connection . module ;
1619+ } ,
1620+ runtimeCondition : reducedRuntimeCondition ,
1621+ nonDeferAccess : reducedNonDeferAccess
1622+ } ) ;
1623+ } ;
1624+
1625+ /**
1626+ * Lists a module and, depth first, everything it concatenates, each after its imports.
1627+ * An explicit stack keeps a long import cycle from overflowing the call stack.
1628+ * @param {ModuleGraphConnection } connection graph connection
1629+ * @param {RuntimeSpec | true } runtimeCondition runtime condition
1630+ * @param {NonDeferAccess } nonDeferAccess non-defer access
1631+ * @returns {void }
1632+ */
1633+ const enterModule = ( connection , runtimeCondition , nonDeferAccess ) => {
1634+ /** @type {{ connection: ModuleGraphConnection, module: Module, runtimeCondition: RuntimeSpec | true, nonDeferAccess: NonDeferAccess, imports: Iterator<{ connection: ModuleGraphConnection, runtimeCondition: RuntimeSpec | true, nonDeferAccess: NonDeferAccess }> }[] } */
1635+ const stack = [ ] ;
1636+ /** @type {{ connection: ModuleGraphConnection, runtimeCondition: RuntimeSpec | true, nonDeferAccess: NonDeferAccess } | undefined } */
1637+ let next = { connection, runtimeCondition, nonDeferAccess } ;
1638+ for ( ; ; ) {
1639+ if ( next !== undefined ) {
1640+ const module = next . connection . module ;
1641+ const imports = enterModuleOnce (
1642+ next . connection ,
1643+ next . runtimeCondition ,
1644+ next . nonDeferAccess
1645+ ) ;
1646+ if ( imports !== undefined ) stack . push ( { ...next , module, imports } ) ;
1647+ next = undefined ;
16231648 }
1649+ if ( stack . length === 0 ) return ;
1650+ const frame = stack [ stack . length - 1 ] ;
1651+ const step = frame . imports . next ( ) ;
1652+ if ( ! step . done ) {
1653+ next = step . value ;
1654+ continue ;
1655+ }
1656+ stack . pop ( ) ;
16241657 list . push ( {
1625- type : "external" ,
1626- wrapped : wrappedModules . has ( connection . module ) ,
1627- get module ( ) {
1628- // We need to use a getter here, because the module in the dependency
1629- // could be replaced by some other process (i. e. also replaced with a
1630- // concatenated module)
1631- return connection . module ;
1632- } ,
1633- runtimeCondition : reducedRuntimeCondition ,
1634- nonDeferAccess : reducedNonDeferAccess
1658+ type : "concatenated" ,
1659+ wrapped : needWrapped ( frame . module ) ,
1660+ module : frame . connection . module ,
1661+ runtimeCondition : frame . runtimeCondition ,
1662+ nonDeferAccess : frame . nonDeferAccess
16351663 } ) ;
16361664 }
16371665 } ;
0 commit comments