1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
10 | if (k2 === undefined) k2 = k;
|
11 | var desc = Object.getOwnPropertyDescriptor(m, k);
|
12 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
13 | desc = { enumerable: true, get: function() { return m[k]; } };
|
14 | }
|
15 | Object.defineProperty(o, k2, desc);
|
16 | }) : (function(o, m, k, k2) {
|
17 | if (k2 === undefined) k2 = k;
|
18 | o[k2] = m[k];
|
19 | }));
|
20 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
21 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
22 | }) : function(o, v) {
|
23 | o["default"] = v;
|
24 | });
|
25 | var __importStar = (this && this.__importStar) || function (mod) {
|
26 | if (mod && mod.__esModule) return mod;
|
27 | var result = {};
|
28 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
29 | __setModuleDefault(result, mod);
|
30 | return result;
|
31 | };
|
32 | Object.defineProperty(exports, "__esModule", { value: true });
|
33 | exports.TypeScriptPathsPlugin = void 0;
|
34 | const path = __importStar(require("path"));
|
35 | class TypeScriptPathsPlugin {
|
36 | baseUrl;
|
37 | patterns;
|
38 | constructor(options) {
|
39 | if (options) {
|
40 | this.update(options);
|
41 | }
|
42 | }
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | update(options) {
|
51 | this.baseUrl = options.baseUrl;
|
52 | this.patterns = undefined;
|
53 | if (options.paths) {
|
54 | for (const [pattern, potentials] of Object.entries(options.paths)) {
|
55 |
|
56 | if (potentials.length === 0 || potentials.every((potential) => potential === '*')) {
|
57 | continue;
|
58 | }
|
59 | const starIndex = pattern.indexOf('*');
|
60 | let prefix = pattern;
|
61 | let suffix;
|
62 | if (starIndex > -1) {
|
63 | prefix = pattern.slice(0, starIndex);
|
64 | if (starIndex < pattern.length - 1) {
|
65 | suffix = pattern.slice(starIndex + 1);
|
66 | }
|
67 | }
|
68 | this.patterns ??= [];
|
69 | this.patterns.push({
|
70 | starIndex,
|
71 | prefix,
|
72 | suffix,
|
73 | potentials: potentials.map((potential) => {
|
74 | const potentialStarIndex = potential.indexOf('*');
|
75 | if (potentialStarIndex === -1) {
|
76 | return { hasStar: false, prefix: potential };
|
77 | }
|
78 | return {
|
79 | hasStar: true,
|
80 | prefix: potential.slice(0, potentialStarIndex),
|
81 | suffix: potentialStarIndex < potential.length - 1
|
82 | ? potential.slice(potentialStarIndex + 1)
|
83 | : undefined,
|
84 | };
|
85 | }),
|
86 | });
|
87 | }
|
88 |
|
89 | this.patterns?.sort((a, b) => {
|
90 | if (a.starIndex === -1) {
|
91 | return -1;
|
92 | }
|
93 | else if (b.starIndex === -1) {
|
94 | return 1;
|
95 | }
|
96 | else {
|
97 | return b.starIndex - a.starIndex;
|
98 | }
|
99 | });
|
100 | }
|
101 | }
|
102 | apply(resolver) {
|
103 | const target = resolver.ensureHook('resolve');
|
104 |
|
105 |
|
106 | resolver
|
107 | .getHook('described-resolve')
|
108 | .tapAsync('TypeScriptPathsPlugin', (request, resolveContext, callback) => {
|
109 |
|
110 | if (!this.patterns) {
|
111 | callback();
|
112 | return;
|
113 | }
|
114 | if (!request || request.typescriptPathMapped) {
|
115 | callback();
|
116 | return;
|
117 | }
|
118 | const originalRequest = request.request || request.path;
|
119 | if (!originalRequest) {
|
120 | callback();
|
121 | return;
|
122 | }
|
123 |
|
124 | if (!request?.context?.issuer?.match(/\.[cm]?[jt]sx?$/)) {
|
125 | callback();
|
126 | return;
|
127 | }
|
128 |
|
129 | if (path.isAbsolute(originalRequest)) {
|
130 | callback();
|
131 | return;
|
132 | }
|
133 | switch (originalRequest[0]) {
|
134 | case '.':
|
135 |
|
136 | callback();
|
137 | return;
|
138 | case '!':
|
139 |
|
140 | if (originalRequest.length > 1 && originalRequest[1] === '!') {
|
141 | callback();
|
142 | return;
|
143 | }
|
144 | break;
|
145 | }
|
146 |
|
147 |
|
148 |
|
149 | const requests = this.createReplacementRequests(request, originalRequest);
|
150 | const tryResolve = () => {
|
151 | const next = requests.next();
|
152 | if (next.done) {
|
153 | callback();
|
154 | return;
|
155 | }
|
156 | resolver.doResolve(target, next.value, '', resolveContext, (error, result) => {
|
157 | if (error) {
|
158 | callback(error);
|
159 | }
|
160 | else if (result) {
|
161 | callback(undefined, result);
|
162 | }
|
163 | else {
|
164 | tryResolve();
|
165 | }
|
166 | });
|
167 | };
|
168 | tryResolve();
|
169 | });
|
170 | }
|
171 | *findReplacements(originalRequest) {
|
172 | if (!this.patterns) {
|
173 | return;
|
174 | }
|
175 |
|
176 | for (const { starIndex, prefix, suffix, potentials } of this.patterns) {
|
177 | let partial;
|
178 | if (starIndex === -1) {
|
179 |
|
180 | if (prefix === originalRequest) {
|
181 | partial = '';
|
182 | }
|
183 | }
|
184 | else if (starIndex === 0 && !suffix) {
|
185 |
|
186 | partial = originalRequest;
|
187 | }
|
188 | else if (!suffix) {
|
189 |
|
190 | if (originalRequest.startsWith(prefix)) {
|
191 | partial = originalRequest.slice(prefix.length);
|
192 | }
|
193 | }
|
194 | else {
|
195 |
|
196 | if (originalRequest.startsWith(prefix) && originalRequest.endsWith(suffix)) {
|
197 | partial = originalRequest.substring(prefix.length, originalRequest.length - suffix.length);
|
198 | }
|
199 | }
|
200 |
|
201 | if (partial === undefined) {
|
202 | continue;
|
203 | }
|
204 |
|
205 |
|
206 | for (const { hasStar, prefix, suffix } of potentials) {
|
207 | let replacement = prefix;
|
208 | if (hasStar) {
|
209 | replacement += partial;
|
210 | if (suffix) {
|
211 | replacement += suffix;
|
212 | }
|
213 | }
|
214 | yield replacement;
|
215 | }
|
216 | }
|
217 | }
|
218 | *createReplacementRequests(request, originalRequest) {
|
219 | for (const replacement of this.findReplacements(originalRequest)) {
|
220 | const targetPath = path.resolve(this.baseUrl ?? '', replacement);
|
221 |
|
222 |
|
223 | yield {
|
224 | ...request,
|
225 | request: targetPath,
|
226 | typescriptPathMapped: true,
|
227 | };
|
228 |
|
229 |
|
230 | const canBeModule = path.extname(targetPath) === '';
|
231 | if (canBeModule) {
|
232 |
|
233 |
|
234 |
|
235 | yield {
|
236 | ...request,
|
237 | path: targetPath,
|
238 | typescriptPathMapped: true,
|
239 | };
|
240 | }
|
241 | }
|
242 | }
|
243 | }
|
244 | exports.TypeScriptPathsPlugin = TypeScriptPathsPlugin;
|