Skip to content

Commit

Permalink
fix: correctly infer the paths root dir
Browse files Browse the repository at this point in the history
Closes #150
  • Loading branch information
aleclarson committed Dec 6, 2024
1 parent 34cb651 commit eea1c17
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit eea1c17

Please sign in to comment.