build tools #

svelte-docinfo is build-tool agnostic: you hand it files as SourceFileInfo objects, from disk, a build pipeline, or editor buffers. This page covers embedding analysis in a bundler, watcher, or editor tool, beyond the JS API basics.

Apart from discovery in analyzeFromFiles, the analyzer reads the disk only for the tsconfig, for type-checker context you don't supply, and, except in analyze(), for the in-project files your inputs import that you didn't supply (context files).

SourceFileInfo
#

analyze and the session's setFile / setFiles take SourceFileInfo, and discoverSourceFiles returns it:

interface SourceFileInfo { id: string; // absolute path (native ok at boundary) content: string; // file contents dependencies?: ReadonlyArray<string>; // optional: pre-resolved deps }
  • id: absolute path. Native Windows paths are accepted (see Paths).
  • content: required; inputs are never read from disk.
  • dependencies: optional absolute paths. When supplied, the session skips its own import parsing for this file (see Pre-resolved dependencies).

Reverse edges (dependents) are always computed by the analyzer.

discoverSourceFiles (standalone)
#

discoverSourceFiles runs file discovery without analysis, for tools that need the source set up front (watcher registration, counts, pre-flight checks) and analyze separately.

import {discoverSourceFiles, createSourceOptions} from 'svelte-docinfo'; const {files, diagnostics} = await discoverSourceFiles({ sourceOptions: createSourceOptions(process.cwd()), // discovery: 'auto' | 'exports' | 'glob' (default 'auto') // include: ['src/**/*.ts'] (forces glob under 'auto') // distDir: 'dist' (for exports discovery) }); // files: Array<SourceFileInfo> — content already loaded from disk // diagnostics: module_unreadable for any file the exports map names // but readFile failed on (permission denied, FS error)

The strategies are the ones discovery describes.

Unlike analyzeFromFiles and the Vite plugin, standalone discovery doesn't widen sourcePaths to cover include, so files outside src/lib would be dropped at analysis. To keep them, build the options with createSourceOptionsWithInclude (from svelte-docinfo/source-config.js), passing the same include.

Session consumers own the discovery-time module_unreadable diagnostics, since sessions don't run discovery (analyzeFromFiles merges them for you). Their message embeds the filesystem error's absolute path, so pass them through normalizeDiagnosticPaths (from svelte-docinfo/analyze-core.js) before publishing.

Pre-resolved dependencies (fast path)
#

Build tools with their own dependency graph (Gro's filer, Rollup's bundle graph, webpack's module graph) can hand it over in SourceFileInfo.dependencies as absolute paths. The session then skips import parsing and resolution for that file.

import type {SourceFileInfo} from 'svelte-docinfo'; // inside your build-tool integration const files: Array<SourceFileInfo> = [...filer.modules].map(([id, mod]) => ({ id, content: mod.content, dependencies: [...mod.dependencies.keys()], // absolute paths })); await session.setFiles(files);

A file re-ingested with the same content and an element-wise equal dependencies array is a cache hit, so building a fresh array per call is fine. See Cache-hit semantics.

Trust contract
#

Pre-resolved dependencies are trusted: the session doesn't check them against content. A buggy upstream resolver goes unnoticed:

  • Edges in dependencies but absent from content are accepted.
  • Imports in content but missing from dependencies are omitted.

Neither case emits a diagnostic, because legitimate sequences across batches (declaring a dependency on a file that is set later, or later deleted) would be flagged too. The default lex+resolve path always follows the actual imports, so use it if you don't fully trust your dependency source.

Type-only edges
#

With pre-resolved dependencies, you decide whether import type {X} from './x' counts as a dependency. Two common stances:

  • Keep type-only edges: what the default lex+resolve path does, for import type, inline type specifiers, and type-only re-exports like export type {X} from.
  • Drop type-only edges: what Gro's filer does, so the output reflects the runtime graph only.

Switching from lex+resolve to Gro's graph therefore removes type-only edges from ModuleJson.dependencies and dependents.

Custom import resolution
#

Without pre-resolved dependencies, the session parses import specifiers from content and resolves them with TypeScript module resolution and your tsconfig. To match the rest of your build instead, pass an ImportResolver as resolveImport; see the session guide.

Paths
#

Native paths are accepted at the API boundary, so Windows callers can pass C:\repo\src\lib\foo.ts. Every path in the output, and the session's list(), is POSIX form (forward slashes). Drive-letter case and \\?\ prefixes aren't normalized.