Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into fix/skip-wrapping-i…
Browse files Browse the repository at this point in the history
…nvalid-indentifiers
  • Loading branch information
timfish committed Jun 29, 2024
2 parents f20179c + 49d69ba commit a9a5345
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
13 changes: 11 additions & 2 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

const { URL } = require('url')
const { inspect } = require('util')
const { isIdentifierName } = require('@babel/helper-validator-identifier')
const specifiers = new Map()
const isWin = process.platform === 'win32'
Expand Down Expand Up @@ -116,6 +117,14 @@ function isBareSpecifier (specifier) {
}
}

function emitWarning (err) {
// Unfortunately, process.emitWarning does not output the full error
// with error.cause like console.warn does so we need to inspect it when
// tracing warnings
const warnMessage = process.execArgv.includes('--trace-warnings') ? inspect(err) : err
process.emitWarning(warnMessage)
}

/**
* Processes a module's exports and builds a set of setter blocks.
*
Expand Down Expand Up @@ -217,7 +226,7 @@ function createHook (meta) {
async function resolve (specifier, context, parentResolve) {
cachedResolve = parentResolve

// See github.com/DataDog/import-in-the-middle/pull/76.
// See github.com/nodejs/import-in-the-middle/pull/76.
if (specifier === iitmURL) {
return {
url: specifier,
Expand Down Expand Up @@ -311,7 +320,7 @@ register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(rea
// it would be very tricky to debug
const err = new Error(`'import-in-the-middle' failed to wrap '${realUrl}'`)
err.cause = cause
console.warn(err)
emitWarning(err)

// Revert back to the non-iitm URL
url = realUrl
Expand Down
36 changes: 21 additions & 15 deletions lib/get-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 await 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 await getCjsExports(url, context, parentLoad, source)
}
} catch (cause) {
const err = new Error(`Failed to parse '${url}'`)
err.cause = cause
throw err
}
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/DataDog/import-in-the-middle.git"
"url": "git+ssh://[email protected]/nodejs/import-in-the-middle.git"
},
"keywords": [
"import",
Expand All @@ -25,9 +25,9 @@
"author": "Bryan English <[email protected]>",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/DataDog/import-in-the-middle/issues"
"url": "https://github.com/nodejs/import-in-the-middle/issues"
},
"homepage": "https://github.com/DataDog/import-in-the-middle#readme",
"homepage": "https://github.com/nodejs/import-in-the-middle#readme",
"imhotap": {
"runner": "node",
"test-env": "NODE_OPTIONS=--no-warnings --require ./test/version-check.js --experimental-loader ./test/generic-loader.mjs"
Expand Down

0 comments on commit a9a5345

Please sign in to comment.