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)

Declarations
#

7 declarations

view source

discoverFromExports
#

exports.ts view source

(options: ExportsDiscoveryOptions): Promise<ExportsDiscoveryResult>

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.

options

discovery configuration

returns

Promise<ExportsDiscoveryResult>

ExportsDiscoveryResult with discovered files and any error diagnostics

ExportEntry
#

exports.ts view source

ExportEntry

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

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 Array<string>

ExportsDiscoveryResult
#

exports.ts view source

ExportsDiscoveryResult

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 Array<SourceFileInfo> | null

diagnostics

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

type Array<Diagnostic>

mapDistToSource
#

exports.ts view source

(distPath: string, condition: string, options: { distDir: string; sourceDir: string; }): string | null

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

Result of reading and parsing package.json exports.

entries

All parsed export entries.

type Array<ExportEntry>

hasExports

Whether the package.json had an exports field.

type boolean

parsePackageExports
#

exports.ts view source

(projectRoot: string): Promise<ParsedExports>

Read and parse the exports field from package.json.

Handles all Node.js export formats: strings, objects with conditions, nested conditions, null exclusions, and wildcard patterns.

projectRoot

absolute path to project root

type string

returns

Promise<ParsedExports>

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

Depends on
#

Imported by
#