Warning
This is an internal project, and is not intended for public use. No support or stability guarantees are provided.
The loadServerCodeSource utility reads source files from the local filesystem and analyzes their dependencies. It is a thin Node.js wrapper around createLoadIsomorphicCodeSource that wires the platform-independent loader to fs/promises and to a filesystem-backed module resolver.
For everything the loader does — import parsing, storeAt modes, comment processing, return shape, examples — see Load Isomorphic Code Source. This page only covers what loadServerCodeSource adds on top.
import { loadServerCodeSource } from '@mui/internal-docs-infra/pipeline/loadServerCodeSource';
const result = await loadServerCodeSource('file:///app/components/button/Component.tsx');
The default export is pre-configured for the common case. To customize options (storeAt, comment prefixes, etc.), use the factory:
import { createLoadServerCodeSource } from '@mui/internal-docs-infra/pipeline/loadServerCodeSource';
const customLoadSource = createLoadServerCodeSource({
storeAt: 'canonical',
removeCommentsWithPrefix: ['@internal'],
notableCommentsPrefix: ['@highlight'],
});
All options are forwarded verbatim to createLoadIsomorphicCodeSource, with two values pinned to Node.js implementations.
fetchSource: Node.js fs/promisesURLs are read with readFile. file:// URLs (including Windows drive letters) are converted via fileURLToPath; plain absolute paths are accepted directly. No URL scheme other than file:// is supported — for HTTP or virtual sources, use createLoadIsomorphicCodeSource directly.
resolveImports: filesystem-backed module resolutionImports are resolved through resolveImportResultWithFs (from @mui/internal-docs-infra/pipeline/loaderUtils), which walks the real filesystem to mirror Node/TypeScript module resolution: extension probing (.ts, .tsx, .js, .jsx, .css, ...), index.* lookup inside directories, and absolute-URL output suitable for recursive loading.
Use loadServerCodeSource when you need to:
CodeHighlighter in React Server Componentsapp/components/[component].tsx)Tip
For build-time optimization, use the Precompute Loader instead, which calls
loadServerCodeSourceduring webpack compilation and caches the results.
Default loadServerCodeSource function that reads a file and extracts its dependencies. This function is used to load source files for demos, resolving their imports and dependencies. It reads the source file, resolves its imports, and returns the processed source along with any additional files and dependencies that were found.
| Parameter | Type | Description |
|---|---|---|
| url | |
Promise<{ source: string; extraFiles?: VariantExtraFiles; extraDependencies?: string[]; externals?: Externals; comments?: SourceComments }>createLoadIsomorphicCodeSource - The platform-independent factory underneath this wrapperloadServerCodeMeta - For loading demo metadata from index.ts filesloadIsomorphicCodeVariant - For recursively loading code with dependencies