@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
43 lines (41 loc) • 1.93 kB
JavaScript
// webpack does not like node: imports
// eslint-disable-next-line n/prefer-node-protocol
import { readFile } from 'fs/promises';
// eslint-disable-next-line n/prefer-node-protocol
import { fileURLToPath } from 'url';
import { resolveImportResultWithFs } from "../loadServerCodeMeta/resolveModulePathWithFs.mjs";
import { createLoadIsomorphicCodeSource } from "../loadIsomorphicCodeSource/index.mjs";
/**
* Reads a source file from the local filesystem.
*
* Uses `fileURLToPath` so `file://` URLs (including Windows drive letters) and
* plain absolute paths are both accepted.
*/
async function readFileFromUrl(url) {
const filePath = url.startsWith('file://') ? fileURLToPath(url) : url;
return readFile(filePath, 'utf8');
}
/**
* 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.
*/
export const loadServerCodeSource = createLoadServerCodeSource();
/**
* Creates a loadSource function that reads a file and extracts its dependencies.
*
* @param options.storeAt - 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 (e.g., './Component.js')
* @param options.removeCommentsWithPrefix - Prefixes for comments to strip from source
* @param options.notableCommentsPrefix - Prefixes for comments to collect
*/
export function createLoadServerCodeSource(options = {}) {
return createLoadIsomorphicCodeSource({
fetchSource: readFileFromUrl,
resolveImports: resolveImportResultWithFs,
...options
});
}