Skip to content

Commit 60d9da3

Browse files
authored
fix: ensure duplicate components are written to unique files (#23)
1 parent 3b61231 commit 60d9da3

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

src/index.ts

+36-5
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,43 @@ export default function plugin(
105105
},
106106
});
107107
} else {
108-
void content.map((component) =>
109-
createData(
110-
`${component.displayName}.json`,
111-
JSON.stringify(component.props),
112-
),
108+
const toProcess = content.reduce<
109+
[
110+
Record<string, string[]>,
111+
{ fileName: string; component: ComponentDoc }[],
112+
]
113+
>(
114+
([processed, components], component) => {
115+
const componentName = component.displayName;
116+
let fileName = componentName;
117+
const alreadyProcessed = processed[componentName];
118+
if (alreadyProcessed && alreadyProcessed.length > 0) {
119+
console.warn(
120+
`Duplicate component '${componentName}' found (existing:
121+
${alreadyProcessed[alreadyProcessed.length - 1]})`,
122+
);
123+
124+
fileName += `${alreadyProcessed.length}`;
125+
console.warn(
126+
`'${component.filePath}' will be written to '${fileName}.json'`,
127+
);
128+
}
129+
return [
130+
{
131+
...processed,
132+
[componentName]: [
133+
...(alreadyProcessed || []),
134+
component.filePath,
135+
],
136+
},
137+
[...components, { fileName, component }],
138+
];
139+
},
140+
[{}, []],
113141
);
142+
toProcess[1].forEach(({ fileName, component }) => {
143+
void createData(`${fileName}.json`, JSON.stringify(component.props));
144+
});
114145
}
115146
},
116147
};

0 commit comments

Comments
 (0)