|
| 1 | +import { VisitorContext } from "../types"; |
| 2 | +import { isBaseDir, isURL, maybeAddRelativeLocalPrefix } 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 | + let baseName = !implicitPackageIndex ? path.basename(moduleName) : void 0; |
| 51 | + let baseNameNoExtension = baseName && removeFileExtension(baseName); |
| 52 | + let extName = baseName && path.extname(moduleName); |
| 53 | + |
| 54 | + // Account for possible false extensions. Example scenario: |
| 55 | + // moduleName = './file.accounting' |
| 56 | + // resolvedBaseName = 'file.accounting.ts' |
| 57 | + // ('accounting' would be considered the extension) |
| 58 | + if (resolvedBaseNameNoExtension && baseName && resolvedBaseNameNoExtension === baseName) { |
| 59 | + baseNameNoExtension = baseName; |
| 60 | + extName = void 0; |
| 61 | + } |
| 62 | + |
| 63 | + // prettier-ignore |
| 64 | + const indexType = |
| 65 | + implicitPackageIndex ? IndexType.ImplicitPackage : |
| 66 | + baseNameNoExtension === 'index' && resolvedBaseNameNoExtension === 'index' ? IndexType.Explicit : |
| 67 | + baseNameNoExtension !== 'index' && resolvedBaseNameNoExtension === 'index' ? IndexType.Implicit : |
| 68 | + IndexType.NonIndex; |
| 69 | + |
| 70 | + if (indexType === IndexType.Implicit) { |
| 71 | + baseName = void 0; |
| 72 | + baseNameNoExtension = void 0; |
| 73 | + extName = void 0; |
| 74 | + } |
| 75 | + |
| 76 | + return { |
| 77 | + baseName, |
| 78 | + baseNameNoExtension, |
| 79 | + extName, |
| 80 | + resolvedBaseName, |
| 81 | + resolvedBaseNameNoExtension, |
| 82 | + resolvedExtName, |
| 83 | + resolvedDir, |
| 84 | + indexType, |
| 85 | + implicitPackageIndex, |
| 86 | + resolvedFileName, |
| 87 | + }; |
| 88 | +} |
| 89 | + |
| 90 | +// endregion |
| 91 | + |
| 92 | +/* ****************************************************************************************************************** */ |
| 93 | +// region: Utils |
| 94 | +/* ****************************************************************************************************************** */ |
| 95 | + |
| 96 | +/** |
| 97 | + * Resolve a module name |
| 98 | + */ |
| 99 | +export function resolveModuleName(context: VisitorContext, moduleName: string): ResolvedModule | undefined { |
| 100 | + const { tsInstance, compilerOptions, sourceFile, config, rootDirs } = context; |
| 101 | + |
| 102 | + // Attempt to resolve with TS Compiler API |
| 103 | + const { resolvedModule, failedLookupLocations } = tsInstance.resolveModuleName( |
| 104 | + moduleName, |
| 105 | + sourceFile.fileName, |
| 106 | + compilerOptions, |
| 107 | + tsInstance.sys |
| 108 | + ); |
| 109 | + |
| 110 | + // Handle non-resolvable module |
| 111 | + if (!resolvedModule) { |
| 112 | + const maybeURL = failedLookupLocations[0]; |
| 113 | + if (!isURL(maybeURL)) return void 0; |
| 114 | + return { |
| 115 | + isURL: true, |
| 116 | + resolvedPath: void 0, |
| 117 | + outputPath: maybeURL, |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + const { |
| 122 | + indexType, |
| 123 | + resolvedBaseNameNoExtension, |
| 124 | + resolvedFileName, |
| 125 | + implicitPackageIndex, |
| 126 | + extName, |
| 127 | + resolvedDir, |
| 128 | + } = getPathDetail(moduleName, resolvedModule); |
| 129 | + |
| 130 | + /* Determine output filename */ |
| 131 | + let outputBaseName = resolvedBaseNameNoExtension ?? ""; |
| 132 | + |
| 133 | + if (indexType === IndexType.Implicit) outputBaseName = outputBaseName.replace(/(\/index$)|(^index$)/, ""); |
| 134 | + if (outputBaseName && extName) outputBaseName = `${outputBaseName}${extName}`; |
| 135 | + |
| 136 | + /* Determine output dir */ |
| 137 | + let srcFileOutputDir = path.dirname(getOutputFile(context, sourceFile.fileName)); |
| 138 | + let moduleFileOutputDir = implicitPackageIndex ? resolvedDir : path.dirname(getOutputFile(context, resolvedFileName)); |
| 139 | + |
| 140 | + // Handle rootDirs remapping |
| 141 | + if (config.useRootDirs && rootDirs) { |
| 142 | + let fileRootDir = ""; |
| 143 | + let moduleRootDir = ""; |
| 144 | + for (const rootDir of rootDirs) { |
| 145 | + if (isBaseDir(rootDir, moduleFileOutputDir) && rootDir.length > moduleRootDir.length) moduleRootDir = rootDir; |
| 146 | + if (isBaseDir(rootDir, srcFileOutputDir) && rootDir.length > fileRootDir.length) fileRootDir = rootDir; |
| 147 | + } |
| 148 | + |
| 149 | + /* Remove base dirs to make relative to root */ |
| 150 | + if (fileRootDir && moduleRootDir) { |
| 151 | + srcFileOutputDir = path.relative(fileRootDir, srcFileOutputDir); |
| 152 | + moduleFileOutputDir = path.relative(moduleRootDir, moduleFileOutputDir); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + const outputDir = path.relative(srcFileOutputDir, moduleFileOutputDir); |
| 157 | + |
| 158 | + /* Compose final output path */ |
| 159 | + const outputPath = maybeAddRelativeLocalPrefix(tsInstance.normalizePath(path.join(outputDir, outputBaseName))); |
| 160 | + |
| 161 | + return { isURL: false, outputPath, resolvedPath: resolvedFileName }; |
| 162 | +} |
| 163 | + |
| 164 | +// endregion |
0 commit comments