| 165 | }; |
| 166 | |
| 167 | export const buildReport = async ( |
| 168 | selected: SelectedReport, |
| 169 | projectRoot: string | RegExp, |
| 170 | measure: (part: CodeContribution) => Promise<ModuleLengths & { id: string }>, |
| 171 | ): Promise<Pick<VisualizerData, "tree" | "nodeParts" | "nodeMetas">> => { |
| 172 | const mapper = new ModuleMapper(projectRoot); |
| 173 | const roots: (ModuleTree | ModuleTreeLeaf)[] = []; |
| 174 | for (const chunk of selected.chunks) { |
| 175 | if (chunk.kind === "fallback") { |
| 176 | const sizes = await measure({ id: chunk.id, code: chunk.part.code }); |
| 177 | const uid = mapper.setNodePart(chunk.id, chunk.part.id, sizes); |
| 178 | roots.push({ name: chunk.id, uid }); |
| 179 | } else { |
| 180 | const parts = await Promise.all( |
| 181 | chunk.parts.map((part) => |
| 182 | "code" in part ? measure(part) : { ...part, gzipLength: 0, brotliLength: 0 }, |
| 183 | ), |
| 184 | ); |
| 185 | roots.push(buildTree(chunk.id, parts, mapper)); |
| 186 | } |
| 187 | } |
| 188 | for (const module of selected.modules.values()) mapper.setNodeMeta(module.id, module); |
| 189 | for (const link of selected.links) { |
| 190 | mapper.addImportedByLink(link.target, link.source); |
| 191 | mapper.addImportedLink(link.source, link.target, link.dynamic); |
| 192 | } |
| 193 | return { |
| 194 | tree: mergeTrees(roots), |
| 195 | nodeParts: mapper.getNodeParts(), |
| 196 | nodeMetas: mapper.getNodeMetas(), |
| 197 | }; |
| 198 | }; |