UNPKG

@modern-js/utils

Version:

A Progressive React Framework for modern web development.

57 lines (56 loc) 2.08 kB
import node_fs from "node:fs"; import node_path from "node:path"; const RESOLVE_EXTENSIONS = [ '.tsx', '.ts', '.jsx', '.js', '.mjs', '.cjs' ]; function normalizeModulePath(filePath) { const absolute = node_path.resolve(filePath); let real = absolute; try { real = node_fs.realpathSync.native(absolute); } catch {} return real.split(node_path.sep).join('/'); } function resolveRouteComponentFile(component, srcDirectory, srcAlias) { const normalizedAlias = srcAlias.replace(/[\\/]+$/, ''); let candidate; if (component === normalizedAlias || component.startsWith(`${normalizedAlias}/`)) { const relative = component.slice(normalizedAlias.length).replace(/^\/+/, ''); candidate = node_path.resolve(srcDirectory, relative); } else { if (!node_path.isAbsolute(component)) return; candidate = node_path.resolve(component); } if (node_path.extname(candidate) && node_fs.existsSync(candidate)) return normalizeModulePath(candidate); for (const ext of RESOLVE_EXTENSIONS)if (node_fs.existsSync(candidate + ext)) return normalizeModulePath(candidate + ext); for (const ext of RESOLVE_EXTENSIONS){ const indexFile = node_path.join(candidate, `index${ext}`); if (node_fs.existsSync(indexFile)) return normalizeModulePath(indexFile); } } function collectRouteComponentFiles(routes, srcDirectory, srcAlias) { const resolvedFiles = new Set(); const unresolvedSpecifiers = []; const walk = (list)=>{ if (!Array.isArray(list)) return; for (const route of list){ if (route._component) { const file = resolveRouteComponentFile(route._component, srcDirectory, srcAlias); if (file) resolvedFiles.add(file); else unresolvedSpecifiers.push(route._component); } walk(route.children); } }; walk(routes); return { resolvedFiles, unresolvedSpecifiers }; } export { collectRouteComponentFiles, normalizeModulePath };