tsc-path-fix
Version:
Zero-runtime TypeScript path resolver - converts aliases to relative paths at compile time. Fast, lightweight, with native watch mode.
119 lines • 4.21 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.TrieNode = void 0;
exports.clearTrieCache = clearTrieCache;
const path_1 = require("path");
const helpers_1 = require("../helpers");
const searchCache = new Map();
const MAX_CACHE_SIZE = 2000;
class TrieNode {
constructor() {
this.children = new Map();
this.data = null;
this.isEndOfWord = false;
}
add(name, data) {
if (name.length <= 0)
return;
const firstChar = name[0];
const node = this.children.has(firstChar)
? this.children.get(firstChar)
: new TrieNode();
if (name.length == 1) {
node.data = data;
node.isEndOfWord = true;
}
else {
node.add(name.substring(1), data);
}
this.children.set(firstChar, node);
searchCache.clear();
}
search(name) {
const cachedResult = searchCache.get(name);
if (cachedResult !== undefined) {
return cachedResult;
}
if (name.length <= 0) {
searchCache.set(name, null);
return null;
}
const firstChar = name[0];
const node = this.children.get(firstChar);
let result = null;
if (node) {
if (name.length === 1) {
result = node.data;
}
else {
const recursiveResult = node.search(name.substring(1));
result = recursiveResult !== null ? recursiveResult : node.data;
}
}
else {
result = this.data;
}
if (searchCache.size >= MAX_CACHE_SIZE) {
const keysToDelete = Array.from(searchCache.keys()).slice(0, Math.floor(MAX_CACHE_SIZE * 0.2));
keysToDelete.forEach(key => searchCache.delete(key));
}
searchCache.set(name, result);
return result;
}
static clearCache() {
searchCache.clear();
}
static buildAliasTrie(config, paths) {
const aliasTrie = new this();
if (!paths) {
return aliasTrie;
}
const resolvedBaseUrl = (0, path_1.resolve)(config.configDir, config.baseUrl);
const aliasMap = Object.keys(paths).map((alias) => {
const shouldPrefixMatchWildly = alias.endsWith('*');
const prefix = alias.replace(/\*$/, '');
const normalizedPaths = paths[alias].map((path) => {
path = path.replace(/\*$/, '');
const dotIndex = path.lastIndexOf('.');
if (dotIndex !== -1) {
const beforeDot = path.slice(0, dotIndex);
const afterDot = path.slice(dotIndex);
if (!afterDot.includes('/') && !afterDot.includes('\\')) {
const extension = afterDot;
if (!isDTS(extension)) {
const normalizedExtension = extension.replace(/\.([mc])?ts(x)?$/, '.$1js$2');
path = beforeDot + normalizedExtension;
}
}
}
if ((0, path_1.isAbsolute)(path)) {
path = (0, path_1.relative)(resolvedBaseUrl, path);
}
return path;
});
return {
shouldPrefixMatchWildly,
prefix,
paths: normalizedPaths
};
});
if (aliasMap.some(alias => alias.paths.some(path => (0, path_1.normalize)(path).includes('..')) &&
!config.configDirInOutPath)) {
(0, helpers_1.relativeOutPathToConfigDir)(config);
}
aliasMap.forEach(alias => {
if (alias.prefix) {
aliasTrie.add(alias.prefix, Object.assign(Object.assign({}, alias), { paths: alias.paths.map((0, helpers_1.findBasePathOfAlias)(config)) }));
}
});
return aliasTrie;
}
}
exports.TrieNode = TrieNode;
function isDTS(extension) {
return /\.d(\..*)?\.[mc]?ts(x)?$/.test(extension);
}
function clearTrieCache() {
searchCache.clear();
}
//# sourceMappingURL=trie.js.map
;