MCPcopy
hub / github.com/TypeStrong/ts-node / resolve

Function resolve

src/esm.ts:153–225  ·  view source on GitHub ↗
(
    specifier: string,
    context: { parentURL: string },
    defaultResolve: typeof resolve
  )

Source from the content-addressed store, hash-verified

151 const rememberResolvedViaCommonjsFallback = new Set();
152
153 async function resolve(
154 specifier: string,
155 context: { parentURL: string },
156 defaultResolve: typeof resolve
157 ): Promise<{ url: string; format?: NodeLoaderHooksFormat }> {
158 const defer = async () => {
159 const r = await defaultResolve(specifier, context, defaultResolve);
160 return r;
161 };
162 // See: https://github.com/nodejs/node/discussions/41711
163 // nodejs will likely implement a similar fallback. Till then, we can do our users a favor and fallback today.
164 async function entrypointFallback(
165 cb: () => ReturnType<typeof resolve> | Awaited<ReturnType<typeof resolve>>
166 ): ReturnType<typeof resolve> {
167 try {
168 const resolution = await cb();
169 if (
170 resolution?.url &&
171 isProbablyEntrypoint(specifier, context.parentURL)
172 )
173 rememberIsProbablyEntrypoint.add(resolution.url);
174 return resolution;
175 } catch (esmResolverError) {
176 if (!isProbablyEntrypoint(specifier, context.parentURL))
177 throw esmResolverError;
178 try {
179 let cjsSpecifier = specifier;
180 // Attempt to convert from ESM file:// to CommonJS path
181 try {
182 if (specifier.startsWith('file://'))
183 cjsSpecifier = fileURLToPath(specifier);
184 } catch {}
185 const resolution = pathToFileURL(
186 createRequire(process.cwd()).resolve(cjsSpecifier)
187 ).toString();
188 rememberIsProbablyEntrypoint.add(resolution);
189 rememberResolvedViaCommonjsFallback.add(resolution);
190 return { url: resolution, format: 'commonjs' };
191 } catch (commonjsResolverError) {
192 throw esmResolverError;
193 }
194 }
195 }
196
197 return addShortCircuitFlag(async () => {
198 const parsed = parseUrl(specifier);
199 const { pathname, protocol, hostname } = parsed;
200
201 if (!isFileUrlOrNodeStyleSpecifier(parsed)) {
202 return entrypointFallback(defer);
203 }
204
205 if (protocol !== null && protocol !== 'file:') {
206 return entrypointFallback(defer);
207 }
208
209 // Malformed file:// URL? We should always see `null` or `''`
210 if (hostname) {

Callers 12

phase2Function · 0.70
getEntryPointInfoFunction · 0.70
phase4Function · 0.70
bin.tsFile · 0.70
combinePathsFunction · 0.70
findAndReadConfigFunction · 0.70
readConfigFunction · 0.70
ava.config.cjsFile · 0.50
mainFunction · 0.50
defaultResolveFunction · 0.50

Calls 3

addShortCircuitFlagFunction · 0.85
entrypointFallbackFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…