diff --git a/src/html.ts b/src/html.ts index aa47728..f39a56c 100644 --- a/src/html.ts +++ b/src/html.ts @@ -86,6 +86,10 @@ function makeMergedBundle(exploreResults: ExploreBundleResult[]): ExploreBundleR type TreeNodesMap = { [source: string]: string[] }; +function getNodePath(parts: string[], depthIndex: number): string { + return parts.slice(0, depthIndex + 1).join('/'); +} + function getTreeNodesMap(fileDataMap: FileDataMap): TreeNodesMap { let partsSourceTuples = Object.keys(fileDataMap).map<[string[], string]>(file => [ file.split('/'), @@ -95,15 +99,22 @@ function getTreeNodesMap(fileDataMap: FileDataMap): TreeNodesMap { const maxDepth = Math.max(...partsSourceTuples.map(([parts]) => parts.length)); for (let depthIndex = 0; depthIndex < maxDepth; depthIndex += 1) { - partsSourceTuples = partsSourceTuples.map(([parts, file]) => { - const rootPart = parts[depthIndex]; - - if (rootPart) { - const sameRootParts = partsSourceTuples.filter( - ([pathParts]) => pathParts[depthIndex] === rootPart - ); - - if (sameRootParts.length === 1) { + partsSourceTuples = partsSourceTuples.map(([parts, file], currentNodeIndex) => { + if (parts[depthIndex]) { + const nodePath = getNodePath(parts, depthIndex); + + const hasSameRootPaths = partsSourceTuples.some(([pathParts], index) => { + if (index === currentNodeIndex) { + return false; + } + if (!pathParts[depthIndex]) { + return false; + } + + return getNodePath(pathParts, depthIndex) === nodePath; + }); + + if (!hasSameRootPaths) { // Collapse non-contributing path parts return [[...parts.slice(0, depthIndex), parts.slice(depthIndex).join('/')], file]; } diff --git a/tests/__snapshots__/cli.test.ts.snap b/tests/__snapshots__/cli.test.ts.snap index 33dba7c..f18715e 100644 --- a/tests/__snapshots__/cli.test.ts.snap +++ b/tests/__snapshots__/cli.test.ts.snap @@ -68,7 +68,7 @@ exports['CLI should multiple bundles output result as html 1'] = ` diff --git a/tests/__snapshots__/html.test.ts.snap b/tests/__snapshots__/html.test.ts.snap index 3af3178..b0058eb 100644 --- a/tests/__snapshots__/html.test.ts.snap +++ b/tests/__snapshots__/html.test.ts.snap @@ -118,7 +118,7 @@ exports['html getWebTreeMapData should collapse non-contributing nodes 1'] = { ] }, { - "name": "j/k.js \u2022 6 B \u2022 21.4%", + "name": "j/i.js \u2022 6 B \u2022 21.4%", "data": { "$area": 6 } diff --git a/tests/html.test.ts b/tests/html.test.ts index 0fb7827..740f956 100644 --- a/tests/html.test.ts +++ b/tests/html.test.ts @@ -12,7 +12,7 @@ describe('html', () => { 'd/f.js': { size: 3 }, 'd/g/h.js': { size: 4 }, 'd/g/i.js': { size: 5 }, - 'd/j/k.js': { size: 6 }, + 'd/j/i.js': { size: 6 }, z: { size: 7 }, };