Warning
This is an internal project, and is not intended for public use. No support or stability guarantees are provided.
The createLoadIsomorphicCodeSource factory builds a LoadSource function from two injected primitives — fetchSource and resolveImports — making it the platform-independent core for loading and analyzing JavaScript/TypeScript/CSS source files.
It handles everything that does not depend on the runtime: parsing imports and comments, reshaping externals, applying the chosen storeAt mode, filtering comments, and assembling extraFiles / extraDependencies for recursive loading. The only concerns left to the caller are how to read a URL and how to resolve relative imports against a real (or virtual) tree.
loadServerCodeSource is a thin wrapper that wires this factory to Node.js fs/promises. Use the isomorphic factory directly when you need to load from somewhere other than the local filesystem.
fetch, readFile, IndexedDB cache, etc../foo against a real filesystem, an HTTP tree, or an in-memory mapextraDependencies arrays for recursive loading by loadIsomorphicCodeVariantstoreAt)sourceEnhancersimport { createLoadIsomorphicCodeSource } from '@mui/internal-docs-infra/pipeline/loadIsomorphicCodeSource';
const loadSource = createLoadIsomorphicCodeSource({
fetchSource: async (url) => (await fetch(url)).text(),
resolveImports: async (imports) => {
const resolved = new Map<string, string>();
for (const importUrl of Object.keys(imports)) {
resolved.set(importUrl, new URL(importUrl, baseUrl).toString());
}
return resolved;
},
});
const result = await loadSource('https://example.com/Demo.tsx');
// {
// source: "import { Button } from './Button';\n...",
// extraFiles: { './Button.tsx': 'https://example.com/Button.tsx' },
// extraDependencies: ['https://example.com/Button.tsx'],
// externals: { 'react': [{ name: 'default', type: 'default', isType: false }] }
// }
The returned function satisfies the LoadSource contract and can be plugged directly into CodeProvider or loadIsomorphicCodeVariant.
fetchSourceRequired. Async function that resolves a URL to its raw source text. It is called once per file and is the only I/O performed by the loader itself.
fetchSource: (url: string) => Promise<string>;
In server contexts this typically wraps fs/promises#readFile; in browsers it wraps fetch.
resolveImportsOptional. Given the imports parsed from a JavaScript/TypeScript module, returns a Map keyed by the original import URL with values rewritten to absolute URLs that can be passed back into loadSource recursively.
resolveImports?: (imports: IsomorphicImports) => Promise<Map<string, string>>;
When omitted, JavaScript modules are processed without a resolver: import URLs are kept as-is for extraDependencies and no rewriting happens. CSS files always skip this step because parseImportsAndComments already resolves @import and url() paths.
The IsomorphicImports shape mirrors what parseImportsAndComments produces after name flattening, so resolvers can inspect each import's named bindings, type-only flag, and source positions:
type IsomorphicImports = Record<
string,
{
url: string;
names: string[];
includeTypeDefs?: true;
positions: Array<{ start: number; end: number }>;
}
>;
The storeAt option controls how imports are stored in extraFiles and how import statements are rewritten in the source.
'flat' (default)Flattens all imports to the current directory and rewrites import statements accordingly. This mode uses intelligent conflict resolution to ensure unique filenames.
// Original source:
import { Button } from '../components/Button';
import styles from './styles.module.css';
// After processing with storeAt: 'flat':
import { Button } from './Button';
import styles from './styles.module.css';
// extraFiles:
{
'./Button.tsx': 'https://example.com/components/Button.tsx',
'./styles.module.css': 'https://example.com/demo/styles.module.css'
}
'canonical'Preserves full relative paths including index files when they exist:
// Original source:
import { Button } from '../components/Button';
// After processing with storeAt: 'canonical':
// (source unchanged)
// extraFiles:
{
'../components/Button/index.tsx': 'https://example.com/components/Button/index.tsx'
}
'import'Uses import paths with file extensions but without index resolution:
// Original source:
import { Button } from '../components/Button';
// After processing with storeAt: 'import':
// (source unchanged)
// extraFiles:
{
'../components/Button.tsx': 'https://example.com/components/Button/index.tsx'
}
interface LoadSourceResult {
source: string; // Processed source code (may have rewritten imports)
extraFiles?: Record<string, string>; // Map of import paths to URLs
extraDependencies?: string[]; // Array of URLs for recursive loading
externals?: Externals; // External npm package dependencies
comments?: Record<number, string[]>; // Comments by line number (for sourceEnhancers)
}
// Externals type
type Externals = Record<string, ExternalImportItem[]>;
interface ExternalImportItem {
name: string; // Import name (e.g., 'Button', 'default')
type: 'named' | 'default' | 'namespace'; // Import type
isType?: boolean; // Whether this is a TypeScript type-only import
}
Important
The values in
extraFilesare URLs, not source code. This allowsloadIsomorphicCodeVariantto recursively load each dependency by callingloadSourceagain with the URL. Whatever URL schemefetchSourceandresolveImportsproduce must be one this same loader can later fetch.
The function extracts comments from source files and returns them for use by sourceEnhancers. Two options control comment behavior:
removeCommentsWithPrefixStrips comments starting with specified prefixes from the output source. When notableCommentsPrefix is not specified, all stripped comments are collected in the comments object:
const loadSource = createLoadIsomorphicCodeSource({
fetchSource,
removeCommentsWithPrefix: ['@internal', '@private'],
});
// Source file:
// // @internal This is an implementation detail
// // User-visible comment
// const x = 1;
const result = await loadSource('https://example.com/example.ts');
// result.source: "// User-visible comment\nconst x = 1;"
// result.comments: { 0: ['@internal This is an implementation detail'] }
// Note: "User-visible comment" is NOT collected because it doesn't match removeCommentsWithPrefix
This is useful for:
notableCommentsPrefixWhen specified, only comments matching these prefixes are collected in comments. Comments matching removeCommentsWithPrefix but not notableCommentsPrefix are still stripped but not collected:
const loadSource = createLoadIsomorphicCodeSource({
fetchSource,
removeCommentsWithPrefix: ['@internal', '@highlight'],
notableCommentsPrefix: ['@highlight'],
});
// Source file:
// // @internal Implementation detail
// // @highlight Important line
// const x = 1;
const result = await loadSource('https://example.com/example.ts');
// result.source: "const x = 1;"
// result.comments: { 0: ['@highlight Important line'] }
// Note: @internal is stripped but NOT collected (not in notableCommentsPrefix)
The function follows a straightforward pipeline:
fetchSourceincludeDependencies is falseparseImportsAndComments — extracts relative imports (starting with ./ or ../), external dependencies (npm packages), and commentsresolveImports (JavaScript/TypeScript only)extraFiles and rewrite import statements according to storeAt, via processRelativeImportsremoveCommentsWithPrefix is set, and collect notable comments into the comments fieldextraDependencies so that loadIsomorphicCodeVariant can recurseNote
CSS files are handled differently—they skip step 4 because
parseImportsAndCommentsalready resolves paths for CSS@importandurl()statements.
const loadSource = createLoadIsomorphicCodeSource({
fetchSource: async (url) => (await fetch(url)).text(),
resolveImports: async (imports) => {
const resolved = new Map<string, string>();
for (const importUrl of Object.keys(imports)) {
// Probe the server for the real file (extension/index resolution)
const real = await probe(importUrl);
resolved.set(importUrl, real);
}
return resolved;
},
});
// File: https://example.com/components/button/Demo.tsx
import { Button } from './Button';
import styles from './styles.module.css';
const result = await loadSource('https://example.com/components/button/Demo.tsx');
// {
// source: "import { Button } from './Button';\nimport styles from './styles.module.css';",
// extraFiles: {
// './Button.tsx': 'https://example.com/components/button/Button.tsx',
// './styles.module.css': 'https://example.com/components/button/styles.module.css'
// },
// extraDependencies: [
// 'https://example.com/components/button/Button.tsx',
// 'https://example.com/components/button/styles.module.css'
// ]
// }
// File: https://example.com/components/button/Button.tsx
import React from 'react';
import { Box } from '@mui/material';
const result = await loadSource('https://example.com/components/button/Button.tsx');
// {
// source: "import React from 'react';\nimport { Box } from '@mui/material';",
// externals: {
// 'react': [{ name: 'default', type: 'default', isType: false }],
// '@mui/material': [{ name: 'Box', type: 'named', isType: false }]
// }
// }
const cache = new Map<string, string>();
const loadSource = createLoadIsomorphicCodeSource({
fetchSource: async (url) => {
if (cache.has(url)) return cache.get(url)!;
const source = await (await fetch(url)).text();
cache.set(url, source);
return source;
},
resolveImports,
});
Use createLoadIsomorphicCodeSource when you need a LoadSource for an environment other than the local Node.js filesystem:
loadServerCodeSourceIf you only need to load files from the local filesystem in a Node/server context, use loadServerCodeSource directly.
Creates a LoadSource function that performs all the platform-independent
work shared between server-side and client-side source loaders: fetching
the file via the supplied fetchSource, parsing its imports and comments,
reshaping externals, and assembling extraFiles / extraDependencies for
recursive loading.
Platform-specific concerns (fs.readFile, fetch, module resolution
against a real filesystem vs. a remote tree) are injected via the
fetchSource and resolveImports options.
| Property | Type | Description |
|---|---|---|
| fetchSource | | Async fetcher that returns the raw source text for a URL. In server contexts this typically wraps |
| resolveImports | | Resolve relative JavaScript/TypeScript imports to absolute URLs that can
be passed back into a When omitted, JavaScript modules are processed without a resolver: import
URLs are used as-is for |
| maxDepth | | Cap on the number of recursive load passes. Forwarded to consumers; this function itself only processes a single file. |
| maxFiles | | Cap on the total number of files surfaced via |
| includeDependencies | | When false, skip dependency parsing and return only the raw source. |
| storeAt | | Controls how imports are stored in
|
| removeCommentsWithPrefix | | Prefixes for comments that should be stripped from the source output.
Comments starting with these prefixes will be removed from the returned
source. They can still be collected via |
| notableCommentsPrefix | | Prefixes for notable comments that should be collected and included in
the result. Comments starting with these prefixes will be returned in the
|
LoadSourceImports record passed to .
Mirrors the shape produced by parseImportsAndComments once names have been
flattened, so resolvers can look up each import by its source path.
type IsomorphicImports = {
[key: string]: {
url: string;
names: string[];
includeTypeDefs?: true;
positions: { start: number; end: number }[];
};
}type LoadIsomorphicCodeSourceOptions = {
/**
* Async fetcher that returns the raw source text for a URL.
*
* In server contexts this typically wraps `fs/promises#readFile`, in client
* contexts it can wrap `fetch`. Required.
*/
fetchSource: (url: string) => Promise<string>;
/**
* Resolve relative JavaScript/TypeScript imports to absolute URLs that can
* be passed back into a `loadSource` recursion. Returning a `Map` keyed by
* the import URL lets the caller rewrite identifiers (e.g. `./foo` →
* `https://.../foo.tsx`).
*
* When omitted, JavaScript modules are processed without a resolver: import
* URLs are used as-is for `extraDependencies` and no rewriting happens.
*/
resolveImports?: (imports: IsomorphicImports) => Promise<Map<string, string>>;
/**
* Cap on the number of recursive load passes. Forwarded to consumers; this
* function itself only processes a single file.
*/
maxDepth?: number;
/** Cap on the total number of files surfaced via `extraFiles`. */
maxFiles?: number;
/** When false, skip dependency parsing and return only the raw source. */
includeDependencies?: boolean;
/**
* Controls how imports are stored in `extraFiles`:
* - 'canonical': Full resolved path (e.g., '../Component/index.js')
* - 'import': Import path with file extension (e.g., '../Component.js')
* - 'flat': Flattened to current directory with rewritten imports
*/
storeAt?: 'canonical' | 'import' | 'flat';
/**
* Prefixes for comments that should be stripped from the source output.
* Comments starting with these prefixes will be removed from the returned
* source. They can still be collected via `notableCommentsPrefix`.
*/
removeCommentsWithPrefix?: string[];
/**
* Prefixes for notable comments that should be collected and included in
* the result. Comments starting with these prefixes will be returned in the
* `comments` field.
*/
notableCommentsPrefix?: string[];
}loadServerCodeSource - Node.js wrapper that wires this factory to fs/promisesloadIsomorphicCodeVariant - Recursive variant loader that drives LoadSourceparseImportsAndComments - Underlying parser used to extract imports and commentsprocessRelativeImports - Applies the storeAt mode and rewrites import statementsresolveImportResult - Reference resolver used by loadServerCodeSource