From b0d8ecb9b862412e93f73b172cc0692259ce01b8 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:06:16 -0500 Subject: [PATCH] fix: once an importer is matched, end directory traversal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/index.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 550f575..c1eed27 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,8 @@ 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 @@ -19,7 +20,7 @@ type Resolver = ( viteResolve: ViteResolve, id: string, importer: string -) => Promise<[resolved: string | undefined, matched: boolean]> +) => Promise export type { PluginOptions } @@ -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. @@ -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) {