diff --git a/lib/quibble.mjs b/lib/quibble.mjs index 125db13..8450361 100644 --- a/lib/quibble.mjs +++ b/lib/quibble.mjs @@ -1,3 +1,5 @@ +import { writeFileSync } from 'node:fs'; // FIXME: remove + import quibble from './quibble.js' import { thisWillRunInUserThread } from './thisWillRunInUserThread.js' @@ -20,63 +22,55 @@ const quibbleLoaderState = { stubModuleGeneration: 0 } -export async function resolve (specifier, context, nextResolve) { - const resolve = () => - nextResolve( - specifier.includes('__quibble') - ? specifier - .replace(/[?&]__quibbleresolveurl/, '') - .replace(/[?&]__quibbleoriginal/, '') - : specifier, - context - ) +export async function resolve (specifier, { parentURL }, nextResolve) { + const { stubModuleGeneration } = quibbleLoaderState + + const { url, ...ctx } = await nextResolve( + specifier.includes('__quibble') + ? specifier + .replace(/[?&]__quibbleresolveurl/, '') + .replace(/[?&]__quibbleoriginal/, '') + : specifier + ) + .catch((error) => { + if (error.code === 'ERR_MODULE_NOT_FOUND') { + return { + url: parentURL + ? addQueryToUrl( + new URL(specifier, parentURL).href, + '__quibble', + stubModuleGeneration + ) + : new URL(specifier).href + } + } else { + throw error + } + }); + let resolvedUrl = url; if (specifier.includes('__quibbleresolveurl')) { - const resolvedUrl = (await resolve()).url const error = new Error() error.code = 'QUIBBLE_RESOLVED_URL' error.resolvedUrl = resolvedUrl throw error } - if (!quibbleLoaderState.quibbledModules) { - return resolve() - } - - if (specifier === 'quibble') { - return resolve() + if ( + quibbleLoaderState.quibbledModules.size + && specifier !== 'quibble' + && !specifier.includes('__quibbleoriginal') + ) { + const quibbledUrl = addQueryToUrl(url, '__quibble', stubModuleGeneration) + if (!url.startsWith('node:') || getStubsInfo(quibbledUrl)) resolvedUrl = quibbledUrl } - if (specifier.includes('__quibbleoriginal')) { - return resolve() + // FIXME: remove + if (specifier.includes('is-promise')) { + writeFileSync(1, `\n[QBL]:\nresolved: ${resolvedUrl}\nsize: ${quibbleLoaderState.quibbledModules.size}\n\n`) } - const stubModuleGeneration = quibbleLoaderState.stubModuleGeneration - const { parentURL } = context - - try { - const { url, ...ctx } = await resolve() - - const quibbledUrl = addQueryToUrl(url, '__quibble', stubModuleGeneration) - - if (url.startsWith('node:') && !getStubsInfo(quibbledUrl)) { - return { ...ctx, url } // It's allowed to change ctx for a builtin (but unlikely) - } - - return { ...ctx, url: quibbledUrl } - } catch (error) { - if (error.code === 'ERR_MODULE_NOT_FOUND') { - return { - url: parentURL - ? addQueryToUrl( - new URL(specifier, parentURL).href - , '__quibble', stubModuleGeneration) - : new URL(specifier).href - } - } else { - throw error - } - } + return { ...ctx, url: resolvedUrl } } /** diff --git a/test/esm-lib/quibble-cjs-esmImportWithPath.test.js b/test/esm-lib/quibble-cjs-esmImportWithPath.test.js index b18b149..8c20ddd 100644 --- a/test/esm-lib/quibble-cjs-esmImportWithPath.test.js +++ b/test/esm-lib/quibble-cjs-esmImportWithPath.test.js @@ -44,7 +44,7 @@ module.exports = { }, 'support importing esm and returning the path even when bare-specifier quibbled': async function () { // This test that `is-promise` is a dual-mode module where - // the entry points are index.js and index.mjs. If thie changes in the future, you + // the entry points are index.js and index.mjs. If this changes in the future, you // can always create a module of your own and put it in node_modules. await quibble.esm('is-promise', undefined, 42) const { modulePath, module } = await quibble.esmImportWithPath('is-promise')