@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
35 lines (34 loc) • 1.49 kB
JavaScript
/**
* Rewrites relative imports in source code based on a provided mapping.
* Converts imports like '../utils/helper' or './components/Button' to their mapped equivalents
*
* @param source - The source code to process
* @param importPathMapping - Map from original import paths to new import paths (without extensions)
* @returns The source code with rewritten imports
*/
export function rewriteImportsToSameDirectory(source, importPathMapping) {
// Handle both types of imports:
// 1. import [something] from 'path' (including import type)
// 2. import 'path' (side-effect imports like CSS)
return source.replace(/import\s+((?:type\s+)?(?:\w+|\*\s+as\s+\w+|{[^}]+})\s+from\s+)['"]([^'"]+)['"]/g, function (match, importPart, modulePath) {
// Only process relative imports
if (modulePath.startsWith('.')) {
// Check if we have a mapping for this import path
if (importPathMapping.has(modulePath)) {
var newPath = importPathMapping.get(modulePath);
return "import ".concat(importPart, "'").concat(newPath, "'");
}
}
return match;
}).replace(/import\s+['"]([^'"]+)['"]/g, function (match, modulePath) {
// Only process relative imports
if (modulePath.startsWith('.')) {
// Check if we have a mapping for this import path
if (importPathMapping.has(modulePath)) {
var newPath = importPathMapping.get(modulePath);
return "import '".concat(newPath, "'");
}
}
return match;
});
}