Skip to content

Commit 5255e9c

Browse files
committed
Update
1 parent 332affb commit 5255e9c

File tree

1 file changed

+82
-59
lines changed

1 file changed

+82
-59
lines changed

src/index.ts

Lines changed: 82 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import isNodeCoreModule from '@nolyfill/is-core-module'
55
import debug from 'debug'
66
import type { FileSystem, ResolveOptions, Resolver } from 'enhanced-resolve'
77
import enhancedResolve from 'enhanced-resolve'
8-
import { createPathsMatcher, getTsconfig } from 'get-tsconfig'
8+
import { createPathsMatcher, getTsconfig, parseTsconfig } from 'get-tsconfig'
99
import type { TsConfigResult } from 'get-tsconfig'
1010
import type { Version } from 'is-bun-module'
1111
import { isBunModule } from 'is-bun-module'
@@ -111,10 +111,14 @@ let cachedOptions: InternalResolverOptions | undefined
111111
let prevCwd: string
112112

113113
let mappersCachedOptions: InternalResolverOptions
114-
let mappers: Array<{
114+
let mappers: Mapper[] = []
115+
116+
type MapperFn = NonNullable<ReturnType<typeof createPathsMatcher>>
117+
118+
interface Mapper {
115119
files: Set<string>
116-
mapperFn: NonNullable<ReturnType<typeof createPathsMatcher>>
117-
}> = []
120+
mapperFn: MapperFn
121+
}
118122

119123
let resolverCachedOptions: InternalResolverOptions
120124
let cachedResolver: Resolver | undefined
@@ -386,8 +390,8 @@ function initMappers(options: InternalResolverOptions) {
386390
typeof options.project === 'string'
387391
? [options.project]
388392
: Array.isArray(options.project)
389-
? options.project
390-
: [process.cwd()]
393+
? options.project
394+
: [process.cwd()]
391395
) // 'tinyglobby' pattern must have POSIX separator
392396
.map(config => replacePathSeparator(config, path.sep, path.posix.sep))
393397

@@ -411,7 +415,7 @@ function initMappers(options: InternalResolverOptions) {
411415
]
412416

413417
mappers = projectPaths
414-
.map(projectPath => {
418+
.flatMap(projectPath => {
415419
let tsconfigResult: TsConfigResult | null
416420

417421
if (isFile(projectPath)) {
@@ -421,65 +425,84 @@ function initMappers(options: InternalResolverOptions) {
421425
tsconfigResult = getTsconfig(projectPath)
422426
}
423427

424-
if (!tsconfigResult) {
425-
// eslint-disable-next-line unicorn/no-useless-undefined
426-
return undefined
427-
}
428+
return getMapper(tsconfigResult)
429+
})
430+
.filter(isDefined)
428431

429-
const mapperFn = createPathsMatcher(tsconfigResult)
432+
const processedPaths = new Set<string>()
430433

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+
}
435440

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))
474450
}
451+
}
475452

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+
{
477501
files: new Set(files.map(toNativePathSeparator)),
478502
mapperFn,
479-
}
480-
})
481-
.filter(isDefined)
482-
503+
},
504+
]
505+
}
483506
mappersCachedOptions = options
484507
}
485508

0 commit comments

Comments
 (0)