ExportsInfo.js 47 KB

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