Skip to content

Commit

Permalink
Fix ESM in Node v18.19.0 (#44)
Browse files Browse the repository at this point in the history
Node v18.19.0 includes a backport of ESM loading on a separate thread --
nodejs/node@bac9b17.
This PR updates the version check to use AST parsing on Node >= 18.19.0.
  • Loading branch information
jsumners-nr authored Dec 11, 2023
1 parent 3cafa99 commit 96c506e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
3 changes: 2 additions & 1 deletion hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const NODE_MINOR = Number(NODE_VERSION[1])

let entrypoint

if (NODE_MAJOR >= 20) {
let getExports
if (NODE_MAJOR >= 20 || (NODE_MAJOR == 18 && NODE_MINOR >= 19)) {
getExports = require('./lib/get-exports.js')
} else {
getExports = (url) => import(url).then(Object.keys)
Expand Down
3 changes: 3 additions & 0 deletions test/get-esm-exports/v18.19-get-esm-exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// v18.19.0 backported ESM hook execution to a separate thread,
// thus being equivalent to >=v20.
require('./v20-get-esm-exports')
15 changes: 10 additions & 5 deletions test/runtest
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ const args = [
...process.argv.slice(2)
]

const [processMajor] = process.versions.node.split('.').map(Number)
const [processMajor, processMinor] = process.versions.node.split('.').map(Number)

const match = filename.match(/v([0-9]+)/)
const match = filename.match(/v([0-9]+)(?:\.([0-9]+))?/)

const versionRequirement = match ? match[1] : 0;
const majorRequirement = match ? match[1] : 0;
const minorRequirement = match && match[2];

if (processMajor < versionRequirement) {
console.log(`skipping ${filename} as this is Node.js v${processMajor} and test wants v${versionRequirement}`);
if (processMajor < majorRequirement && minorRequirement === undefined) {
console.log(`skipping ${filename} as this is Node.js v${processMajor} and test wants v${majorRequirement}`);
process.exit(0);
}
if (processMajor < majorRequirement && processMinor < minorRequirement) {
console.log(`skipping ${filename} as this is Node.js v${processMajor}.${processMinor} and test wants >=v${majorRequirement}.${minorRequirement}`);
process.exit(0);
}

Expand Down

0 comments on commit 96c506e

Please sign in to comment.