UNPKG

@mui/internal-docs-infra

Version:

MUI Infra - internal documentation creation tools.

92 lines (91 loc) 4.41 kB
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; import { readFile } from 'node:fs/promises'; import { resolveVariantPathsWithFs } from "../loaderUtils/resolveModulePathWithFs.js"; import { parseCreateFactoryCall } from "../loadPrecomputedCodeHighlighter/parseCreateFactoryCall.js"; import { getFileNameFromUrl } from "../loaderUtils/index.js"; /** * Default loadServerCodeMeta function that resolves variant paths from demo files. * This function is used to load code metadata for demos, specifically resolving paths for variants defined in the demo files. * It reads the demo file, parses it to find `createDemo` calls with variants, and resolves the paths for those variants. * It returns a Code object mapping variant names to their resolved file URLs. */ export var loadServerCodeMeta = createLoadServerCodeMeta(); /** * Creates a loadCodeMeta function that resolves variant paths from demo files. * * This factory function creates a LoadCodeMeta implementation that: * 1. Parses the demo file to find createDemo calls with variants * 2. Resolves all variant entry point paths using resolveVariantPaths * 3. Returns a Code object mapping variant names to their resolved file URLs * * The actual loading, parsing, and transformation of the variants is handled * elsewhere by the CodeHighlighter component using loadVariant. * * @param options - Configuration options (currently unused) * @returns LoadCodeMeta function that takes a URL and returns Promise<Code> */ export function createLoadServerCodeMeta() { var _options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; return /*#__PURE__*/function () { var _loadCodeMeta = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(url) { var filePath, source, demoCall, code, resolvedVariantMap; return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: // Remove file:// prefix if present to get file path filePath = url.replace('file://', ''); // Read the source file to find createDemo calls _context.next = 3; return readFile(filePath, 'utf-8'); case 3: source = _context.sent; _context.next = 6; return parseCreateFactoryCall(source, filePath); case 6: demoCall = _context.sent; if (!(!demoCall || !demoCall.variants)) { _context.next = 9; break; } return _context.abrupt("return", {}); case 9: code = {}; // Resolve all variant paths and get them as file URLs _context.next = 12; return resolveVariantPathsWithFs(demoCall.variants); case 12: resolvedVariantMap = _context.sent; // Build Code object from the resolved variant map Array.from(resolvedVariantMap.entries()).forEach(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), variantName = _ref2[0], fileUrl = _ref2[1]; var namedExport = demoCall.namedExports[variantName]; code[variantName] = fileUrl; if (namedExport) { var _getFileNameFromUrl = getFileNameFromUrl(fileUrl), fileName = _getFileNameFromUrl.fileName; if (!fileName) { throw new Error("Cannot determine fileName from URL \"".concat(fileUrl, "\" for variant \"").concat(variantName, "\". ") + "Please ensure the URL has a valid file extension."); } code[variantName] = { url: fileUrl, fileName: fileName, namedExport: namedExport }; } // TODO: will this cause loadVariantMeta not to run? Maybe we should always run it }); return _context.abrupt("return", code); case 15: case "end": return _context.stop(); } }, _callee); })); function loadCodeMeta(_x) { return _loadCodeMeta.apply(this, arguments); } return loadCodeMeta; }(); }