Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct check for existing replacements #109

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 39 additions & 45 deletions lib/quibble.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { writeFileSync } from 'node:fs'; // FIXME: remove

import quibble from './quibble.js'
import { thisWillRunInUserThread } from './thisWillRunInUserThread.js'

Expand All @@ -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 }
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/esm-lib/quibble-cjs-esmImportWithPath.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading