processExportInfo.js 1.8 KB

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