@modern-js/utils
Version:
A Progressive React Framework for modern web development.
34 lines (33 loc) • 1.4 kB
JavaScript
import node_fs from "node:fs";
import node_path from "node:path";
const SOURCE_EXTENSIONS = [
'.ts',
'.js'
];
const JS_LIKE_EXTENSION_RE = /\.(?:c|m)?js$/;
const isFile = (filepath)=>node_fs.existsSync(filepath) && node_fs.statSync(filepath).isFile();
const findSourceEntry = (resolvedPath)=>{
const ext = node_path.extname(resolvedPath);
if (ext) {
if (isFile(resolvedPath)) return resolvedPath;
if (JS_LIKE_EXTENSION_RE.test(resolvedPath)) {
const tsPath = `${resolvedPath.slice(0, -ext.length)}.ts`;
if (isFile(tsPath)) return tsPath;
}
return;
}
for (const candidateExt of SOURCE_EXTENSIONS){
const filePath = `${resolvedPath}${candidateExt}`;
if (isFile(filePath)) return filePath;
}
for (const candidateExt of SOURCE_EXTENSIONS){
const indexPath = node_path.join(resolvedPath, `index${candidateExt}`);
if (isFile(indexPath)) return indexPath;
}
};
const findMatchedSourcePath = (matchPath, specifier)=>{
let matchedPath = matchPath(specifier, void 0, void 0, SOURCE_EXTENSIONS);
if (!matchedPath && JS_LIKE_EXTENSION_RE.test(specifier)) matchedPath = matchPath(specifier.replace(JS_LIKE_EXTENSION_RE, ''), void 0, void 0, SOURCE_EXTENSIONS);
return matchedPath;
};
export { JS_LIKE_EXTENSION_RE, SOURCE_EXTENSIONS, findMatchedSourcePath, findSourceEntry };