ModuleGraph.js 28 KB

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