|
1 | | -import ContentMissing from '@/components/ContentMissing'; |
2 | | - |
3 | 1 | # Architecture |
4 | 2 |
|
5 | | -<ContentMissing /> |
| 3 | +react-docgen works in four steps: |
| 4 | + |
| 5 | +1. Parse the source code with Babel. |
| 6 | +2. Build a [`FileState`](../reference/file-state) object around the AST. |
| 7 | +3. Run a resolver to find React component definitions. |
| 8 | +4. Run handlers on each component definition and build the final documentation. |
| 9 | + |
| 10 | +## Parsing |
| 11 | + |
| 12 | +`parse()` accepts source code and a config object. The source is parsed with |
| 13 | +Babel. If no Babel config file is found, react-docgen applies default parser |
| 14 | +plugins for JSX and either TypeScript or Flow. |
| 15 | + |
| 16 | +TypeScript syntax is enabled by default for filenames ending in `.ts`, `.tsx`, |
| 17 | +`.mts`, or `.cts`. Other files use Flow syntax by default. |
| 18 | + |
| 19 | +## FileState |
| 20 | + |
| 21 | +`FileState` stores the AST, root program path, source code, parser options, and |
| 22 | +the configured importer. It also provides helpers used by resolvers, handlers, |
| 23 | +and utilities: |
| 24 | + |
| 25 | +- `file.traverse()` to walk the AST |
| 26 | +- `file.import()` to resolve imported values |
| 27 | +- `file.parse()` to parse another file with the same importer |
| 28 | + |
| 29 | +## Resolvers |
| 30 | + |
| 31 | +The resolver receives the `FileState` and returns component definition paths. |
| 32 | + |
| 33 | +The default parser config uses a chain of built-in resolvers to find exported |
| 34 | +components and annotated components. Custom resolvers can replace that behavior. |
| 35 | + |
| 36 | +## Handlers |
| 37 | + |
| 38 | +Handlers receive a `DocumentationBuilder` and one component definition. Each |
| 39 | +handler adds one kind of information, such as prop types, default props, display |
| 40 | +name, or component methods. |
| 41 | + |
| 42 | +When all handlers have run, the builder produces a `Documentation` object. |
| 43 | + |
| 44 | +## Importers |
| 45 | + |
| 46 | +Importers are used when react-docgen needs to follow imports. The default |
| 47 | +filesystem importer resolves imported source files from disk. The ignore |
| 48 | +importer disables cross-file resolution. |
| 49 | + |
| 50 | +## Output |
| 51 | + |
| 52 | +The JavaScript API returns an array of `Documentation` objects. The CLI returns |
| 53 | +a JSON object keyed by file path, where each value is an array of |
| 54 | +`Documentation` objects for that file. |
0 commit comments