The core server-side function for processing TypeScript types. This function handles the heavy lifting of type extraction, formatting, and HAST generation, and is used by loadPrecomputedTypesMeta (webpack loader).
loadServerTypesMeta is the core type processing function that:
This function is separated from the webpack loader to allow reuse in other contexts (e.g., API routes, scripts, or custom build tools).
import { loadServerTypesMeta } from '@mui/internal-docs-infra/pipeline/loadServerTypesMeta';
import { parseCreateFactoryCall } from '@mui/internal-docs-infra/pipeline/loadPrecomputedCodeHighlighter/parseCreateFactoryCall';
// Parse a types.ts file to get the factory call info
const source = await fs.readFile('path/to/types.ts', 'utf-8');
const typesMetaCall = await parseCreateFactoryCall(source, 'path/to/types.ts', {
allowExternalVariants: true,
});
// Process the types
const result = await loadServerTypesMeta({
resourcePath: '/absolute/path/to/types.ts',
resourceName: 'Button',
rootContext: '/absolute/path/to/project',
relativePath: 'app/components/button/types.ts',
typesMetaCall,
formattingOptions: {
shortTypeUnionPrintWidth: 40,
defaultValueUnionPrintWidth: 40,
},
});
// Use the results
console.log(result.allTypes); // TypesMeta[]
console.log(result.allDependencies); // string[] - files to watch
console.log(result.highlightedVariantData); // Ready for precompute injection
const result = await loadServerTypesMeta({
resourcePath: '/absolute/path/to/types.ts',
resourceName: 'Button',
rootContext: '/absolute/path/to/project',
relativePath: 'app/components/button/types.ts',
typesMetaCall,
socketDir: '/tmp/my-custom-socket-dir', // For Windows compatibility
performanceLogging: true,
});
interface LoadServerTypesMetaOptions {
/** Absolute path to the resource being processed */
resourcePath: string;
/** Name of the resource (for display purposes) */
resourceName: string;
/** Root context directory (workspace root) */
rootContext: string;
/** Relative path from rootContext to resourcePath */
relativePath: string;
/** Parsed createTypesMeta call information */
typesMetaCall: ParsedCreateFactory;
/** Options for formatting types in tables */
formattingOptions?: FormatInlineTypeOptions;
/**
* Directory path for socket and lock files used for IPC between workers.
* Useful for Windows where the default temp directory may not support Unix domain sockets.
*/
socketDir?: string;
/** Enable performance logging */
performanceLogging?: boolean;
}
interface LoadServerTypesMetaResult {
/** Highlighted variant data ready for precompute injection */
highlightedVariantData: Record<
string,
{ types: TypesMeta[]; typeNameMap?: Record<string, string> }
>;
/** All dependencies that should be watched for changes */
allDependencies: string[];
/** All processed types for external use */
allTypes: TypesMeta[];
/** Type name map from variant processing */
typeNameMap?: Record<string, string>;
}
The function returns an array of TypesMeta objects, which can be one of three types:
interface ComponentTypeMeta {
type: 'component';
name: string;
data: {
description?: HastRoot;
props: Record<string, FormattedProperty>;
dataAttributes: Record<string, FormattedProperty>;
cssVariables: Record<string, FormattedProperty>;
};
}
interface HookTypeMeta {
type: 'hook';
name: string;
data: {
description?: HastRoot;
parameters: Record<string, FormattedParameter>;
returnValue?: HastRoot | Record<string, FormattedProperty>;
};
}
interface OtherTypeMeta {
type: 'other';
name: string;
data: ExportNode; // Raw export from typescript-api-extractor
reExportOf?: string; // If this is a re-export of component props
}
Loads and caches tsconfig.json from the root context:
const config = await loadTypescriptConfig(path.join(rootContext, 'tsconfig.json'));
Resolves variant paths through tsconfig paths, handling:
@base-ui/react/*)Automatically discovers DataAttributes.ts and CssVars.ts files:
const metaFiles = await findMetaFiles(componentDirectory);
// Returns: ['CheckboxDataAttributes.ts', 'CheckboxCssVars.ts']
Sends type extraction work to a dedicated worker thread:
const workerResult = await workerManager.processTypes({
projectPath: config.projectPath,
compilerOptions: config.options,
allEntrypoints,
globalTypes,
resolvedVariantMap,
});
Converts raw TypeScript exports to formatted HAST structures:
const formattedData = await formatComponentData(exportNode, allTypes, namespaces, typeNameMap, {
formatting: formattingOptions,
});
Creates a .md file alongside the types file:
const markdown = await generateTypesMarkdown(resourceName, allTypes, typeNameMap);
await writeFile(markdownFilePath, markdown);
Applies syntax highlighting to all HAST nodes:
const highlightedVariantData = await highlightTypes(variantData);
The function uses a worker-based architecture for efficient TypeScript processing:
typescript-api-extractorFor multi-process environments (Next.js with multiple workers):
~500-1200ms to create TypeScript program and parse types
~100-140ms using cached program and file data
87-92% performance improvement vs. creating separate language services per process
loadPrecomputedTypesMeta - Webpack loader that uses this functionabstractCreateTypes - Create type documentation factoriestypescript-api-extractor - Underlying type extraction library