UNPKG

7.94 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14 Object.defineProperty(o, "default", { enumerable: true, value: v });
15}) : function(o, v) {
16 o["default"] = v;
17});
18var __importStar = (this && this.__importStar) || function (mod) {
19 if (mod && mod.__esModule) return mod;
20 var result = {};
21 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22 __setModuleDefault(result, mod);
23 return result;
24};
25Object.defineProperty(exports, "__esModule", { value: true });
26exports.resolveModuleName = void 0;
27const general_utils_1 = require("./general-utils");
28const path = __importStar(require("path"));
29const typescript_1 = require("typescript");
30const ts_helpers_1 = require("./ts-helpers");
31var IndexType;
32(function (IndexType) {
33 IndexType[IndexType["NonIndex"] = 0] = "NonIndex";
34 IndexType[IndexType["Explicit"] = 1] = "Explicit";
35 IndexType[IndexType["Implicit"] = 2] = "Implicit";
36 IndexType[IndexType["ImplicitPackage"] = 3] = "ImplicitPackage";
37})(IndexType || (IndexType = {}));
38// endregion
39/* ****************************************************************************************************************** */
40// region: Helpers
41/* ****************************************************************************************************************** */
42function getPathDetail(moduleName, resolvedModule) {
43 var _a, _b;
44 let resolvedFileName = (_a = resolvedModule.originalPath) !== null && _a !== void 0 ? _a : resolvedModule.resolvedFileName;
45 const implicitPackageIndex = (_b = resolvedModule.packageId) === null || _b === void 0 ? void 0 : _b.subModuleName;
46 const resolvedDir = implicitPackageIndex
47 ? (0, typescript_1.removeSuffix)(resolvedFileName, `/${implicitPackageIndex}`)
48 : path.dirname(resolvedFileName);
49 const resolvedBaseName = implicitPackageIndex ? void 0 : path.basename(resolvedFileName);
50 const resolvedBaseNameNoExtension = resolvedBaseName && (0, typescript_1.removeFileExtension)(resolvedBaseName);
51 const resolvedExtName = resolvedBaseName && path.extname(resolvedFileName);
52 let baseName = !implicitPackageIndex ? path.basename(moduleName) : void 0;
53 let baseNameNoExtension = baseName && (0, typescript_1.removeFileExtension)(baseName);
54 let extName = baseName && path.extname(moduleName);
55 // Account for possible false extensions. Example scenario:
56 // moduleName = './file.accounting'
57 // resolvedBaseName = 'file.accounting.ts'
58 // ('accounting' would be considered the extension)
59 if (resolvedBaseNameNoExtension && baseName && resolvedBaseNameNoExtension === baseName) {
60 baseNameNoExtension = baseName;
61 extName = void 0;
62 }
63 // prettier-ignore
64 const indexType = implicitPackageIndex ? IndexType.ImplicitPackage :
65 baseNameNoExtension === 'index' && resolvedBaseNameNoExtension === 'index' ? IndexType.Explicit :
66 baseNameNoExtension !== 'index' && resolvedBaseNameNoExtension === 'index' ? IndexType.Implicit :
67 IndexType.NonIndex;
68 if (indexType === IndexType.Implicit) {
69 baseName = void 0;
70 baseNameNoExtension = void 0;
71 extName = void 0;
72 }
73 return {
74 baseName,
75 baseNameNoExtension,
76 extName,
77 resolvedBaseName,
78 resolvedBaseNameNoExtension,
79 resolvedExtName,
80 resolvedDir,
81 indexType,
82 implicitPackageIndex,
83 resolvedFileName,
84 };
85}
86function getResolvedSourceFile(context, fileName) {
87 let res;
88 const { program, tsInstance } = context;
89 if (program) {
90 /* Attempt to directly pull from Program */
91 res = program.getSourceFile(fileName);
92 if (res)
93 return res;
94 /* Attempt to find without extension */
95 res = program.getSourceFiles().find((s) => (0, typescript_1.removeFileExtension)(s.fileName) === (0, typescript_1.removeFileExtension)(fileName));
96 if (res)
97 return res;
98 }
99 /*
100 * Create basic synthetic SourceFile for use with compiler API - Applies if SourceFile not found in program due to
101 * import being added by another transformer
102 */
103 return tsInstance.createSourceFile(fileName, ``, tsInstance.ScriptTarget.ESNext, /* setParentNodes */ false);
104}
105// endregion
106/* ****************************************************************************************************************** */
107// region: Utils
108/* ****************************************************************************************************************** */
109/**
110 * Resolve a module name
111 */
112function resolveModuleName(context, moduleName) {
113 const { tsInstance, compilerOptions, sourceFile, config, rootDirs } = context;
114 // Attempt to resolve with TS Compiler API
115 const { resolvedModule, failedLookupLocations } = tsInstance.resolveModuleName(moduleName, sourceFile.fileName, compilerOptions, tsInstance.sys);
116 // Handle non-resolvable module
117 if (!resolvedModule) {
118 const maybeURL = failedLookupLocations[0];
119 if (!(0, general_utils_1.isURL)(maybeURL))
120 return void 0;
121 return {
122 isURL: true,
123 resolvedPath: void 0,
124 outputPath: maybeURL,
125 };
126 }
127 const resolvedSourceFile = getResolvedSourceFile(context, resolvedModule.resolvedFileName);
128 const { indexType, resolvedBaseNameNoExtension, resolvedFileName, implicitPackageIndex, extName, resolvedDir } = getPathDetail(moduleName, resolvedModule);
129 /* Determine output filename */
130 let outputBaseName = resolvedBaseNameNoExtension !== null && resolvedBaseNameNoExtension !== void 0 ? resolvedBaseNameNoExtension : "";
131 if (indexType === IndexType.Implicit)
132 outputBaseName = outputBaseName.replace(/(\/index$)|(^index$)/, "");
133 if (outputBaseName && extName)
134 outputBaseName = `${outputBaseName}${extName}`;
135 /* Determine output dir */
136 let srcFileOutputDir = (0, ts_helpers_1.getOutputDirForSourceFile)(context, sourceFile);
137 let moduleFileOutputDir = implicitPackageIndex ? resolvedDir : (0, ts_helpers_1.getOutputDirForSourceFile)(context, resolvedSourceFile);
138 // Handle rootDirs remapping
139 if (config.useRootDirs && rootDirs) {
140 let fileRootDir = "";
141 let moduleRootDir = "";
142 for (const rootDir of rootDirs) {
143 if ((0, general_utils_1.isBaseDir)(rootDir, moduleFileOutputDir) && rootDir.length > moduleRootDir.length)
144 moduleRootDir = rootDir;
145 if ((0, general_utils_1.isBaseDir)(rootDir, srcFileOutputDir) && rootDir.length > fileRootDir.length)
146 fileRootDir = rootDir;
147 }
148 /* Remove base dirs to make relative to root */
149 if (fileRootDir && moduleRootDir) {
150 srcFileOutputDir = path.relative(fileRootDir, srcFileOutputDir);
151 moduleFileOutputDir = path.relative(moduleRootDir, moduleFileOutputDir);
152 }
153 }
154 const outputDir = path.relative(srcFileOutputDir, moduleFileOutputDir);
155 /* Compose final output path */
156 const outputPath = (0, general_utils_1.maybeAddRelativeLocalPrefix)(tsInstance.normalizePath(path.join(outputDir, outputBaseName)));
157 return { isURL: false, outputPath, resolvedPath: resolvedFileName };
158}
159exports.resolveModuleName = resolveModuleName;
160// endregion
161//# sourceMappingURL=resolve-module-name.js.map
\No newline at end of file