dep-resolver.ts

Import resolver primitives for the analysis session.

Exposes the ImportResolver token contract ({resolve, identity, invalidate?}) plus building blocks the session uses inside its three-phase setFiles pipeline:

  • lexImports — sync specifier extraction from already-prepared content (raw TS/JS, or svelte2tsx virtual content for .svelte files).
  • createDefaultResolver — the TS + tsconfig fallback used when neither AnalysisSessionOptions.resolveImport nor a per-call override is provided. Carries a fresh symbol identity per session (cache-key-safe) and an invalidate that clears its module-resolution cache.
  • normalizeResolveImport — coerces the public ResolveImport union (bare function or token-paired resolver) to an ImportResolver at each boundary.
  • wrapResolveImport — the fn→token primitive behind normalizeResolveImport: wraps a bare function in an ImportResolver, synthesizing a throwaway identity unless a stable one is passed.

ImportResolver, ResolveImport, ResolveImportFn, and normalizeResolveImport are on the main barrel; the lexing and default-resolver building blocks are subpath-only and not stable API.

see also

  • ``session.ts`` for the three-phase ingestion pipeline that consumes these

  • ``loadTsconfig`` in typescript-program.ts for producing the merged compiler options the default resolver consumes (the session reuses its LS's parse via getCompilerOptions() rather than calling it again)

view source

Declarations
#

9 declarations

createDefaultResolver
#

dep-resolver.ts view source

(compilerOptions: CompilerOptions, projectRoot: string, host?: ModuleResolutionHost): ImportResolver import {createDefaultResolver} from 'svelte-docinfo/dep-resolver.js';

Create the default ImportResolver (TypeScript + tsconfig).

Uses ts.resolveModuleName against host (default ts.sys) — no ts.Program is built. Identity is a fresh symbol per call, so each session that constructs its own default gets a unique cache scope. Multiple sessions sharing one resolver instance share the cache scope (correct, since resolver state is shared too).

ts.resolveModuleName cannot resolve real .svelte files (no compiler option teaches it the extension), so relative and absolute .svelte specifiers — with or without the extension written — fall back to manual resolution against fromFile through the same host. Non-relative .svelte specifiers (tsconfig paths aliases, package subpaths) stay unresolved; supply a custom resolveImport for those setups.

The resolution cache lives for the resolver's lifetime and caches failed lookups alongside successful ones, so invalidate clears it — the session calls that whenever its owned set's membership changed (see ImportResolver).

compilerOptions

parsed tsconfig (from loadTsconfig, or the LS handle's getCompilerOptions())

type CompilerOptions

projectRoot

absolute project root for the module-resolution cache

type string

host?

resolution surface; the session passes an owned-content-aware host so dependency edges see the same world the checker does (in-memory files, including ones in directories that exist nowhere on disk), disk-only callers take the ts.sys default

type ModuleResolutionHost
optional default ts.sys

returns

ImportResolver

ImportResolver
#

dep-resolver.ts view source

also exported from index.ts

ImportResolver import type {ImportResolver} from 'svelte-docinfo/dep-resolver.js';

Token-paired import resolver.

identity is a stable opaque token that keys the session's resolve cache alongside content. Cache hits require both (a) byte-for-byte identical source content and (b) identity equality. Naive function-reference keys would silently destroy cache reuse when callers wrap the resolver in fresh closures (a very common pattern in Vite/Rollup plugins) — opaque tokens lift the responsibility to the caller, where it can be done correctly.

resolve

Resolve an import specifier to an absolute file path, or null.

type (specifier: string, fromFile: string): string | Promise<string | null> | null

specifier

type string

fromFile

type string
returns string | Promise<string | null> | null

identity

Stable opaque token identifying this resolver's cache scope.

Two ImportResolvers with identity === to each other are treated as cache-equivalent. string for human-readable identities (e.g., 'vite-plugin-container'); symbol for generated sentinels.

type string | symbol

invalidate?

Drop any cached resolution state. Optional — implement it when the resolver caches results across calls, and especially when it caches *failed* lookups, which go stale the moment a previously-missing file appears.

The session calls this at the start of a batch whenever its owned file set gained or lost a path since this resolver last ran, so a dep ingested after an importer already failed to resolve it still produces an edge. Content-only changes never trigger it (they can't change what exists). Files appearing on *disk* without being ingested are invisible to the session — a resolver caching disk misses owns that invalidation itself.

type (): void

returns void

isNodeBuiltin
#

dep-resolver.ts view source

(specifier: string): boolean import {isNodeBuiltin} from 'svelte-docinfo/dep-resolver.js';

Whether a lexed specifier names a Node builtin (fs, node:fs/promises, …).

Builtins can never be a project source file, so resolving them is pointless — and routing them through a host resolver (Vite/Rollup) makes that host emit "externalized for browser compatibility" warnings for browser-targeted configs. Callers skip resolution for these and treat them as unresolved (null), which the downstream isSource filter would do anyway.

specifier

type string

returns

boolean

lexImports
#

dep-resolver.ts view source

(content: string, path?: string | undefined): string[] import {lexImports} from 'svelte-docinfo/dep-resolver.js';

Lex import specifiers from prepared content.

Parses with TypeScript (ts.createSourceFile, no binding or checking) rather than lexing as JS, so TypeScript syntax a JS lexer misreads as a regex or comparison (a! / b, Promise<Result<A, B>> => {}) can't derail it, and every ES form is seen, including export * as ns from (which TypeScript's lighter preProcessFile scanner misses).

Collects, in source order: static imports and re-exports (type-only ones included: import type, inline type specifiers, `export type {...} from, export type * from), import x = require()`, module augmentations (declare module '…' in a module), and anywhere in the file, dynamic import() with a string literal and import('…') types. CommonJS require() calls and non-literal import() are omitted.

For .svelte files, pass the svelte2tsx-transformed virtual content, not the raw .svelte source.

content

prepared file content (TS/JS, or svelte2tsx virtual)

type string

path?

the file's path, when its extension should pick the parse: .tsx and .jsx content parses as TSX, since the TS parse reads JSX markup as type assertions and can lose the imports inside it; any other path, or none, parses as TS

type string
optional

returns

string[]

specifiers in source order

noDepsResolver
#

dep-resolver.ts view source

ImportResolver import {noDepsResolver} from 'svelte-docinfo/dep-resolver.js';

Shared no-op ImportResolver for the "dependency resolution disabled" case.

Stable string identity ('no-deps') instead of a per-call symbol so that repeated ingests within a long-lived session cache-hit on identity. Each session still owns its own cache (entries live on the per-session owned map; sessions don't share state), but within one session every reference to noDepsResolver is === to every other — so a Vite re-save of byte-identical content with resolveDependencies: false exercises the cache-hit branch. The resolver always returns null, so any two calls with identical content under this identity produce identical resolution results — string identity correctly captures that. Used by both the one-shot analyzeFromFiles path and the long-lived Vite plugin (resolveDependencies: false).

normalizeResolveImport
#

dep-resolver.ts view source

also exported from index.ts

(value: ResolveImport | undefined): ImportResolver | undefined import {normalizeResolveImport} from 'svelte-docinfo/dep-resolver.js';

Normalize the public ResolveImport union to an ImportResolver.

Wraps a bare function via wrapResolveImport (fresh synthesized identity); passes a token-paired resolver through unchanged. undefined in, undefined out — callers fall back to the session default. Call once per logical resolver scope: at session construction for the default, per call for an override.

value

type ResolveImport | undefined

returns

ImportResolver | undefined

ResolveImport
#

dep-resolver.ts view source

also exported from index.ts

ResolveImport

type (ResolveImportFn) | ImportResolver

import type {ResolveImport} from 'svelte-docinfo/dep-resolver.js';

Public resolver shape accepted across the API — a bare ResolveImportFn or the token-paired ImportResolver.

Every entry point that accepts resolveImport (analyze, analyzeFromFiles, createAnalysisSession, and the session's setFile/setFiles per-call override) takes this union, so the same value copy-pastes between them. Pass:

  • a bare function for the common case — a fresh cache identity is synthesized at the boundary. Correct for one-shot use (analyze, analyzeFromFiles) and for a session default (wrapped once at construction, so identity is stable for the session's lifetime).
  • an ImportResolver with a stable identity when you need the session to reuse its resolve cache across calls where the same logical resolver is rebuilt as a fresh closure (Vite/Rollup plugins). A bare function handed to a *per-call* setFile/setFiles override is treated as a distinct resolver each call (fresh identity → touched files re-resolve), which is the expected behavior for a deliberate one-off override.

ResolveImportFn
#

dep-resolver.ts view source

also exported from index.ts

ResolveImportFn import type {ResolveImportFn} from 'svelte-docinfo/dep-resolver.js';

Bare import-resolver function — the convenience form.

Resolve an import specifier to an absolute file path, or null if the specifier is unresolvable (external package, missing file, etc.). May return synchronously or asynchronously; sync returns are awaited harmlessly in the session's parallel resolve phase.

(call)

type (specifier: string, fromFile: string): string | Promise<string | null> | null

specifier

type string

fromFile

type string
returns string | Promise<string | null> | null

wrapResolveImport
#

dep-resolver.ts view source

(resolveImport: ResolveImportFn, identity?: string | symbol): ImportResolver import {wrapResolveImport} from 'svelte-docinfo/dep-resolver.js';

Wrap a bare resolveImport function into an ImportResolver token.

identity is optional; when omitted, a fresh Symbol('wrapped') is synthesized per call. The synthesized default suits single-use scopes — normalizeResolveImport relies on it to wrap a bare function for a one-shot analyze call or for a session default (wrapped once at construction, so the identity stays stable for the session's lifetime).

For a long-lived session that re-wraps the *same* logical resolver across calls (Vite plugin, LSP), a throwaway identity cache-misses every time — the fresh Symbol('wrapped') never compares equal. Pass a stable identity here, or — simpler — construct an ImportResolver ({resolve, identity}) directly and pass it through the ResolveImport union.

resolveImport

identity?

type string | symbol
optional default Symbol('wrapped')

returns

ImportResolver

Depends on
#

Imported by
#