(specifier, context, nextResolve)
| 32 | |
| 33 | /** @type {import('module').ResolveHook} */ |
| 34 | export const resolve = async (specifier, context, nextResolve) => { |
| 35 | // True when it's a non-module import without explicit extensions. |
| 36 | const isNonModuleExtensionlessImport = |
| 37 | nonModuleImportRe.test(specifier) && !explicitExtensionRe.test(specifier); |
| 38 | const pathMappings = !nonModuleImportRe.test(specifier) ? pathMappingMatcher(specifier) : []; |
| 39 | |
| 40 | // If it's neither path mapped, nor an extension-less import that may be fixed up, exit early. |
| 41 | if (!isNonModuleExtensionlessImport && pathMappings.length === 0) { |
| 42 | return nextResolve(specifier, context); |
| 43 | } |
| 44 | |
| 45 | if (pathMappings.length > 0) { |
| 46 | for (const mapping of pathMappings) { |
| 47 | const res = await catchError(() => resolve(mapping, context, nextResolve)); |
| 48 | if (res !== null) { |
| 49 | return res; |
| 50 | } |
| 51 | } |
| 52 | } else { |
| 53 | const fixedResult = |
| 54 | (await catchError(() => nextResolve(`${specifier}.js`, context))) || |
| 55 | (await catchError(() => nextResolve(`${specifier}/index.js`, context))) || |
| 56 | // Legacy variants for the `zone.js` variant using still `ts_library`. |
| 57 | // TODO(rules_js migration): Remove this. |
| 58 | (await catchError(() => nextResolve(`${specifier}.mjs`, context))) || |
| 59 | (await catchError(() => nextResolve(`${specifier}/index.mjs`, context))); |
| 60 | |
| 61 | if (fixedResult !== null) { |
| 62 | return fixedResult; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return await nextResolve(specifier, context); |
| 67 | }; |
| 68 | |
| 69 | async function catchError(fn) { |
| 70 | try { |
searching dependent graphs…