From 5255e9cd7b072365bfaca80d4560c57353fead28 Mon Sep 17 00:00:00 2001 From: Alex <8125011+alex-kinokon@users.noreply.github.com> Date: Mon, 17 Feb 2025 08:06:56 +0000 Subject: [PATCH 1/4] Update --- src/index.ts | 141 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 59 deletions(-) diff --git a/src/index.ts b/src/index.ts index 226d6ec..ea5f40b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import isNodeCoreModule from '@nolyfill/is-core-module' import debug from 'debug' import type { FileSystem, ResolveOptions, Resolver } from 'enhanced-resolve' import enhancedResolve from 'enhanced-resolve' -import { createPathsMatcher, getTsconfig } from 'get-tsconfig' +import { createPathsMatcher, getTsconfig, parseTsconfig } from 'get-tsconfig' import type { TsConfigResult } from 'get-tsconfig' import type { Version } from 'is-bun-module' import { isBunModule } from 'is-bun-module' @@ -111,10 +111,14 @@ let cachedOptions: InternalResolverOptions | undefined let prevCwd: string let mappersCachedOptions: InternalResolverOptions -let mappers: Array<{ +let mappers: Mapper[] = [] + +type MapperFn = NonNullable> + +interface Mapper { files: Set - mapperFn: NonNullable> -}> = [] + mapperFn: MapperFn +} let resolverCachedOptions: InternalResolverOptions let cachedResolver: Resolver | undefined @@ -386,8 +390,8 @@ function initMappers(options: InternalResolverOptions) { typeof options.project === 'string' ? [options.project] : Array.isArray(options.project) - ? options.project - : [process.cwd()] + ? options.project + : [process.cwd()] ) // 'tinyglobby' pattern must have POSIX separator .map(config => replacePathSeparator(config, path.sep, path.posix.sep)) @@ -411,7 +415,7 @@ function initMappers(options: InternalResolverOptions) { ] mappers = projectPaths - .map(projectPath => { + .flatMap(projectPath => { let tsconfigResult: TsConfigResult | null if (isFile(projectPath)) { @@ -421,65 +425,84 @@ function initMappers(options: InternalResolverOptions) { tsconfigResult = getTsconfig(projectPath) } - if (!tsconfigResult) { - // eslint-disable-next-line unicorn/no-useless-undefined - return undefined - } + return getMapper(tsconfigResult) + }) + .filter(isDefined) - const mapperFn = createPathsMatcher(tsconfigResult) + const processedPaths = new Set() - if (!mapperFn) { - // eslint-disable-next-line unicorn/no-useless-undefined - return undefined - } + function getMapper(tsconfigResult: TsConfigResult | null): Mapper[] { + const list: Mapper[] = [] + + if (!tsconfigResult) { + return list + } - const files = - tsconfigResult.config.files === undefined && - tsconfigResult.config.include === undefined - ? // Include everything if no files or include options - globSync(defaultInclude, { - ignore: [ - ...(tsconfigResult.config.exclude ?? []), - ...defaultIgnore, - ], - absolute: true, - cwd: path.dirname(tsconfigResult.path), - }) - : [ - // https://www.typescriptlang.org/tsconfig/#files - ...(tsconfigResult.config.files !== undefined && - tsconfigResult.config.files.length > 0 - ? tsconfigResult.config.files.map(file => - path.normalize( - path.resolve(path.dirname(tsconfigResult!.path), file), - ), - ) - : []), - // https://www.typescriptlang.org/tsconfig/#include - ...(tsconfigResult.config.include !== undefined && - tsconfigResult.config.include.length > 0 - ? globSync(tsconfigResult.config.include, { - ignore: [ - ...(tsconfigResult.config.exclude ?? []), - ...defaultIgnore, - ], - absolute: true, - }) - : []), - ] - - if (files.length === 0) { - // eslint-disable-next-line unicorn/no-useless-undefined - return undefined + if (tsconfigResult.config.references) { + const references = tsconfigResult.config.references + .map(ref => path.resolve(path.dirname(tsconfigResult.path), ref.path)) + .filter(path => !processedPaths.has(path)) + .map(path => ({ path, config: parseTsconfig(path) })) + + for (const ref of references) { + processedPaths.add(ref.path) + list.push(...getMapper(ref)) } + } - return { + const mapperFn = createPathsMatcher(tsconfigResult) + + if (!mapperFn) { + return list + } + + const files = + tsconfigResult.config.files === undefined && + tsconfigResult.config.include === undefined + ? // Include everything if no files or include options + globSync(defaultInclude, { + ignore: [ + ...(tsconfigResult.config.exclude ?? []), + ...defaultIgnore, + ], + absolute: true, + cwd: path.dirname(tsconfigResult.path), + }) + : [ + // https://www.typescriptlang.org/tsconfig/#files + ...(tsconfigResult.config.files !== undefined && + tsconfigResult.config.files.length > 0 + ? tsconfigResult.config.files.map(file => + path.normalize( + path.resolve(path.dirname(tsconfigResult!.path), file), + ), + ) + : []), + // https://www.typescriptlang.org/tsconfig/#include + ...(tsconfigResult.config.include !== undefined && + tsconfigResult.config.include.length > 0 + ? globSync(tsconfigResult.config.include, { + ignore: [ + ...(tsconfigResult.config.exclude ?? []), + ...defaultIgnore, + ], + absolute: true, + }) + : []), + ] + + if (files.length === 0) { + return list + } + + return [ + ...list, + { files: new Set(files.map(toNativePathSeparator)), mapperFn, - } - }) - .filter(isDefined) - + }, + ] + } mappersCachedOptions = options } From 58b787fe9c6e774fb28edd0596c856fe46af0be0 Mon Sep 17 00:00:00 2001 From: Alex <8125011+alex-kinokon@users.noreply.github.com> Date: Mon, 17 Feb 2025 08:12:43 +0000 Subject: [PATCH 2/4] Update --- src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.ts b/src/index.ts index ea5f40b..5556077 100644 --- a/src/index.ts +++ b/src/index.ts @@ -438,6 +438,8 @@ function initMappers(options: InternalResolverOptions) { return list } + processedPaths.add(tsconfigResult.path) + if (tsconfigResult.config.references) { const references = tsconfigResult.config.references .map(ref => path.resolve(path.dirname(tsconfigResult.path), ref.path)) From 6c2f36c66e1c803a47cb3194509d955e66b29e18 Mon Sep 17 00:00:00 2001 From: Alex <8125011+alex-kinokon@users.noreply.github.com> Date: Mon, 17 Feb 2025 08:13:46 +0000 Subject: [PATCH 3/4] Update --- src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 5556077..796faef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -447,7 +447,6 @@ function initMappers(options: InternalResolverOptions) { .map(path => ({ path, config: parseTsconfig(path) })) for (const ref of references) { - processedPaths.add(ref.path) list.push(...getMapper(ref)) } } From 978f478a86b742c88709b3d299cd7fdddebb3ce9 Mon Sep 17 00:00:00 2001 From: Alex <8125011+alex-kinokon@users.noreply.github.com> Date: Mon, 17 Feb 2025 08:15:06 +0000 Subject: [PATCH 4/4] TDZ fix --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 796faef..97e8b8d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -414,6 +414,8 @@ function initMappers(options: InternalResolverOptions) { ]), ] + const processedPaths = new Set() + mappers = projectPaths .flatMap(projectPath => { let tsconfigResult: TsConfigResult | null @@ -429,8 +431,6 @@ function initMappers(options: InternalResolverOptions) { }) .filter(isDefined) - const processedPaths = new Set() - function getMapper(tsconfigResult: TsConfigResult | null): Mapper[] { const list: Mapper[] = []