ModuleGraph.js 25 KB

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