reqab
Version:
A simple alias-based module resolver using custom pathconfig.
34 lines (26 loc) • 928 B
JavaScript
module.exports = function (requirePath) {
if (typeof requirePath !== "string") {
throw new Error("requirePath must be a string");
}
let config;
const cwd = process.cwd();
try {
config = require(`${cwd}/pathconfig`);
} catch (e) {
throw new Error("pathconfig.js is not found");
}
const pathConfig = config.aliases;
const parsedPath = requirePath.split("/");
const alias = parsedPath.find((p) => p.startsWith("@"));
const isAbsolute = requirePath.startsWith("/");
// if the path is absolute or does not contain an alias, we require it directly
if (isAbsolute || !alias) {
return require(requirePath);
}
const aliasPath = pathConfig[alias.replace("@", "")];
if (aliasPath === undefined) {
throw new Error(`Alias "${aliasPath}" not found in path configuration`);
}
const fullPath = `${cwd}/${aliasPath}/${parsedPath.slice(1).join("/")}`;
return require(fullPath);
};