From eea1c17d0fe4ad6e3f8bc324216bc09173e3c778 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Fri, 6 Dec 2024 11:06:43 -0500 Subject: [PATCH] fix: correctly infer the `paths` root dir Closes #150 --- src/index.ts | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index c1eed27..bf74656 100644 --- a/src/index.ts +++ b/src/index.ts @@ -211,14 +211,27 @@ export default (opts: PluginOptions = {}): Plugin => { }, } + type TsConfig = { + files?: string[] + include?: string[] + exclude?: string[] + compilerOptions?: CompilerOptions + } + + function resolvePathsRootDir(project: TSConfckParseResult): string { + const baseUrl = (project.tsconfig as TsConfig).compilerOptions?.baseUrl + if (baseUrl) { + return baseUrl + } + const projectWithPaths = project.extended?.find( + (p) => (p.tsconfig as TsConfig).compilerOptions?.paths + ) + return dirname((projectWithPaths ?? project).tsconfigFile) + } + function createResolver(project: TSConfckParseResult): Resolver | null { const configPath = normalizePath(project.tsconfigFile) - const config = project.tsconfig as { - files?: string[] - include?: string[] - exclude?: string[] - compilerOptions?: CompilerOptions - } + const config = project.tsconfig as TsConfig debug('config loaded:', inspect({ configPath, config }, false, 10, true)) @@ -256,10 +269,9 @@ export default (opts: PluginOptions = {}): Plugin => { let resolveId: InternalResolver if (paths) { - const pathMappings = resolvePathMappings( - paths, - baseUrl ?? dirname(configPath) - ) + const pathsRootDir = resolvePathsRootDir(project) + const pathMappings = resolvePathMappings(paths, pathsRootDir) + const resolveWithPaths: InternalResolver = async ( viteResolve, id,