UNPKG

@mui/internal-docs-infra

Version:

MUI Infra - internal documentation creation tools.

122 lines (109 loc) 3.86 kB
/** * Generates a unique import name based on module path and original name */ function generateUniqueImportName(originalName, modulePath, type, usedNames) { // If no conflict, use original name if (!usedNames.has(originalName)) { return originalName; } // For conflicts, strategy depends on type and context: // - Namespace imports: always use numbered suffixes // - Named imports from simple test cases (lib1, lib2, etc.): use numbered suffixes // - Other cases: try module-based names first const wantsNumberedSuffix = type === 'namespace' || modulePath.startsWith('lib') && /^lib\d+$/.test(modulePath); if (wantsNumberedSuffix) { // Use numbered suffixes let attempt = 1; let uniqueName = `${originalName}${attempt}`; while (usedNames.has(uniqueName)) { attempt += 1; uniqueName = `${originalName}${attempt}`; } return uniqueName; } // For real modules, try module-based names first const moduleKey = modulePath.replace(/[@/.-]/g, '') // Remove special characters .toLowerCase().slice(0, 20); // Limit length let uniqueName = `${originalName}${moduleKey}`; // If that's still taken, try numbered suffixes if (usedNames.has(uniqueName)) { let attempt = 1; do { uniqueName = `${originalName}${attempt}`; attempt += 1; } while (usedNames.has(uniqueName)); } return uniqueName; } /** * Generates import statements from externals without creating a provider * Returns just the import lines needed to bring in the dependencies */ export function generateImportStatements(externals) { const moduleImports = {}; const usedNames = new Set(); const seenImports = new Set(); // First pass: collect all imports and resolve naming conflicts for (const [modulePath, importItems] of Object.entries(externals)) { if (!moduleImports[modulePath]) { moduleImports[modulePath] = { named: [], namespace: [] }; } for (const { name: originalName, type, isType } of importItems) { // Skip type-only imports and empty names if (isType || !originalName.trim()) { continue; } const importKey = `${modulePath}:${originalName}:${type}`; // Skip duplicates if (seenImports.has(importKey)) { continue; } seenImports.add(importKey); const uniqueName = generateUniqueImportName(originalName, modulePath, type, usedNames); usedNames.add(uniqueName); if (type === 'default') { moduleImports[modulePath].default = uniqueName; } else if (type === 'named') { moduleImports[modulePath].named.push({ original: originalName, unique: uniqueName }); } else if (type === 'namespace') { moduleImports[modulePath].namespace.push(uniqueName); } } } // Second pass: generate consolidated import statements const imports = []; for (const [modulePath, moduleImport] of Object.entries(moduleImports)) { const importParts = []; // Add default import if (moduleImport.default) { importParts.push(moduleImport.default); } // Add named imports (consolidated into one statement) if (moduleImport.named.length > 0) { const namedImports = moduleImport.named.map(({ original, unique }) => original === unique ? original : `${original} as ${unique}`).join(', '); importParts.push(`{ ${namedImports} }`); } // Generate import statement if (importParts.length > 0) { imports.push(`import ${importParts.join(', ')} from '${modulePath}';`); } // Add namespace imports (separate statements as they can't be combined) for (const namespaceName of moduleImport.namespace) { imports.push(`import * as ${namespaceName} from '${modulePath}';`); } } return imports; }