import { type PathLike } from '../utils/path.ts'; import { type Cache } from './Cache.ts'; import type { FileSystem } from './FileSystem.ts'; import { type RepositoryInput } from './Repository.ts'; import { Directory, JavaScriptFile, type DirectoryOptions, type DirectoryStructure, type FileSystemEntry, type FileStructure, type PackageStructure, type WithDefaultTypes } from './entries.ts'; import type { StructureOptions } from './types.ts'; type ModuleRuntimeResult = Value | Promise | (() => Value | Promise); type ModuleExports = { [exportName: string]: Value; }; /** A runtime loader for a specific package export (no path/file arguments). */ type PackageExportLoader> = (path: string) => ModuleRuntimeResult; /** Shape of the `loader` map accepted by `Package`. */ type PackageExportLoaderMap = Record>>; type UnwrapModuleRuntimeResult = T extends () => infer R ? Awaited : Awaited; type InferPackageExportModule = Fn extends (...args: any[]) => infer Return ? UnwrapModuleRuntimeResult extends ModuleExports ? UnwrapModuleRuntimeResult : ModuleExports : ModuleExports; export interface PackageExportOptions = Record, LoaderTypes extends WithDefaultTypes = WithDefaultTypes> extends Omit, 'path' | 'fileSystem'> { path?: PathLike; } export interface PackageOptions = Record, LoaderTypes extends WithDefaultTypes = WithDefaultTypes, ExportLoaders extends PackageExportLoaderMap = {}> { name?: string; path?: PathLike; directory?: PathLike | Directory; sourcePath?: PathLike | null; fileSystem?: FileSystem; exports?: Record>; /** Explicit repository context created with `Repository.local(...)` or `Repository.remote(...)`. */ repository?: RepositoryInput; /** * Optional runtime loaders for individual package exports or a resolver that * will be invoked with the export path (e.g. "remark/add-sections"). */ loader?: ExportLoaders | PackageExportLoader>; /** Optional cache provider for package-level caches. */ cache?: Cache; } type PackageEntrySource = 'manifest' | 'override'; export interface PackageExportDirectory = Record, LoaderTypes extends WithDefaultTypes = WithDefaultTypes> extends Directory { getExportPath(): string; getSource(): PackageEntrySource; isPattern(): boolean; } export declare class PackageExportDirectory = Record, LoaderTypes extends WithDefaultTypes = WithDefaultTypes> extends Directory { #private; constructor(exportPath: string, options: DirectoryOptions, source: PackageEntrySource, isPattern: boolean); } /** How the source file(s) were resolved for an export. */ export type ExportSourceResolveKind = 'override' | 'declarationMap' | 'sourceMap' | 'heuristic' | 'typesField' | 'legacyField' | 'unresolved' | 'unsupportedPattern'; /** Result of resolving an export to its source file(s). */ export interface ResolvedExportSource { /** Export subpath key: ".", "./foo", "./components/Button" */ exportKey: string; /** Built file we started from, relative to package root */ builtTarget?: string; /** Resolved source files (relative to package root) */ sources: string[]; /** How we got these sources */ kind: ExportSourceResolveKind; /** Optional extra notes (e.g. no maps, no candidates, etc.) */ reason?: string; } /** Options for resolving export sources. */ export interface ResolveExportSourcesOptions { /** * Manual mapping when inference is impossible or the package doesn't publish maps/sources. * Keys are export keys ('.', './foo', './components/Button'). * Values are paths relative to package root (string or list of strings). */ overrides?: Record; /** * Which conditional export targets to prefer when choosing a built file from `exports`. * You can provide a fixed list, or a function per exportKey. * * Examples: * ['types', 'import', 'default', 'require'] * (exportKey) => exportKey === '.' ? ['types', 'default'] : ['import', 'default'] */ conditions?: string[] | ((exportKey: string) => string[]); /** * Candidate source roots for heuristic mapping. * Defaults to ['src', 'lib', 'source']. */ sourceRoots?: string[]; /** * Optional rule-based rewrites for common build layouts. * These are applied to the built target *before* heuristic guessing. * * Examples: * { from: /^dist\/esm\//, to: '' } * { from: 'dist/', to: '' } */ rewrites?: Array<{ from: string | RegExp; to: string; }>; } export declare class Package = Record, LoaderTypes extends WithDefaultTypes = WithDefaultTypes, ExportLoaders extends PackageExportLoaderMap = {}> { #private; constructor(options: PackageOptions); get name(): string | undefined; getExports(): PackageExportDirectory[]; /** * Resolve package exports to their original source files. * * This method attempts to reverse-engineer built files back to their source * files using the following strategies (in order): * * 1. **Manual overrides** - User-provided mappings * 2. **Declaration maps** - `.d.ts.map` files pointing to original TS/TSX * 3. **Source maps** - `.js.map` / `.mjs.map` files pointing to sources * 4. **Heuristics** - Pattern-based guessing (optionally aided by `rewrites`) */ resolveExportSources(config?: ResolveExportSourcesOptions): ResolvedExportSource[]; /** Resolve a single export key to its source file(s). */ resolveExportSource( /** The export key to resolve (e.g., ".", "./utils", "./components/Button"). */ exportKey: string, /** Options for source resolution. */ options?: ResolveExportSourcesOptions): ResolvedExportSource | undefined; /** * Get the source file path for the main export ("."). * * This is a convenience method that returns the first source file * for the main package export. */ getMainSourcePath(options?: ResolveExportSourcesOptions): string | undefined; getStructure(options?: StructureOptions): Promise>; getExport(exportSpecifier: Key, extension?: string | string[]): Promise, LoaderTypes>>; getExport>(exportSpecifier: string, extension?: string | string[]): Promise>; getExport(exportSpecifier: string, extension?: string | string[]): Promise>; getImports(): PackageImportEntry[]; /** Get a single import entry by its specifier (e.g. "#internal/*"). */ getImport(importSpecifier: string): PackageImportEntry | undefined; } export declare class PackageImportEntry { #private; constructor(key: string, isPattern: boolean, source: PackageEntrySource); getImportPath(): string; getSource(): PackageEntrySource; isPattern(): boolean; } export {};