ExportsInfo.js 46 KB

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