docusaurus-plugin-react-docgen-typescript
Version:
A small plugin that integrates react-docgen-typescript with docusaurus 2.x
107 lines • 3.06 kB
JavaScript
// src/index.ts
import path from "node:path";
import {
withCustomConfig,
withCompilerOptions,
withDefaultConfig
} from "react-docgen-typescript";
import { glob } from "glob";
function getParser(config, options, parserOptions) {
if (config) {
return withCustomConfig(config, parserOptions ?? {}).parse;
} else if (options) {
return withCompilerOptions(options, parserOptions).parse;
}
return withDefaultConfig(parserOptions).parse;
}
function plugin(context, {
src,
ignore,
global = false,
route,
tsConfig,
compilerOptions,
parserOptions
}) {
return {
name: "docusaurus-plugin-react-docgen-typescript",
async loadContent() {
return getParser(
tsConfig,
compilerOptions,
parserOptions
)(
await glob(src, {
absolute: true,
ignore
})
);
},
configureWebpack(config) {
return {
resolve: {
alias: {
"@docgen": path.join(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
config.resolve.alias["@generated"],
"docusaurus-plugin-react-docgen-typescript",
"default"
)
}
}
};
},
async contentLoaded({ content, actions }) {
const { createData, setGlobalData, addRoute } = actions;
if (global) {
console.warn(
"Using global data can potentially slow down your entire app. Use with care \u2764\uFE0F"
);
setGlobalData(content);
} else if (route) {
addRoute({
...route,
modules: {
docgen: await createData("docgen.json", JSON.stringify(content))
}
});
} else {
const toProcess = content.reduce(
([processed, components], component) => {
const componentName = component.displayName;
let fileName = componentName;
const alreadyProcessed = processed[componentName];
if (alreadyProcessed && alreadyProcessed.length > 0) {
console.warn(
`Duplicate component '${componentName}' found (existing:
${alreadyProcessed[alreadyProcessed.length - 1]})`
);
fileName += `${alreadyProcessed.length}`;
console.warn(
`'${component.filePath}' will be written to '${fileName}.json'`
);
}
return [
{
...processed,
[componentName]: [
...alreadyProcessed || [],
component.filePath
]
},
[...components, { fileName, component }]
];
},
[{}, []]
);
toProcess[1].forEach(({ fileName, component }) => {
void createData(`${fileName}.json`, JSON.stringify(component.props));
});
}
}
};
}
export {
plugin as default
};
//# sourceMappingURL=index.mjs.map