Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure duplicate components are written to unique files #20

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 54 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import path from 'path';
import { promisify } from 'util';
import Glob from 'glob';
import path from "path";
import { promisify } from "util";
import Glob from "glob";

import {
withCustomConfig,
import type {
ParserOptions,
ComponentDoc,
FileParser,
} from "react-docgen-typescript";
import {
withCustomConfig,
withCompilerOptions,
withDefaultConfig,
} from 'react-docgen-typescript';
} from "react-docgen-typescript";

import { Plugin, DocusaurusContext, RouteConfig } from '@docusaurus/types';
import { CompilerOptions } from 'typescript';
import type { Plugin, DocusaurusContext, RouteConfig } from "@docusaurus/types";
import type { CompilerOptions } from "typescript";

const glob = promisify(Glob);

type Route = Pick<RouteConfig, 'exact' | 'component' | 'path' | 'priority'>;
type Route = Pick<RouteConfig, "exact" | "component" | "path" | "priority">;

type Union =
| {
Expand All @@ -37,10 +39,10 @@ export type Options = Union & {
};

const getParser = (
config?: Options['tsConfig'],
options?: Options['compilerOptions'],
parserOptions?: Options['parserOptions']
): FileParser['parse'] => {
config?: Options["tsConfig"],
options?: Options["compilerOptions"],
parserOptions?: Options["parserOptions"]
): FileParser["parse"] => {
if (config) {
return withCustomConfig(config, parserOptions).parse;
} else if (options) {
Expand All @@ -52,17 +54,24 @@ const getParser = (

export default function plugin(
context: DocusaurusContext,
{ src, global = false, route, tsConfig, compilerOptions, parserOptions }: Options
{
src,
global = false,
route,
tsConfig,
compilerOptions,
parserOptions,
}: Options
): Plugin<ComponentDoc[]> {
return {
name: 'docusaurus-plugin-react-docgen-typescript',
name: "docusaurus-plugin-react-docgen-typescript",
async loadContent() {
return getParser(
tsConfig,
compilerOptions,
parserOptions
)(
await glob(Array.isArray(src) ? src.join(',') : src, {
await glob(Array.isArray(src) ? src.join(",") : src, {
absolute: true,
})
);
Expand All @@ -71,10 +80,10 @@ export default function plugin(
return {
resolve: {
alias: {
'@docgen': path.join(
config.resolve.alias['@generated'],
'docusaurus-plugin-react-docgen-typescript',
'default'
"@docgen": path.join(
config.resolve.alias["@generated"],
"docusaurus-plugin-react-docgen-typescript",
"default"
),
},
},
Expand All @@ -85,21 +94,41 @@ export default function plugin(

if (global) {
console.warn(
'Using global data can potentially slow down your entire app. Use with care ❤️'
"Using global data can potentially slow down your entire app. Use with care ❤️"
);

setGlobalData(content);
} else if (route) {
addRoute({
...route,
modules: {
docgen: await createData('docgen.json', JSON.stringify(content)),
docgen: await createData("docgen.json", JSON.stringify(content)),
},
});
} else {
content.map(component =>
createData(`${component.displayName}.json`, JSON.stringify(component.props))
);
const processed = {};
content.map((component) => {
const componentName = component.displayName;
let fileName = componentName;
if (componentName in processed) {
console.warn(
`Duplicate component '${componentName}' found (existing:
${
processed[componentName][processed[componentName].length - 1]
})`
);

fileName += `${processed[componentName].length}`;
console.warn(
`'${component.filePath}' will be written to '${fileName}.json'`
);
}
createData(`${fileName}.json`, JSON.stringify(component.props));
if (!(componentName in processed)) {
processed[componentName] = [];
}
processed[componentName].push(component.filePath);
});
}
},
};
Expand Down