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'] = `