processExportInfo.js 1.7 KB

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