ExportsInfo.js 44 KB

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