Vite plugin #
The Vite plugin is the recommended path for
SvelteKit and Vite projects. It runs analysis at build time and serves the result as 'virtual:svelte-docinfo'. In dev it watches your source
files, plus the other files they import like internal/ modules, and reloads the
page when an edit changes the result.
The plugin keeps a persistent createAnalysisSession, so dev re-analysis reuses parsed ASTs and svelte2tsx output. To write the data to a file instead, use the CLI.
Setup #
Requires Vite 6 or later.
Add the plugin to
vite.config.ts:import {defineConfig} from 'vite'; import {sveltekit} from '@sveltejs/kit/vite'; import svelteDocinfo from 'svelte-docinfo/vite.js'; export default defineConfig({ plugins: [sveltekit(), svelteDocinfo()], });Add TypeScript support in your
app.d.ts:/// <reference types="svelte-docinfo/virtual-svelte-docinfo.js" />Import the virtual module anywhere in your app:
import {modules, diagnostics} from 'virtual:svelte-docinfo'; // or use the default export: import data from 'virtual:svelte-docinfo'; // data.modules and data.diagnostics are the same as the named exportsThe data is AnalyzeResultJson in its wire form (AnalyzeResultJsonWire).
modulesanddiagnosticsare always arrays, but entries inmodulesomit empty arrays andfalseflags. Treat absent as empty, or restore the defaults per module with parseModuleJson fromsvelte-docinfo/types.js, which is light enough for the browser (see compact JSON). See diagnostics fordiagnostics.
If TypeScript reports Cannot find module 'virtual:svelte-docinfo', check the /// <reference> line in app.d.ts.
Options #
All options are optional; the minimal call discovers files from package.json exports, falling back to glob:
svelteDocinfo() Every option, with its default in the comment above it:
import svelteDocinfo from 'svelte-docinfo/vite.js';
svelteDocinfo({
// Project root directory. Default: Vite's resolved config.root.
projectRoot: process.cwd(),
// Glob patterns for file discovery. Forces glob mode under discovery: 'auto'.
// Default: undefined (use exports discovery).
// Each pattern's static base joins sourceOptions.sourcePaths; a pattern
// with no base makes the whole project root source and logs an info line.
include: ['src/**/*.ts', 'src/**/*.svelte'],
// Exclude globs. An array replaces the default
// ['**/*.{test,spec}.{ts,mts,tsx,js,mjs,jsx}', '**/internal/**']; a callback extends
// it. Replaces sourceOptions.exclude when both are set. node_modules and
// dot-directories below a source path are never source.
exclude: (defaults) => [...defaults, '**/*.gen.ts'],
// Discovery strategy: 'auto' | 'exports' | 'glob'. Default: 'auto'.
// 'auto' → exports first, glob fallback
// 'exports' → strict; throws if package.json exports is missing or resolves to no files
// 'glob' → skip exports, use glob patterns
discovery: 'auto',
// Dist directory for exports discovery. Default: 'dist'.
distDir: 'dist',
// Resolve module dependency graph. Default: true.
resolveDependencies: true,
// Partial overrides for default source options (SvelteKit src/lib layout).
// Merged into createSourceOptions(projectRoot, sourceOptions).
sourceOptions: {sourcePaths: ['src/lib']},
// dev re-analysis debounce in ms. Default: 100.
hmrDebounceMs: 100,
// Opt-in check that names are unique across modules, for packages with a
// flat export namespace. Default: undefined (off).
// 'diagnostic' → emit duplicate_declaration diagnostics
// 'warn' → also log them
// 'throw' → also fail the build
onDuplicates: undefined,
// tsconfig path relative to projectRoot (searched upward from it), or false
// to analyze without one. Default: 'tsconfig.json'.
tsconfig: 'tsconfig.json',
// Compiler options merged over the tsconfig's. Default: none.
compilerOptions: undefined,
// The program's root files, in place of the tsconfig's: a list, or a
// function over the tsconfig's roots. Default: the tsconfig's roots.
// See the source files guide for the declarationFileRoots preset and its tradeoff.
rootFileNames: undefined,
}) The plugin runs the same pipeline as analyzeFromFiles: discover,
resolve dependencies, analyze. In dev, imports resolve through Vite's resolveId,
so Vite aliases apply; in builds, the TypeScript default resolver is used (it honors tsconfig paths). hmrDebounceMs only affects the dev watcher.
source files covers how discovery, include, exclude, sourceOptions, and the tsconfig options decide what's
documented.
Dev behavior #
Options are validated when Vite resolves its config, so a bad one (like discovery: 'exports' with include, or a path
outside the project root) fails at startup.
In dev the plugin watches the source files and the files they import, debounces re-analysis by hmrDebounceMs, and re-parses only what changed. Only the files discovery selects
emit modules, as in a build: an edit elsewhere refreshes the analysis without adding a module,
and a newly created file is checked against discovery again.
Any change to the output reloads the page, even one that only shifts a declaration's source
line, instead of letting Svelte HMR update components in place. Importers build state from the
data (like a layout building a library from modules), so re-running the module
alone would leave them stale.