generated from atomicpages/ts-starter
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.ts
159 lines (143 loc) · 5.13 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import path from 'path';
import globby from 'globby';
import docgen, { ParserOptions, ComponentDoc, FileParser } from 'react-docgen-typescript';
import { DEFAULT_PLUGIN_ID } from '@docusaurus/core/lib/constants';
import { Plugin, LoadContext, RouteConfig } from '@docusaurus/types';
import { docuHash } from '@docusaurus/utils';
import { CompilerOptions } from 'typescript';
import { PropSidebarItem } from '@docusaurus/plugin-content-docs-types';
type Route = Pick<RouteConfig, 'exact' | 'component' | 'path' | 'priority'>;
type Union =
| {
global?: undefined | false;
route: Route;
}
| {
global: boolean;
route?: Route;
};
type Options = Union & {
id: string;
src: string | string[];
tsConfig?: string;
compilerOptions?: CompilerOptions;
parserOptions?: ParserOptions;
createRoutes?: boolean;
baseRoute?: string;
};
const getParser = (
config?: Options['tsConfig'],
options?: Options['compilerOptions'],
parserOptions?: Options['parserOptions']
): FileParser['parse'] => {
if (config) {
return docgen.withCustomConfig(config, parserOptions).parse;
} else if (options) {
return docgen.withCompilerOptions(options, parserOptions).parse;
}
return docgen.withDefaultConfig(parserOptions).parse;
};
export default function plugin(
context: LoadContext,
{
id,
src,
global = false,
route,
tsConfig,
compilerOptions,
parserOptions,
createRoutes,
baseRoute = '/docs/react',
}: Options
): Plugin<ComponentDoc[]> {
const { generatedFilesDir } = context;
const pluginId = id ?? DEFAULT_PLUGIN_ID;
const pluginName = 'docusaurus-plugin-react-docgen-typescript';
const pluginDataDirRoot = path.join(generatedFilesDir, pluginName);
const dataDir = path.join(pluginDataDirRoot, pluginId);
return {
name: pluginName,
async loadContent() {
return getParser(tsConfig, compilerOptions, parserOptions)(await globby(src));
},
configureWebpack() {
return {
resolve: {
alias: {
'@docgen': dataDir,
},
},
};
},
async contentLoaded({ content, actions }): Promise<void> {
const { createData, setGlobalData, addRoute } = actions;
if (global) {
console.warn(
'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)),
},
});
} else {
const sidebarName = `react-docgen-typescript-sidebar-${pluginId}`;
const sidebar: PropSidebarItem[] = content.map(component => ({
type: 'link',
href: `${baseRoute}/${component.displayName}`,
label: component.displayName,
}));
const componentMetadataPath = await createData(
`${docuHash('component-metadata-prop')}.json`,
JSON.stringify(
{
apiSidebars: {
[sidebarName]: sidebar,
},
},
null,
2
)
);
const routes: RouteConfig[] = await Promise.all(
content.map(async component => {
const componentData = await createData(
`${component.displayName}.json`,
JSON.stringify(component)
);
if (createRoutes) {
return {
path: `${baseRoute}/${component.displayName}`,
exact: true,
component: '@theme/ReactComponentItem',
sidebar: sidebarName,
modules: {
componentMetadata: componentMetadataPath,
data: componentData,
},
};
}
})
);
if (createRoutes) {
addRoute({
path: baseRoute,
component: '@theme/ReactComponentPage',
sidebar: sidebarName,
routes,
modules: {
componentMetadata: componentMetadataPath,
},
});
}
}
},
getThemePath(): string {
return path.resolve(__dirname, './theme');
},
};
}