6
6
"use strict" ;
7
7
8
8
const path = require ( "path" ) ;
9
- const { isAbsolute , join, normalize } = require ( "./util/path" ) ;
9
+ const { join, normalize } = require ( "./util/path" ) ;
10
10
11
11
/** @typedef {import("./Resolver") } Resolver */
12
12
/** @typedef {import("./Resolver").FileSystem } FileSystem */
@@ -47,34 +47,6 @@ function sortByLongestPrefix(arr) {
47
47
return [ ...arr ] . sort ( ( a , b ) => getPrefixLength ( b ) - getPrefixLength ( a ) ) ;
48
48
}
49
49
50
- /**
51
- * Converts an absolute baseUrl and paths to an array of absolute mapping entries.
52
- * The array is sorted by longest prefix.
53
- * Having an array with entries allows us to keep a sorting order rather than
54
- * sort by keys each time we use the mappings.
55
- * @param {{[key: string]: string[]} } paths TypeScript paths mapping
56
- * @param {string } baseUrl Base URL for resolving paths
57
- * @returns {AliasOption[] } Array of alias options
58
- */
59
- function getAbsoluteMappingEntries ( paths , baseUrl ) {
60
- /** @type {string[] } */
61
- const sortedKeys = sortByLongestPrefix ( Object . keys ( paths ) ) ;
62
- /** @type {AliasOption[] } */
63
- const absolutePaths = [ ] ;
64
-
65
- for ( const pattern of sortedKeys ) {
66
- const mappings = paths [ pattern ] ;
67
- // const aliasName = pattern.replace(/\/\*$/, "");
68
- // Convert targets like "dir/*" -> "dir"
69
- const aliasTargets = mappings . map ( ( mapping ) => join ( baseUrl , mapping ) ) ;
70
- if ( aliasTargets . length > 0 ) {
71
- absolutePaths . push ( { name : pattern , alias : aliasTargets } ) ;
72
- }
73
- }
74
-
75
- return absolutePaths ;
76
- }
77
-
78
50
/**
79
51
* Merge two tsconfig objects
80
52
* @param {Tsconfig } base base config
@@ -235,11 +207,9 @@ async function readTsconfigCompilerOptions(fileSystem, absTsconfigPath) {
235
207
} ;
236
208
let { baseUrl } = compilerOptions ;
237
209
238
- if ( ! baseUrl ) {
239
- baseUrl = path . dirname ( absTsconfigPath ) ;
240
- } else if ( ! isAbsolute ( baseUrl ) ) {
241
- baseUrl = join ( path . dirname ( absTsconfigPath ) , baseUrl ) ;
242
- }
210
+ baseUrl = ! baseUrl
211
+ ? path . dirname ( absTsconfigPath )
212
+ : join ( path . dirname ( absTsconfigPath ) , baseUrl ) ;
243
213
244
214
const paths = compilerOptions . paths || { } ;
245
215
return { options : { baseUrl, paths } , fileDependencies } ;
@@ -250,30 +220,42 @@ async function readTsconfigCompilerOptions(fileSystem, absTsconfigPath) {
250
220
251
221
/**
252
222
* Convert tsconfig paths to resolver options
253
- * @param {AliasOption[] } aliases the aliases
223
+ * @param {{[key: string]: string[]} } paths TypeScript paths mapping
224
+ * @param {string } baseUrl Base URL for resolving paths
254
225
* @returns {Omit<TsconfigPathsData, "fileDependencies"> } the resolver options
255
226
*/
256
- function pathsToResolveOptions ( aliases ) {
227
+ function pathsToResolveOptions ( paths , baseUrl ) {
228
+ /** @type {string[] } */
229
+ const sortedKeys = sortByLongestPrefix ( Object . keys ( paths ) ) ;
257
230
/** @type {AliasOption[] } */
258
231
const alias = [ ] ;
259
232
/** @type {string[] } */
260
233
const modules = [ ] ;
261
- for ( const opt of aliases ) {
262
- if ( opt . name === "*" ) {
263
- modules . push (
264
- .../** @type {Array<string> } */ ( opt . alias )
265
- . map ( ( dir ) => {
266
- if ( / [ / \\ ] \* $ / . test ( dir ) ) {
267
- return dir . replace ( / [ / \\ ] \* $ / , "" ) ;
268
- }
269
- return "" ;
270
- } )
271
- . filter ( Boolean ) ,
272
- ) ;
273
- } else {
274
- alias . push ( opt ) ;
234
+
235
+ for ( const pattern of sortedKeys ) {
236
+ const mappings = paths [ pattern ] ;
237
+ const aliasTargets = mappings . map ( ( mapping ) => join ( baseUrl , mapping ) ) ;
238
+
239
+ if ( aliasTargets . length > 0 ) {
240
+ if ( pattern === "*" ) {
241
+ // Handle "*" pattern - extract modules from wildcard targets
242
+ modules . push (
243
+ ...aliasTargets
244
+ . map ( ( dir ) => {
245
+ if ( / [ / \\ ] \* $ / . test ( dir ) ) {
246
+ return dir . replace ( / [ / \\ ] \* $ / , "" ) ;
247
+ }
248
+ return "" ;
249
+ } )
250
+ . filter ( Boolean ) ,
251
+ ) ;
252
+ } else {
253
+ // Handle regular patterns - add as alias
254
+ alias . push ( { name : pattern , alias : aliasTargets } ) ;
255
+ }
275
256
}
276
257
}
258
+
277
259
return {
278
260
alias,
279
261
modules,
@@ -293,25 +275,17 @@ async function loadTsconfigPathsData(
293
275
context = process . cwd ( ) ,
294
276
) {
295
277
try {
296
- const absTsconfigPath = isAbsolute ( configFile )
297
- ? configFile
298
- : join ( context , configFile ) ;
299
-
278
+ const absTsconfigPath = join ( context , configFile ) ;
300
279
const result = await readTsconfigCompilerOptions (
301
280
fileSystem ,
302
281
absTsconfigPath ,
303
282
) ;
304
283
if ( ! result ) return null ;
305
284
const { options : compilerOptions , fileDependencies } = result ;
306
285
if ( ! compilerOptions ) return null ;
307
- /** @type {AliasOption[] } */
308
- const absolutePaths = getAbsoluteMappingEntries (
309
- compilerOptions . paths ,
310
- compilerOptions . baseUrl ,
311
- ) ;
312
286
313
287
return {
314
- ...pathsToResolveOptions ( absolutePaths ) ,
288
+ ...pathsToResolveOptions ( compilerOptions . paths , compilerOptions . baseUrl ) ,
315
289
fileDependencies,
316
290
} ;
317
291
} catch ( _e ) {
0 commit comments