ModuleGraph.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const util = require("util");
  7. const ExportsInfo = require("./ExportsInfo");
  8. const ModuleGraphConnection = require("./ModuleGraphConnection");
  9. const SortableSet = require("./util/SortableSet");
  10. const WeakTupleMap = require("./util/WeakTupleMap");
  11. /** @typedef {import("./DependenciesBlock")} DependenciesBlock */
  12. /** @typedef {import("./Dependency")} Dependency */
  13. /** @typedef {import("./ExportsInfo").ExportInfo} ExportInfo */
  14. /** @typedef {import("./Module")} Module */
  15. /** @typedef {import("./ModuleProfile")} ModuleProfile */
  16. /** @typedef {import("./RequestShortener")} RequestShortener */
  17. /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
  18. /**
  19. * @callback OptimizationBailoutFunction
  20. * @param {RequestShortener} requestShortener
  21. * @returns {string}
  22. */
  23. const EMPTY_SET = new Set();
  24. /**
  25. * @param {SortableSet<ModuleGraphConnection>} set input
  26. * @returns {readonly Map<Module | undefined, readonly ModuleGraphConnection[]>} mapped by origin module
  27. */
  28. const getConnectionsByOriginModule = set => {
  29. const map = new Map();
  30. /** @type {Module | 0} */
  31. let lastModule = 0;
  32. /** @type {ModuleGraphConnection[] | undefined} */
  33. let lastList = undefined;
  34. for (const connection of set) {
  35. const { originModule } = connection;
  36. if (lastModule === originModule) {
  37. /** @type {ModuleGraphConnection[]} */
  38. (lastList).push(connection);
  39. } else {
  40. lastModule = /** @type {Module} */ (originModule);
  41. const list = map.get(originModule);
  42. if (list !== undefined) {
  43. lastList = list;
  44. list.push(connection);
  45. } else {
  46. const list = [connection];
  47. lastList = list;
  48. map.set(originModule, list);
  49. }
  50. }
  51. }
  52. return map;
  53. };
  54. /**
  55. * @param {SortableSet<ModuleGraphConnection>} set input
  56. * @returns {readonly Map<Module | undefined, readonly ModuleGraphConnection[]>} mapped by module
  57. */
  58. const getConnectionsByModule = set => {
  59. const map = new Map();
  60. /** @type {Module | 0} */
  61. let lastModule = 0;
  62. /** @type {ModuleGraphConnection[] | undefined} */
  63. let lastList = undefined;
  64. for (const connection of set) {
  65. const { module } = connection;
  66. if (lastModule === module) {
  67. /** @type {ModuleGraphConnection[]} */
  68. (lastList).push(connection);
  69. } else {
  70. lastModule = module;
  71. const list = map.get(module);
  72. if (list !== undefined) {
  73. lastList = list;
  74. list.push(connection);
  75. } else {
  76. const list = [connection];
  77. lastList = list;
  78. map.set(module, list);
  79. }
  80. }
  81. }
  82. return map;
  83. };
  84. class ModuleGraphModule {
  85. constructor() {
  86. /** @type {SortableSet<ModuleGraphConnection>} */
  87. this.incomingConnections = new SortableSet();
  88. /** @type {SortableSet<ModuleGraphConnection> | undefined} */
  89. this.outgoingConnections = undefined;
  90. /** @type {Module | null} */
  91. this.issuer = undefined;
  92. /** @type {(string | OptimizationBailoutFunction)[]} */
  93. this.optimizationBailout = [];
  94. /** @type {ExportsInfo} */
  95. this.exports = new ExportsInfo();
  96. /** @type {number | null} */
  97. this.preOrderIndex = null;
  98. /** @type {number | null} */
  99. this.postOrderIndex = null;
  100. /** @type {number | null} */
  101. this.depth = null;
  102. /** @type {ModuleProfile | undefined | null} */
  103. this.profile = undefined;
  104. /** @type {boolean} */
  105. this.async = false;
  106. /** @type {ModuleGraphConnection[]} */
  107. this._unassignedConnections = undefined;
  108. }
  109. }
  110. class ModuleGraph {
  111. constructor() {
  112. /** @type {WeakMap<Dependency, ModuleGraphConnection | null>} */
  113. this._dependencyMap = new WeakMap();
  114. /** @type {Map<Module, ModuleGraphModule>} */
  115. this._moduleMap = new Map();
  116. /** @type {WeakMap<any, Object>} */
  117. this._metaMap = new WeakMap();
  118. /** @type {WeakTupleMap<any[], any> | undefined} */
  119. this._cache = undefined;
  120. /** @type {Map<Module, WeakTupleMap<any, any>>} */
  121. this._moduleMemCaches = undefined;
  122. /** @type {string | undefined} */
  123. this._cacheStage = undefined;
  124. }
  125. /**
  126. * @param {Module} module the module
  127. * @returns {ModuleGraphModule} the internal module
  128. */
  129. _getModuleGraphModule(module) {
  130. let mgm = this._moduleMap.get(module);
  131. if (mgm === undefined) {
  132. mgm = new ModuleGraphModule();
  133. this._moduleMap.set(module, mgm);
  134. }
  135. return mgm;
  136. }
  137. /**
  138. * @param {Dependency} dependency the dependency
  139. * @param {DependenciesBlock} block parent block
  140. * @param {Module} module parent module
  141. * @param {number=} indexInBlock position in block
  142. * @returns {void}
  143. */
  144. setParents(dependency, block, module, indexInBlock = -1) {
  145. dependency._parentDependenciesBlockIndex = indexInBlock;
  146. dependency._parentDependenciesBlock = block;
  147. dependency._parentModule = module;
  148. }
  149. /**
  150. * @param {Dependency} dependency the dependency
  151. * @returns {Module} parent module
  152. */
  153. getParentModule(dependency) {
  154. return dependency._parentModule;
  155. }
  156. /**
  157. * @param {Dependency} dependency the dependency
  158. * @returns {DependenciesBlock} parent block
  159. */
  160. getParentBlock(dependency) {
  161. return dependency._parentDependenciesBlock;
  162. }
  163. /**
  164. * @param {Dependency} dependency the dependency
  165. * @returns {number} index
  166. */
  167. getParentBlockIndex(dependency) {
  168. return dependency._parentDependenciesBlockIndex;
  169. }
  170. /**
  171. * @param {Module} originModule the referencing module
  172. * @param {Dependency} dependency the referencing dependency
  173. * @param {Module} module the referenced module
  174. * @returns {void}
  175. */
  176. setResolvedModule(originModule, dependency, module) {
  177. const connection = new ModuleGraphConnection(
  178. originModule,
  179. dependency,
  180. module,
  181. undefined,
  182. dependency.weak,
  183. dependency.getCondition(this)
  184. );
  185. const connections = this._getModuleGraphModule(module).incomingConnections;
  186. connections.add(connection);
  187. if (originModule) {
  188. const mgm = this._getModuleGraphModule(originModule);
  189. if (mgm._unassignedConnections === undefined) {
  190. mgm._unassignedConnections = [];
  191. }
  192. mgm._unassignedConnections.push(connection);
  193. if (mgm.outgoingConnections === undefined) {
  194. mgm.outgoingConnections = new SortableSet();
  195. }
  196. mgm.outgoingConnections.add(connection);
  197. } else {
  198. this._dependencyMap.set(dependency, connection);
  199. }
  200. }
  201. /**
  202. * @param {Dependency} dependency the referencing dependency
  203. * @param {Module} module the referenced module
  204. * @returns {void}
  205. */
  206. updateModule(dependency, module) {
  207. const connection =
  208. /** @type {ModuleGraphConnection} */
  209. (this.getConnection(dependency));
  210. if (connection.module === module) return;
  211. const newConnection = connection.clone();
  212. newConnection.module = module;
  213. this._dependencyMap.set(dependency, newConnection);
  214. connection.setActive(false);
  215. const originMgm = this._getModuleGraphModule(connection.originModule);
  216. originMgm.outgoingConnections.add(newConnection);
  217. const targetMgm = this._getModuleGraphModule(module);
  218. targetMgm.incomingConnections.add(newConnection);
  219. }
  220. /**
  221. * @param {Dependency} dependency the referencing dependency
  222. * @returns {void}
  223. */
  224. removeConnection(dependency) {
  225. const connection = this.getConnection(dependency);
  226. const targetMgm = this._getModuleGraphModule(connection.module);
  227. targetMgm.incomingConnections.delete(connection);
  228. const originMgm = this._getModuleGraphModule(connection.originModule);
  229. originMgm.outgoingConnections.delete(connection);
  230. this._dependencyMap.set(dependency, null);
  231. }
  232. /**
  233. * @param {Dependency} dependency the referencing dependency
  234. * @param {string} explanation an explanation
  235. * @returns {void}
  236. */
  237. addExplanation(dependency, explanation) {
  238. const connection = this.getConnection(dependency);
  239. connection.addExplanation(explanation);
  240. }
  241. /**
  242. * @param {Module} sourceModule the source module
  243. * @param {Module} targetModule the target module
  244. * @returns {void}
  245. */
  246. cloneModuleAttributes(sourceModule, targetModule) {
  247. const oldMgm = this._getModuleGraphModule(sourceModule);
  248. const newMgm = this._getModuleGraphModule(targetModule);
  249. newMgm.postOrderIndex = oldMgm.postOrderIndex;
  250. newMgm.preOrderIndex = oldMgm.preOrderIndex;
  251. newMgm.depth = oldMgm.depth;
  252. newMgm.exports = oldMgm.exports;
  253. newMgm.async = oldMgm.async;
  254. }
  255. /**
  256. * @param {Module} module the module
  257. * @returns {void}
  258. */
  259. removeModuleAttributes(module) {
  260. const mgm = this._getModuleGraphModule(module);
  261. mgm.postOrderIndex = null;
  262. mgm.preOrderIndex = null;
  263. mgm.depth = null;
  264. mgm.async = false;
  265. }
  266. /**
  267. * @returns {void}
  268. */
  269. removeAllModuleAttributes() {
  270. for (const mgm of this._moduleMap.values()) {
  271. mgm.postOrderIndex = null;
  272. mgm.preOrderIndex = null;
  273. mgm.depth = null;
  274. mgm.async = false;
  275. }
  276. }
  277. /**
  278. * @param {Module} oldModule the old referencing module
  279. * @param {Module} newModule the new referencing module
  280. * @param {function(ModuleGraphConnection): boolean} filterConnection filter predicate for replacement
  281. * @returns {void}
  282. */
  283. moveModuleConnections(oldModule, newModule, filterConnection) {
  284. if (oldModule === newModule) return;
  285. const oldMgm = this._getModuleGraphModule(oldModule);
  286. const newMgm = this._getModuleGraphModule(newModule);
  287. // Outgoing connections
  288. const oldConnections = oldMgm.outgoingConnections;
  289. if (oldConnections !== undefined) {
  290. if (newMgm.outgoingConnections === undefined) {
  291. newMgm.outgoingConnections = new SortableSet();
  292. }
  293. const newConnections = newMgm.outgoingConnections;
  294. for (const connection of oldConnections) {
  295. if (filterConnection(connection)) {
  296. connection.originModule = newModule;
  297. newConnections.add(connection);
  298. oldConnections.delete(connection);
  299. }
  300. }
  301. }
  302. // Incoming connections
  303. const oldConnections2 = oldMgm.incomingConnections;
  304. const newConnections2 = newMgm.incomingConnections;
  305. for (const connection of oldConnections2) {
  306. if (filterConnection(connection)) {
  307. connection.module = newModule;
  308. newConnections2.add(connection);
  309. oldConnections2.delete(connection);
  310. }
  311. }
  312. }
  313. /**
  314. * @param {Module} oldModule the old referencing module
  315. * @param {Module} newModule the new referencing module
  316. * @param {function(ModuleGraphConnection): boolean} filterConnection filter predicate for replacement
  317. * @returns {void}
  318. */
  319. copyOutgoingModuleConnections(oldModule, newModule, filterConnection) {
  320. if (oldModule === newModule) return;
  321. const oldMgm = this._getModuleGraphModule(oldModule);
  322. const newMgm = this._getModuleGraphModule(newModule);
  323. // Outgoing connections
  324. const oldConnections = oldMgm.outgoingConnections;
  325. if (oldConnections !== undefined) {
  326. if (newMgm.outgoingConnections === undefined) {
  327. newMgm.outgoingConnections = new SortableSet();
  328. }
  329. const newConnections = newMgm.outgoingConnections;
  330. for (const connection of oldConnections) {
  331. if (filterConnection(connection)) {
  332. const newConnection = connection.clone();
  333. newConnection.originModule = newModule;
  334. newConnections.add(newConnection);
  335. if (newConnection.module !== undefined) {
  336. const otherMgm = this._getModuleGraphModule(newConnection.module);
  337. otherMgm.incomingConnections.add(newConnection);
  338. }
  339. }
  340. }
  341. }
  342. }
  343. /**
  344. * @param {Module} module the referenced module
  345. * @param {string} explanation an explanation why it's referenced
  346. * @returns {void}
  347. */
  348. addExtraReason(module, explanation) {
  349. const connections = this._getModuleGraphModule(module).incomingConnections;
  350. connections.add(new ModuleGraphConnection(null, null, module, explanation));
  351. }
  352. /**
  353. * @param {Dependency} dependency the dependency to look for a referenced module
  354. * @returns {Module | null} the referenced module
  355. */
  356. getResolvedModule(dependency) {
  357. const connection = this.getConnection(dependency);
  358. return connection !== undefined ? connection.resolvedModule : null;
  359. }
  360. /**
  361. * @param {Dependency} dependency the dependency to look for a referenced module
  362. * @returns {ModuleGraphConnection | undefined} the connection
  363. */
  364. getConnection(dependency) {
  365. const connection = this._dependencyMap.get(dependency);
  366. if (connection === undefined) {
  367. const module = this.getParentModule(dependency);
  368. if (module !== undefined) {
  369. const mgm = this._getModuleGraphModule(module);
  370. if (
  371. mgm._unassignedConnections &&
  372. mgm._unassignedConnections.length !== 0
  373. ) {
  374. let foundConnection;
  375. for (const connection of mgm._unassignedConnections) {
  376. this._dependencyMap.set(
  377. /** @type {Dependency} */ (connection.dependency),
  378. connection
  379. );
  380. if (connection.dependency === dependency)
  381. foundConnection = connection;
  382. }
  383. mgm._unassignedConnections.length = 0;
  384. if (foundConnection !== undefined) {
  385. return foundConnection;
  386. }
  387. }
  388. }
  389. this._dependencyMap.set(dependency, null);
  390. return undefined;
  391. }
  392. return connection === null ? undefined : connection;
  393. }
  394. /**
  395. * @param {Dependency} dependency the dependency to look for a referenced module
  396. * @returns {Module | null} the referenced module
  397. */
  398. getModule(dependency) {
  399. const connection = this.getConnection(dependency);
  400. return connection !== undefined ? connection.module : null;
  401. }
  402. /**
  403. * @param {Dependency} dependency the dependency to look for a referencing module
  404. * @returns {Module | null} the referencing module
  405. */
  406. getOrigin(dependency) {
  407. const connection = this.getConnection(dependency);
  408. return connection !== undefined ? connection.originModule : null;
  409. }
  410. /**
  411. * @param {Dependency} dependency the dependency to look for a referencing module
  412. * @returns {Module | null} the original referencing module
  413. */
  414. getResolvedOrigin(dependency) {
  415. const connection = this.getConnection(dependency);
  416. return connection !== undefined ? connection.resolvedOriginModule : null;
  417. }
  418. /**
  419. * @param {Module} module the module
  420. * @returns {Iterable<ModuleGraphConnection>} reasons why a module is included
  421. */
  422. getIncomingConnections(module) {
  423. const connections = this._getModuleGraphModule(module).incomingConnections;
  424. return connections;
  425. }
  426. /**
  427. * @param {Module} module the module
  428. * @returns {Iterable<ModuleGraphConnection>} list of outgoing connections
  429. */
  430. getOutgoingConnections(module) {
  431. const connections = this._getModuleGraphModule(module).outgoingConnections;
  432. return connections === undefined ? EMPTY_SET : connections;
  433. }
  434. /**
  435. * @param {Module} module the module
  436. * @returns {readonly Map<Module | undefined, readonly ModuleGraphConnection[]>} reasons why a module is included, in a map by source module
  437. */
  438. getIncomingConnectionsByOriginModule(module) {
  439. const connections = this._getModuleGraphModule(module).incomingConnections;
  440. return connections.getFromUnorderedCache(getConnectionsByOriginModule);
  441. }
  442. /**
  443. * @param {Module} module the module
  444. * @returns {readonly Map<Module | undefined, readonly ModuleGraphConnection[]> | undefined} connections to modules, in a map by module
  445. */
  446. getOutgoingConnectionsByModule(module) {
  447. const connections = this._getModuleGraphModule(module).outgoingConnections;
  448. return connections === undefined
  449. ? undefined
  450. : connections.getFromUnorderedCache(getConnectionsByModule);
  451. }
  452. /**
  453. * @param {Module} module the module
  454. * @returns {ModuleProfile | null} the module profile
  455. */
  456. getProfile(module) {
  457. const mgm = this._getModuleGraphModule(module);
  458. return mgm.profile;
  459. }
  460. /**
  461. * @param {Module} module the module
  462. * @param {ModuleProfile | null} profile the module profile
  463. * @returns {void}
  464. */
  465. setProfile(module, profile) {
  466. const mgm = this._getModuleGraphModule(module);
  467. mgm.profile = profile;
  468. }
  469. /**
  470. * @param {Module} module the module
  471. * @returns {Module | null} the issuer module
  472. */
  473. getIssuer(module) {
  474. const mgm = this._getModuleGraphModule(module);
  475. return mgm.issuer;
  476. }
  477. /**
  478. * @param {Module} module the module
  479. * @param {Module | null} issuer the issuer module
  480. * @returns {void}
  481. */
  482. setIssuer(module, issuer) {
  483. const mgm = this._getModuleGraphModule(module);
  484. mgm.issuer = issuer;
  485. }
  486. /**
  487. * @param {Module} module the module
  488. * @param {Module | null} issuer the issuer module
  489. * @returns {void}
  490. */
  491. setIssuerIfUnset(module, issuer) {
  492. const mgm = this._getModuleGraphModule(module);
  493. if (mgm.issuer === undefined) mgm.issuer = issuer;
  494. }
  495. /**
  496. * @param {Module} module the module
  497. * @returns {(string | OptimizationBailoutFunction)[]} optimization bailouts
  498. */
  499. getOptimizationBailout(module) {
  500. const mgm = this._getModuleGraphModule(module);
  501. return mgm.optimizationBailout;
  502. }
  503. /**
  504. * @param {Module} module the module
  505. * @returns {true | string[] | null} the provided exports
  506. */
  507. getProvidedExports(module) {
  508. const mgm = this._getModuleGraphModule(module);
  509. return mgm.exports.getProvidedExports();
  510. }
  511. /**
  512. * @param {Module} module the module
  513. * @param {string | string[]} exportName a name of an export
  514. * @returns {boolean | null} true, if the export is provided by the module.
  515. * null, if it's unknown.
  516. * false, if it's not provided.
  517. */
  518. isExportProvided(module, exportName) {
  519. const mgm = this._getModuleGraphModule(module);
  520. const result = mgm.exports.isExportProvided(exportName);
  521. return result === undefined ? null : result;
  522. }
  523. /**
  524. * @param {Module} module the module
  525. * @returns {ExportsInfo} info about the exports
  526. */
  527. getExportsInfo(module) {
  528. const mgm = this._getModuleGraphModule(module);
  529. return mgm.exports;
  530. }
  531. /**
  532. * @param {Module} module the module
  533. * @param {string} exportName the export
  534. * @returns {ExportInfo} info about the export
  535. */
  536. getExportInfo(module, exportName) {
  537. const mgm = this._getModuleGraphModule(module);
  538. return mgm.exports.getExportInfo(exportName);
  539. }
  540. /**
  541. * @param {Module} module the module
  542. * @param {string} exportName the export
  543. * @returns {ExportInfo} info about the export (do not modify)
  544. */
  545. getReadOnlyExportInfo(module, exportName) {
  546. const mgm = this._getModuleGraphModule(module);
  547. return mgm.exports.getReadOnlyExportInfo(exportName);
  548. }
  549. /**
  550. * @param {Module} module the module
  551. * @param {RuntimeSpec} runtime the runtime
  552. * @returns {false | true | SortableSet<string> | null} the used exports
  553. * false: module is not used at all.
  554. * true: the module namespace/object export is used.
  555. * SortableSet<string>: these export names are used.
  556. * empty SortableSet<string>: module is used but no export.
  557. * null: unknown, worst case should be assumed.
  558. */
  559. getUsedExports(module, runtime) {
  560. const mgm = this._getModuleGraphModule(module);
  561. return mgm.exports.getUsedExports(runtime);
  562. }
  563. /**
  564. * @param {Module} module the module
  565. * @returns {number | null} the index of the module
  566. */
  567. getPreOrderIndex(module) {
  568. const mgm = this._getModuleGraphModule(module);
  569. return mgm.preOrderIndex;
  570. }
  571. /**
  572. * @param {Module} module the module
  573. * @returns {number | null} the index of the module
  574. */
  575. getPostOrderIndex(module) {
  576. const mgm = this._getModuleGraphModule(module);
  577. return mgm.postOrderIndex;
  578. }
  579. /**
  580. * @param {Module} module the module
  581. * @param {number} index the index of the module
  582. * @returns {void}
  583. */
  584. setPreOrderIndex(module, index) {
  585. const mgm = this._getModuleGraphModule(module);
  586. mgm.preOrderIndex = index;
  587. }
  588. /**
  589. * @param {Module} module the module
  590. * @param {number} index the index of the module
  591. * @returns {boolean} true, if the index was set
  592. */
  593. setPreOrderIndexIfUnset(module, index) {
  594. const mgm = this._getModuleGraphModule(module);
  595. if (mgm.preOrderIndex === null) {
  596. mgm.preOrderIndex = index;
  597. return true;
  598. }
  599. return false;
  600. }
  601. /**
  602. * @param {Module} module the module
  603. * @param {number} index the index of the module
  604. * @returns {void}
  605. */
  606. setPostOrderIndex(module, index) {
  607. const mgm = this._getModuleGraphModule(module);
  608. mgm.postOrderIndex = index;
  609. }
  610. /**
  611. * @param {Module} module the module
  612. * @param {number} index the index of the module
  613. * @returns {boolean} true, if the index was set
  614. */
  615. setPostOrderIndexIfUnset(module, index) {
  616. const mgm = this._getModuleGraphModule(module);
  617. if (mgm.postOrderIndex === null) {
  618. mgm.postOrderIndex = index;
  619. return true;
  620. }
  621. return false;
  622. }
  623. /**
  624. * @param {Module} module the module
  625. * @returns {number | null} the depth of the module
  626. */
  627. getDepth(module) {
  628. const mgm = this._getModuleGraphModule(module);
  629. return mgm.depth;
  630. }
  631. /**
  632. * @param {Module} module the module
  633. * @param {number} depth the depth of the module
  634. * @returns {void}
  635. */
  636. setDepth(module, depth) {
  637. const mgm = this._getModuleGraphModule(module);
  638. mgm.depth = depth;
  639. }
  640. /**
  641. * @param {Module} module the module
  642. * @param {number} depth the depth of the module
  643. * @returns {boolean} true, if the depth was set
  644. */
  645. setDepthIfLower(module, depth) {
  646. const mgm = this._getModuleGraphModule(module);
  647. if (mgm.depth === null || mgm.depth > depth) {
  648. mgm.depth = depth;
  649. return true;
  650. }
  651. return false;
  652. }
  653. /**
  654. * @param {Module} module the module
  655. * @returns {boolean} true, if the module is async
  656. */
  657. isAsync(module) {
  658. const mgm = this._getModuleGraphModule(module);
  659. return mgm.async;
  660. }
  661. /**
  662. * @param {Module} module the module
  663. * @returns {void}
  664. */
  665. setAsync(module) {
  666. const mgm = this._getModuleGraphModule(module);
  667. mgm.async = true;
  668. }
  669. /**
  670. * @param {any} thing any thing
  671. * @returns {Object} metadata
  672. */
  673. getMeta(thing) {
  674. let meta = this._metaMap.get(thing);
  675. if (meta === undefined) {
  676. meta = Object.create(null);
  677. this._metaMap.set(thing, /** @type {Object} */ (meta));
  678. }
  679. return /** @type {Object} */ (meta);
  680. }
  681. /**
  682. * @param {any} thing any thing
  683. * @returns {Object | undefined} metadata
  684. */
  685. getMetaIfExisting(thing) {
  686. return this._metaMap.get(thing);
  687. }
  688. /**
  689. * @param {string=} cacheStage a persistent stage name for caching
  690. */
  691. freeze(cacheStage) {
  692. this._cache = new WeakTupleMap();
  693. this._cacheStage = cacheStage;
  694. }
  695. unfreeze() {
  696. this._cache = undefined;
  697. this._cacheStage = undefined;
  698. }
  699. /**
  700. * @template {any[]} T
  701. * @template V
  702. * @param {(moduleGraph: ModuleGraph, ...args: T) => V} fn computer
  703. * @param {T} args arguments
  704. * @returns {V} computed value or cached
  705. */
  706. cached(fn, ...args) {
  707. if (this._cache === undefined) return fn(this, ...args);
  708. return this._cache.provide(fn, ...args, () => fn(this, ...args));
  709. }
  710. /**
  711. * @param {Map<Module, WeakTupleMap<any, any>>} moduleMemCaches mem caches for modules for better caching
  712. */
  713. setModuleMemCaches(moduleMemCaches) {
  714. this._moduleMemCaches = moduleMemCaches;
  715. }
  716. /**
  717. * @param {Dependency} dependency dependency
  718. * @param {...any} args arguments, last argument is a function called with moduleGraph, dependency, ...args
  719. * @returns {any} computed value or cached
  720. */
  721. dependencyCacheProvide(dependency, ...args) {
  722. /** @type {(moduleGraph: ModuleGraph, dependency: Dependency, ...args: any[]) => any} */
  723. const fn = args.pop();
  724. if (this._moduleMemCaches && this._cacheStage) {
  725. const memCache = this._moduleMemCaches.get(
  726. this.getParentModule(dependency)
  727. );
  728. if (memCache !== undefined) {
  729. return memCache.provide(dependency, this._cacheStage, ...args, () =>
  730. fn(this, dependency, ...args)
  731. );
  732. }
  733. }
  734. if (this._cache === undefined) return fn(this, dependency, ...args);
  735. return this._cache.provide(dependency, ...args, () =>
  736. fn(this, dependency, ...args)
  737. );
  738. }
  739. // TODO remove in webpack 6
  740. /**
  741. * @param {Module} module the module
  742. * @param {string} deprecateMessage message for the deprecation message
  743. * @param {string} deprecationCode code for the deprecation
  744. * @returns {ModuleGraph} the module graph
  745. */
  746. static getModuleGraphForModule(module, deprecateMessage, deprecationCode) {
  747. const fn = deprecateMap.get(deprecateMessage);
  748. if (fn) return fn(module);
  749. const newFn = util.deprecate(
  750. /**
  751. * @param {Module} module the module
  752. * @returns {ModuleGraph} the module graph
  753. */
  754. module => {
  755. const moduleGraph = moduleGraphForModuleMap.get(module);
  756. if (!moduleGraph)
  757. throw new Error(
  758. deprecateMessage +
  759. "There was no ModuleGraph assigned to the Module for backward-compat (Use the new API)"
  760. );
  761. return moduleGraph;
  762. },
  763. deprecateMessage + ": Use new ModuleGraph API",
  764. deprecationCode
  765. );
  766. deprecateMap.set(deprecateMessage, newFn);
  767. return newFn(module);
  768. }
  769. // TODO remove in webpack 6
  770. /**
  771. * @param {Module} module the module
  772. * @param {ModuleGraph} moduleGraph the module graph
  773. * @returns {void}
  774. */
  775. static setModuleGraphForModule(module, moduleGraph) {
  776. moduleGraphForModuleMap.set(module, moduleGraph);
  777. }
  778. // TODO remove in webpack 6
  779. /**
  780. * @param {Module} module the module
  781. * @returns {void}
  782. */
  783. static clearModuleGraphForModule(module) {
  784. moduleGraphForModuleMap.delete(module);
  785. }
  786. }
  787. // TODO remove in webpack 6
  788. /** @type {WeakMap<Module, ModuleGraph>} */
  789. const moduleGraphForModuleMap = new WeakMap();
  790. // TODO remove in webpack 6
  791. /** @type {Map<string, (module: Module) => ModuleGraph>} */
  792. const deprecateMap = new Map();
  793. module.exports = ModuleGraph;
  794. module.exports.ModuleGraphConnection = ModuleGraphConnection;