Skip to content

Commit

Permalink
fix: once an importer is matched, end directory traversal
Browse files Browse the repository at this point in the history
The plugin starts with the importer‘s immediate parent directory, then uses `path.dirname` to continue up the ancestry until a matching tsconfig is found. A match is determined by the `include` and `exclude` arrays of a tsconfig. Currently, the `files` array is mostly ignored.

Anyway, the bug was that we were returning `[undefined, false]` in cases where the tsconfig matched, which was incorrect. The right behavior is to return `[undefined, true]` to indicate the search for a matching tsconfig can end early.

This bug affected both correctness and performance, although the latter was possibly negligible.
  • Loading branch information
aleclarson committed Nov 19, 2024
1 parent 7160d6e commit b0d8ecb
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import { basename, dirname, isAbsolute, join, relative } from './path'
import { PluginOptions } from './types'
import { debug, debugResolve } from './debug'

const noMatch = [undefined, false] as [undefined, false]
const notApplicable = [undefined, false] as const
const notFound = [undefined, true] as const

type ViteResolve = (id: string, importer: string) => Promise<string | undefined>

type Resolver = (
viteResolve: ViteResolve,
id: string,
importer: string
) => Promise<[resolved: string | undefined, matched: boolean]>
) => Promise<readonly [resolved: string | undefined, matched: boolean]>

export type { PluginOptions }

Expand Down Expand Up @@ -332,14 +333,14 @@ export default (opts: PluginOptions = {}): Plugin => {
// Ignore importers with unsupported extensions.
if (!importerExtRE.test(importerFile)) {
debugResolve('importer has unsupported extension. skipping...')
return noMatch
return notApplicable
}

// Respect the include/exclude properties.
const relativeImporterFile = relative(configDir, importerFile)
if (!isIncludedRelative(relativeImporterFile)) {
debugResolve('importer is not included. skipping...')
return noMatch
return notApplicable
}

// Find and remove Vite's suffix (e.g. "?url") if present.
Expand All @@ -353,7 +354,7 @@ export default (opts: PluginOptions = {}): Plugin => {
if (!resolvedId) {
resolvedId = await resolveId(viteResolve, id, importer)
if (!resolvedId) {
return noMatch
return notFound
}
resolutionCache.set(id, resolvedId)
if (debugResolve.enabled) {
Expand Down

0 comments on commit b0d8ecb

Please sign in to comment.