processExportInfo.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { UsageState } = require("../ExportsInfo");
  7. /** @typedef {import("../Dependency").RawReferencedExports} RawReferencedExports */
  8. /** @typedef {import("../ExportsInfo").ExportInfo} ExportInfo */
  9. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  10. /**
  11. * Process export info.
  12. * @param {RuntimeSpec} runtime the runtime
  13. * @param {RawReferencedExports} referencedExports list of referenced exports, will be added to
  14. * @param {string[]} prefix export prefix
  15. * @param {ExportInfo=} exportInfo the export info
  16. * @param {boolean} defaultPointsToSelf when true, using default will reference itself
  17. * @param {Set<ExportInfo>} alreadyVisited already visited export info (to handle circular reexports)
  18. */
  19. const processExportInfo = (
  20. runtime,
  21. referencedExports,
  22. prefix,
  23. exportInfo,
  24. defaultPointsToSelf = false,
  25. alreadyVisited = new Set()
  26. ) => {
  27. if (!exportInfo) {
  28. referencedExports.push(prefix);
  29. return;
  30. }
  31. const used = exportInfo.getUsed(runtime);
  32. if (used === UsageState.Unused) return;
  33. if (alreadyVisited.has(exportInfo)) {
  34. referencedExports.push(prefix);
  35. return;
  36. }
  37. alreadyVisited.add(exportInfo);
  38. if (
  39. used !== UsageState.OnlyPropertiesUsed ||
  40. !exportInfo.exportsInfo ||
  41. exportInfo.exportsInfo.otherExportsInfo.getUsed(runtime) !==
  42. UsageState.Unused
  43. ) {
  44. alreadyVisited.delete(exportInfo);
  45. referencedExports.push(prefix);
  46. return;
  47. }
  48. const exportsInfo = exportInfo.exportsInfo;
  49. for (const exportInfo of exportsInfo.orderedExports) {
  50. processExportInfo(
  51. runtime,
  52. referencedExports,
  53. defaultPointsToSelf && exportInfo.name === "default"
  54. ? prefix
  55. : [...prefix, exportInfo.name],
  56. exportInfo,
  57. false,
  58. alreadyVisited
  59. );
  60. }
  61. alreadyVisited.delete(exportInfo);
  62. };
  63. module.exports = processExportInfo;