ExportsInfo.js 47 KB

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