@@ -5,7 +5,7 @@ import isNodeCoreModule from '@nolyfill/is-core-module'
5
5
import debug from 'debug'
6
6
import type { FileSystem , ResolveOptions , Resolver } from 'enhanced-resolve'
7
7
import enhancedResolve from 'enhanced-resolve'
8
- import { createPathsMatcher , getTsconfig } from 'get-tsconfig'
8
+ import { createPathsMatcher , getTsconfig , parseTsconfig } from 'get-tsconfig'
9
9
import type { TsConfigResult } from 'get-tsconfig'
10
10
import type { Version } from 'is-bun-module'
11
11
import { isBunModule } from 'is-bun-module'
@@ -111,10 +111,14 @@ let cachedOptions: InternalResolverOptions | undefined
111
111
let prevCwd : string
112
112
113
113
let mappersCachedOptions : InternalResolverOptions
114
- let mappers : Array < {
114
+ let mappers : Mapper [ ] = [ ]
115
+
116
+ type MapperFn = NonNullable < ReturnType < typeof createPathsMatcher > >
117
+
118
+ interface Mapper {
115
119
files : Set < string >
116
- mapperFn : NonNullable < ReturnType < typeof createPathsMatcher > >
117
- } > = [ ]
120
+ mapperFn : MapperFn
121
+ }
118
122
119
123
let resolverCachedOptions : InternalResolverOptions
120
124
let cachedResolver : Resolver | undefined
@@ -386,8 +390,8 @@ function initMappers(options: InternalResolverOptions) {
386
390
typeof options . project === 'string'
387
391
? [ options . project ]
388
392
: Array . isArray ( options . project )
389
- ? options . project
390
- : [ process . cwd ( ) ]
393
+ ? options . project
394
+ : [ process . cwd ( ) ]
391
395
) // 'tinyglobby' pattern must have POSIX separator
392
396
. map ( config => replacePathSeparator ( config , path . sep , path . posix . sep ) )
393
397
@@ -411,7 +415,7 @@ function initMappers(options: InternalResolverOptions) {
411
415
]
412
416
413
417
mappers = projectPaths
414
- . map ( projectPath => {
418
+ . flatMap ( projectPath => {
415
419
let tsconfigResult : TsConfigResult | null
416
420
417
421
if ( isFile ( projectPath ) ) {
@@ -421,65 +425,84 @@ function initMappers(options: InternalResolverOptions) {
421
425
tsconfigResult = getTsconfig ( projectPath )
422
426
}
423
427
424
- if ( ! tsconfigResult ) {
425
- // eslint-disable-next-line unicorn/no-useless-undefined
426
- return undefined
427
- }
428
+ return getMapper ( tsconfigResult )
429
+ } )
430
+ . filter ( isDefined )
428
431
429
- const mapperFn = createPathsMatcher ( tsconfigResult )
432
+ const processedPaths = new Set < string > ( )
430
433
431
- if ( ! mapperFn ) {
432
- // eslint-disable-next-line unicorn/no-useless-undefined
433
- return undefined
434
- }
434
+ function getMapper ( tsconfigResult : TsConfigResult | null ) : Mapper [ ] {
435
+ const list : Mapper [ ] = [ ]
436
+
437
+ if ( ! tsconfigResult ) {
438
+ return list
439
+ }
435
440
436
- const files =
437
- tsconfigResult . config . files === undefined &&
438
- tsconfigResult . config . include === undefined
439
- ? // Include everything if no files or include options
440
- globSync ( defaultInclude , {
441
- ignore : [
442
- ...( tsconfigResult . config . exclude ?? [ ] ) ,
443
- ...defaultIgnore ,
444
- ] ,
445
- absolute : true ,
446
- cwd : path . dirname ( tsconfigResult . path ) ,
447
- } )
448
- : [
449
- // https://www.typescriptlang.org/tsconfig/#files
450
- ...( tsconfigResult . config . files !== undefined &&
451
- tsconfigResult . config . files . length > 0
452
- ? tsconfigResult . config . files . map ( file =>
453
- path . normalize (
454
- path . resolve ( path . dirname ( tsconfigResult ! . path ) , file ) ,
455
- ) ,
456
- )
457
- : [ ] ) ,
458
- // https://www.typescriptlang.org/tsconfig/#include
459
- ...( tsconfigResult . config . include !== undefined &&
460
- tsconfigResult . config . include . length > 0
461
- ? globSync ( tsconfigResult . config . include , {
462
- ignore : [
463
- ...( tsconfigResult . config . exclude ?? [ ] ) ,
464
- ...defaultIgnore ,
465
- ] ,
466
- absolute : true ,
467
- } )
468
- : [ ] ) ,
469
- ]
470
-
471
- if ( files . length === 0 ) {
472
- // eslint-disable-next-line unicorn/no-useless-undefined
473
- return undefined
441
+ if ( tsconfigResult . config . references ) {
442
+ const references = tsconfigResult . config . references
443
+ . map ( ref => path . resolve ( path . dirname ( tsconfigResult . path ) , ref . path ) )
444
+ . filter ( path => ! processedPaths . has ( path ) )
445
+ . map ( path => ( { path, config : parseTsconfig ( path ) } ) )
446
+
447
+ for ( const ref of references ) {
448
+ processedPaths . add ( ref . path )
449
+ list . push ( ...getMapper ( ref ) )
474
450
}
451
+ }
475
452
476
- return {
453
+ const mapperFn = createPathsMatcher ( tsconfigResult )
454
+
455
+ if ( ! mapperFn ) {
456
+ return list
457
+ }
458
+
459
+ const files =
460
+ tsconfigResult . config . files === undefined &&
461
+ tsconfigResult . config . include === undefined
462
+ ? // Include everything if no files or include options
463
+ globSync ( defaultInclude , {
464
+ ignore : [
465
+ ...( tsconfigResult . config . exclude ?? [ ] ) ,
466
+ ...defaultIgnore ,
467
+ ] ,
468
+ absolute : true ,
469
+ cwd : path . dirname ( tsconfigResult . path ) ,
470
+ } )
471
+ : [
472
+ // https://www.typescriptlang.org/tsconfig/#files
473
+ ...( tsconfigResult . config . files !== undefined &&
474
+ tsconfigResult . config . files . length > 0
475
+ ? tsconfigResult . config . files . map ( file =>
476
+ path . normalize (
477
+ path . resolve ( path . dirname ( tsconfigResult ! . path ) , file ) ,
478
+ ) ,
479
+ )
480
+ : [ ] ) ,
481
+ // https://www.typescriptlang.org/tsconfig/#include
482
+ ...( tsconfigResult . config . include !== undefined &&
483
+ tsconfigResult . config . include . length > 0
484
+ ? globSync ( tsconfigResult . config . include , {
485
+ ignore : [
486
+ ...( tsconfigResult . config . exclude ?? [ ] ) ,
487
+ ...defaultIgnore ,
488
+ ] ,
489
+ absolute : true ,
490
+ } )
491
+ : [ ] ) ,
492
+ ]
493
+
494
+ if ( files . length === 0 ) {
495
+ return list
496
+ }
497
+
498
+ return [
499
+ ...list ,
500
+ {
477
501
files : new Set ( files . map ( toNativePathSeparator ) ) ,
478
502
mapperFn,
479
- }
480
- } )
481
- . filter ( isDefined )
482
-
503
+ } ,
504
+ ]
505
+ }
483
506
mappersCachedOptions = options
484
507
}
485
508
0 commit comments