@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
73 lines (68 loc) • 3.56 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
/**
* Adds or replaces precompute data in createDemo function calls.
*
* This function handles multiple scenarios:
* 1. Replaces any existing 'precompute' property with actual data
* 2. Adds precompute property to existing options object when missing
* 3. Adds entire options object with precompute when no options exist
* 4. Optionally adds an ExternalsProvider property for external dependencies
*
* @param source - The source code string containing createDemo calls
* @param precomputeData - The data object to inject
* @param demoCallInfo - Information about the parsed demo call structure
* @param externalsProviderPath - Optional path to the generated externals provider file
* @returns The modified source code with precompute data injected
*/
export function replacePrecomputeValue(source, precomputeData, demoCallInfo, externalsProviderPath) {
// Convert the data to a properly formatted JSON string
var precomputeDataString = JSON.stringify(precomputeData, null, 2);
// If no demoCallInfo provided, return unchanged
if (!demoCallInfo) {
return source;
}
var callInfo = demoCallInfo;
var fullMatch = callInfo.fullMatch,
optionsObjectStr = callInfo.optionsObjectStr,
hasOptions = callInfo.hasOptions,
hasPrecompute = callInfo.hasPrecompute,
precomputeKeyStart = callInfo.precomputeKeyStart,
precomputeValueEnd = callInfo.precomputeValueEnd;
// Prepare externals provider import and property if needed
var modifiedSource = source;
var additionalProperties = '';
if (externalsProviderPath) {
// Add import statement at the top of the file
var importStatement = "import { CodeExternalsProvider } from '".concat(externalsProviderPath, "';\n");
modifiedSource = importStatement + modifiedSource;
// Prepare the CodeExternalsProvider property
additionalProperties = ", CodeExternalsProvider";
}
// Case 1: Replace existing precompute property (from key start to value end)
if (hasPrecompute && precomputeKeyStart !== undefined && precomputeValueEnd !== undefined) {
// Replace the entire property from key start to value end
var beforeProperty = optionsObjectStr.substring(0, precomputeKeyStart);
var afterProperty = optionsObjectStr.substring(precomputeValueEnd);
var newOptionsStr = "".concat(beforeProperty, "precompute: ").concat(precomputeDataString).concat(additionalProperties).concat(afterProperty);
return modifiedSource.replace(optionsObjectStr, newOptionsStr);
}
// Case 2: Add precompute to existing options object
if (hasOptions) {
var optionsMatch = optionsObjectStr.match(/^(\s*\{)([\s\S]*?)(\s*\}\s*)$/);
if (optionsMatch) {
var _optionsMatch = _slicedToArray(optionsMatch, 4),
openBrace = _optionsMatch[1],
content = _optionsMatch[2],
closeBrace = _optionsMatch[3];
var trimmedContent = content.trim();
var needsComma = trimmedContent !== '' && !trimmedContent.endsWith(',');
var newOptions = "".concat(openBrace).concat(content).concat(needsComma ? ',' : '', "\n precompute: ").concat(precomputeDataString).concat(additionalProperties).concat(closeBrace);
return modifiedSource.replace(optionsObjectStr, newOptions);
}
} else {
// Case 3: Add entire options object
var newCall = fullMatch.replace(/(\s*)\)$/, "$1, { precompute: ".concat(precomputeDataString).concat(additionalProperties, " })"));
return modifiedSource.replace(fullMatch, newCall);
}
return source;
}