(
specifier: string,
context: { parentURL: string },
defaultResolve: typeof resolve
)
| 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) { |
no test coverage detected
searching dependent graphs…