tsc-path-fix
Version:
Zero-runtime TypeScript path resolver - converts aliases to relative paths at compile time. Fast, lightweight, with native watch mode.
104 lines • 4.46 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearPathResolutionCache = exports.newStringRegex = exports.replaceSourceImportPaths = exports.newImportStatementRegex = exports.resolveFullImportPaths = void 0;
const normalizePath = require("normalize-path");
const fs_1 = require("fs");
const path_1 = require("path");
const anyQuote = `["']`;
const pathStringContent = `[^"'\r\n]+`;
const importString = `(?:${anyQuote}${pathStringContent}${anyQuote})`;
const funcStyle = `(?:\\b(?:import|require)\\s*\\(\\s*(\\/\\*.*\\*\\/\\s*)?${importString}\\s*\\))`;
const globalStyle = `(?:\\bimport\\s+${importString})`;
const globalMinimizedStyle = `(?:\\bimport${importString})`;
const fromStyle = `(?:\\bfrom\\s+${importString})`;
const fromMinimizedStyle = `(?:\\bfrom${importString})`;
const moduleStyle = `(?:\\bmodule\\s+${importString})`;
const importRegexString = `(?:${[
funcStyle,
globalStyle,
globalMinimizedStyle,
fromStyle,
fromMinimizedStyle,
moduleStyle
].join(`|`)})`;
const importRegexGlobal = new RegExp(importRegexString, 'g');
const importRegexNonGlobal = new RegExp(importRegexString);
const stringRegex = new RegExp(`(?<pathWithQuotes>${anyQuote}(?<path>${pathStringContent})${anyQuote})`);
const pathResolutionCache = new Map();
class ImportPathResolver {
constructor(source, sourcePath) {
this.source = source;
this.sourcePath = sourcePath;
}
get sourceDir() {
return (0, path_1.dirname)(this.sourcePath);
}
replaceSourceImportPaths(replacer) {
this.source = this.source.replace(importRegexGlobal, replacer);
return this;
}
resolveFullImportPaths(ext = '.js') {
this.replaceSourceImportPaths((importStatement) => {
const importPathMatch = importStatement.match(stringRegex);
if (!importPathMatch) {
return importStatement;
}
const { path, pathWithQuotes } = importPathMatch.groups;
const fullPath = normalizePath(this.resolveFullPath(path, ext));
return importStatement.replace(pathWithQuotes, pathWithQuotes.replace(path, fullPath));
});
return this;
}
resolveFullPath(importPath, ext = '.js') {
if (!importPath.startsWith('.') ||
importPath.match(new RegExp(`\${ext}$`))) {
return importPath;
}
const cacheKey = `${this.sourceDir}:${importPath}:${ext}`;
if (pathResolutionCache.has(cacheKey)) {
return pathResolutionCache.get(cacheKey);
}
let resolvedPath = importPath;
if (!importPath.match(/[/\\]$/)) {
const asFilePath = `${importPath}${ext}`;
if ((0, fs_1.existsSync)((0, path_1.resolve)(this.sourceDir, asFilePath))) {
resolvedPath = asFilePath;
pathResolutionCache.set(cacheKey, resolvedPath);
return resolvedPath;
}
}
let asFilePath = (0, path_1.join)(importPath, 'index' + ext);
if ((importPath.startsWith('./') || importPath === '.') &&
!asFilePath.startsWith('./')) {
asFilePath = './' + asFilePath;
}
resolvedPath = (0, fs_1.existsSync)((0, path_1.resolve)(this.sourceDir, asFilePath))
? asFilePath
: importPath;
pathResolutionCache.set(cacheKey, resolvedPath);
return resolvedPath;
}
static newStringRegex() {
return stringRegex;
}
static newImportStatementRegex(flags = '') {
return flags === 'g' ? importRegexGlobal : importRegexNonGlobal;
}
static resolveFullImportPaths(code, path, ext = '.js') {
return new ImportPathResolver(code, path).resolveFullImportPaths(ext)
.source;
}
static replaceSourceImportPaths(code, path, replacer) {
return new ImportPathResolver(code, path).replaceSourceImportPaths(replacer)
.source;
}
static clearCache() {
pathResolutionCache.clear();
}
}
exports.resolveFullImportPaths = ImportPathResolver.resolveFullImportPaths;
exports.newImportStatementRegex = ImportPathResolver.newImportStatementRegex;
exports.replaceSourceImportPaths = ImportPathResolver.replaceSourceImportPaths;
exports.newStringRegex = ImportPathResolver.newStringRegex;
exports.clearPathResolutionCache = ImportPathResolver.clearCache;
//# sourceMappingURL=import-path-resolver.js.map
;