|
| 1 | +import { VisitorContext } from "../types"; |
| 2 | +import { isBaseDir, isURL } from "./general-utils"; |
| 3 | +import * as path from "path"; |
| 4 | +import { removeFileExtension, removeSuffix, ResolvedModuleFull } from "typescript"; |
| 5 | +import { getOutputFile } from "./ts-helpers"; |
| 6 | + |
| 7 | +/* ****************************************************************************************************************** */ |
| 8 | +// region: Types |
| 9 | +/* ****************************************************************************************************************** */ |
| 10 | + |
| 11 | +export interface ResolvedModule { |
| 12 | + /** |
| 13 | + * Absolute path to resolved module |
| 14 | + */ |
| 15 | + resolvedPath: string | undefined; |
| 16 | + /** |
| 17 | + * Output path |
| 18 | + */ |
| 19 | + outputPath: string; |
| 20 | + /** |
| 21 | + * Resolved to URL |
| 22 | + */ |
| 23 | + isURL: boolean; |
| 24 | +} |
| 25 | + |
| 26 | +enum IndexType { |
| 27 | + NonIndex, |
| 28 | + Explicit, |
| 29 | + Implicit, |
| 30 | + ImplicitPackage, |
| 31 | +} |
| 32 | + |
| 33 | +// endregion |
| 34 | + |
| 35 | +/* ****************************************************************************************************************** */ |
| 36 | +// region: Helpers |
| 37 | +/* ****************************************************************************************************************** */ |
| 38 | + |
| 39 | +function getPathDetail(moduleName: string, resolvedModule: ResolvedModuleFull) { |
| 40 | + let resolvedFileName = resolvedModule.originalPath ?? resolvedModule.resolvedFileName; |
| 41 | + const implicitPackageIndex = resolvedModule.packageId?.subModuleName; |
| 42 | + |
| 43 | + const resolvedDir = implicitPackageIndex |
| 44 | + ? removeSuffix(resolvedFileName, `/${implicitPackageIndex}`) |
| 45 | + : path.dirname(resolvedFileName); |
| 46 | + const resolvedBaseName = implicitPackageIndex ? void 0 : path.basename(resolvedFileName); |
| 47 | + const resolvedBaseNameNoExtension = resolvedBaseName && removeFileExtension(resolvedBaseName); |
| 48 | + const resolvedExtName = resolvedBaseName && path.extname(resolvedFileName); |
| 49 | + |
| 50 | + const baseName = !implicitPackageIndex ? path.basename(moduleName) : void 0; |
| 51 | + const baseNameNoExtension = baseName && removeFileExtension(baseName); |
| 52 | + const extName = baseName && path.extname(moduleName); |
| 53 | + |
| 54 | + // prettier-ignore |
| 55 | + const indexType = |
| 56 | + implicitPackageIndex ? IndexType.ImplicitPackage : |
| 57 | + baseNameNoExtension === 'index' && resolvedBaseNameNoExtension === 'index' ? IndexType.Explicit : |
| 58 | + baseNameNoExtension !== 'index' && resolvedBaseNameNoExtension === 'index' ? IndexType.Implicit : |
| 59 | + IndexType.NonIndex; |
| 60 | + |
| 61 | + return { |
| 62 | + baseName, |
| 63 | + baseNameNoExtension, |
| 64 | + extName, |
| 65 | + resolvedBaseName, |
| 66 | + resolvedBaseNameNoExtension, |
| 67 | + resolvedExtName, |
| 68 | + resolvedDir, |
| 69 | + indexType, |
| 70 | + implicitPackageIndex, |
| 71 | + resolvedFileName, |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +// endregion |
| 76 | + |
| 77 | +/* ****************************************************************************************************************** */ |
| 78 | +// region: Utils |
| 79 | +/* ****************************************************************************************************************** */ |
| 80 | + |
| 81 | +/** |
| 82 | + * Resolve a module name |
| 83 | + */ |
| 84 | +export function resolveModuleName(context: VisitorContext, moduleName: string): ResolvedModule | undefined { |
| 85 | + const { tsInstance, compilerOptions, sourceFile, config, rootDirs } = context; |
| 86 | + const { removeSuffix } = tsInstance; |
| 87 | + |
| 88 | + // Attempt to resolve with TS Compiler API |
| 89 | + const { resolvedModule, failedLookupLocations } = tsInstance.resolveModuleName( |
| 90 | + moduleName, |
| 91 | + sourceFile.fileName, |
| 92 | + compilerOptions, |
| 93 | + tsInstance.sys |
| 94 | + ); |
| 95 | + |
| 96 | + // Handle non-resolvable module |
| 97 | + if (!resolvedModule) { |
| 98 | + const maybeURL = failedLookupLocations[0]; |
| 99 | + if (!isURL(maybeURL)) return void 0; |
| 100 | + return { |
| 101 | + isURL: true, |
| 102 | + resolvedPath: void 0, |
| 103 | + outputPath: maybeURL, |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + const { |
| 108 | + indexType, |
| 109 | + resolvedBaseNameNoExtension, |
| 110 | + resolvedFileName, |
| 111 | + implicitPackageIndex, |
| 112 | + extName, |
| 113 | + resolvedDir, |
| 114 | + } = getPathDetail(moduleName, resolvedModule); |
| 115 | + |
| 116 | + /* Determine output filename */ |
| 117 | + let outputBaseName = resolvedBaseNameNoExtension ?? ""; |
| 118 | + |
| 119 | + if (indexType === IndexType.Implicit) outputBaseName = removeSuffix(outputBaseName, "/index"); |
| 120 | + if (outputBaseName && extName) outputBaseName = `${outputBaseName}${extName}`; |
| 121 | + |
| 122 | + /* Determine output dir */ |
| 123 | + let srcFileOutputDir = path.dirname(getOutputFile(context, sourceFile.fileName)); |
| 124 | + let moduleFileOutputDir = implicitPackageIndex ? resolvedDir : path.dirname(getOutputFile(context, resolvedFileName)); |
| 125 | + |
| 126 | + // Handle rootDirs remapping |
| 127 | + if (config.useRootDirs && rootDirs) { |
| 128 | + let fileRootDir = ""; |
| 129 | + let moduleRootDir = ""; |
| 130 | + for (const rootDir of rootDirs) { |
| 131 | + if (isBaseDir(rootDir, moduleFileOutputDir) && rootDir.length > moduleRootDir.length) moduleRootDir = rootDir; |
| 132 | + if (isBaseDir(rootDir, srcFileOutputDir) && rootDir.length > fileRootDir.length) fileRootDir = rootDir; |
| 133 | + } |
| 134 | + |
| 135 | + /* Remove base dirs to make relative to root */ |
| 136 | + if (fileRootDir && moduleRootDir) { |
| 137 | + srcFileOutputDir = path.relative(fileRootDir, srcFileOutputDir); |
| 138 | + moduleFileOutputDir = path.relative(moduleRootDir, moduleFileOutputDir); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + const outputDir = path.relative(srcFileOutputDir, moduleFileOutputDir); |
| 143 | + |
| 144 | + /* Compose final output path */ |
| 145 | + let outputPath = tsInstance.normalizePath(path.join(outputDir, outputBaseName)); |
| 146 | + outputPath = outputPath[0] === "." ? outputPath : `./${outputPath}`; |
| 147 | + |
| 148 | + return { isURL: false, outputPath, resolvedPath: resolvedFileName }; |
| 149 | +} |
| 150 | + |
| 151 | +// endregion |
0 commit comments