From ff306bec72961b649083a26661b76c62afc9506e Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sun, 16 Jun 2024 01:21:49 +0200 Subject: [PATCH 1/4] fix: cjs resolution when falling back to `parentLoad` --- hook.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/hook.js b/hook.js index 8e534e0..33feaa5 100644 --- a/hook.js +++ b/hook.js @@ -273,6 +273,8 @@ function createHook (meta) { parentResolve: cachedResolve }) return { + shortCircuit: true, + format: 'module', source: ` import { register } from '${iitmURL}' import * as namespace from ${JSON.stringify(realUrl)} @@ -316,12 +318,7 @@ register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(rea // For Node.js 16.12.0 and higher. async function load (url, context, parentLoad) { if (hasIitm(url)) { - const { source } = await getSource(url, context, parentLoad) - return { - source, - shortCircuit: true, - format: 'module' - } + return getSource(url, context, parentLoad) } return parentLoad(url, context, parentLoad) From 607f7b52a561ca8174525e9b900dbfe644645307 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sun, 16 Jun 2024 01:32:40 +0200 Subject: [PATCH 2/4] remove getSource --- hook.js | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/hook.js b/hook.js index 33feaa5..a7c3632 100644 --- a/hook.js +++ b/hook.js @@ -261,7 +261,7 @@ function createHook (meta) { } } - async function getSource (url, context, parentGetSource) { + async function load (url, context, parentLoad) { if (hasIitm(url)) { const realUrl = deleteIitm(url) @@ -269,7 +269,7 @@ function createHook (meta) { const setters = await processModule({ srcUrl: realUrl, context, - parentGetSource, + parentGetSource: parentLoad, parentResolve: cachedResolve }) return { @@ -312,15 +312,6 @@ register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(rea } } - return parentGetSource(url, context, parentGetSource) - } - - // For Node.js 16.12.0 and higher. - async function load (url, context, parentLoad) { - if (hasIitm(url)) { - return getSource(url, context, parentLoad) - } - return parentLoad(url, context, parentLoad) } @@ -330,7 +321,7 @@ register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(rea return { load, resolve, - getSource, + getSource: load, getFormat (url, context, parentGetFormat) { if (hasIitm(url)) { return { From 167cb52e5a45bb95daa65a6102bb5feabd1a83df Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Mon, 17 Jun 2024 11:08:36 +0200 Subject: [PATCH 3/4] Better parser error --- lib/get-exports.js | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/get-exports.js b/lib/get-exports.js index 9a78e8c..84d1cb5 100644 --- a/lib/get-exports.js +++ b/lib/get-exports.js @@ -92,23 +92,29 @@ async function getExports (url, context, parentLoad) { source = readFileSync(fileURLToPath(url), 'utf8') } - if (format === 'module') { - return getEsmExports(source) - } + try { + if (format === 'module') { + return getEsmExports(source) + } - if (format === 'commonjs') { - return getCjsExports(url, context, parentLoad, source) - } + if (format === 'commonjs') { + return getCjsExports(url, context, parentLoad, source) + } - // At this point our `format` is either undefined or not known by us. Fall - // back to parsing as ESM/CJS. - const esmExports = getEsmExports(source) - if (!esmExports.length) { - // TODO(bengl) it's might be possible to get here if somehow the format - // isn't set at first and yet we have an ESM module with no exports. - // I couldn't construct an example that would do this, so maybe it's - // impossible? - return getCjsExports(url, context, parentLoad, source) + // At this point our `format` is either undefined or not known by us. Fall + // back to parsing as ESM/CJS. + const esmExports = getEsmExports(source) + if (!esmExports.length) { + // TODO(bengl) it's might be possible to get here if somehow the format + // isn't set at first and yet we have an ESM module with no exports. + // I couldn't construct an example that would do this, so maybe it's + // impossible? + return getCjsExports(url, context, parentLoad, source) + } + } catch (cause) { + const err = new Error(`Failed to parse '${url}'`) + err.cause = cause + throw err } } From f2bbb22090e6c4dd841a07c8488f9d42516394b1 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 18 Jun 2024 09:13:45 +0200 Subject: [PATCH 4/4] await so the try/catch works! --- lib/get-exports.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/get-exports.js b/lib/get-exports.js index 84d1cb5..b0872c7 100644 --- a/lib/get-exports.js +++ b/lib/get-exports.js @@ -98,7 +98,7 @@ async function getExports (url, context, parentLoad) { } if (format === 'commonjs') { - return getCjsExports(url, context, parentLoad, source) + return await getCjsExports(url, context, parentLoad, source) } // At this point our `format` is either undefined or not known by us. Fall @@ -109,7 +109,7 @@ async function getExports (url, context, parentLoad) { // isn't set at first and yet we have an ESM module with no exports. // I couldn't construct an example that would do this, so maybe it's // impossible? - return getCjsExports(url, context, parentLoad, source) + return await getCjsExports(url, context, parentLoad, source) } } catch (cause) { const err = new Error(`Failed to parse '${url}'`)