ExportsInfo.js 47 KB

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