ExportsInfo.js 49 KB

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