ExportsInfo.js 43 KB

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