Skip to content

Commit

Permalink
fix: DRY up code and address additional cases
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobJingleheimer committed Mar 21, 2024
1 parent c819f30 commit 19e15f2
Showing 1 changed file with 39 additions and 45 deletions.
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.size) {
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

0 comments on commit 19e15f2

Please sign in to comment.