MUI Docs Infra

Warning

This is an internal project, and is not intended for public use. No support or stability guarantees are provided.

Load Server Code Source

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.

Usage

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.

What This Wrapper Adds

fetchSource: Node.js fs/promises

URLs 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 resolution

Imports 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.

When to Use

Use loadServerCodeSource when you need to:

  • Implement server-side code loading for CodeHighlighter in React Server Components
  • Build demo processing pipelines that need automatic dependency resolution from disk
  • Analyze import dependencies in source files for documentation generation
  • Load code with its dependencies at request time (e.g., dynamic routes like app/components/[component].tsx)

Tip

For build-time optimization, use the Precompute Loader instead, which calls loadServerCodeSource during webpack compilation and caches the results.

Types

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.

ParameterTypeDescription
url
string
Return Type
Promise<{ source: string; extraFiles?: VariantExtraFiles; extraDependencies?: string[]; externals?: Externals; comments?: SourceComments }>