MCPcopy Create free account
hub / github.com/btd/rollup-plugin-visualizer / ModuleMapper

Class ModuleMapper

plugin/module-mapper.ts:35–161  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

33};
34
35export class ModuleMapper {
36 private nodeParts: Record<ModuleUID, ModulePart> = {};
37 private nodeMetas: Record<string, ModuleIdStorage> = {};
38 private counter: number = 0;
39
40 constructor(private projectRoot: string | RegExp) {}
41
42 trimProjectRootId(moduleId: string): string {
43 if (typeof this.projectRoot === "string" && moduleId.startsWith(this.projectRoot)) {
44 return moduleId.slice(this.projectRoot.length);
45 }
46 return moduleId.replace(this.projectRoot, "");
47 }
48
49 uniqueId(): ModuleUID {
50 return `${HASH_PLACEHOLDER}-${this.counter++}`;
51 }
52
53 getModuleUid(moduleId: string): ModuleUID {
54 if (!(moduleId in this.nodeMetas)) {
55 this.nodeMetas[moduleId] = {
56 uid: this.uniqueId(),
57 meta: {
58 id: this.trimProjectRootId(moduleId),
59 moduleParts: {},
60 imported: new Set(),
61 importedBy: new Set(),
62 },
63 };
64 }
65
66 return this.nodeMetas[moduleId].uid;
67 }
68
69 getBundleModuleUid(bundleId: string, moduleId: string): ModuleUID {
70 if (!(moduleId in this.nodeMetas)) {
71 this.nodeMetas[moduleId] = {
72 uid: this.uniqueId(),
73 meta: {
74 id: this.trimProjectRootId(moduleId),
75 moduleParts: {},
76 imported: new Set(),
77 importedBy: new Set(),
78 },
79 };
80 }
81 if (!(bundleId in this.nodeMetas[moduleId].meta.moduleParts)) {
82 this.nodeMetas[moduleId].meta.moduleParts[bundleId] = this.uniqueId();
83 }
84
85 return this.nodeMetas[moduleId].meta.moduleParts[bundleId];
86 }
87
88 setNodePart(bundleId: string, moduleId: string, value: ModuleLengths): ModuleUID {
89 const uid = this.getBundleModuleUid(bundleId, moduleId);
90 if (uid in this.nodeParts) {
91 throw new Error(
92 `Override module: bundle id ${bundleId}, module id ${moduleId}, value ${JSON.stringify(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected