exports.ts

Package.json exports field discovery for source files.

Maps dist paths in package.json exports to source file paths, enabling zero-config file discovery without glob patterns.

@see source.ts for SourceFileInfo @see analyze.ts for analyzeFromFiles (primary consumer)

view source

Declarations
#

8 declarations

createBlockedSpecifierChecker
#

exports.ts view source

(parsed: ParsedExports): ((specifier: string) => boolean) | null import {createBlockedSpecifierChecker} from 'svelte-docinfo/exports.js';

Build a predicate deciding whether an export specifier is blocked, per Node's resolution semantics: an exact (starless) key wins outright, else the best-matching wildcard key (comparePatternKeys) decides — and when that winner's target is null, the subpath is not exported.

Blocking is only observable when blocked keys exist, so this returns null for the common no-blocked-keys case and callers skip specifier computation entirely.

Exported for consumers reading ParsedExports.blocked directly — this is the one implementation of the interpretation rule.

parsed

returns

((specifier: string) => boolean) | null

discoverFromExports
#

exports.ts view source

(options: ExportsDiscoveryOptions): Promise<ExportsDiscoveryResult> import {discoverFromExports} from 'svelte-docinfo/exports.js';

Discover source files using package.json exports field.

Reads package.json, parses exports, maps dist paths to source paths, expands wildcard patterns, and loads file content.

Returns {files: null} when no package.json or no exports field exists, signaling the caller to fall back to glob discovery. Returns {files: []} when exports exist but resolve no source files (likely misconfigured mapping).

For concrete exports, maps directly to source paths and verifies existence. For wildcard exports, globs the source directory for matching files.

Null-target keys are honored with Node's resolution semantics: a subpath whose most-specific matching key is null is not exported, so its file is not discovered — "./internal/*": null beside the usual "./*.js" wildcards keeps src/lib/internal/ out of discovery exactly as it keeps the subpaths unresolvable for consumers (the src/lib/internal/ convention's exports half).

options

discovery configuration

returns

Promise<ExportsDiscoveryResult>

ExportsDiscoveryResult with discovered files and any error diagnostics

ExportEntry
#

exports.ts view source

ExportEntry import type {ExportEntry} from 'svelte-docinfo/exports.js';

A parsed entry from package.json exports field.

specifier

The export specifier (e.g., ".", "./*.js").

type string

isPattern

Whether the specifier contains a wildcard (*).

type boolean

conditions

Resolved dist paths by condition (e.g., {types: "./dist/index.d.ts", default: "./dist/index.js"}).

type Record<string, string>

ExportsDiscoveryOptions
#

exports.ts view source

ExportsDiscoveryOptions import type {ExportsDiscoveryOptions} from 'svelte-docinfo/exports.js';

Options for discoverFromExports.

projectRoot

Absolute path to project root.

type string

distDir?

Dist directory name relative to projectRoot. Default: 'dist'.

type string

sourceDir?

Source directory name relative to projectRoot. Default: 'src/lib'.

type string

exclude?

Glob patterns to exclude from discovered files.

type string[]

ExportsDiscoveryResult
#

exports.ts view source

ExportsDiscoveryResult import type {ExportsDiscoveryResult} from 'svelte-docinfo/exports.js';

Result of discovering source files from package.json exports.

Self-contained: includes both the discovered files and any error diagnostics (e.g., files that exist but could not be read).

files

Discovered source files, or null if no exports field found. Empty array means exports field exists but resolved no source files (likely a misconfigured dist-to-source mapping).

type SourceFileInfo[] | null

diagnostics

Error diagnostics for files that exist but could not be read.

type ({ symbolName: string; file: string; message: string; severity: "error" | "warning"; kind: "type_extraction_failed"; line?: number | undefined; column?: number | undefined; } | { functionName: string; ... 5 more ...; column?: number | undefined; } | ... 13 more ... | { ...; })[]

mapDistToSource
#

exports.ts view source

(distPath: string, condition: string, options: { distDir: string; sourceDir: string; }): string | null import {mapDistToSource} from 'svelte-docinfo/exports.js';

Map a dist file path to its source file path.

Replaces the dist directory prefix with the source directory and maps file extensions based on the export condition.

distPath

the dist path from exports (e.g., "./dist/index.js")

type string

condition

the export condition (e.g., "default", "svelte", "types")

type string

options

mapping configuration

type { distDir: string; sourceDir: string; }

returns

string | null

source path relative to project root, or null if not mappable

ParsedExports
#

exports.ts view source

ParsedExports import type {ParsedExports} from 'svelte-docinfo/exports.js';

Result of reading and parsing package.json exports.

entries

All parsed export entries.

type ExportEntry[]

blocked

Specifiers (exact or wildcard patterns) whose export target resolves nothing — a literal null, or a conditions object with no usable target. Node's explicit-exclusion form: "./internal/*": null blocks the subpaths a broader wildcard would otherwise expose. Discovery honors these with Node's best-match semantics: a subpath whose most-specific matching key is blocked is not exported, so its source file is not discovered.

Interpret via createBlockedSpecifierChecker — a naive membership check (blocked.includes(specifier)) is wrong for wildcard keys and ignores the positive keys that can out-match a blocked one.

type string[]

hasExports

Whether the package.json had an exports field.

type boolean

parsePackageExports
#

exports.ts view source

(projectRoot: string): Promise<ParsedExports> import {parsePackageExports} from 'svelte-docinfo/exports.js';

Read and parse the exports field from package.json.

Handles all Node.js export formats: strings, objects with conditions, nested conditions, fallback arrays (first usable element), null exclusions (surfaced on blocked for best-match blocking during discovery), and wildcard patterns.

projectRoot

absolute path to project root

type string

returns

Promise<ParsedExports>

parsed ParsedExports, or {entries: [], blocked: [], hasExports: false} if no exports field

Depends on
#

Imported by
#