ModuleGraph.js 24 KB

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