ModuleGraph.js 24 KB

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