1 | "use strict";
|
2 | var __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 | }));
|
13 | var __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 | });
|
18 | var __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 | };
|
25 | Object.defineProperty(exports, "__esModule", { value: true });
|
26 | exports.resolveModuleName = void 0;
|
27 | const general_utils_1 = require("./general-utils");
|
28 | const path = __importStar(require("path"));
|
29 | const typescript_1 = require("typescript");
|
30 | const ts_helpers_1 = require("./ts-helpers");
|
31 | var 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 |
|
39 |
|
40 |
|
41 |
|
42 | function 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 |
|
56 |
|
57 |
|
58 |
|
59 | if (resolvedBaseNameNoExtension && baseName && resolvedBaseNameNoExtension === baseName) {
|
60 | baseNameNoExtension = baseName;
|
61 | extName = void 0;
|
62 | }
|
63 |
|
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 | }
|
86 | function getResolvedSourceFile(context, fileName) {
|
87 | let res;
|
88 | const { program, tsInstance } = context;
|
89 | if (program) {
|
90 |
|
91 | res = program.getSourceFile(fileName);
|
92 | if (res)
|
93 | return res;
|
94 |
|
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 |
|
101 |
|
102 |
|
103 | return tsInstance.createSourceFile(fileName, ``, tsInstance.ScriptTarget.ESNext, false);
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | function resolveModuleName(context, moduleName) {
|
113 | const { tsInstance, compilerOptions, sourceFile, config, rootDirs } = context;
|
114 |
|
115 | const { resolvedModule, failedLookupLocations } = tsInstance.resolveModuleName(moduleName, sourceFile.fileName, compilerOptions, tsInstance.sys);
|
116 |
|
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 |
|
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 |
|
136 | let srcFileOutputDir = (0, ts_helpers_1.getOutputDirForSourceFile)(context, sourceFile);
|
137 | let moduleFileOutputDir = implicitPackageIndex ? resolvedDir : (0, ts_helpers_1.getOutputDirForSourceFile)(context, resolvedSourceFile);
|
138 |
|
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 |
|
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 |
|
156 | const outputPath = (0, general_utils_1.maybeAddRelativeLocalPrefix)(tsInstance.normalizePath(path.join(outputDir, outputBaseName)));
|
157 | return { isURL: false, outputPath, resolvedPath: resolvedFileName };
|
158 | }
|
159 | exports.resolveModuleName = resolveModuleName;
|
160 |
|
161 |
|
\ | No newline at end of file |