-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathdata.ts
More file actions
153 lines (129 loc) Β· 3.65 KB
/
Copy pathdata.ts
File metadata and controls
153 lines (129 loc) Β· 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import type { GetModuleInfo } from "./rollup-types.js";
import { isModuleTree, ModuleLengths, ModuleTree, ModuleTreeLeaf } from "../shared/types.js";
import { ModuleMapper } from "./module-mapper.js";
interface MappedNode {
uid: string;
}
const addToPath = (
moduleId: string,
tree: ModuleTree,
modulePath: string[],
node: MappedNode,
): void => {
if (modulePath.length === 0) {
throw new Error(`Error adding node to path ${moduleId}`);
}
const [head, ...rest] = modulePath;
if (rest.length === 0) {
tree.children.push({ ...node, name: head });
return;
} else {
let newTree = tree.children.find(
(folder): folder is ModuleTree => folder.name === head && isModuleTree(folder),
);
if (!newTree) {
newTree = { name: head, children: [] };
tree.children.push(newTree);
}
addToPath(moduleId, newTree, rest, node);
return;
}
};
// TODO try to make it without recursion, but still typesafe
const mergeSingleChildTrees = (tree: ModuleTree): ModuleTree | ModuleTreeLeaf => {
if (tree.children.length === 1) {
const child = tree.children[0];
const name = `${tree.name}/${child.name}`;
if (isModuleTree(child)) {
tree.name = name;
tree.children = child.children;
return mergeSingleChildTrees(tree);
} else {
return {
name,
uid: child.uid,
};
}
} else {
tree.children = tree.children.map((node) => {
if (isModuleTree(node)) {
return mergeSingleChildTrees(node);
} else {
return node;
}
});
return tree;
}
};
export const buildTree = (
bundleId: string,
modules: Array<ModuleLengths & { id: string }>,
mapper: ModuleMapper,
): ModuleTree => {
const tree: ModuleTree = {
name: bundleId,
children: [],
};
for (const { id, renderedLength, gzipLength, brotliLength } of modules) {
const bundleModuleUid = mapper.setNodePart(bundleId, id, {
renderedLength,
gzipLength,
brotliLength,
});
const trimmedModuleId = mapper.trimProjectRootId(id);
const pathParts = trimmedModuleId.split(/\\|\//).filter((p) => p !== "");
addToPath(trimmedModuleId, tree, pathParts, { uid: bundleModuleUid });
}
tree.children = tree.children.map((node) => {
if (isModuleTree(node)) {
return mergeSingleChildTrees(node);
} else {
return node;
}
});
return tree;
};
export const mergeTrees = (trees: Array<ModuleTree | ModuleTreeLeaf>): ModuleTree => {
const newTree = {
name: "root",
children: trees,
isRoot: true,
};
return newTree;
};
export const addLinks = (
startModuleId: string,
getModuleInfo: GetModuleInfo,
mapper: ModuleMapper,
): void => {
const processedNodes: Record<string, boolean> = {};
const moduleIds = [startModuleId];
while (moduleIds.length > 0) {
const moduleId = moduleIds.shift() as string;
if (processedNodes[moduleId]) {
continue;
} else {
processedNodes[moduleId] = true;
}
const moduleInfo = getModuleInfo(moduleId);
if (!moduleInfo) {
return;
}
if (moduleInfo.isEntry) {
mapper.setNodeMeta(moduleId, { isEntry: true });
}
if (moduleInfo.isExternal) {
mapper.setNodeMeta(moduleId, { isExternal: true });
}
for (const importedId of moduleInfo.importedIds) {
mapper.addImportedByLink(importedId, moduleId);
mapper.addImportedLink(moduleId, importedId);
moduleIds.push(importedId);
}
for (const importedId of moduleInfo.dynamicallyImportedIds || []) {
mapper.addImportedByLink(importedId, moduleId);
mapper.addImportedLink(moduleId, importedId, true);
moduleIds.push(importedId);
}
}
};