ExportsInfo.js 44 KB

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