UNPKG

6.75 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
90declare namespace resolve {
91 interface Opts {
92 /** directory to begin resolving from (defaults to __dirname) */
93 basedir?: string | undefined;
94 /** package.json data applicable to the module being loaded */
95 package?: any;
96 /** set to false to exclude node core modules (e.g. fs) from the search */
97 includeCoreModules?: boolean | undefined;
98 /** array of file extensions to search in order (defaults to ['.js']) */
99 extensions?: string | ReadonlyArray<string> | undefined;
100 /** transform the parsed package.json contents before looking at the "main" field */
101 packageFilter?: ((pkg: any, pkgfile: string) => any) | undefined;
102 /** transform a path within a package */
103 pathFilter?: ((pkg: any, path: string, relativePath: string) => string) | undefined;
104 /** require.paths array to use if nothing is found on the normal node_modules recursive walk (probably don't use this) */
105 paths?: string | ReadonlyArray<string> | undefined;
106 /** return the list of candidate paths where the packages sources may be found (probably don't use this) */
107 packageIterator?: ((request: string, start: string, getPackageCandidates: () => string[], opts: Opts) => string[]) | undefined;
108 /** directory (or directories) in which to recursively look for modules. (default to 'node_modules') */
109 moduleDirectory?: string | ReadonlyArray<string> | undefined;
110 /**
111 * if true, doesn't resolve `basedir` to real path before resolving.
112 * This is the way Node resolves dependencies when executed with the --preserve-symlinks flag.
113 *
114 * Note: this property is currently true by default but it will be changed to false in the next major version because Node's resolution
115 * algorithm does not preserve symlinks by default.
116 */
117 preserveSymlinks?: boolean | undefined;
118 }
119
120 interface BaseAsyncOpts extends Opts {
121 /** function to asynchronously test whether a file exists */
122 isFile?: ((file: string, cb: existsCallback) => void) | undefined;
123 /** function to asynchronously test whether a directory exists */
124 isDirectory?: ((directory: string, cb: existsCallback) => void) | undefined;
125 /** function to asynchronously resolve a potential symlink to its real path */
126 realpath?: ((file: string, cb: realpathCallback) => void) | undefined;
127 }
128
129 export type AsyncOpts = BaseAsyncOpts & ({
130 /** how to read files asynchronously (defaults to fs.readFile) */
131 readFile?: ((file: string, cb: readFileCallback) => void) | undefined;
132 /** function to asynchronously read and parse a package.json file */
133 readPackage?: never | undefined;
134 } | {
135 /** how to read files asynchronously (defaults to fs.readFile) */
136 readFile?: never | undefined
137 /** function to asynchronously read and parse a package.json file */
138 readPackage?: ((readFile: (file: string, cb: readFileCallback) => void, pkgfile: string, cb: readPackageCallback) => void) | undefined;
139 });
140
141 interface BaseSyncOpts extends Opts {
142 /** function to synchronously test whether a file exists */
143 isFile?: ((file: string) => boolean) | undefined;
144 /** function to synchronously test whether a directory exists */
145 isDirectory?: ((directory: string) => boolean) | undefined;
146 /** function to synchronously resolve a potential symlink to its real path */
147 realpathSync?: ((file: string) => string) | undefined;
148 }
149
150 export type SyncOpts = BaseSyncOpts & ({
151 /** how to read files synchronously (defaults to fs.readFileSync) */
152 readFileSync?: ((file: string) => StringOrToString) | undefined;
153 /** function to synchronously read and parse a package.json file */
154 readPackageSync?: never | undefined;
155 } | {
156 /** how to read files synchronously (defaults to fs.readFileSync) */
157 readFileSync?: never | undefined;
158 /** function to synchronously read and parse a package.json file */
159 readPackageSync?: ((readFileSync: (file: string) => StringOrToString, pkgfile: string) => Record<string, unknown> | undefined) | undefined;
160 });
161
162 export var sync: typeof resolveSync;
163 export var isCore: typeof resolveIsCore;
164}
165
166export = resolve;
167
\No newline at end of file