docusaurus-plugin-react-docgen-typescript
Version:
A small plugin that integrates react-docgen-typescript with docusaurus 2.x
1 lines • 6.13 kB
Source Map (JSON)
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/unbound-method */\nimport path from \"node:path\";\n\nimport type {\n ParserOptions,\n ComponentDoc,\n FileParser,\n} from \"react-docgen-typescript\";\nimport {\n withCustomConfig,\n withCompilerOptions,\n withDefaultConfig,\n} from \"react-docgen-typescript\";\n\nimport type { Plugin, DocusaurusContext, RouteConfig } from \"@docusaurus/types\";\nimport { glob } from \"glob\";\nimport type { CompilerOptions } from \"typescript\";\n\ntype Route = Pick<RouteConfig, \"exact\" | \"component\" | \"path\" | \"priority\">;\n\ntype Union =\n | {\n global?: undefined | false;\n route: Route;\n }\n | {\n global: boolean;\n route?: Route;\n };\n\nexport type Options = Union & {\n src: string | string[];\n ignore?: string[];\n tsConfig?: string;\n compilerOptions?: CompilerOptions;\n parserOptions?: ParserOptions;\n globOptions: null;\n};\n\nfunction getParser(\n config?: Options[\"tsConfig\"],\n options?: Options[\"compilerOptions\"],\n parserOptions?: Options[\"parserOptions\"],\n): FileParser[\"parse\"] {\n if (config) {\n return withCustomConfig(config, parserOptions ?? {}).parse;\n } else if (options) {\n return withCompilerOptions(options, parserOptions).parse;\n }\n\n return withDefaultConfig(parserOptions).parse;\n}\n\nexport default function plugin(\n context: DocusaurusContext,\n {\n src,\n ignore,\n global = false,\n route,\n tsConfig,\n compilerOptions,\n parserOptions,\n }: Options,\n): Plugin<ComponentDoc[]> {\n return {\n name: \"docusaurus-plugin-react-docgen-typescript\",\n async loadContent() {\n return getParser(\n tsConfig,\n compilerOptions,\n parserOptions,\n )(\n await glob(src, {\n absolute: true,\n ignore,\n }),\n );\n },\n configureWebpack(config) {\n return {\n resolve: {\n alias: {\n \"@docgen\": path.join(\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access\n (config.resolve!.alias as any)[\"@generated\"],\n \"docusaurus-plugin-react-docgen-typescript\",\n \"default\",\n ),\n },\n },\n };\n },\n async contentLoaded({ content, actions }): Promise<void> {\n const { createData, setGlobalData, addRoute } = actions;\n\n if (global) {\n console.warn(\n \"Using global data can potentially slow down your entire app. Use with care ❤️\",\n );\n\n setGlobalData(content);\n } else if (route) {\n addRoute({\n ...route,\n modules: {\n docgen: await createData(\"docgen.json\", JSON.stringify(content)),\n },\n });\n } else {\n const toProcess = content.reduce<\n [\n Record<string, string[]>,\n { fileName: string; component: ComponentDoc }[],\n ]\n >(\n ([processed, components], component) => {\n const componentName = component.displayName;\n let fileName = componentName;\n const alreadyProcessed = processed[componentName];\n if (alreadyProcessed && alreadyProcessed.length > 0) {\n console.warn(\n `Duplicate component '${componentName}' found (existing:\n ${alreadyProcessed[alreadyProcessed.length - 1]})`,\n );\n\n fileName += `${alreadyProcessed.length}`;\n console.warn(\n `'${component.filePath}' will be written to '${fileName}.json'`,\n );\n }\n return [\n {\n ...processed,\n [componentName]: [\n ...(alreadyProcessed || []),\n component.filePath,\n ],\n },\n [...components, { fileName, component }],\n ];\n },\n [{}, []],\n );\n toProcess[1].forEach(({ fileName, component }) => {\n void createData(`${fileName}.json`, JSON.stringify(component.props));\n });\n }\n },\n };\n}\n"],"mappings":";AACA,OAAO,UAAU;AAOjB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAGP,SAAS,YAAY;AAwBrB,SAAS,UACP,QACA,SACA,eACqB;AACrB,MAAI,QAAQ;AACV,WAAO,iBAAiB,QAAQ,iBAAiB,CAAC,CAAC,EAAE;AAAA,EACvD,WAAW,SAAS;AAClB,WAAO,oBAAoB,SAAS,aAAa,EAAE;AAAA,EACrD;AAEA,SAAO,kBAAkB,aAAa,EAAE;AAC1C;AAEe,SAAR,OACL,SACA;AAAA,EACE;AAAA,EACA;AAAA,EACA,SAAS;AAAA,EACT;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GACwB;AACxB,SAAO;AAAA,IAC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