UNPKG

7.33 kBTypeScriptView Raw
1interface PackageMeta {
2 name: string;
3 version: string;
4 [key: string]: any;
5}
6
7interface ToString {
8 toString(): string;
9}
10
11type StringOrToString = string | ToString;
12
13/**
14 * Callback invoked when resolving asynchronously
15 *
16 * @param error
17 * @param resolved Absolute path to resolved identifier
18 */
19type resolveCallback = (err: Error | null, resolved?: string, pkg?: PackageMeta) => void;
20
21/**
22 * Callback invoked when checking if a file or directory exists
23 *
24 * @param error
25 * @param exists If the given file or directory exists
26 */
27type existsCallback = (err: Error | null, isFile?: boolean) => void;
28
29/**
30 * Callback invoked when reading a file
31 *
32 * @param error
33 * @param isFile If the given file exists
34 */
35type readFileCallback = (err: Error | null, file?: StringOrToString) => void;
36
37/**
38 * Callback invoked when resolving a potential symlink
39 *
40 * @param error
41 * @param resolved Absolute path to the resolved file
42 */
43type realpathCallback = (err: Error | null, resolved?: string) => void;
44
45/**
46 * Callback invoked when reading and parsing a package.json file
47 *
48 * @param error
49 * @param resolved Absolute path to the resolved file
50 */
51type readPackageCallback = (err: Error | null, package?: Record<string, unknown>) => void;
52
53/**
54 * Asynchronously resolve the module path string id into cb(err, res [, pkg]), where pkg (if defined) is the data from package.json
55 *
56 * @param id Identifier to resolve
57 * @param callback
58 */
59declare function resolve(id: string, cb: resolveCallback): void;
60
61/**
62 * Asynchronously resolve the module path string id into cb(err, res [, pkg]), where pkg (if defined) is the data from package.json
63 *
64 * @param id Identifier to resolve
65 * @param options Options to use for resolving, optional.
66 * @param callback
67 */
68declare function resolve(id: string, opts: resolve.AsyncOpts, cb: resolveCallback): void;
69
70/**
71 * Synchronously resolve the module path string id, returning the result and throwing an error when id can't be resolved.
72 *
73 * @param id Identifier to resolve
74 * @param options Options to use for resolving, optional.
75 */
76declare function resolveSync(id: string, opts?: resolve.SyncOpts): string;
77
78/**
79 * Return whether a package is in core
80 */
81declare function resolveIsCore(id: string): boolean | undefined;
82
83// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
84type JSONValue = string | number | boolean | JSONObject | JSONArray;
85interface JSONObject {
86 [x: string]: JSONValue;
87}
88interface JSONArray extends Array<JSONValue> {}
89
90declare namespace resolve {
91 export type PackageJSON = JSONObject;
92
93 interface Opts {
94 /** directory to begin resolving from (defaults to __dirname) */
95 basedir?: string | undefined;
96 /** package.json data applicable to the module being loaded */
97 package?: any;
98 /** set to false to exclude node core modules (e.g. fs) from the search */
99 includeCoreModules?: boolean | undefined;
100 /** array of file extensions to search in order (defaults to ['.js']) */
101 extensions?: string | readonly string[] | undefined;
102 /** transform the parsed package.json contents before looking at the "main" field */
103 packageFilter?: ((pkg: PackageJSON, pkgFile: string, dir: string) => PackageJSON) | undefined;
104 /** transform a path within a package */
105 pathFilter?: ((pkg: PackageJSON, path: string, relativePath: string) => string) | undefined;
106 /** require.paths array to use if nothing is found on the normal node_modules recursive walk (probably don't use this) */
107 paths?: string | readonly string[] | undefined;
108 /** return the list of candidate paths where the packages sources may be found (probably don't use this) */
109 packageIterator?:
110 | ((request: string, start: string, getPackageCandidates: () => string[], opts: Opts) => string[])
111 | undefined;
112 /** directory (or directories) in which to recursively look for modules. (default to 'node_modules') */
113 moduleDirectory?: string | readonly string[] | undefined;
114 /**
115 * if true, doesn't resolve `basedir` to real path before resolving.
116 * This is the way Node resolves dependencies when executed with the --preserve-symlinks flag.
117 *
118 * Note: this property is currently true by default but it will be changed to false in the next major version because Node's resolution
119 * algorithm does not preserve symlinks by default.
120 */
121 preserveSymlinks?: boolean | undefined;
122 }
123
124 interface BaseAsyncOpts extends Opts {
125 /** function to asynchronously test whether a file exists */
126 isFile?: ((file: string, cb: existsCallback) => void) | undefined;
127 /** function to asynchronously test whether a directory exists */
128 isDirectory?: ((directory: string, cb: existsCallback) => void) | undefined;
129 /** function to asynchronously resolve a potential symlink to its real path */
130 realpath?: ((file: string, cb: realpathCallback) => void) | undefined;
131 }
132
133 export type AsyncOpts =
134 & BaseAsyncOpts
135 & ({
136 /** how to read files asynchronously (defaults to fs.readFile) */
137 readFile?: ((file: string, cb: readFileCallback) => void) | undefined;
138 /** function to asynchronously read and parse a package.json file */
139 readPackage?: never | undefined;
140 } | {
141 /** how to read files asynchronously (defaults to fs.readFile) */
142 readFile?: never | undefined;
143 /** function to asynchronously read and parse a package.json file */
144 readPackage?:
145 | ((
146 readFile: (file: string, cb: readFileCallback) => void,
147 pkgfile: string,
148 cb: readPackageCallback,
149 ) => void)
150 | undefined;
151 });
152
153 interface BaseSyncOpts extends Opts {
154 /** function to synchronously test whether a file exists */
155 isFile?: ((file: string) => boolean) | undefined;
156 /** function to synchronously test whether a directory exists */
157 isDirectory?: ((directory: string) => boolean) | undefined;
158 /** function to synchronously resolve a potential symlink to its real path */
159 realpathSync?: ((file: string) => string) | undefined;
160 }
161
162 export type SyncOpts =
163 & BaseSyncOpts
164 & ({
165 /** how to read files synchronously (defaults to fs.readFileSync) */
166 readFileSync?: ((file: string) => StringOrToString) | undefined;
167 /** function to synchronously read and parse a package.json file */
168 readPackageSync?: never | undefined;
169 } | {
170 /** how to read files synchronously (defaults to fs.readFileSync) */
171 readFileSync?: never | undefined;
172 /** function to synchronously read and parse a package.json file */
173 readPackageSync?:
174 | ((
175 readFileSync: (file: string) => StringOrToString,
176 pkgfile: string,
177 ) => Record<string, unknown> | undefined)
178 | undefined;
179 });
180
181 export var sync: typeof resolveSync;
182 export var isCore: typeof resolveIsCore;
183}
184
185export = resolve;
186
\No newline at end of file