A library to extract information from React components for documentation generation.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
System for resolving module imports and dependencies during component analysis, enabling cross-file component discovery.
The base interface for all module import resolvers.
type Importer = (
path: ImportPath,
name: string,
file: FileState
) => NodePath | null;
type ImportPath = NodePath<
ExportAllDeclaration | ExportNamedDeclaration | ImportDeclaration
>;Parameters:
path: AST path of the import/export statementname: The identifier being imported/exportedfile: Current file state with parsing contextReturns:
NodePath: Resolved AST node if foundnull: If import cannot be resolvedResolves imports using Node.js filesystem resolution algorithm.
const fsImporter: Importer;Resolution Algorithm:
require.resolve() logicUsage Example:
import { parse, fsImporter } from "react-docgen";
// Component with imports
const componentCode = `
import { Button } from './Button';
import { theme } from '../theme';
export default function App() {
return <Button theme={theme} />;
}
`;
const docs = parse(componentCode, {
importer: fsImporter,
filename: '/path/to/App.jsx'
});Ignores all import resolution, useful for browser environments or when imports are not available.
const ignoreImporter: Importer;Use Cases:
Usage Example:
import { parse, ignoreImporter } from "react-docgen";
// Parse without resolving imports
const docs = parse(componentCode, {
importer: ignoreImporter
});Creates customized filesystem importers with specific resolution options.
function makeFsImporter(options?: FsImporterOptions): Importer;
interface FsImporterOptions {
/** Custom resolve function */
resolve?: (id: string, basedir: string) => string;
/** File extensions to resolve */
extensions?: string[];
/** Whether to follow symlinks */
preserveSymlinks?: boolean;
}Usage Example:
import { makeFsImporter, parse } from "react-docgen";
const customImporter = makeFsImporter({
extensions: ['.tsx', '.ts', '.jsx', '.js'],
preserveSymlinks: false
});
const docs = parse(componentCode, {
importer: customImporter,
filename: '/path/to/component.tsx'
});React-docgen automatically handles browser field mappings in package.json for browser compatibility.
// Automatic browser field mappings:
const browserMappings = {
"./dist/importer/fsImporter.js": "./dist/importer/ignoreImporter.js",
"./src/importer/fsImporter.ts": "./src/importer/ignoreImporter.ts",
"./dist/importer/makeFsImporter.js": "./dist/importer/makeIgnoreImporter.js",
"./src/importer/makeFsImporter.ts": "./src/importer/makeIgnoreImporter.ts"
};When used in browser environments, filesystem-based importers are automatically replaced with ignore-based importers.
The import resolution process follows these steps:
Supported Import/Export Patterns:
// Named imports
import { Component } from './Component';
import { Component as MyComponent } from './Component';
// Default imports
import Component from './Component';
// Namespace imports
import * as Components from './Components';
// Named exports
export { Component };
export { Component as Button };
// Default exports
export default Component;
// Re-exports
export { Component } from './Component';
export * from './Components';Importers can be customized for specific resolution strategies or environments.
Custom Importer Example:
import type { Importer } from "react-docgen";
const customImporter: Importer = (path, name, file) => {
// Custom resolution logic
const modulePath = path.node.source?.value;
if (modulePath?.startsWith('@components/')) {
// Handle component library imports
return resolveComponentLibrary(modulePath, name);
}
if (modulePath?.startsWith('http://')) {
// Handle remote imports (not recommended for production)
return resolveRemoteModule(modulePath, name);
}
// Fallback to default resolution
return null;
};
// Usage with custom importer
const docs = parse(sourceCode, {
importer: customImporter
});The default importer used when no custom importer is specified.
const defaultImporter: Importer;The default configuration uses fsImporter with standard Node.js resolution for most environments, with automatic browser field mapping support.
Import resolution errors are handled gracefully:
null, allowing parsing to continueError Handling Example:
import { parse } from "react-docgen";
const componentWithMissingImport = `
import { MissingComponent } from './does-not-exist';
export default function App() {
return <MissingComponent />;
}
`;
// Parsing continues even with unresolvable imports
const docs = parse(componentWithMissingImport);
// docs will contain available information, missing imports are ignoredInstall with Tessl CLI
npx tessl i tessl/npm-react-docgen