module-mapper.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ModuleMapper = void 0;
  4. const uid_1 = require("./uid");
  5. const nanoid = (0, uid_1.getUid)("1234567890abcdef", 4);
  6. const UNIQUE_PREFIX = nanoid();
  7. let COUNTER = 0;
  8. const uniqueId = () => `${UNIQUE_PREFIX}-${COUNTER++}`;
  9. class ModuleMapper {
  10. constructor(projectRoot) {
  11. this.projectRoot = projectRoot;
  12. this.nodeParts = {};
  13. this.nodeMetas = {};
  14. }
  15. trimProjectRootId(moduleId) {
  16. if (typeof this.projectRoot === 'string' && moduleId.startsWith(this.projectRoot)) {
  17. return moduleId.slice(this.projectRoot.length);
  18. }
  19. return moduleId.replace(this.projectRoot, "");
  20. }
  21. getModuleUid(moduleId) {
  22. if (!(moduleId in this.nodeMetas)) {
  23. this.nodeMetas[moduleId] = {
  24. uid: uniqueId(),
  25. meta: {
  26. id: this.trimProjectRootId(moduleId),
  27. moduleParts: {},
  28. imported: new Set(),
  29. importedBy: new Set(),
  30. },
  31. };
  32. }
  33. return this.nodeMetas[moduleId].uid;
  34. }
  35. getBundleModuleUid(bundleId, moduleId) {
  36. if (!(moduleId in this.nodeMetas)) {
  37. this.nodeMetas[moduleId] = {
  38. uid: uniqueId(),
  39. meta: {
  40. id: this.trimProjectRootId(moduleId),
  41. moduleParts: {},
  42. imported: new Set(),
  43. importedBy: new Set(),
  44. },
  45. };
  46. }
  47. if (!(bundleId in this.nodeMetas[moduleId].meta.moduleParts)) {
  48. this.nodeMetas[moduleId].meta.moduleParts[bundleId] = uniqueId();
  49. }
  50. return this.nodeMetas[moduleId].meta.moduleParts[bundleId];
  51. }
  52. setNodePart(bundleId, moduleId, value) {
  53. const uid = this.getBundleModuleUid(bundleId, moduleId);
  54. if (uid in this.nodeParts) {
  55. throw new Error(`Override module: bundle id ${bundleId}, module id ${moduleId}, value ${JSON.stringify(value)}, existing value: ${JSON.stringify(this.nodeParts[uid])}`);
  56. }
  57. this.nodeParts[uid] = { ...value, metaUid: this.getModuleUid(moduleId) };
  58. return uid;
  59. }
  60. setNodeMeta(moduleId, value) {
  61. this.getModuleUid(moduleId);
  62. this.nodeMetas[moduleId].meta.isEntry = value.isEntry;
  63. this.nodeMetas[moduleId].meta.isExternal = value.isExternal;
  64. }
  65. hasNodePart(bundleId, moduleId) {
  66. if (!(moduleId in this.nodeMetas)) {
  67. return false;
  68. }
  69. if (!(bundleId in this.nodeMetas[moduleId].meta.moduleParts)) {
  70. return false;
  71. }
  72. if (!(this.nodeMetas[moduleId].meta.moduleParts[bundleId] in this.nodeParts)) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. getNodeParts() {
  78. return this.nodeParts;
  79. }
  80. getNodeMetas() {
  81. const nodeMetas = {};
  82. for (const { uid, meta } of Object.values(this.nodeMetas)) {
  83. nodeMetas[uid] = {
  84. ...meta,
  85. imported: [...meta.imported].map((rawImport) => {
  86. const [uid, dynamic] = rawImport.split(",");
  87. const importData = { uid };
  88. if (dynamic === "true") {
  89. importData.dynamic = true;
  90. }
  91. return importData;
  92. }),
  93. importedBy: [...meta.importedBy].map((rawImport) => {
  94. const [uid, dynamic] = rawImport.split(",");
  95. const importData = { uid };
  96. if (dynamic === "true") {
  97. importData.dynamic = true;
  98. }
  99. return importData;
  100. }),
  101. };
  102. }
  103. return nodeMetas;
  104. }
  105. addImportedByLink(targetId, sourceId) {
  106. const sourceUid = this.getModuleUid(sourceId);
  107. this.getModuleUid(targetId);
  108. this.nodeMetas[targetId].meta.importedBy.add(sourceUid);
  109. }
  110. addImportedLink(sourceId, targetId, dynamic = false) {
  111. const targetUid = this.getModuleUid(targetId);
  112. this.getModuleUid(sourceId);
  113. this.nodeMetas[sourceId].meta.imported.add(String([targetUid, dynamic]));
  114. }
  115. }
  116. exports.ModuleMapper = ModuleMapper;