ModuleGraph.js 25 KB

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