ExportsInfo.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { equals } = require("./util/ArrayHelpers");
  7. const SortableSet = require("./util/SortableSet");
  8. const makeSerializable = require("./util/makeSerializable");
  9. const { forEachRuntime } = require("./util/runtime");
  10. /** @typedef {import("./Dependency").RuntimeSpec} RuntimeSpec */
  11. /** @typedef {import("./Module")} Module */
  12. /** @typedef {import("./ModuleGraph")} ModuleGraph */
  13. /** @typedef {import("./ModuleGraphConnection")} ModuleGraphConnection */
  14. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  15. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  16. /** @typedef {import("./util/Hash")} Hash */
  17. /** @typedef {typeof UsageState.OnlyPropertiesUsed | typeof UsageState.NoInfo | typeof UsageState.Unknown | typeof UsageState.Used} RuntimeUsageStateType */
  18. /** @typedef {typeof UsageState.Unused | RuntimeUsageStateType} UsageStateType */
  19. const UsageState = Object.freeze({
  20. Unused: /** @type {0} */ (0),
  21. OnlyPropertiesUsed: /** @type {1} */ (1),
  22. NoInfo: /** @type {2} */ (2),
  23. Unknown: /** @type {3} */ (3),
  24. Used: /** @type {4} */ (4)
  25. });
  26. const RETURNS_TRUE = () => true;
  27. const CIRCULAR = Symbol("circular target");
  28. class RestoreProvidedData {
  29. constructor(
  30. exports,
  31. otherProvided,
  32. otherCanMangleProvide,
  33. otherTerminalBinding
  34. ) {
  35. this.exports = exports;
  36. this.otherProvided = otherProvided;
  37. this.otherCanMangleProvide = otherCanMangleProvide;
  38. this.otherTerminalBinding = otherTerminalBinding;
  39. }
  40. /**
  41. * @param {ObjectSerializerContext} context context
  42. */
  43. serialize({ write }) {
  44. write(this.exports);
  45. write(this.otherProvided);
  46. write(this.otherCanMangleProvide);
  47. write(this.otherTerminalBinding);
  48. }
  49. /**
  50. * @param {ObjectDeserializerContext} context context
  51. * @returns {RestoreProvidedData} RestoreProvidedData
  52. */
  53. static deserialize({ read }) {
  54. return new RestoreProvidedData(read(), read(), read(), read());
  55. }
  56. }
  57. makeSerializable(
  58. RestoreProvidedData,
  59. "webpack/lib/ModuleGraph",
  60. "RestoreProvidedData"
  61. );
  62. class ExportsInfo {
  63. constructor() {
  64. /** @type {Map<string, ExportInfo>} */
  65. this._exports = new Map();
  66. this._otherExportsInfo = new ExportInfo(null);
  67. this._sideEffectsOnlyInfo = new ExportInfo("*side effects only*");
  68. this._exportsAreOrdered = false;
  69. /** @type {ExportsInfo=} */
  70. this._redirectTo = undefined;
  71. }
  72. /**
  73. * @returns {Iterable<ExportInfo>} all owned exports in any order
  74. */
  75. get ownedExports() {
  76. return this._exports.values();
  77. }
  78. /**
  79. * @returns {Iterable<ExportInfo>} all owned exports in order
  80. */
  81. get orderedOwnedExports() {
  82. if (!this._exportsAreOrdered) {
  83. this._sortExports();
  84. }
  85. return this._exports.values();
  86. }
  87. /**
  88. * @returns {Iterable<ExportInfo>} all exports in any order
  89. */
  90. get exports() {
  91. if (this._redirectTo !== undefined) {
  92. const map = new Map(this._redirectTo._exports);
  93. for (const [key, value] of this._exports) {
  94. map.set(key, value);
  95. }
  96. return map.values();
  97. }
  98. return this._exports.values();
  99. }
  100. /**
  101. * @returns {Iterable<ExportInfo>} all exports in order
  102. */
  103. get orderedExports() {
  104. if (!this._exportsAreOrdered) {
  105. this._sortExports();
  106. }
  107. if (this._redirectTo !== undefined) {
  108. const map = new Map(
  109. Array.from(this._redirectTo.orderedExports, item => [item.name, item])
  110. );
  111. for (const [key, value] of this._exports) {
  112. map.set(key, value);
  113. }
  114. // sorting should be pretty fast as map contains
  115. // a lot of presorted items
  116. this._sortExportsMap(map);
  117. return map.values();
  118. }
  119. return this._exports.values();
  120. }
  121. /**
  122. * @returns {ExportInfo} the export info of unlisted exports
  123. */
  124. get otherExportsInfo() {
  125. if (this._redirectTo !== undefined)
  126. return this._redirectTo.otherExportsInfo;
  127. return this._otherExportsInfo;
  128. }
  129. _sortExportsMap(exports) {
  130. if (exports.size > 1) {
  131. const namesInOrder = [];
  132. for (const entry of exports.values()) {
  133. namesInOrder.push(entry.name);
  134. }
  135. namesInOrder.sort();
  136. let i = 0;
  137. for (const entry of exports.values()) {
  138. const name = namesInOrder[i];
  139. if (entry.name !== name) break;
  140. i++;
  141. }
  142. for (; i < namesInOrder.length; i++) {
  143. const name = namesInOrder[i];
  144. const correctEntry = exports.get(name);
  145. exports.delete(name);
  146. exports.set(name, correctEntry);
  147. }
  148. }
  149. }
  150. _sortExports() {
  151. this._sortExportsMap(this._exports);
  152. this._exportsAreOrdered = true;
  153. }
  154. setRedirectNamedTo(exportsInfo) {
  155. if (this._redirectTo === exportsInfo) return false;
  156. this._redirectTo = exportsInfo;
  157. return true;
  158. }
  159. setHasProvideInfo() {
  160. for (const exportInfo of this._exports.values()) {
  161. if (exportInfo.provided === undefined) {
  162. exportInfo.provided = false;
  163. }
  164. if (exportInfo.canMangleProvide === undefined) {
  165. exportInfo.canMangleProvide = true;
  166. }
  167. }
  168. if (this._redirectTo !== undefined) {
  169. this._redirectTo.setHasProvideInfo();
  170. } else {
  171. if (this._otherExportsInfo.provided === undefined) {
  172. this._otherExportsInfo.provided = false;
  173. }
  174. if (this._otherExportsInfo.canMangleProvide === undefined) {
  175. this._otherExportsInfo.canMangleProvide = true;
  176. }
  177. }
  178. }
  179. setHasUseInfo() {
  180. for (const exportInfo of this._exports.values()) {
  181. exportInfo.setHasUseInfo();
  182. }
  183. this._sideEffectsOnlyInfo.setHasUseInfo();
  184. if (this._redirectTo !== undefined) {
  185. this._redirectTo.setHasUseInfo();
  186. } else {
  187. this._otherExportsInfo.setHasUseInfo();
  188. }
  189. }
  190. /**
  191. * @param {string} name export name
  192. * @returns {ExportInfo} export info for this name
  193. */
  194. getOwnExportInfo(name) {
  195. const info = this._exports.get(name);
  196. if (info !== undefined) return info;
  197. const newInfo = new ExportInfo(name, this._otherExportsInfo);
  198. this._exports.set(name, newInfo);
  199. this._exportsAreOrdered = false;
  200. return newInfo;
  201. }
  202. /**
  203. * @param {string} name export name
  204. * @returns {ExportInfo} export info for this name
  205. */
  206. getExportInfo(name) {
  207. const info = this._exports.get(name);
  208. if (info !== undefined) return info;
  209. if (this._redirectTo !== undefined)
  210. return this._redirectTo.getExportInfo(name);
  211. const newInfo = new ExportInfo(name, this._otherExportsInfo);
  212. this._exports.set(name, newInfo);
  213. this._exportsAreOrdered = false;
  214. return newInfo;
  215. }
  216. /**
  217. * @param {string} name export name
  218. * @returns {ExportInfo} export info for this name
  219. */
  220. getReadOnlyExportInfo(name) {
  221. const info = this._exports.get(name);
  222. if (info !== undefined) return info;
  223. if (this._redirectTo !== undefined)
  224. return this._redirectTo.getReadOnlyExportInfo(name);
  225. return this._otherExportsInfo;
  226. }
  227. /**
  228. * @param {string[]} name export name
  229. * @returns {ExportInfo | undefined} export info for this name
  230. */
  231. getReadOnlyExportInfoRecursive(name) {
  232. const exportInfo = this.getReadOnlyExportInfo(name[0]);
  233. if (name.length === 1) return exportInfo;
  234. if (!exportInfo.exportsInfo) return undefined;
  235. return exportInfo.exportsInfo.getReadOnlyExportInfoRecursive(name.slice(1));
  236. }
  237. /**
  238. * @param {string[]=} name the export name
  239. * @returns {ExportsInfo | undefined} the nested exports info
  240. */
  241. getNestedExportsInfo(name) {
  242. if (Array.isArray(name) && name.length > 0) {
  243. const info = this.getReadOnlyExportInfo(name[0]);
  244. if (!info.exportsInfo) return undefined;
  245. return info.exportsInfo.getNestedExportsInfo(name.slice(1));
  246. }
  247. return this;
  248. }
  249. /**
  250. * @param {boolean=} canMangle true, if exports can still be mangled (defaults to false)
  251. * @param {Set<string>=} excludeExports list of unaffected exports
  252. * @param {any=} targetKey use this as key for the target
  253. * @param {ModuleGraphConnection=} targetModule set this module as target
  254. * @param {number=} priority priority
  255. * @returns {boolean} true, if this call changed something
  256. */
  257. setUnknownExportsProvided(
  258. canMangle,
  259. excludeExports,
  260. targetKey,
  261. targetModule,
  262. priority
  263. ) {
  264. let changed = false;
  265. if (excludeExports) {
  266. for (const name of excludeExports) {
  267. // Make sure these entries exist, so they can get different info
  268. this.getExportInfo(name);
  269. }
  270. }
  271. for (const exportInfo of this._exports.values()) {
  272. if (!canMangle && exportInfo.canMangleProvide !== false) {
  273. exportInfo.canMangleProvide = false;
  274. changed = true;
  275. }
  276. if (excludeExports && excludeExports.has(exportInfo.name)) continue;
  277. if (exportInfo.provided !== true && exportInfo.provided !== null) {
  278. exportInfo.provided = null;
  279. changed = true;
  280. }
  281. if (targetKey) {
  282. exportInfo.setTarget(
  283. targetKey,
  284. /** @type {ModuleGraphConnection} */ (targetModule),
  285. [exportInfo.name],
  286. -1
  287. );
  288. }
  289. }
  290. if (this._redirectTo !== undefined) {
  291. if (
  292. this._redirectTo.setUnknownExportsProvided(
  293. canMangle,
  294. excludeExports,
  295. targetKey,
  296. targetModule,
  297. priority
  298. )
  299. ) {
  300. changed = true;
  301. }
  302. } else {
  303. if (
  304. this._otherExportsInfo.provided !== true &&
  305. this._otherExportsInfo.provided !== null
  306. ) {
  307. this._otherExportsInfo.provided = null;
  308. changed = true;
  309. }
  310. if (!canMangle && this._otherExportsInfo.canMangleProvide !== false) {
  311. this._otherExportsInfo.canMangleProvide = false;
  312. changed = true;
  313. }
  314. if (targetKey) {
  315. this._otherExportsInfo.setTarget(
  316. targetKey,
  317. /** @type {ModuleGraphConnection} */ (targetModule),
  318. undefined,
  319. priority
  320. );
  321. }
  322. }
  323. return changed;
  324. }
  325. /**
  326. * @param {RuntimeSpec} runtime the runtime
  327. * @returns {boolean} true, when something changed
  328. */
  329. setUsedInUnknownWay(runtime) {
  330. let changed = false;
  331. for (const exportInfo of this._exports.values()) {
  332. if (exportInfo.setUsedInUnknownWay(runtime)) {
  333. changed = true;
  334. }
  335. }
  336. if (this._redirectTo !== undefined) {
  337. if (this._redirectTo.setUsedInUnknownWay(runtime)) {
  338. changed = true;
  339. }
  340. } else {
  341. if (
  342. this._otherExportsInfo.setUsedConditionally(
  343. used => used < UsageState.Unknown,
  344. UsageState.Unknown,
  345. runtime
  346. )
  347. ) {
  348. changed = true;
  349. }
  350. if (this._otherExportsInfo.canMangleUse !== false) {
  351. this._otherExportsInfo.canMangleUse = false;
  352. changed = true;
  353. }
  354. }
  355. return changed;
  356. }
  357. /**
  358. * @param {RuntimeSpec} runtime the runtime
  359. * @returns {boolean} true, when something changed
  360. */
  361. setUsedWithoutInfo(runtime) {
  362. let changed = false;
  363. for (const exportInfo of this._exports.values()) {
  364. if (exportInfo.setUsedWithoutInfo(runtime)) {
  365. changed = true;
  366. }
  367. }
  368. if (this._redirectTo !== undefined) {
  369. if (this._redirectTo.setUsedWithoutInfo(runtime)) {
  370. changed = true;
  371. }
  372. } else {
  373. if (this._otherExportsInfo.setUsed(UsageState.NoInfo, runtime)) {
  374. changed = true;
  375. }
  376. if (this._otherExportsInfo.canMangleUse !== false) {
  377. this._otherExportsInfo.canMangleUse = false;
  378. changed = true;
  379. }
  380. }
  381. return changed;
  382. }
  383. /**
  384. * @param {RuntimeSpec} runtime the runtime
  385. * @returns {boolean} true, when something changed
  386. */
  387. setAllKnownExportsUsed(runtime) {
  388. let changed = false;
  389. for (const exportInfo of this._exports.values()) {
  390. if (!exportInfo.provided) continue;
  391. if (exportInfo.setUsed(UsageState.Used, runtime)) {
  392. changed = true;
  393. }
  394. }
  395. return changed;
  396. }
  397. /**
  398. * @param {RuntimeSpec} runtime the runtime
  399. * @returns {boolean} true, when something changed
  400. */
  401. setUsedForSideEffectsOnly(runtime) {
  402. return this._sideEffectsOnlyInfo.setUsedConditionally(
  403. used => used === UsageState.Unused,
  404. UsageState.Used,
  405. runtime
  406. );
  407. }
  408. /**
  409. * @param {RuntimeSpec} runtime the runtime
  410. * @returns {boolean} true, when the module exports are used in any way
  411. */
  412. isUsed(runtime) {
  413. if (this._redirectTo !== undefined) {
  414. if (this._redirectTo.isUsed(runtime)) {
  415. return true;
  416. }
  417. } else {
  418. if (this._otherExportsInfo.getUsed(runtime) !== UsageState.Unused) {
  419. return true;
  420. }
  421. }
  422. for (const exportInfo of this._exports.values()) {
  423. if (exportInfo.getUsed(runtime) !== UsageState.Unused) {
  424. return true;
  425. }
  426. }
  427. return false;
  428. }
  429. /**
  430. * @param {RuntimeSpec} runtime the runtime
  431. * @returns {boolean} true, when the module is used in any way
  432. */
  433. isModuleUsed(runtime) {
  434. if (this.isUsed(runtime)) return true;
  435. if (this._sideEffectsOnlyInfo.getUsed(runtime) !== UsageState.Unused)
  436. return true;
  437. return false;
  438. }
  439. /**
  440. * @param {RuntimeSpec} runtime the runtime
  441. * @returns {SortableSet<string> | boolean | null} set of used exports, or true (when namespace object is used), or false (when unused), or null (when unknown)
  442. */
  443. getUsedExports(runtime) {
  444. if (!this._redirectTo !== undefined) {
  445. switch (this._otherExportsInfo.getUsed(runtime)) {
  446. case UsageState.NoInfo:
  447. return null;
  448. case UsageState.Unknown:
  449. case UsageState.OnlyPropertiesUsed:
  450. case UsageState.Used:
  451. return true;
  452. }
  453. }
  454. const array = [];
  455. if (!this._exportsAreOrdered) this._sortExports();
  456. for (const exportInfo of this._exports.values()) {
  457. switch (exportInfo.getUsed(runtime)) {
  458. case UsageState.NoInfo:
  459. return null;
  460. case UsageState.Unknown:
  461. return true;
  462. case UsageState.OnlyPropertiesUsed:
  463. case UsageState.Used:
  464. array.push(exportInfo.name);
  465. }
  466. }
  467. if (this._redirectTo !== undefined) {
  468. const inner = this._redirectTo.getUsedExports(runtime);
  469. if (inner === null) return null;
  470. if (inner === true) return true;
  471. if (inner !== false) {
  472. for (const item of inner) {
  473. array.push(item);
  474. }
  475. }
  476. }
  477. if (array.length === 0) {
  478. switch (this._sideEffectsOnlyInfo.getUsed(runtime)) {
  479. case UsageState.NoInfo:
  480. return null;
  481. case UsageState.Unused:
  482. return false;
  483. }
  484. }
  485. return new SortableSet(array);
  486. }
  487. /**
  488. * @returns {null | true | string[]} list of exports when known
  489. */
  490. getProvidedExports() {
  491. if (!this._redirectTo !== undefined) {
  492. switch (this._otherExportsInfo.provided) {
  493. case undefined:
  494. return null;
  495. case null:
  496. return true;
  497. case true:
  498. return true;
  499. }
  500. }
  501. const array = [];
  502. if (!this._exportsAreOrdered) this._sortExports();
  503. for (const exportInfo of this._exports.values()) {
  504. switch (exportInfo.provided) {
  505. case undefined:
  506. return null;
  507. case null:
  508. return true;
  509. case true:
  510. array.push(exportInfo.name);
  511. }
  512. }
  513. if (this._redirectTo !== undefined) {
  514. const inner = this._redirectTo.getProvidedExports();
  515. if (inner === null) return null;
  516. if (inner === true) return true;
  517. for (const item of inner) {
  518. if (!array.includes(item)) {
  519. array.push(item);
  520. }
  521. }
  522. }
  523. return array;
  524. }
  525. /**
  526. * @param {RuntimeSpec} runtime the runtime
  527. * @returns {ExportInfo[]} exports that are relevant (not unused and potential provided)
  528. */
  529. getRelevantExports(runtime) {
  530. const list = [];
  531. for (const exportInfo of this._exports.values()) {
  532. const used = exportInfo.getUsed(runtime);
  533. if (used === UsageState.Unused) continue;
  534. if (exportInfo.provided === false) continue;
  535. list.push(exportInfo);
  536. }
  537. if (this._redirectTo !== undefined) {
  538. for (const exportInfo of this._redirectTo.getRelevantExports(runtime)) {
  539. if (!this._exports.has(exportInfo.name)) list.push(exportInfo);
  540. }
  541. }
  542. if (
  543. this._otherExportsInfo.provided !== false &&
  544. this._otherExportsInfo.getUsed(runtime) !== UsageState.Unused
  545. ) {
  546. list.push(this._otherExportsInfo);
  547. }
  548. return list;
  549. }
  550. /**
  551. * @param {string | string[]} name the name of the export
  552. * @returns {boolean | undefined | null} if the export is provided
  553. */
  554. isExportProvided(name) {
  555. if (Array.isArray(name)) {
  556. const info = this.getReadOnlyExportInfo(name[0]);
  557. if (info.exportsInfo && name.length > 1) {
  558. return info.exportsInfo.isExportProvided(name.slice(1));
  559. }
  560. return info.provided ? name.length === 1 || undefined : info.provided;
  561. }
  562. const info = this.getReadOnlyExportInfo(name);
  563. return info.provided;
  564. }
  565. /**
  566. * @param {RuntimeSpec} runtime runtime
  567. * @returns {string} key representing the usage
  568. */
  569. getUsageKey(runtime) {
  570. const key = [];
  571. if (this._redirectTo !== undefined) {
  572. key.push(this._redirectTo.getUsageKey(runtime));
  573. } else {
  574. key.push(this._otherExportsInfo.getUsed(runtime));
  575. }
  576. key.push(this._sideEffectsOnlyInfo.getUsed(runtime));
  577. for (const exportInfo of this.orderedOwnedExports) {
  578. key.push(exportInfo.getUsed(runtime));
  579. }
  580. return key.join("|");
  581. }
  582. /**
  583. * @param {RuntimeSpec} runtimeA first runtime
  584. * @param {RuntimeSpec} runtimeB second runtime
  585. * @returns {boolean} true, when equally used
  586. */
  587. isEquallyUsed(runtimeA, runtimeB) {
  588. if (this._redirectTo !== undefined) {
  589. if (!this._redirectTo.isEquallyUsed(runtimeA, runtimeB)) return false;
  590. } else {
  591. if (
  592. this._otherExportsInfo.getUsed(runtimeA) !==
  593. this._otherExportsInfo.getUsed(runtimeB)
  594. ) {
  595. return false;
  596. }
  597. }
  598. if (
  599. this._sideEffectsOnlyInfo.getUsed(runtimeA) !==
  600. this._sideEffectsOnlyInfo.getUsed(runtimeB)
  601. ) {
  602. return false;
  603. }
  604. for (const exportInfo of this.ownedExports) {
  605. if (exportInfo.getUsed(runtimeA) !== exportInfo.getUsed(runtimeB))
  606. return false;
  607. }
  608. return true;
  609. }
  610. /**
  611. * @param {string | string[]} name export name
  612. * @param {RuntimeSpec} runtime check usage for this runtime only
  613. * @returns {UsageStateType} usage status
  614. */
  615. getUsed(name, runtime) {
  616. if (Array.isArray(name)) {
  617. if (name.length === 0) return this.otherExportsInfo.getUsed(runtime);
  618. let info = this.getReadOnlyExportInfo(name[0]);
  619. if (info.exportsInfo && name.length > 1) {
  620. return info.exportsInfo.getUsed(name.slice(1), runtime);
  621. }
  622. return info.getUsed(runtime);
  623. }
  624. let info = this.getReadOnlyExportInfo(name);
  625. return info.getUsed(runtime);
  626. }
  627. /**
  628. * @param {string | string[] | undefined} name the export name
  629. * @param {RuntimeSpec} runtime check usage for this runtime only
  630. * @returns {string | string[] | false} the used name
  631. */
  632. getUsedName(name, runtime) {
  633. if (Array.isArray(name)) {
  634. // TODO improve this
  635. if (name.length === 0) {
  636. if (!this.isUsed(runtime)) return false;
  637. return name;
  638. }
  639. let info = this.getReadOnlyExportInfo(name[0]);
  640. const x = info.getUsedName(name[0], runtime);
  641. if (x === false) return false;
  642. const arr = x === name[0] && name.length === 1 ? name : [x];
  643. if (name.length === 1) {
  644. return arr;
  645. }
  646. if (
  647. info.exportsInfo &&
  648. info.getUsed(runtime) === UsageState.OnlyPropertiesUsed
  649. ) {
  650. const nested = info.exportsInfo.getUsedName(name.slice(1), runtime);
  651. if (!nested) return false;
  652. return arr.concat(nested);
  653. } else {
  654. return arr.concat(name.slice(1));
  655. }
  656. } else {
  657. let info = this.getReadOnlyExportInfo(name);
  658. const usedName = info.getUsedName(name, runtime);
  659. return usedName;
  660. }
  661. }
  662. /**
  663. * @param {Hash} hash the hash
  664. * @param {RuntimeSpec} runtime the runtime
  665. * @returns {void}
  666. */
  667. updateHash(hash, runtime) {
  668. this._updateHash(hash, runtime, new Set());
  669. }
  670. /**
  671. * @param {Hash} hash the hash
  672. * @param {RuntimeSpec} runtime the runtime
  673. * @param {Set<ExportsInfo>} alreadyVisitedExportsInfo for circular references
  674. * @returns {void}
  675. */
  676. _updateHash(hash, runtime, alreadyVisitedExportsInfo) {
  677. const set = new Set(alreadyVisitedExportsInfo);
  678. set.add(this);
  679. for (const exportInfo of this.orderedExports) {
  680. if (exportInfo.hasInfo(this._otherExportsInfo, runtime)) {
  681. exportInfo._updateHash(hash, runtime, set);
  682. }
  683. }
  684. this._sideEffectsOnlyInfo._updateHash(hash, runtime, set);
  685. this._otherExportsInfo._updateHash(hash, runtime, set);
  686. if (this._redirectTo !== undefined) {
  687. this._redirectTo._updateHash(hash, runtime, set);
  688. }
  689. }
  690. getRestoreProvidedData() {
  691. const otherProvided = this._otherExportsInfo.provided;
  692. const otherCanMangleProvide = this._otherExportsInfo.canMangleProvide;
  693. const otherTerminalBinding = this._otherExportsInfo.terminalBinding;
  694. const exports = [];
  695. for (const exportInfo of this.orderedExports) {
  696. if (
  697. exportInfo.provided !== otherProvided ||
  698. exportInfo.canMangleProvide !== otherCanMangleProvide ||
  699. exportInfo.terminalBinding !== otherTerminalBinding ||
  700. exportInfo.exportsInfoOwned
  701. ) {
  702. exports.push({
  703. name: exportInfo.name,
  704. provided: exportInfo.provided,
  705. canMangleProvide: exportInfo.canMangleProvide,
  706. terminalBinding: exportInfo.terminalBinding,
  707. exportsInfo: exportInfo.exportsInfoOwned
  708. ? exportInfo.exportsInfo.getRestoreProvidedData()
  709. : undefined
  710. });
  711. }
  712. }
  713. return new RestoreProvidedData(
  714. exports,
  715. otherProvided,
  716. otherCanMangleProvide,
  717. otherTerminalBinding
  718. );
  719. }
  720. /**
  721. * @param {{ otherProvided: any, otherCanMangleProvide: any, otherTerminalBinding: any, exports: any }} data data
  722. */
  723. restoreProvided({
  724. otherProvided,
  725. otherCanMangleProvide,
  726. otherTerminalBinding,
  727. exports
  728. }) {
  729. let wasEmpty = true;
  730. for (const exportInfo of this._exports.values()) {
  731. wasEmpty = false;
  732. exportInfo.provided = otherProvided;
  733. exportInfo.canMangleProvide = otherCanMangleProvide;
  734. exportInfo.terminalBinding = otherTerminalBinding;
  735. }
  736. this._otherExportsInfo.provided = otherProvided;
  737. this._otherExportsInfo.canMangleProvide = otherCanMangleProvide;
  738. this._otherExportsInfo.terminalBinding = otherTerminalBinding;
  739. for (const exp of exports) {
  740. const exportInfo = this.getExportInfo(exp.name);
  741. exportInfo.provided = exp.provided;
  742. exportInfo.canMangleProvide = exp.canMangleProvide;
  743. exportInfo.terminalBinding = exp.terminalBinding;
  744. if (exp.exportsInfo) {
  745. const exportsInfo = exportInfo.createNestedExportsInfo();
  746. exportsInfo.restoreProvided(exp.exportsInfo);
  747. }
  748. }
  749. if (wasEmpty) this._exportsAreOrdered = true;
  750. }
  751. }
  752. class ExportInfo {
  753. /**
  754. * @param {string} name the original name of the export
  755. * @param {ExportInfo=} initFrom init values from this ExportInfo
  756. */
  757. constructor(name, initFrom) {
  758. /** @type {string} */
  759. this.name = name;
  760. /** @private @type {string | null} */
  761. this._usedName = initFrom ? initFrom._usedName : null;
  762. /** @private @type {UsageStateType} */
  763. this._globalUsed = initFrom ? initFrom._globalUsed : undefined;
  764. /** @private @type {Map<string, RuntimeUsageStateType>} */
  765. this._usedInRuntime =
  766. initFrom && initFrom._usedInRuntime
  767. ? new Map(initFrom._usedInRuntime)
  768. : undefined;
  769. /** @private @type {boolean} */
  770. this._hasUseInRuntimeInfo = initFrom
  771. ? initFrom._hasUseInRuntimeInfo
  772. : false;
  773. /**
  774. * true: it is provided
  775. * false: it is not provided
  776. * null: only the runtime knows if it is provided
  777. * undefined: it was not determined if it is provided
  778. * @type {boolean | null | undefined}
  779. */
  780. this.provided = initFrom ? initFrom.provided : undefined;
  781. /**
  782. * is the export a terminal binding that should be checked for export star conflicts
  783. * @type {boolean}
  784. */
  785. this.terminalBinding = initFrom ? initFrom.terminalBinding : false;
  786. /**
  787. * true: it can be mangled
  788. * false: is can not be mangled
  789. * undefined: it was not determined if it can be mangled
  790. * @type {boolean | undefined}
  791. */
  792. this.canMangleProvide = initFrom ? initFrom.canMangleProvide : undefined;
  793. /**
  794. * true: it can be mangled
  795. * false: is can not be mangled
  796. * undefined: it was not determined if it can be mangled
  797. * @type {boolean | undefined}
  798. */
  799. this.canMangleUse = initFrom ? initFrom.canMangleUse : undefined;
  800. /** @type {boolean} */
  801. this.exportsInfoOwned = false;
  802. /** @type {ExportsInfo=} */
  803. this.exportsInfo = undefined;
  804. /** @type {Map<any, { connection: ModuleGraphConnection | null, export: string[], priority: number }>=} */
  805. this._target = undefined;
  806. if (initFrom && initFrom._target) {
  807. this._target = new Map();
  808. for (const [key, value] of initFrom._target) {
  809. this._target.set(key, {
  810. connection: value.connection,
  811. export: value.export || [name],
  812. priority: value.priority
  813. });
  814. }
  815. }
  816. /** @type {Map<any, { connection: ModuleGraphConnection | null, export: string[], priority: number }>=} */
  817. this._maxTarget = undefined;
  818. }
  819. // TODO webpack 5 remove
  820. /** @private */
  821. get used() {
  822. throw new Error("REMOVED");
  823. }
  824. /** @private */
  825. get usedName() {
  826. throw new Error("REMOVED");
  827. }
  828. /**
  829. * @private
  830. * @param {*} v v
  831. */
  832. set used(v) {
  833. throw new Error("REMOVED");
  834. }
  835. /**
  836. * @private
  837. * @param {*} v v
  838. */
  839. set usedName(v) {
  840. throw new Error("REMOVED");
  841. }
  842. get canMangle() {
  843. switch (this.canMangleProvide) {
  844. case undefined:
  845. return this.canMangleUse === false ? false : undefined;
  846. case false:
  847. return false;
  848. case true:
  849. switch (this.canMangleUse) {
  850. case undefined:
  851. return undefined;
  852. case false:
  853. return false;
  854. case true:
  855. return true;
  856. }
  857. }
  858. throw new Error(
  859. `Unexpected flags for canMangle ${this.canMangleProvide} ${this.canMangleUse}`
  860. );
  861. }
  862. /**
  863. * @param {RuntimeSpec} runtime only apply to this runtime
  864. * @returns {boolean} true, when something changed
  865. */
  866. setUsedInUnknownWay(runtime) {
  867. let changed = false;
  868. if (
  869. this.setUsedConditionally(
  870. used => used < UsageState.Unknown,
  871. UsageState.Unknown,
  872. runtime
  873. )
  874. ) {
  875. changed = true;
  876. }
  877. if (this.canMangleUse !== false) {
  878. this.canMangleUse = false;
  879. changed = true;
  880. }
  881. return changed;
  882. }
  883. /**
  884. * @param {RuntimeSpec} runtime only apply to this runtime
  885. * @returns {boolean} true, when something changed
  886. */
  887. setUsedWithoutInfo(runtime) {
  888. let changed = false;
  889. if (this.setUsed(UsageState.NoInfo, runtime)) {
  890. changed = true;
  891. }
  892. if (this.canMangleUse !== false) {
  893. this.canMangleUse = false;
  894. changed = true;
  895. }
  896. return changed;
  897. }
  898. setHasUseInfo() {
  899. if (!this._hasUseInRuntimeInfo) {
  900. this._hasUseInRuntimeInfo = true;
  901. }
  902. if (this.canMangleUse === undefined) {
  903. this.canMangleUse = true;
  904. }
  905. if (this.exportsInfoOwned) {
  906. this.exportsInfo.setHasUseInfo();
  907. }
  908. }
  909. /**
  910. * @param {function(UsageStateType): boolean} condition compare with old value
  911. * @param {UsageStateType} newValue set when condition is true
  912. * @param {RuntimeSpec} runtime only apply to this runtime
  913. * @returns {boolean} true when something has changed
  914. */
  915. setUsedConditionally(condition, newValue, runtime) {
  916. if (runtime === undefined) {
  917. if (this._globalUsed === undefined) {
  918. this._globalUsed = newValue;
  919. return true;
  920. } else {
  921. if (this._globalUsed !== newValue && condition(this._globalUsed)) {
  922. this._globalUsed = newValue;
  923. return true;
  924. }
  925. }
  926. } else if (this._usedInRuntime === undefined) {
  927. if (newValue !== UsageState.Unused && condition(UsageState.Unused)) {
  928. this._usedInRuntime = new Map();
  929. forEachRuntime(runtime, runtime =>
  930. this._usedInRuntime.set(runtime, newValue)
  931. );
  932. return true;
  933. }
  934. } else {
  935. let changed = false;
  936. forEachRuntime(runtime, runtime => {
  937. /** @type {UsageStateType} */
  938. let oldValue = this._usedInRuntime.get(runtime);
  939. if (oldValue === undefined) oldValue = UsageState.Unused;
  940. if (newValue !== oldValue && condition(oldValue)) {
  941. if (newValue === UsageState.Unused) {
  942. this._usedInRuntime.delete(runtime);
  943. } else {
  944. this._usedInRuntime.set(runtime, newValue);
  945. }
  946. changed = true;
  947. }
  948. });
  949. if (changed) {
  950. if (this._usedInRuntime.size === 0) this._usedInRuntime = undefined;
  951. return true;
  952. }
  953. }
  954. return false;
  955. }
  956. /**
  957. * @param {UsageStateType} newValue new value of the used state
  958. * @param {RuntimeSpec} runtime only apply to this runtime
  959. * @returns {boolean} true when something has changed
  960. */
  961. setUsed(newValue, runtime) {
  962. if (runtime === undefined) {
  963. if (this._globalUsed !== newValue) {
  964. this._globalUsed = newValue;
  965. return true;
  966. }
  967. } else if (this._usedInRuntime === undefined) {
  968. if (newValue !== UsageState.Unused) {
  969. this._usedInRuntime = new Map();
  970. forEachRuntime(runtime, runtime =>
  971. this._usedInRuntime.set(runtime, newValue)
  972. );
  973. return true;
  974. }
  975. } else {
  976. let changed = false;
  977. forEachRuntime(runtime, runtime => {
  978. /** @type {UsageStateType} */
  979. let oldValue = this._usedInRuntime.get(runtime);
  980. if (oldValue === undefined) oldValue = UsageState.Unused;
  981. if (newValue !== oldValue) {
  982. if (newValue === UsageState.Unused) {
  983. this._usedInRuntime.delete(runtime);
  984. } else {
  985. this._usedInRuntime.set(runtime, newValue);
  986. }
  987. changed = true;
  988. }
  989. });
  990. if (changed) {
  991. if (this._usedInRuntime.size === 0) this._usedInRuntime = undefined;
  992. return true;
  993. }
  994. }
  995. return false;
  996. }
  997. /**
  998. * @param {any} key the key
  999. * @returns {boolean} true, if something has changed
  1000. */
  1001. unsetTarget(key) {
  1002. if (!this._target) return false;
  1003. if (this._target.delete(key)) {
  1004. this._maxTarget = undefined;
  1005. return true;
  1006. }
  1007. return false;
  1008. }
  1009. /**
  1010. * @param {any} key the key
  1011. * @param {ModuleGraphConnection} connection the target module if a single one
  1012. * @param {string[]=} exportName the exported name
  1013. * @param {number=} priority priority
  1014. * @returns {boolean} true, if something has changed
  1015. */
  1016. setTarget(key, connection, exportName, priority = 0) {
  1017. if (exportName) exportName = [...exportName];
  1018. if (!this._target) {
  1019. this._target = new Map();
  1020. this._target.set(key, { connection, export: exportName, priority });
  1021. return true;
  1022. }
  1023. const oldTarget = this._target.get(key);
  1024. if (!oldTarget) {
  1025. if (oldTarget === null && !connection) return false;
  1026. this._target.set(key, { connection, export: exportName, priority });
  1027. this._maxTarget = undefined;
  1028. return true;
  1029. }
  1030. if (
  1031. oldTarget.connection !== connection ||
  1032. oldTarget.priority !== priority ||
  1033. (exportName
  1034. ? !oldTarget.export || !equals(oldTarget.export, exportName)
  1035. : oldTarget.export)
  1036. ) {
  1037. oldTarget.connection = connection;
  1038. oldTarget.export = exportName;
  1039. oldTarget.priority = priority;
  1040. this._maxTarget = undefined;
  1041. return true;
  1042. }
  1043. return false;
  1044. }
  1045. /**
  1046. * @param {RuntimeSpec} runtime for this runtime
  1047. * @returns {UsageStateType} usage state
  1048. */
  1049. getUsed(runtime) {
  1050. if (!this._hasUseInRuntimeInfo) return UsageState.NoInfo;
  1051. if (this._globalUsed !== undefined) return this._globalUsed;
  1052. if (this._usedInRuntime === undefined) {
  1053. return UsageState.Unused;
  1054. } else if (typeof runtime === "string") {
  1055. const value = this._usedInRuntime.get(runtime);
  1056. return value === undefined ? UsageState.Unused : value;
  1057. } else if (runtime === undefined) {
  1058. /** @type {UsageStateType} */
  1059. let max = UsageState.Unused;
  1060. for (const value of this._usedInRuntime.values()) {
  1061. if (value === UsageState.Used) {
  1062. return UsageState.Used;
  1063. }
  1064. if (max < value) max = value;
  1065. }
  1066. return max;
  1067. } else {
  1068. /** @type {UsageStateType} */
  1069. let max = UsageState.Unused;
  1070. for (const item of runtime) {
  1071. const value = this._usedInRuntime.get(item);
  1072. if (value !== undefined) {
  1073. if (value === UsageState.Used) {
  1074. return UsageState.Used;
  1075. }
  1076. if (max < value) max = value;
  1077. }
  1078. }
  1079. return max;
  1080. }
  1081. }
  1082. /**
  1083. * get used name
  1084. * @param {string | undefined} fallbackName fallback name for used exports with no name
  1085. * @param {RuntimeSpec} runtime check usage for this runtime only
  1086. * @returns {string | false} used name
  1087. */
  1088. getUsedName(fallbackName, runtime) {
  1089. if (this._hasUseInRuntimeInfo) {
  1090. if (this._globalUsed !== undefined) {
  1091. if (this._globalUsed === UsageState.Unused) return false;
  1092. } else {
  1093. if (this._usedInRuntime === undefined) return false;
  1094. if (typeof runtime === "string") {
  1095. if (!this._usedInRuntime.has(runtime)) {
  1096. return false;
  1097. }
  1098. } else if (runtime !== undefined) {
  1099. if (
  1100. Array.from(runtime).every(
  1101. runtime => !this._usedInRuntime.has(runtime)
  1102. )
  1103. ) {
  1104. return false;
  1105. }
  1106. }
  1107. }
  1108. }
  1109. if (this._usedName !== null) return this._usedName;
  1110. return this.name || fallbackName;
  1111. }
  1112. /**
  1113. * @returns {boolean} true, when a mangled name of this export is set
  1114. */
  1115. hasUsedName() {
  1116. return this._usedName !== null;
  1117. }
  1118. /**
  1119. * Sets the mangled name of this export
  1120. * @param {string} name the new name
  1121. * @returns {void}
  1122. */
  1123. setUsedName(name) {
  1124. this._usedName = name;
  1125. }
  1126. /**
  1127. * @param {ModuleGraph} moduleGraph the module graph
  1128. * @param {function({ module: Module, export: string[] | undefined }): boolean} resolveTargetFilter filter function to further resolve target
  1129. * @returns {ExportInfo | ExportsInfo | undefined} the terminal binding export(s) info if known
  1130. */
  1131. getTerminalBinding(moduleGraph, resolveTargetFilter = RETURNS_TRUE) {
  1132. if (this.terminalBinding) return this;
  1133. const target = this.getTarget(moduleGraph, resolveTargetFilter);
  1134. if (!target) return undefined;
  1135. const exportsInfo = moduleGraph.getExportsInfo(target.module);
  1136. if (!target.export) return exportsInfo;
  1137. return exportsInfo.getReadOnlyExportInfoRecursive(target.export);
  1138. }
  1139. isReexport() {
  1140. return !this.terminalBinding && this._target && this._target.size > 0;
  1141. }
  1142. _getMaxTarget() {
  1143. if (this._maxTarget !== undefined) return this._maxTarget;
  1144. if (this._target.size <= 1) return (this._maxTarget = this._target);
  1145. let maxPriority = -Infinity;
  1146. let minPriority = Infinity;
  1147. for (const { priority } of this._target.values()) {
  1148. if (maxPriority < priority) maxPriority = priority;
  1149. if (minPriority > priority) minPriority = priority;
  1150. }
  1151. // This should be very common
  1152. if (maxPriority === minPriority) return (this._maxTarget = this._target);
  1153. // This is an edge case
  1154. const map = new Map();
  1155. for (const [key, value] of this._target) {
  1156. if (maxPriority === value.priority) {
  1157. map.set(key, value);
  1158. }
  1159. }
  1160. this._maxTarget = map;
  1161. return map;
  1162. }
  1163. /**
  1164. * @param {ModuleGraph} moduleGraph the module graph
  1165. * @param {function(Module): boolean} validTargetModuleFilter a valid target module
  1166. * @returns {{ module: Module, export: string[] | undefined } | undefined | false} the target, undefined when there is no target, false when no target is valid
  1167. */
  1168. findTarget(moduleGraph, validTargetModuleFilter) {
  1169. return this._findTarget(moduleGraph, validTargetModuleFilter, new Set());
  1170. }
  1171. /**
  1172. * @param {ModuleGraph} moduleGraph the module graph
  1173. * @param {function(Module): boolean} validTargetModuleFilter a valid target module
  1174. * @param {Set<ExportInfo>} alreadyVisited set of already visited export info to avoid circular references
  1175. * @returns {{ module: Module, export: string[] | undefined } | undefined | false} the target, undefined when there is no target, false when no target is valid
  1176. */
  1177. _findTarget(moduleGraph, validTargetModuleFilter, alreadyVisited) {
  1178. if (!this._target || this._target.size === 0) return undefined;
  1179. let rawTarget = this._getMaxTarget().values().next().value;
  1180. if (!rawTarget) return undefined;
  1181. /** @type {{ module: Module, export: string[] | undefined }} */
  1182. let target = {
  1183. module: rawTarget.connection.module,
  1184. export: rawTarget.export
  1185. };
  1186. for (;;) {
  1187. if (validTargetModuleFilter(target.module)) return target;
  1188. const exportsInfo = moduleGraph.getExportsInfo(target.module);
  1189. const exportInfo = exportsInfo.getExportInfo(target.export[0]);
  1190. if (alreadyVisited.has(exportInfo)) return null;
  1191. const newTarget = exportInfo._findTarget(
  1192. moduleGraph,
  1193. validTargetModuleFilter,
  1194. alreadyVisited
  1195. );
  1196. if (!newTarget) return false;
  1197. if (target.export.length === 1) {
  1198. target = newTarget;
  1199. } else {
  1200. target = {
  1201. module: newTarget.module,
  1202. export: newTarget.export
  1203. ? newTarget.export.concat(target.export.slice(1))
  1204. : target.export.slice(1)
  1205. };
  1206. }
  1207. }
  1208. }
  1209. /**
  1210. * @param {ModuleGraph} moduleGraph the module graph
  1211. * @param {function({ module: Module, export: string[] | undefined }): boolean} resolveTargetFilter filter function to further resolve target
  1212. * @returns {{ module: Module, export: string[] | undefined } | undefined} the target
  1213. */
  1214. getTarget(moduleGraph, resolveTargetFilter = RETURNS_TRUE) {
  1215. const result = this._getTarget(moduleGraph, resolveTargetFilter, undefined);
  1216. if (result === CIRCULAR) return undefined;
  1217. return result;
  1218. }
  1219. /**
  1220. * @param {ModuleGraph} moduleGraph the module graph
  1221. * @param {function({ module: Module, connection: ModuleGraphConnection, export: string[] | undefined }): boolean} resolveTargetFilter filter function to further resolve target
  1222. * @param {Set<ExportInfo> | undefined} alreadyVisited set of already visited export info to avoid circular references
  1223. * @returns {{ module: Module, connection: ModuleGraphConnection, export: string[] | undefined } | CIRCULAR | undefined} the target
  1224. */
  1225. _getTarget(moduleGraph, resolveTargetFilter, alreadyVisited) {
  1226. /**
  1227. * @param {{ connection: ModuleGraphConnection, export: string[] | undefined } | null} inputTarget unresolved target
  1228. * @param {Set<ExportInfo>} alreadyVisited set of already visited export info to avoid circular references
  1229. * @returns {{ module: Module, connection: ModuleGraphConnection, export: string[] | undefined } | CIRCULAR | null} resolved target
  1230. */
  1231. const resolveTarget = (inputTarget, alreadyVisited) => {
  1232. if (!inputTarget) return null;
  1233. if (!inputTarget.export) {
  1234. return {
  1235. module: inputTarget.connection.module,
  1236. connection: inputTarget.connection,
  1237. export: undefined
  1238. };
  1239. }
  1240. /** @type {{ module: Module, connection: ModuleGraphConnection, export: string[] | undefined }} */
  1241. let target = {
  1242. module: inputTarget.connection.module,
  1243. connection: inputTarget.connection,
  1244. export: inputTarget.export
  1245. };
  1246. if (!resolveTargetFilter(target)) return target;
  1247. let alreadyVisitedOwned = false;
  1248. for (;;) {
  1249. const exportsInfo = moduleGraph.getExportsInfo(target.module);
  1250. const exportInfo = exportsInfo.getExportInfo(target.export[0]);
  1251. if (!exportInfo) return target;
  1252. if (alreadyVisited.has(exportInfo)) return CIRCULAR;
  1253. const newTarget = exportInfo._getTarget(
  1254. moduleGraph,
  1255. resolveTargetFilter,
  1256. alreadyVisited
  1257. );
  1258. if (newTarget === CIRCULAR) return CIRCULAR;
  1259. if (!newTarget) return target;
  1260. if (target.export.length === 1) {
  1261. target = newTarget;
  1262. if (!target.export) return target;
  1263. } else {
  1264. target = {
  1265. module: newTarget.module,
  1266. connection: newTarget.connection,
  1267. export: newTarget.export
  1268. ? newTarget.export.concat(target.export.slice(1))
  1269. : target.export.slice(1)
  1270. };
  1271. }
  1272. if (!resolveTargetFilter(target)) return target;
  1273. if (!alreadyVisitedOwned) {
  1274. alreadyVisited = new Set(alreadyVisited);
  1275. alreadyVisitedOwned = true;
  1276. }
  1277. alreadyVisited.add(exportInfo);
  1278. }
  1279. };
  1280. if (!this._target || this._target.size === 0) return undefined;
  1281. if (alreadyVisited && alreadyVisited.has(this)) return CIRCULAR;
  1282. const newAlreadyVisited = new Set(alreadyVisited);
  1283. newAlreadyVisited.add(this);
  1284. const values = this._getMaxTarget().values();
  1285. const target = resolveTarget(values.next().value, newAlreadyVisited);
  1286. if (target === CIRCULAR) return CIRCULAR;
  1287. if (target === null) return undefined;
  1288. let result = values.next();
  1289. while (!result.done) {
  1290. const t = resolveTarget(result.value, newAlreadyVisited);
  1291. if (t === CIRCULAR) return CIRCULAR;
  1292. if (t === null) return undefined;
  1293. if (t.module !== target.module) return undefined;
  1294. if (!t.export !== !target.export) return undefined;
  1295. if (
  1296. target.export &&
  1297. !equals(/** @type {ArrayLike<string>} */ (t.export), target.export)
  1298. )
  1299. return undefined;
  1300. result = values.next();
  1301. }
  1302. return target;
  1303. }
  1304. /**
  1305. * Move the target forward as long resolveTargetFilter is fulfilled
  1306. * @param {ModuleGraph} moduleGraph the module graph
  1307. * @param {function({ module: Module, export: string[] | undefined }): boolean} resolveTargetFilter filter function to further resolve target
  1308. * @param {function({ module: Module, export: string[] | undefined }): ModuleGraphConnection=} updateOriginalConnection updates the original connection instead of using the target connection
  1309. * @returns {{ module: Module, export: string[] | undefined } | undefined} the resolved target when moved
  1310. */
  1311. moveTarget(moduleGraph, resolveTargetFilter, updateOriginalConnection) {
  1312. const target = this._getTarget(moduleGraph, resolveTargetFilter, undefined);
  1313. if (target === CIRCULAR) return undefined;
  1314. if (!target) return undefined;
  1315. const originalTarget = this._getMaxTarget().values().next().value;
  1316. if (
  1317. originalTarget.connection === target.connection &&
  1318. originalTarget.export === target.export
  1319. ) {
  1320. return undefined;
  1321. }
  1322. this._target.clear();
  1323. this._target.set(undefined, {
  1324. connection: updateOriginalConnection
  1325. ? updateOriginalConnection(target)
  1326. : target.connection,
  1327. export: target.export,
  1328. priority: 0
  1329. });
  1330. return target;
  1331. }
  1332. createNestedExportsInfo() {
  1333. if (this.exportsInfoOwned) return this.exportsInfo;
  1334. this.exportsInfoOwned = true;
  1335. const oldExportsInfo = this.exportsInfo;
  1336. this.exportsInfo = new ExportsInfo();
  1337. this.exportsInfo.setHasProvideInfo();
  1338. if (oldExportsInfo) {
  1339. this.exportsInfo.setRedirectNamedTo(oldExportsInfo);
  1340. }
  1341. return this.exportsInfo;
  1342. }
  1343. getNestedExportsInfo() {
  1344. return this.exportsInfo;
  1345. }
  1346. hasInfo(baseInfo, runtime) {
  1347. return (
  1348. (this._usedName && this._usedName !== this.name) ||
  1349. this.provided ||
  1350. this.terminalBinding ||
  1351. this.getUsed(runtime) !== baseInfo.getUsed(runtime)
  1352. );
  1353. }
  1354. updateHash(hash, runtime) {
  1355. this._updateHash(hash, runtime, new Set());
  1356. }
  1357. _updateHash(hash, runtime, alreadyVisitedExportsInfo) {
  1358. hash.update(
  1359. `${this._usedName || this.name}${this.getUsed(runtime)}${this.provided}${
  1360. this.terminalBinding
  1361. }`
  1362. );
  1363. if (this.exportsInfo && !alreadyVisitedExportsInfo.has(this.exportsInfo)) {
  1364. this.exportsInfo._updateHash(hash, runtime, alreadyVisitedExportsInfo);
  1365. }
  1366. }
  1367. getUsedInfo() {
  1368. if (this._globalUsed !== undefined) {
  1369. switch (this._globalUsed) {
  1370. case UsageState.Unused:
  1371. return "unused";
  1372. case UsageState.NoInfo:
  1373. return "no usage info";
  1374. case UsageState.Unknown:
  1375. return "maybe used (runtime-defined)";
  1376. case UsageState.Used:
  1377. return "used";
  1378. case UsageState.OnlyPropertiesUsed:
  1379. return "only properties used";
  1380. }
  1381. } else if (this._usedInRuntime !== undefined) {
  1382. /** @type {Map<RuntimeUsageStateType, string[]>} */
  1383. const map = new Map();
  1384. for (const [runtime, used] of this._usedInRuntime) {
  1385. const list = map.get(used);
  1386. if (list !== undefined) list.push(runtime);
  1387. else map.set(used, [runtime]);
  1388. }
  1389. const specificInfo = Array.from(map, ([used, runtimes]) => {
  1390. switch (used) {
  1391. case UsageState.NoInfo:
  1392. return `no usage info in ${runtimes.join(", ")}`;
  1393. case UsageState.Unknown:
  1394. return `maybe used in ${runtimes.join(", ")} (runtime-defined)`;
  1395. case UsageState.Used:
  1396. return `used in ${runtimes.join(", ")}`;
  1397. case UsageState.OnlyPropertiesUsed:
  1398. return `only properties used in ${runtimes.join(", ")}`;
  1399. }
  1400. });
  1401. if (specificInfo.length > 0) {
  1402. return specificInfo.join("; ");
  1403. }
  1404. }
  1405. return this._hasUseInRuntimeInfo ? "unused" : "no usage info";
  1406. }
  1407. getProvidedInfo() {
  1408. switch (this.provided) {
  1409. case undefined:
  1410. return "no provided info";
  1411. case null:
  1412. return "maybe provided (runtime-defined)";
  1413. case true:
  1414. return "provided";
  1415. case false:
  1416. return "not provided";
  1417. }
  1418. }
  1419. getRenameInfo() {
  1420. if (this._usedName !== null && this._usedName !== this.name) {
  1421. return `renamed to ${JSON.stringify(this._usedName).slice(1, -1)}`;
  1422. }
  1423. switch (this.canMangleProvide) {
  1424. case undefined:
  1425. switch (this.canMangleUse) {
  1426. case undefined:
  1427. return "missing provision and use info prevents renaming";
  1428. case false:
  1429. return "usage prevents renaming (no provision info)";
  1430. case true:
  1431. return "missing provision info prevents renaming";
  1432. }
  1433. break;
  1434. case true:
  1435. switch (this.canMangleUse) {
  1436. case undefined:
  1437. return "missing usage info prevents renaming";
  1438. case false:
  1439. return "usage prevents renaming";
  1440. case true:
  1441. return "could be renamed";
  1442. }
  1443. break;
  1444. case false:
  1445. switch (this.canMangleUse) {
  1446. case undefined:
  1447. return "provision prevents renaming (no use info)";
  1448. case false:
  1449. return "usage and provision prevents renaming";
  1450. case true:
  1451. return "provision prevents renaming";
  1452. }
  1453. break;
  1454. }
  1455. throw new Error(
  1456. `Unexpected flags for getRenameInfo ${this.canMangleProvide} ${this.canMangleUse}`
  1457. );
  1458. }
  1459. }
  1460. module.exports = ExportsInfo;
  1461. module.exports.ExportInfo = ExportInfo;
  1462. module.exports.UsageState = UsageState;