|
| 1 | +/** |
| 2 | + * Asynchronously resolve a module path, like `require.resolve()`, on behalf of files. |
| 3 | + * |
| 4 | + * @param id - The module identifier to resolve. |
| 5 | + * @param options - Resolution options. |
| 6 | + * @param callback - Called with `(err, resolved, pkg)` when resolution completes. |
| 7 | + */ |
| 8 | +declare function resolve(id: string, callback: resolve.Callback): void; |
| 9 | +declare function resolve(id: string, options: resolve.AsyncOptions, callback: resolve.Callback): void; |
| 10 | + |
| 11 | +declare namespace resolve { |
| 12 | + /** Asynchronously resolve a module path, like `require.resolve()`, on behalf of files. */ |
| 13 | + function async(id: string, callback: Callback): void; |
| 14 | + function async(id: string, options: AsyncOptions, callback: Callback): void; |
| 15 | + |
| 16 | + /** |
| 17 | + * Synchronously resolve a module path, like `require.resolve()`, on behalf of files. |
| 18 | + * |
| 19 | + * @param id - The module identifier to resolve. |
| 20 | + * @param options - Resolution options. |
| 21 | + * @returns The resolved file path. |
| 22 | + * @throws If the module cannot be found. |
| 23 | + */ |
| 24 | + function sync(id: string, options?: SyncOptions): string; |
| 25 | + |
| 26 | + /** A parsed package.json object. */ |
| 27 | + interface PackageJSON { |
| 28 | + [key: string]: unknown; |
| 29 | + name?: string; |
| 30 | + main?: string; |
| 31 | + exports?: unknown; |
| 32 | + engines?: { node?: string }; |
| 33 | + } |
| 34 | + |
| 35 | + /** Callback for asynchronous resolution. */ |
| 36 | + type Callback = (err: Error | null, resolved?: string, pkg?: PackageJSON) => void; |
| 37 | + |
| 38 | + /** Options shared by both async and sync resolution. */ |
| 39 | + interface BaseOptions { |
| 40 | + /** Directory to resolve from. Defaults to `__dirname` of the calling file. */ |
| 41 | + basedir?: string; |
| 42 | + /** The `package.json` object associated with the calling module. */ |
| 43 | + package?: PackageJSON; |
| 44 | + /** File extensions to search, in order. Defaults to `['.js']`. */ |
| 45 | + extensions?: ReadonlyArray<string>; |
| 46 | + /** Whether to include core modules (e.g. `fs`, `path`) in results. Defaults to `true`. */ |
| 47 | + includeCoreModules?: boolean; |
| 48 | + /** Whether to preserve symlinks instead of resolving them. Defaults to `false`. */ |
| 49 | + preserveSymlinks?: boolean; |
| 50 | + /** Additional lookup paths. */ |
| 51 | + paths?: ReadonlyArray<string> | ((request: string, start: string, getNodeModulesDirs: () => string[], opts: BaseOptions) => string[]); |
| 52 | + /** The filename used for error messages and as a fallback for basedir. */ |
| 53 | + filename?: string; |
| 54 | + /** The directory name(s) to use for node_modules lookups. Defaults to `['node_modules']`. */ |
| 55 | + moduleDirectory?: string | ReadonlyArray<string>; |
| 56 | + /** |
| 57 | + * Transform a package.json object before its `main` field is used. |
| 58 | + * |
| 59 | + * @param pkg - The parsed package.json. |
| 60 | + * @param pkgFile - The path to the package.json file. |
| 61 | + * @param dir - The directory containing the package.json. |
| 62 | + * @returns The (possibly modified) package.json object. |
| 63 | + */ |
| 64 | + packageFilter?: (pkg: PackageJSON, pkgFile: string, dir: string) => PackageJSON; |
| 65 | + /** |
| 66 | + * Transform a resolved path before it is used. |
| 67 | + * |
| 68 | + * @param pkg - The parsed package.json. |
| 69 | + * @param path - The resolved path. |
| 70 | + * @param relativePath - The path relative to the package directory. |
| 71 | + * @returns An alternative path, or undefined/falsy to use the original. |
| 72 | + */ |
| 73 | + pathFilter?: (pkg: PackageJSON, path: string, relativePath: string) => string | undefined; |
| 74 | + /** |
| 75 | + * Override the default node_modules candidate iterator. |
| 76 | + * |
| 77 | + * @param request - The module being resolved. |
| 78 | + * @param start - The starting directory. |
| 79 | + * @param thunk - A function returning the default candidate directories. |
| 80 | + * @param opts - The resolve options. |
| 81 | + * @returns An array of candidate directories. |
| 82 | + */ |
| 83 | + packageIterator?: (request: string, start: string, thunk: () => string[], opts: BaseOptions) => string[]; |
| 84 | + /** |
| 85 | + * An exports category string, as defined by `node-exports-info`. |
| 86 | + * Mutually exclusive with `engines`. |
| 87 | + */ |
| 88 | + exportsCategory?: string; |
| 89 | + /** |
| 90 | + * When `true`, reads the consumer's `engines.node` to determine the exports category. |
| 91 | + * When a string, it is treated as a semver range for engines.node. |
| 92 | + * Mutually exclusive with `exportsCategory`. |
| 93 | + */ |
| 94 | + engines?: boolean | string; |
| 95 | + /** Custom conditions for package.json `exports` resolution. */ |
| 96 | + conditions?: ReadonlyArray<string>; |
| 97 | + } |
| 98 | + |
| 99 | + /** Options for asynchronous resolution. */ |
| 100 | + interface AsyncOptions extends BaseOptions { |
| 101 | + /** |
| 102 | + * Check whether a path is a file. |
| 103 | + * |
| 104 | + * @param file - The path to check. |
| 105 | + * @param cb - Called with `(err, isFile)`. |
| 106 | + */ |
| 107 | + isFile?: (file: string, cb: (err: Error | null, isFile?: boolean) => void) => void; |
| 108 | + /** |
| 109 | + * Check whether a path is a directory. |
| 110 | + * |
| 111 | + * @param dir - The path to check. |
| 112 | + * @param cb - Called with `(err, isDirectory)`. |
| 113 | + */ |
| 114 | + isDirectory?: (dir: string, cb: (err: Error | null, isDirectory?: boolean) => void) => void; |
| 115 | + /** |
| 116 | + * Resolve a path's real location, following symlinks. |
| 117 | + * |
| 118 | + * @param file - The path to resolve. |
| 119 | + * @param cb - Called with `(err, realPath)`. |
| 120 | + */ |
| 121 | + realpath?: (file: string, cb: (err: Error | null, realPath?: string) => void) => void; |
| 122 | + /** |
| 123 | + * Read a file's contents. |
| 124 | + * Mutually exclusive with `readPackage`. |
| 125 | + * |
| 126 | + * @param file - The path to read. |
| 127 | + * @param cb - Called with `(err, contents)`. |
| 128 | + */ |
| 129 | + readFile?: (file: string, cb: (err: Error | null, contents?: string | Buffer) => void) => void; |
| 130 | + /** |
| 131 | + * Read and parse a package.json file. |
| 132 | + * Mutually exclusive with `readFile`. |
| 133 | + * |
| 134 | + * @param readFile - The file-reading function. |
| 135 | + * @param pkgFile - The path to the package.json. |
| 136 | + * @param cb - Called with `(err, pkg)`. |
| 137 | + */ |
| 138 | + readPackage?: (readFile: (file: string, cb: (err: Error | null, contents?: string | Buffer) => void) => void, pkgFile: string, cb: (err: Error | null, pkg?: PackageJSON) => void) => void; |
| 139 | + } |
| 140 | + |
| 141 | + /** Options for synchronous resolution. */ |
| 142 | + interface SyncOptions extends BaseOptions { |
| 143 | + /** |
| 144 | + * Check whether a path is a file. |
| 145 | + * |
| 146 | + * @param file - The path to check. |
| 147 | + * @returns `true` if the path is a file. |
| 148 | + */ |
| 149 | + isFile?: (file: string) => boolean; |
| 150 | + /** |
| 151 | + * Check whether a path is a directory. |
| 152 | + * |
| 153 | + * @param dir - The path to check. |
| 154 | + * @returns `true` if the path is a directory. |
| 155 | + */ |
| 156 | + isDirectory?: (dir: string) => boolean; |
| 157 | + /** |
| 158 | + * Resolve a path's real location, following symlinks. |
| 159 | + * |
| 160 | + * @param file - The path to resolve. |
| 161 | + * @returns The resolved real path. |
| 162 | + */ |
| 163 | + realpathSync?: (file: string) => string; |
| 164 | + /** |
| 165 | + * Read a file's contents synchronously. |
| 166 | + * Mutually exclusive with `readPackageSync`. |
| 167 | + * |
| 168 | + * @param file - The path to read. |
| 169 | + * @returns The file contents. |
| 170 | + */ |
| 171 | + readFileSync?: (file: string) => string | Buffer; |
| 172 | + /** |
| 173 | + * Read and parse a package.json file synchronously. |
| 174 | + * Mutually exclusive with `readFileSync`. |
| 175 | + * |
| 176 | + * @param readFileSync - The synchronous file-reading function. |
| 177 | + * @param pkgFile - The path to the package.json. |
| 178 | + * @returns The parsed package.json object. |
| 179 | + */ |
| 180 | + readPackageSync?: (readFileSync: (file: string) => string | Buffer, pkgFile: string) => PackageJSON; |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +export = resolve; |
0 commit comments