buildChunkGraph.js 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const AsyncDependencyToInitialChunkError = require("./AsyncDependencyToInitialChunkError");
  7. const { connectChunkGroupParentAndChild } = require("./GraphHelpers");
  8. const ModuleGraphConnection = require("./ModuleGraphConnection");
  9. const { getEntryRuntime, mergeRuntime } = require("./util/runtime");
  10. /** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
  11. /** @typedef {import("./Chunk")} Chunk */
  12. /** @typedef {import("./ChunkGroup")} ChunkGroup */
  13. /** @typedef {import("./Compilation")} Compilation */
  14. /** @typedef {import("./DependenciesBlock")} DependenciesBlock */
  15. /** @typedef {import("./Dependency")} Dependency */
  16. /** @typedef {import("./Entrypoint")} Entrypoint */
  17. /** @typedef {import("./Module")} Module */
  18. /** @typedef {import("./ModuleGraph")} ModuleGraph */
  19. /** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
  20. /** @typedef {import("./logging/Logger").Logger} Logger */
  21. /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
  22. /**
  23. * @typedef {Object} QueueItem
  24. * @property {number} action
  25. * @property {DependenciesBlock} block
  26. * @property {Module} module
  27. * @property {Chunk} chunk
  28. * @property {ChunkGroup} chunkGroup
  29. * @property {ChunkGroupInfo} chunkGroupInfo
  30. */
  31. /** @typedef {Set<Module> & { plus: Set<Module> }} ModuleSetPlus */
  32. /**
  33. * @typedef {Object} ChunkGroupInfo
  34. * @property {ChunkGroup} chunkGroup the chunk group
  35. * @property {RuntimeSpec} runtime the runtimes
  36. * @property {ModuleSetPlus} minAvailableModules current minimal set of modules available at this point
  37. * @property {boolean} minAvailableModulesOwned true, if minAvailableModules is owned and can be modified
  38. * @property {ModuleSetPlus[]} availableModulesToBeMerged enqueued updates to the minimal set of available modules
  39. * @property {Set<Module>=} skippedItems modules that were skipped because module is already available in parent chunks (need to reconsider when minAvailableModules is shrinking)
  40. * @property {Set<[Module, ConnectionState]>=} skippedModuleConnections referenced modules that where skipped because they were not active in this runtime
  41. * @property {ModuleSetPlus} resultingAvailableModules set of modules available including modules from this chunk group
  42. * @property {Set<ChunkGroupInfo>} children set of children chunk groups, that will be revisited when availableModules shrink
  43. * @property {Set<ChunkGroupInfo>} availableSources set of chunk groups that are the source for minAvailableModules
  44. * @property {Set<ChunkGroupInfo>} availableChildren set of chunk groups which depend on the this chunk group as availableSource
  45. * @property {number} preOrderIndex next pre order index
  46. * @property {number} postOrderIndex next post order index
  47. * @property {boolean} chunkLoading has a chunk loading mechanism
  48. * @property {boolean} asyncChunks create async chunks
  49. */
  50. /**
  51. * @typedef {Object} BlockChunkGroupConnection
  52. * @property {ChunkGroupInfo} originChunkGroupInfo origin chunk group
  53. * @property {ChunkGroup} chunkGroup referenced chunk group
  54. */
  55. const EMPTY_SET = /** @type {ModuleSetPlus} */ (new Set());
  56. EMPTY_SET.plus = EMPTY_SET;
  57. /**
  58. * @param {ModuleSetPlus} a first set
  59. * @param {ModuleSetPlus} b second set
  60. * @returns {number} cmp
  61. */
  62. const bySetSize = (a, b) => {
  63. return b.size + b.plus.size - a.size - a.plus.size;
  64. };
  65. const extractBlockModules = (module, moduleGraph, runtime, blockModulesMap) => {
  66. let blockCache;
  67. let modules;
  68. const arrays = [];
  69. const queue = [module];
  70. while (queue.length > 0) {
  71. const block = queue.pop();
  72. const arr = [];
  73. arrays.push(arr);
  74. blockModulesMap.set(block, arr);
  75. for (const b of block.blocks) {
  76. queue.push(b);
  77. }
  78. }
  79. for (const connection of moduleGraph.getOutgoingConnections(module)) {
  80. const d = connection.dependency;
  81. // We skip connections without dependency
  82. if (!d) continue;
  83. const m = connection.module;
  84. // We skip connections without Module pointer
  85. if (!m) continue;
  86. // We skip weak connections
  87. if (connection.weak) continue;
  88. const state = connection.getActiveState(runtime);
  89. // We skip inactive connections
  90. if (state === false) continue;
  91. const block = moduleGraph.getParentBlock(d);
  92. let index = moduleGraph.getParentBlockIndex(d);
  93. // deprecated fallback
  94. if (index < 0) {
  95. index = block.dependencies.indexOf(d);
  96. }
  97. if (blockCache !== block) {
  98. modules = blockModulesMap.get((blockCache = block));
  99. }
  100. const i = index << 2;
  101. modules[i] = m;
  102. modules[i + 1] = state;
  103. }
  104. for (const modules of arrays) {
  105. if (modules.length === 0) continue;
  106. let indexMap;
  107. let length = 0;
  108. outer: for (let j = 0; j < modules.length; j += 2) {
  109. const m = modules[j];
  110. if (m === undefined) continue;
  111. const state = modules[j + 1];
  112. if (indexMap === undefined) {
  113. let i = 0;
  114. for (; i < length; i += 2) {
  115. if (modules[i] === m) {
  116. const merged = modules[i + 1];
  117. if (merged === true) continue outer;
  118. modules[i + 1] = ModuleGraphConnection.addConnectionStates(
  119. merged,
  120. state
  121. );
  122. }
  123. }
  124. modules[length] = m;
  125. length++;
  126. modules[length] = state;
  127. length++;
  128. if (length > 30) {
  129. // To avoid worse case performance, we will use an index map for
  130. // linear cost access, which allows to maintain O(n) complexity
  131. // while keeping allocations down to a minimum
  132. indexMap = new Map();
  133. for (let i = 0; i < length; i += 2) {
  134. indexMap.set(modules[i], i + 1);
  135. }
  136. }
  137. } else {
  138. const idx = indexMap.get(m);
  139. if (idx !== undefined) {
  140. const merged = modules[idx];
  141. if (merged === true) continue outer;
  142. modules[idx] = ModuleGraphConnection.addConnectionStates(
  143. merged,
  144. state
  145. );
  146. } else {
  147. modules[length] = m;
  148. length++;
  149. modules[length] = state;
  150. indexMap.set(m, length);
  151. length++;
  152. }
  153. }
  154. }
  155. modules.length = length;
  156. }
  157. };
  158. /**
  159. *
  160. * @param {Logger} logger a logger
  161. * @param {Compilation} compilation the compilation
  162. * @param {Map<Entrypoint, Module[]>} inputEntrypointsAndModules chunk groups which are processed with the modules
  163. * @param {Map<ChunkGroup, ChunkGroupInfo>} chunkGroupInfoMap mapping from chunk group to available modules
  164. * @param {Map<AsyncDependenciesBlock, BlockChunkGroupConnection[]>} blockConnections connection for blocks
  165. * @param {Set<DependenciesBlock>} blocksWithNestedBlocks flag for blocks that have nested blocks
  166. * @param {Set<ChunkGroup>} allCreatedChunkGroups filled with all chunk groups that are created here
  167. */
  168. const visitModules = (
  169. logger,
  170. compilation,
  171. inputEntrypointsAndModules,
  172. chunkGroupInfoMap,
  173. blockConnections,
  174. blocksWithNestedBlocks,
  175. allCreatedChunkGroups
  176. ) => {
  177. const { moduleGraph, chunkGraph, moduleMemCaches } = compilation;
  178. const blockModulesRuntimeMap = new Map();
  179. /** @type {RuntimeSpec | false} */
  180. let blockModulesMapRuntime = false;
  181. let blockModulesMap;
  182. /**
  183. *
  184. * @param {DependenciesBlock} block block
  185. * @param {RuntimeSpec} runtime runtime
  186. * @returns {(Module | ConnectionState)[]} block modules in flatten tuples
  187. */
  188. const getBlockModules = (block, runtime) => {
  189. if (blockModulesMapRuntime !== runtime) {
  190. blockModulesMap = blockModulesRuntimeMap.get(runtime);
  191. if (blockModulesMap === undefined) {
  192. blockModulesMap = new Map();
  193. blockModulesRuntimeMap.set(runtime, blockModulesMap);
  194. }
  195. }
  196. let blockModules = blockModulesMap.get(block);
  197. if (blockModules !== undefined) return blockModules;
  198. const module = /** @type {Module} */ (block.getRootBlock());
  199. const memCache = moduleMemCaches && moduleMemCaches.get(module);
  200. if (memCache !== undefined) {
  201. const map = memCache.provide(
  202. "bundleChunkGraph.blockModules",
  203. runtime,
  204. () => {
  205. logger.time("visitModules: prepare");
  206. const map = new Map();
  207. extractBlockModules(module, moduleGraph, runtime, map);
  208. logger.timeAggregate("visitModules: prepare");
  209. return map;
  210. }
  211. );
  212. for (const [block, blockModules] of map)
  213. blockModulesMap.set(block, blockModules);
  214. return map.get(block);
  215. } else {
  216. logger.time("visitModules: prepare");
  217. extractBlockModules(module, moduleGraph, runtime, blockModulesMap);
  218. blockModules = blockModulesMap.get(block);
  219. logger.timeAggregate("visitModules: prepare");
  220. return blockModules;
  221. }
  222. };
  223. let statProcessedQueueItems = 0;
  224. let statProcessedBlocks = 0;
  225. let statConnectedChunkGroups = 0;
  226. let statProcessedChunkGroupsForMerging = 0;
  227. let statMergedAvailableModuleSets = 0;
  228. let statForkedAvailableModules = 0;
  229. let statForkedAvailableModulesCount = 0;
  230. let statForkedAvailableModulesCountPlus = 0;
  231. let statForkedMergedModulesCount = 0;
  232. let statForkedMergedModulesCountPlus = 0;
  233. let statForkedResultModulesCount = 0;
  234. let statChunkGroupInfoUpdated = 0;
  235. let statChildChunkGroupsReconnected = 0;
  236. let nextChunkGroupIndex = 0;
  237. let nextFreeModulePreOrderIndex = 0;
  238. let nextFreeModulePostOrderIndex = 0;
  239. /** @type {Map<DependenciesBlock, ChunkGroupInfo>} */
  240. const blockChunkGroups = new Map();
  241. /** @type {Map<string, ChunkGroupInfo>} */
  242. const namedChunkGroups = new Map();
  243. /** @type {Map<string, ChunkGroupInfo>} */
  244. const namedAsyncEntrypoints = new Map();
  245. const ADD_AND_ENTER_ENTRY_MODULE = 0;
  246. const ADD_AND_ENTER_MODULE = 1;
  247. const ENTER_MODULE = 2;
  248. const PROCESS_BLOCK = 3;
  249. const PROCESS_ENTRY_BLOCK = 4;
  250. const LEAVE_MODULE = 5;
  251. /** @type {QueueItem[]} */
  252. let queue = [];
  253. /** @type {Map<ChunkGroupInfo, Set<ChunkGroupInfo>>} */
  254. const queueConnect = new Map();
  255. /** @type {Set<ChunkGroupInfo>} */
  256. const chunkGroupsForCombining = new Set();
  257. // Fill queue with entrypoint modules
  258. // Create ChunkGroupInfo for entrypoints
  259. for (const [chunkGroup, modules] of inputEntrypointsAndModules) {
  260. const runtime = getEntryRuntime(
  261. compilation,
  262. chunkGroup.name,
  263. chunkGroup.options
  264. );
  265. /** @type {ChunkGroupInfo} */
  266. const chunkGroupInfo = {
  267. chunkGroup,
  268. runtime,
  269. minAvailableModules: undefined,
  270. minAvailableModulesOwned: false,
  271. availableModulesToBeMerged: [],
  272. skippedItems: undefined,
  273. resultingAvailableModules: undefined,
  274. children: undefined,
  275. availableSources: undefined,
  276. availableChildren: undefined,
  277. preOrderIndex: 0,
  278. postOrderIndex: 0,
  279. chunkLoading:
  280. chunkGroup.options.chunkLoading !== undefined
  281. ? chunkGroup.options.chunkLoading !== false
  282. : compilation.outputOptions.chunkLoading !== false,
  283. asyncChunks:
  284. chunkGroup.options.asyncChunks !== undefined
  285. ? chunkGroup.options.asyncChunks
  286. : compilation.outputOptions.asyncChunks !== false
  287. };
  288. chunkGroup.index = nextChunkGroupIndex++;
  289. if (chunkGroup.getNumberOfParents() > 0) {
  290. // minAvailableModules for child entrypoints are unknown yet, set to undefined.
  291. // This means no module is added until other sets are merged into
  292. // this minAvailableModules (by the parent entrypoints)
  293. const skippedItems = new Set();
  294. for (const module of modules) {
  295. skippedItems.add(module);
  296. }
  297. chunkGroupInfo.skippedItems = skippedItems;
  298. chunkGroupsForCombining.add(chunkGroupInfo);
  299. } else {
  300. // The application may start here: We start with an empty list of available modules
  301. chunkGroupInfo.minAvailableModules = EMPTY_SET;
  302. const chunk = chunkGroup.getEntrypointChunk();
  303. for (const module of modules) {
  304. queue.push({
  305. action: ADD_AND_ENTER_MODULE,
  306. block: module,
  307. module,
  308. chunk,
  309. chunkGroup,
  310. chunkGroupInfo
  311. });
  312. }
  313. }
  314. chunkGroupInfoMap.set(chunkGroup, chunkGroupInfo);
  315. if (chunkGroup.name) {
  316. namedChunkGroups.set(chunkGroup.name, chunkGroupInfo);
  317. }
  318. }
  319. // Fill availableSources with parent-child dependencies between entrypoints
  320. for (const chunkGroupInfo of chunkGroupsForCombining) {
  321. const { chunkGroup } = chunkGroupInfo;
  322. chunkGroupInfo.availableSources = new Set();
  323. for (const parent of chunkGroup.parentsIterable) {
  324. const parentChunkGroupInfo = chunkGroupInfoMap.get(parent);
  325. chunkGroupInfo.availableSources.add(parentChunkGroupInfo);
  326. if (parentChunkGroupInfo.availableChildren === undefined) {
  327. parentChunkGroupInfo.availableChildren = new Set();
  328. }
  329. parentChunkGroupInfo.availableChildren.add(chunkGroupInfo);
  330. }
  331. }
  332. // pop() is used to read from the queue
  333. // so it need to be reversed to be iterated in
  334. // correct order
  335. queue.reverse();
  336. /** @type {Set<ChunkGroupInfo>} */
  337. const outdatedChunkGroupInfo = new Set();
  338. /** @type {Set<ChunkGroupInfo>} */
  339. const chunkGroupsForMerging = new Set();
  340. /** @type {QueueItem[]} */
  341. let queueDelayed = [];
  342. /** @type {[Module, ConnectionState][]} */
  343. const skipConnectionBuffer = [];
  344. /** @type {Module[]} */
  345. const skipBuffer = [];
  346. /** @type {QueueItem[]} */
  347. const queueBuffer = [];
  348. /** @type {Module} */
  349. let module;
  350. /** @type {Chunk} */
  351. let chunk;
  352. /** @type {ChunkGroup} */
  353. let chunkGroup;
  354. /** @type {DependenciesBlock} */
  355. let block;
  356. /** @type {ChunkGroupInfo} */
  357. let chunkGroupInfo;
  358. // For each async Block in graph
  359. /**
  360. * @param {AsyncDependenciesBlock} b iterating over each Async DepBlock
  361. * @returns {void}
  362. */
  363. const iteratorBlock = b => {
  364. // 1. We create a chunk group with single chunk in it for this Block
  365. // but only once (blockChunkGroups map)
  366. let cgi = blockChunkGroups.get(b);
  367. /** @type {ChunkGroup} */
  368. let c;
  369. /** @type {Entrypoint} */
  370. let entrypoint;
  371. const entryOptions = b.groupOptions && b.groupOptions.entryOptions;
  372. if (cgi === undefined) {
  373. const chunkName = (b.groupOptions && b.groupOptions.name) || b.chunkName;
  374. if (entryOptions) {
  375. cgi = namedAsyncEntrypoints.get(chunkName);
  376. if (!cgi) {
  377. entrypoint = compilation.addAsyncEntrypoint(
  378. entryOptions,
  379. module,
  380. b.loc,
  381. b.request
  382. );
  383. entrypoint.index = nextChunkGroupIndex++;
  384. cgi = {
  385. chunkGroup: entrypoint,
  386. runtime: entrypoint.options.runtime || entrypoint.name,
  387. minAvailableModules: EMPTY_SET,
  388. minAvailableModulesOwned: false,
  389. availableModulesToBeMerged: [],
  390. skippedItems: undefined,
  391. resultingAvailableModules: undefined,
  392. children: undefined,
  393. availableSources: undefined,
  394. availableChildren: undefined,
  395. preOrderIndex: 0,
  396. postOrderIndex: 0,
  397. chunkLoading:
  398. entryOptions.chunkLoading !== undefined
  399. ? entryOptions.chunkLoading !== false
  400. : chunkGroupInfo.chunkLoading,
  401. asyncChunks:
  402. entryOptions.asyncChunks !== undefined
  403. ? entryOptions.asyncChunks
  404. : chunkGroupInfo.asyncChunks
  405. };
  406. chunkGroupInfoMap.set(entrypoint, cgi);
  407. chunkGraph.connectBlockAndChunkGroup(b, entrypoint);
  408. if (chunkName) {
  409. namedAsyncEntrypoints.set(chunkName, cgi);
  410. }
  411. } else {
  412. entrypoint = /** @type {Entrypoint} */ (cgi.chunkGroup);
  413. // TODO merge entryOptions
  414. entrypoint.addOrigin(module, b.loc, b.request);
  415. chunkGraph.connectBlockAndChunkGroup(b, entrypoint);
  416. }
  417. // 2. We enqueue the DependenciesBlock for traversal
  418. queueDelayed.push({
  419. action: PROCESS_ENTRY_BLOCK,
  420. block: b,
  421. module: module,
  422. chunk: entrypoint.chunks[0],
  423. chunkGroup: entrypoint,
  424. chunkGroupInfo: cgi
  425. });
  426. } else if (!chunkGroupInfo.asyncChunks || !chunkGroupInfo.chunkLoading) {
  427. // Just queue the block into the current chunk group
  428. queue.push({
  429. action: PROCESS_BLOCK,
  430. block: b,
  431. module: module,
  432. chunk,
  433. chunkGroup,
  434. chunkGroupInfo
  435. });
  436. } else {
  437. cgi = chunkName && namedChunkGroups.get(chunkName);
  438. if (!cgi) {
  439. c = compilation.addChunkInGroup(
  440. b.groupOptions || b.chunkName,
  441. module,
  442. b.loc,
  443. b.request
  444. );
  445. c.index = nextChunkGroupIndex++;
  446. cgi = {
  447. chunkGroup: c,
  448. runtime: chunkGroupInfo.runtime,
  449. minAvailableModules: undefined,
  450. minAvailableModulesOwned: undefined,
  451. availableModulesToBeMerged: [],
  452. skippedItems: undefined,
  453. resultingAvailableModules: undefined,
  454. children: undefined,
  455. availableSources: undefined,
  456. availableChildren: undefined,
  457. preOrderIndex: 0,
  458. postOrderIndex: 0,
  459. chunkLoading: chunkGroupInfo.chunkLoading,
  460. asyncChunks: chunkGroupInfo.asyncChunks
  461. };
  462. allCreatedChunkGroups.add(c);
  463. chunkGroupInfoMap.set(c, cgi);
  464. if (chunkName) {
  465. namedChunkGroups.set(chunkName, cgi);
  466. }
  467. } else {
  468. c = cgi.chunkGroup;
  469. if (c.isInitial()) {
  470. compilation.errors.push(
  471. new AsyncDependencyToInitialChunkError(chunkName, module, b.loc)
  472. );
  473. c = chunkGroup;
  474. } else {
  475. c.addOptions(b.groupOptions);
  476. }
  477. c.addOrigin(module, b.loc, b.request);
  478. }
  479. blockConnections.set(b, []);
  480. }
  481. blockChunkGroups.set(b, cgi);
  482. } else if (entryOptions) {
  483. entrypoint = /** @type {Entrypoint} */ (cgi.chunkGroup);
  484. } else {
  485. c = cgi.chunkGroup;
  486. }
  487. if (c !== undefined) {
  488. // 2. We store the connection for the block
  489. // to connect it later if needed
  490. blockConnections.get(b).push({
  491. originChunkGroupInfo: chunkGroupInfo,
  492. chunkGroup: c
  493. });
  494. // 3. We enqueue the chunk group info creation/updating
  495. let connectList = queueConnect.get(chunkGroupInfo);
  496. if (connectList === undefined) {
  497. connectList = new Set();
  498. queueConnect.set(chunkGroupInfo, connectList);
  499. }
  500. connectList.add(cgi);
  501. // TODO check if this really need to be done for each traversal
  502. // or if it is enough when it's queued when created
  503. // 4. We enqueue the DependenciesBlock for traversal
  504. queueDelayed.push({
  505. action: PROCESS_BLOCK,
  506. block: b,
  507. module: module,
  508. chunk: c.chunks[0],
  509. chunkGroup: c,
  510. chunkGroupInfo: cgi
  511. });
  512. } else if (entrypoint !== undefined) {
  513. chunkGroupInfo.chunkGroup.addAsyncEntrypoint(entrypoint);
  514. }
  515. };
  516. /**
  517. * @param {DependenciesBlock} block the block
  518. * @returns {void}
  519. */
  520. const processBlock = block => {
  521. statProcessedBlocks++;
  522. // get prepared block info
  523. const blockModules = getBlockModules(block, chunkGroupInfo.runtime);
  524. if (blockModules !== undefined) {
  525. const { minAvailableModules } = chunkGroupInfo;
  526. // Buffer items because order need to be reversed to get indices correct
  527. // Traverse all referenced modules
  528. for (let i = 0; i < blockModules.length; i += 2) {
  529. const refModule = /** @type {Module} */ (blockModules[i]);
  530. if (chunkGraph.isModuleInChunk(refModule, chunk)) {
  531. // skip early if already connected
  532. continue;
  533. }
  534. const activeState = /** @type {ConnectionState} */ (
  535. blockModules[i + 1]
  536. );
  537. if (activeState !== true) {
  538. skipConnectionBuffer.push([refModule, activeState]);
  539. if (activeState === false) continue;
  540. }
  541. if (
  542. activeState === true &&
  543. (minAvailableModules.has(refModule) ||
  544. minAvailableModules.plus.has(refModule))
  545. ) {
  546. // already in parent chunks, skip it for now
  547. skipBuffer.push(refModule);
  548. continue;
  549. }
  550. // enqueue, then add and enter to be in the correct order
  551. // this is relevant with circular dependencies
  552. queueBuffer.push({
  553. action: activeState === true ? ADD_AND_ENTER_MODULE : PROCESS_BLOCK,
  554. block: refModule,
  555. module: refModule,
  556. chunk,
  557. chunkGroup,
  558. chunkGroupInfo
  559. });
  560. }
  561. // Add buffered items in reverse order
  562. if (skipConnectionBuffer.length > 0) {
  563. let { skippedModuleConnections } = chunkGroupInfo;
  564. if (skippedModuleConnections === undefined) {
  565. chunkGroupInfo.skippedModuleConnections = skippedModuleConnections =
  566. new Set();
  567. }
  568. for (let i = skipConnectionBuffer.length - 1; i >= 0; i--) {
  569. skippedModuleConnections.add(skipConnectionBuffer[i]);
  570. }
  571. skipConnectionBuffer.length = 0;
  572. }
  573. if (skipBuffer.length > 0) {
  574. let { skippedItems } = chunkGroupInfo;
  575. if (skippedItems === undefined) {
  576. chunkGroupInfo.skippedItems = skippedItems = new Set();
  577. }
  578. for (let i = skipBuffer.length - 1; i >= 0; i--) {
  579. skippedItems.add(skipBuffer[i]);
  580. }
  581. skipBuffer.length = 0;
  582. }
  583. if (queueBuffer.length > 0) {
  584. for (let i = queueBuffer.length - 1; i >= 0; i--) {
  585. queue.push(queueBuffer[i]);
  586. }
  587. queueBuffer.length = 0;
  588. }
  589. }
  590. // Traverse all Blocks
  591. for (const b of block.blocks) {
  592. iteratorBlock(b);
  593. }
  594. if (block.blocks.length > 0 && module !== block) {
  595. blocksWithNestedBlocks.add(block);
  596. }
  597. };
  598. /**
  599. * @param {DependenciesBlock} block the block
  600. * @returns {void}
  601. */
  602. const processEntryBlock = block => {
  603. statProcessedBlocks++;
  604. // get prepared block info
  605. const blockModules = getBlockModules(block, chunkGroupInfo.runtime);
  606. if (blockModules !== undefined) {
  607. // Traverse all referenced modules
  608. for (let i = 0; i < blockModules.length; i += 2) {
  609. const refModule = /** @type {Module} */ (blockModules[i]);
  610. const activeState = /** @type {ConnectionState} */ (
  611. blockModules[i + 1]
  612. );
  613. // enqueue, then add and enter to be in the correct order
  614. // this is relevant with circular dependencies
  615. queueBuffer.push({
  616. action:
  617. activeState === true ? ADD_AND_ENTER_ENTRY_MODULE : PROCESS_BLOCK,
  618. block: refModule,
  619. module: refModule,
  620. chunk,
  621. chunkGroup,
  622. chunkGroupInfo
  623. });
  624. }
  625. // Add buffered items in reverse order
  626. if (queueBuffer.length > 0) {
  627. for (let i = queueBuffer.length - 1; i >= 0; i--) {
  628. queue.push(queueBuffer[i]);
  629. }
  630. queueBuffer.length = 0;
  631. }
  632. }
  633. // Traverse all Blocks
  634. for (const b of block.blocks) {
  635. iteratorBlock(b);
  636. }
  637. if (block.blocks.length > 0 && module !== block) {
  638. blocksWithNestedBlocks.add(block);
  639. }
  640. };
  641. const processQueue = () => {
  642. while (queue.length) {
  643. statProcessedQueueItems++;
  644. const queueItem = queue.pop();
  645. module = queueItem.module;
  646. block = queueItem.block;
  647. chunk = queueItem.chunk;
  648. chunkGroup = queueItem.chunkGroup;
  649. chunkGroupInfo = queueItem.chunkGroupInfo;
  650. switch (queueItem.action) {
  651. case ADD_AND_ENTER_ENTRY_MODULE:
  652. chunkGraph.connectChunkAndEntryModule(
  653. chunk,
  654. module,
  655. /** @type {Entrypoint} */ (chunkGroup)
  656. );
  657. // fallthrough
  658. case ADD_AND_ENTER_MODULE: {
  659. if (chunkGraph.isModuleInChunk(module, chunk)) {
  660. // already connected, skip it
  661. break;
  662. }
  663. // We connect Module and Chunk
  664. chunkGraph.connectChunkAndModule(chunk, module);
  665. }
  666. // fallthrough
  667. case ENTER_MODULE: {
  668. const index = chunkGroup.getModulePreOrderIndex(module);
  669. if (index === undefined) {
  670. chunkGroup.setModulePreOrderIndex(
  671. module,
  672. chunkGroupInfo.preOrderIndex++
  673. );
  674. }
  675. if (
  676. moduleGraph.setPreOrderIndexIfUnset(
  677. module,
  678. nextFreeModulePreOrderIndex
  679. )
  680. ) {
  681. nextFreeModulePreOrderIndex++;
  682. }
  683. // reuse queueItem
  684. queueItem.action = LEAVE_MODULE;
  685. queue.push(queueItem);
  686. }
  687. // fallthrough
  688. case PROCESS_BLOCK: {
  689. processBlock(block);
  690. break;
  691. }
  692. case PROCESS_ENTRY_BLOCK: {
  693. processEntryBlock(block);
  694. break;
  695. }
  696. case LEAVE_MODULE: {
  697. const index = chunkGroup.getModulePostOrderIndex(module);
  698. if (index === undefined) {
  699. chunkGroup.setModulePostOrderIndex(
  700. module,
  701. chunkGroupInfo.postOrderIndex++
  702. );
  703. }
  704. if (
  705. moduleGraph.setPostOrderIndexIfUnset(
  706. module,
  707. nextFreeModulePostOrderIndex
  708. )
  709. ) {
  710. nextFreeModulePostOrderIndex++;
  711. }
  712. break;
  713. }
  714. }
  715. }
  716. };
  717. const calculateResultingAvailableModules = chunkGroupInfo => {
  718. if (chunkGroupInfo.resultingAvailableModules)
  719. return chunkGroupInfo.resultingAvailableModules;
  720. const minAvailableModules = chunkGroupInfo.minAvailableModules;
  721. // Create a new Set of available modules at this point
  722. // We want to be as lazy as possible. There are multiple ways doing this:
  723. // Note that resultingAvailableModules is stored as "(a) + (b)" as it's a ModuleSetPlus
  724. // - resultingAvailableModules = (modules of chunk) + (minAvailableModules + minAvailableModules.plus)
  725. // - resultingAvailableModules = (minAvailableModules + modules of chunk) + (minAvailableModules.plus)
  726. // We choose one depending on the size of minAvailableModules vs minAvailableModules.plus
  727. let resultingAvailableModules;
  728. if (minAvailableModules.size > minAvailableModules.plus.size) {
  729. // resultingAvailableModules = (modules of chunk) + (minAvailableModules + minAvailableModules.plus)
  730. resultingAvailableModules =
  731. /** @type {Set<Module> & {plus: Set<Module>}} */ (new Set());
  732. for (const module of minAvailableModules.plus)
  733. minAvailableModules.add(module);
  734. minAvailableModules.plus = EMPTY_SET;
  735. resultingAvailableModules.plus = minAvailableModules;
  736. chunkGroupInfo.minAvailableModulesOwned = false;
  737. } else {
  738. // resultingAvailableModules = (minAvailableModules + modules of chunk) + (minAvailableModules.plus)
  739. resultingAvailableModules =
  740. /** @type {Set<Module> & {plus: Set<Module>}} */ (
  741. new Set(minAvailableModules)
  742. );
  743. resultingAvailableModules.plus = minAvailableModules.plus;
  744. }
  745. // add the modules from the chunk group to the set
  746. for (const chunk of chunkGroupInfo.chunkGroup.chunks) {
  747. for (const m of chunkGraph.getChunkModulesIterable(chunk)) {
  748. resultingAvailableModules.add(m);
  749. }
  750. }
  751. return (chunkGroupInfo.resultingAvailableModules =
  752. resultingAvailableModules);
  753. };
  754. const processConnectQueue = () => {
  755. // Figure out new parents for chunk groups
  756. // to get new available modules for these children
  757. for (const [chunkGroupInfo, targets] of queueConnect) {
  758. // 1. Add new targets to the list of children
  759. if (chunkGroupInfo.children === undefined) {
  760. chunkGroupInfo.children = targets;
  761. } else {
  762. for (const target of targets) {
  763. chunkGroupInfo.children.add(target);
  764. }
  765. }
  766. // 2. Calculate resulting available modules
  767. const resultingAvailableModules =
  768. calculateResultingAvailableModules(chunkGroupInfo);
  769. const runtime = chunkGroupInfo.runtime;
  770. // 3. Update chunk group info
  771. for (const target of targets) {
  772. target.availableModulesToBeMerged.push(resultingAvailableModules);
  773. chunkGroupsForMerging.add(target);
  774. const oldRuntime = target.runtime;
  775. const newRuntime = mergeRuntime(oldRuntime, runtime);
  776. if (oldRuntime !== newRuntime) {
  777. target.runtime = newRuntime;
  778. outdatedChunkGroupInfo.add(target);
  779. }
  780. }
  781. statConnectedChunkGroups += targets.size;
  782. }
  783. queueConnect.clear();
  784. };
  785. const processChunkGroupsForMerging = () => {
  786. statProcessedChunkGroupsForMerging += chunkGroupsForMerging.size;
  787. // Execute the merge
  788. for (const info of chunkGroupsForMerging) {
  789. const availableModulesToBeMerged = info.availableModulesToBeMerged;
  790. let cachedMinAvailableModules = info.minAvailableModules;
  791. statMergedAvailableModuleSets += availableModulesToBeMerged.length;
  792. // 1. Get minimal available modules
  793. // It doesn't make sense to traverse a chunk again with more available modules.
  794. // This step calculates the minimal available modules and skips traversal when
  795. // the list didn't shrink.
  796. if (availableModulesToBeMerged.length > 1) {
  797. availableModulesToBeMerged.sort(bySetSize);
  798. }
  799. let changed = false;
  800. merge: for (const availableModules of availableModulesToBeMerged) {
  801. if (cachedMinAvailableModules === undefined) {
  802. cachedMinAvailableModules = availableModules;
  803. info.minAvailableModules = cachedMinAvailableModules;
  804. info.minAvailableModulesOwned = false;
  805. changed = true;
  806. } else {
  807. if (info.minAvailableModulesOwned) {
  808. // We own it and can modify it
  809. if (cachedMinAvailableModules.plus === availableModules.plus) {
  810. for (const m of cachedMinAvailableModules) {
  811. if (!availableModules.has(m)) {
  812. cachedMinAvailableModules.delete(m);
  813. changed = true;
  814. }
  815. }
  816. } else {
  817. for (const m of cachedMinAvailableModules) {
  818. if (!availableModules.has(m) && !availableModules.plus.has(m)) {
  819. cachedMinAvailableModules.delete(m);
  820. changed = true;
  821. }
  822. }
  823. for (const m of cachedMinAvailableModules.plus) {
  824. if (!availableModules.has(m) && !availableModules.plus.has(m)) {
  825. // We can't remove modules from the plus part
  826. // so we need to merge plus into the normal part to allow modifying it
  827. const iterator =
  828. cachedMinAvailableModules.plus[Symbol.iterator]();
  829. // fast forward add all modules until m
  830. /** @type {IteratorResult<Module>} */
  831. let it;
  832. while (!(it = iterator.next()).done) {
  833. const module = it.value;
  834. if (module === m) break;
  835. cachedMinAvailableModules.add(module);
  836. }
  837. // check the remaining modules before adding
  838. while (!(it = iterator.next()).done) {
  839. const module = it.value;
  840. if (
  841. availableModules.has(module) ||
  842. availableModules.plus.has(module)
  843. ) {
  844. cachedMinAvailableModules.add(module);
  845. }
  846. }
  847. cachedMinAvailableModules.plus = EMPTY_SET;
  848. changed = true;
  849. continue merge;
  850. }
  851. }
  852. }
  853. } else if (cachedMinAvailableModules.plus === availableModules.plus) {
  854. // Common and fast case when the plus part is shared
  855. // We only need to care about the normal part
  856. if (availableModules.size < cachedMinAvailableModules.size) {
  857. // the new availableModules is smaller so it's faster to
  858. // fork from the new availableModules
  859. statForkedAvailableModules++;
  860. statForkedAvailableModulesCount += availableModules.size;
  861. statForkedMergedModulesCount += cachedMinAvailableModules.size;
  862. // construct a new Set as intersection of cachedMinAvailableModules and availableModules
  863. const newSet = /** @type {ModuleSetPlus} */ (new Set());
  864. newSet.plus = availableModules.plus;
  865. for (const m of availableModules) {
  866. if (cachedMinAvailableModules.has(m)) {
  867. newSet.add(m);
  868. }
  869. }
  870. statForkedResultModulesCount += newSet.size;
  871. cachedMinAvailableModules = newSet;
  872. info.minAvailableModulesOwned = true;
  873. info.minAvailableModules = newSet;
  874. changed = true;
  875. continue merge;
  876. }
  877. for (const m of cachedMinAvailableModules) {
  878. if (!availableModules.has(m)) {
  879. // cachedMinAvailableModules need to be modified
  880. // but we don't own it
  881. statForkedAvailableModules++;
  882. statForkedAvailableModulesCount +=
  883. cachedMinAvailableModules.size;
  884. statForkedMergedModulesCount += availableModules.size;
  885. // construct a new Set as intersection of cachedMinAvailableModules and availableModules
  886. // as the plus part is equal we can just take over this one
  887. const newSet = /** @type {ModuleSetPlus} */ (new Set());
  888. newSet.plus = availableModules.plus;
  889. const iterator = cachedMinAvailableModules[Symbol.iterator]();
  890. // fast forward add all modules until m
  891. /** @type {IteratorResult<Module>} */
  892. let it;
  893. while (!(it = iterator.next()).done) {
  894. const module = it.value;
  895. if (module === m) break;
  896. newSet.add(module);
  897. }
  898. // check the remaining modules before adding
  899. while (!(it = iterator.next()).done) {
  900. const module = it.value;
  901. if (availableModules.has(module)) {
  902. newSet.add(module);
  903. }
  904. }
  905. statForkedResultModulesCount += newSet.size;
  906. cachedMinAvailableModules = newSet;
  907. info.minAvailableModulesOwned = true;
  908. info.minAvailableModules = newSet;
  909. changed = true;
  910. continue merge;
  911. }
  912. }
  913. } else {
  914. for (const m of cachedMinAvailableModules) {
  915. if (!availableModules.has(m) && !availableModules.plus.has(m)) {
  916. // cachedMinAvailableModules need to be modified
  917. // but we don't own it
  918. statForkedAvailableModules++;
  919. statForkedAvailableModulesCount +=
  920. cachedMinAvailableModules.size;
  921. statForkedAvailableModulesCountPlus +=
  922. cachedMinAvailableModules.plus.size;
  923. statForkedMergedModulesCount += availableModules.size;
  924. statForkedMergedModulesCountPlus += availableModules.plus.size;
  925. // construct a new Set as intersection of cachedMinAvailableModules and availableModules
  926. const newSet = /** @type {ModuleSetPlus} */ (new Set());
  927. newSet.plus = EMPTY_SET;
  928. const iterator = cachedMinAvailableModules[Symbol.iterator]();
  929. // fast forward add all modules until m
  930. /** @type {IteratorResult<Module>} */
  931. let it;
  932. while (!(it = iterator.next()).done) {
  933. const module = it.value;
  934. if (module === m) break;
  935. newSet.add(module);
  936. }
  937. // check the remaining modules before adding
  938. while (!(it = iterator.next()).done) {
  939. const module = it.value;
  940. if (
  941. availableModules.has(module) ||
  942. availableModules.plus.has(module)
  943. ) {
  944. newSet.add(module);
  945. }
  946. }
  947. // also check all modules in cachedMinAvailableModules.plus
  948. for (const module of cachedMinAvailableModules.plus) {
  949. if (
  950. availableModules.has(module) ||
  951. availableModules.plus.has(module)
  952. ) {
  953. newSet.add(module);
  954. }
  955. }
  956. statForkedResultModulesCount += newSet.size;
  957. cachedMinAvailableModules = newSet;
  958. info.minAvailableModulesOwned = true;
  959. info.minAvailableModules = newSet;
  960. changed = true;
  961. continue merge;
  962. }
  963. }
  964. for (const m of cachedMinAvailableModules.plus) {
  965. if (!availableModules.has(m) && !availableModules.plus.has(m)) {
  966. // cachedMinAvailableModules need to be modified
  967. // but we don't own it
  968. statForkedAvailableModules++;
  969. statForkedAvailableModulesCount +=
  970. cachedMinAvailableModules.size;
  971. statForkedAvailableModulesCountPlus +=
  972. cachedMinAvailableModules.plus.size;
  973. statForkedMergedModulesCount += availableModules.size;
  974. statForkedMergedModulesCountPlus += availableModules.plus.size;
  975. // construct a new Set as intersection of cachedMinAvailableModules and availableModules
  976. // we already know that all modules directly from cachedMinAvailableModules are in availableModules too
  977. const newSet = /** @type {ModuleSetPlus} */ (
  978. new Set(cachedMinAvailableModules)
  979. );
  980. newSet.plus = EMPTY_SET;
  981. const iterator =
  982. cachedMinAvailableModules.plus[Symbol.iterator]();
  983. // fast forward add all modules until m
  984. /** @type {IteratorResult<Module>} */
  985. let it;
  986. while (!(it = iterator.next()).done) {
  987. const module = it.value;
  988. if (module === m) break;
  989. newSet.add(module);
  990. }
  991. // check the remaining modules before adding
  992. while (!(it = iterator.next()).done) {
  993. const module = it.value;
  994. if (
  995. availableModules.has(module) ||
  996. availableModules.plus.has(module)
  997. ) {
  998. newSet.add(module);
  999. }
  1000. }
  1001. statForkedResultModulesCount += newSet.size;
  1002. cachedMinAvailableModules = newSet;
  1003. info.minAvailableModulesOwned = true;
  1004. info.minAvailableModules = newSet;
  1005. changed = true;
  1006. continue merge;
  1007. }
  1008. }
  1009. }
  1010. }
  1011. }
  1012. availableModulesToBeMerged.length = 0;
  1013. if (changed) {
  1014. info.resultingAvailableModules = undefined;
  1015. outdatedChunkGroupInfo.add(info);
  1016. }
  1017. }
  1018. chunkGroupsForMerging.clear();
  1019. };
  1020. const processChunkGroupsForCombining = () => {
  1021. for (const info of chunkGroupsForCombining) {
  1022. for (const source of info.availableSources) {
  1023. if (!source.minAvailableModules) {
  1024. chunkGroupsForCombining.delete(info);
  1025. break;
  1026. }
  1027. }
  1028. }
  1029. for (const info of chunkGroupsForCombining) {
  1030. const availableModules = /** @type {ModuleSetPlus} */ (new Set());
  1031. availableModules.plus = EMPTY_SET;
  1032. const mergeSet = set => {
  1033. if (set.size > availableModules.plus.size) {
  1034. for (const item of availableModules.plus) availableModules.add(item);
  1035. availableModules.plus = set;
  1036. } else {
  1037. for (const item of set) availableModules.add(item);
  1038. }
  1039. };
  1040. // combine minAvailableModules from all resultingAvailableModules
  1041. for (const source of info.availableSources) {
  1042. const resultingAvailableModules =
  1043. calculateResultingAvailableModules(source);
  1044. mergeSet(resultingAvailableModules);
  1045. mergeSet(resultingAvailableModules.plus);
  1046. }
  1047. info.minAvailableModules = availableModules;
  1048. info.minAvailableModulesOwned = false;
  1049. info.resultingAvailableModules = undefined;
  1050. outdatedChunkGroupInfo.add(info);
  1051. }
  1052. chunkGroupsForCombining.clear();
  1053. };
  1054. const processOutdatedChunkGroupInfo = () => {
  1055. statChunkGroupInfoUpdated += outdatedChunkGroupInfo.size;
  1056. // Revisit skipped elements
  1057. for (const info of outdatedChunkGroupInfo) {
  1058. // 1. Reconsider skipped items
  1059. if (info.skippedItems !== undefined) {
  1060. const { minAvailableModules } = info;
  1061. for (const module of info.skippedItems) {
  1062. if (
  1063. !minAvailableModules.has(module) &&
  1064. !minAvailableModules.plus.has(module)
  1065. ) {
  1066. queue.push({
  1067. action: ADD_AND_ENTER_MODULE,
  1068. block: module,
  1069. module,
  1070. chunk: info.chunkGroup.chunks[0],
  1071. chunkGroup: info.chunkGroup,
  1072. chunkGroupInfo: info
  1073. });
  1074. info.skippedItems.delete(module);
  1075. }
  1076. }
  1077. }
  1078. // 2. Reconsider skipped connections
  1079. if (info.skippedModuleConnections !== undefined) {
  1080. const { minAvailableModules } = info;
  1081. for (const entry of info.skippedModuleConnections) {
  1082. const [module, activeState] = entry;
  1083. if (activeState === false) continue;
  1084. if (activeState === true) {
  1085. info.skippedModuleConnections.delete(entry);
  1086. }
  1087. if (
  1088. activeState === true &&
  1089. (minAvailableModules.has(module) ||
  1090. minAvailableModules.plus.has(module))
  1091. ) {
  1092. info.skippedItems.add(module);
  1093. continue;
  1094. }
  1095. queue.push({
  1096. action: activeState === true ? ADD_AND_ENTER_MODULE : PROCESS_BLOCK,
  1097. block: module,
  1098. module,
  1099. chunk: info.chunkGroup.chunks[0],
  1100. chunkGroup: info.chunkGroup,
  1101. chunkGroupInfo: info
  1102. });
  1103. }
  1104. }
  1105. // 2. Reconsider children chunk groups
  1106. if (info.children !== undefined) {
  1107. statChildChunkGroupsReconnected += info.children.size;
  1108. for (const cgi of info.children) {
  1109. let connectList = queueConnect.get(info);
  1110. if (connectList === undefined) {
  1111. connectList = new Set();
  1112. queueConnect.set(info, connectList);
  1113. }
  1114. connectList.add(cgi);
  1115. }
  1116. }
  1117. // 3. Reconsider chunk groups for combining
  1118. if (info.availableChildren !== undefined) {
  1119. for (const cgi of info.availableChildren) {
  1120. chunkGroupsForCombining.add(cgi);
  1121. }
  1122. }
  1123. }
  1124. outdatedChunkGroupInfo.clear();
  1125. };
  1126. // Iterative traversal of the Module graph
  1127. // Recursive would be simpler to write but could result in Stack Overflows
  1128. while (queue.length || queueConnect.size) {
  1129. logger.time("visitModules: visiting");
  1130. processQueue();
  1131. logger.timeAggregateEnd("visitModules: prepare");
  1132. logger.timeEnd("visitModules: visiting");
  1133. if (chunkGroupsForCombining.size > 0) {
  1134. logger.time("visitModules: combine available modules");
  1135. processChunkGroupsForCombining();
  1136. logger.timeEnd("visitModules: combine available modules");
  1137. }
  1138. if (queueConnect.size > 0) {
  1139. logger.time("visitModules: calculating available modules");
  1140. processConnectQueue();
  1141. logger.timeEnd("visitModules: calculating available modules");
  1142. if (chunkGroupsForMerging.size > 0) {
  1143. logger.time("visitModules: merging available modules");
  1144. processChunkGroupsForMerging();
  1145. logger.timeEnd("visitModules: merging available modules");
  1146. }
  1147. }
  1148. if (outdatedChunkGroupInfo.size > 0) {
  1149. logger.time("visitModules: check modules for revisit");
  1150. processOutdatedChunkGroupInfo();
  1151. logger.timeEnd("visitModules: check modules for revisit");
  1152. }
  1153. // Run queueDelayed when all items of the queue are processed
  1154. // This is important to get the global indexing correct
  1155. // Async blocks should be processed after all sync blocks are processed
  1156. if (queue.length === 0) {
  1157. const tempQueue = queue;
  1158. queue = queueDelayed.reverse();
  1159. queueDelayed = tempQueue;
  1160. }
  1161. }
  1162. logger.log(
  1163. `${statProcessedQueueItems} queue items processed (${statProcessedBlocks} blocks)`
  1164. );
  1165. logger.log(`${statConnectedChunkGroups} chunk groups connected`);
  1166. logger.log(
  1167. `${statProcessedChunkGroupsForMerging} chunk groups processed for merging (${statMergedAvailableModuleSets} module sets, ${statForkedAvailableModules} forked, ${statForkedAvailableModulesCount} + ${statForkedAvailableModulesCountPlus} modules forked, ${statForkedMergedModulesCount} + ${statForkedMergedModulesCountPlus} modules merged into fork, ${statForkedResultModulesCount} resulting modules)`
  1168. );
  1169. logger.log(
  1170. `${statChunkGroupInfoUpdated} chunk group info updated (${statChildChunkGroupsReconnected} already connected chunk groups reconnected)`
  1171. );
  1172. };
  1173. /**
  1174. *
  1175. * @param {Compilation} compilation the compilation
  1176. * @param {Set<DependenciesBlock>} blocksWithNestedBlocks flag for blocks that have nested blocks
  1177. * @param {Map<AsyncDependenciesBlock, BlockChunkGroupConnection[]>} blockConnections connection for blocks
  1178. * @param {Map<ChunkGroup, ChunkGroupInfo>} chunkGroupInfoMap mapping from chunk group to available modules
  1179. */
  1180. const connectChunkGroups = (
  1181. compilation,
  1182. blocksWithNestedBlocks,
  1183. blockConnections,
  1184. chunkGroupInfoMap
  1185. ) => {
  1186. const { chunkGraph } = compilation;
  1187. /**
  1188. * Helper function to check if all modules of a chunk are available
  1189. *
  1190. * @param {ChunkGroup} chunkGroup the chunkGroup to scan
  1191. * @param {ModuleSetPlus} availableModules the comparator set
  1192. * @returns {boolean} return true if all modules of a chunk are available
  1193. */
  1194. const areModulesAvailable = (chunkGroup, availableModules) => {
  1195. for (const chunk of chunkGroup.chunks) {
  1196. for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
  1197. if (!availableModules.has(module) && !availableModules.plus.has(module))
  1198. return false;
  1199. }
  1200. }
  1201. return true;
  1202. };
  1203. // For each edge in the basic chunk graph
  1204. for (const [block, connections] of blockConnections) {
  1205. // 1. Check if connection is needed
  1206. // When none of the dependencies need to be connected
  1207. // we can skip all of them
  1208. // It's not possible to filter each item so it doesn't create inconsistent
  1209. // connections and modules can only create one version
  1210. // TODO maybe decide this per runtime
  1211. if (
  1212. // TODO is this needed?
  1213. !blocksWithNestedBlocks.has(block) &&
  1214. connections.every(({ chunkGroup, originChunkGroupInfo }) =>
  1215. areModulesAvailable(
  1216. chunkGroup,
  1217. originChunkGroupInfo.resultingAvailableModules
  1218. )
  1219. )
  1220. ) {
  1221. continue;
  1222. }
  1223. // 2. Foreach edge
  1224. for (let i = 0; i < connections.length; i++) {
  1225. const { chunkGroup, originChunkGroupInfo } = connections[i];
  1226. // 3. Connect block with chunk
  1227. chunkGraph.connectBlockAndChunkGroup(block, chunkGroup);
  1228. // 4. Connect chunk with parent
  1229. connectChunkGroupParentAndChild(
  1230. originChunkGroupInfo.chunkGroup,
  1231. chunkGroup
  1232. );
  1233. }
  1234. }
  1235. };
  1236. /**
  1237. * Remove all unconnected chunk groups
  1238. * @param {Compilation} compilation the compilation
  1239. * @param {Iterable<ChunkGroup>} allCreatedChunkGroups all chunk groups that where created before
  1240. */
  1241. const cleanupUnconnectedGroups = (compilation, allCreatedChunkGroups) => {
  1242. const { chunkGraph } = compilation;
  1243. for (const chunkGroup of allCreatedChunkGroups) {
  1244. if (chunkGroup.getNumberOfParents() === 0) {
  1245. for (const chunk of chunkGroup.chunks) {
  1246. compilation.chunks.delete(chunk);
  1247. chunkGraph.disconnectChunk(chunk);
  1248. }
  1249. chunkGraph.disconnectChunkGroup(chunkGroup);
  1250. chunkGroup.remove();
  1251. }
  1252. }
  1253. };
  1254. /**
  1255. * This method creates the Chunk graph from the Module graph
  1256. * @param {Compilation} compilation the compilation
  1257. * @param {Map<Entrypoint, Module[]>} inputEntrypointsAndModules chunk groups which are processed with the modules
  1258. * @returns {void}
  1259. */
  1260. const buildChunkGraph = (compilation, inputEntrypointsAndModules) => {
  1261. const logger = compilation.getLogger("webpack.buildChunkGraph");
  1262. // SHARED STATE
  1263. /** @type {Map<AsyncDependenciesBlock, BlockChunkGroupConnection[]>} */
  1264. const blockConnections = new Map();
  1265. /** @type {Set<ChunkGroup>} */
  1266. const allCreatedChunkGroups = new Set();
  1267. /** @type {Map<ChunkGroup, ChunkGroupInfo>} */
  1268. const chunkGroupInfoMap = new Map();
  1269. /** @type {Set<DependenciesBlock>} */
  1270. const blocksWithNestedBlocks = new Set();
  1271. // PART ONE
  1272. logger.time("visitModules");
  1273. visitModules(
  1274. logger,
  1275. compilation,
  1276. inputEntrypointsAndModules,
  1277. chunkGroupInfoMap,
  1278. blockConnections,
  1279. blocksWithNestedBlocks,
  1280. allCreatedChunkGroups
  1281. );
  1282. logger.timeEnd("visitModules");
  1283. // PART TWO
  1284. logger.time("connectChunkGroups");
  1285. connectChunkGroups(
  1286. compilation,
  1287. blocksWithNestedBlocks,
  1288. blockConnections,
  1289. chunkGroupInfoMap
  1290. );
  1291. logger.timeEnd("connectChunkGroups");
  1292. for (const [chunkGroup, chunkGroupInfo] of chunkGroupInfoMap) {
  1293. for (const chunk of chunkGroup.chunks)
  1294. chunk.runtime = mergeRuntime(chunk.runtime, chunkGroupInfo.runtime);
  1295. }
  1296. // Cleanup work
  1297. logger.time("cleanup");
  1298. cleanupUnconnectedGroups(compilation, allCreatedChunkGroups);
  1299. logger.timeEnd("cleanup");
  1300. };
  1301. module.exports = buildChunkGraph;