UNPKG

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