Skip to content

Commit 9d748c2

Browse files
committed
fix: convert all modules running in loader thread to ESM
This is a second attempt at fixing the double-loader issue in #205. The tests from that commit are included.
1 parent ffb5682 commit 9d748c2

File tree

8 files changed

+63
-29
lines changed

8 files changed

+63
-29
lines changed

hook.js renamed to create-hook.mjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
//
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
44

5-
const { URL, fileURLToPath } = require('url')
6-
const { inspect } = require('util')
7-
const { builtinModules } = require('module')
5+
import { URL, fileURLToPath } from 'url'
6+
import { inspect } from 'util'
7+
import { builtinModules } from 'module'
8+
89
const specifiers = new Map()
910
const isWin = process.platform === 'win32'
1011
let experimentalPatchInternals = false
@@ -21,7 +22,7 @@ let entrypoint
2122

2223
let getExports
2324
if (NODE_MAJOR >= 20 || (NODE_MAJOR === 18 && NODE_MINOR >= 19)) {
24-
getExports = require('./lib/get-exports.js')
25+
getExports = (await import('./lib/get-exports.mjs')).default
2526
} else {
2627
getExports = (url) => import(url).then(Object.keys)
2728
}
@@ -35,7 +36,7 @@ function hasIitm (url) {
3536
}
3637

3738
function isIitm (url, meta) {
38-
return url === meta.url || url === meta.url.replace('hook.mjs', 'hook.js')
39+
return url === meta.url || url === meta.url.replace('hook.mjs', 'create-hook.mjs')
3940
}
4041

4142
function deleteIitm (url) {
@@ -284,7 +285,7 @@ function addIitm (url) {
284285
return needsToAddFileProtocol(urlObj) ? 'file:' + urlObj.href : urlObj.href
285286
}
286287

287-
function createHook (meta) {
288+
export function createHook (meta) {
288289
let cachedResolve
289290
const iitmURL = new URL('lib/register.js', meta.url).toString()
290291
let includeModules, excludeModules
@@ -432,6 +433,7 @@ register(${JSON.stringify(realUrl)}, _, set, get, ${JSON.stringify(specifiers.ge
432433
`
433434
}
434435
} catch (cause) {
436+
process._rawDebug(cause)
435437
// If there are other ESM loader hooks registered as well as iitm,
436438
// depending on the order they are registered, source might not be
437439
// JavaScript.
@@ -494,5 +496,3 @@ register(${JSON.stringify(realUrl)}, _, set, get, ${JSON.stringify(specifiers.ge
494496
}
495497
}
496498
}
497-
498-
module.exports = { createHook }

hook.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
44

5-
import { createHook } from './hook.js'
5+
import { createHook } from './create-hook.mjs'
66

77
const { initialize, load, resolve, getFormat, getSource } = createHook(import.meta)
88

lib/get-esm-exports.js renamed to lib/get-esm-exports.mjs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

3-
const { Parser } = require('acorn')
4-
const { importAttributesOrAssertions } = require('acorn-import-attributes')
3+
import { Parser } from 'acorn'
4+
import { importAttributesOrAssertions } from 'acorn-import-attributes'
55

66
const acornOpts = {
77
ecmaVersion: 'latest',
@@ -32,7 +32,7 @@ function warn (txt) {
3232
* @returns {Set<string>} The identifiers exported by the module along with any
3333
* custom directives.
3434
*/
35-
function getEsmExports (moduleSource) {
35+
export default function getEsmExports (moduleSource) {
3636
const exportedNames = new Set()
3737
const tree = parser.parse(moduleSource, acornOpts)
3838
for (const node of tree.body) {
@@ -114,5 +114,3 @@ function parseSpecifiers (node, exportedNames) {
114114
}
115115
}
116116
}
117-
118-
module.exports = getEsmExports

lib/get-exports.js renamed to lib/get-exports.mjs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict'
22

3-
const getEsmExports = require('./get-esm-exports.js')
4-
const { parse: parseCjs } = require('cjs-module-lexer')
5-
const { readFileSync } = require('fs')
6-
const { builtinModules } = require('module')
7-
const { fileURLToPath, pathToFileURL } = require('url')
8-
const { dirname } = require('path')
3+
import getEsmExports from './get-esm-exports.mjs'
4+
import { parse as parseCjs, init as parserInit } from 'cjs-module-lexer'
5+
import { readFileSync } from 'fs'
6+
import { builtinModules, createRequire } from 'module'
7+
import { fileURLToPath, pathToFileURL } from 'url'
8+
import { dirname } from 'path'
9+
10+
await parserInit()
911

1012
function addDefault (arr) {
1113
return new Set(['default', ...arr])
@@ -14,9 +16,15 @@ function addDefault (arr) {
1416
// Cached exports for Node built-in modules
1517
const BUILT_INS = new Map()
1618

19+
let require
20+
1721
function getExportsForNodeBuiltIn (name) {
1822
let exports = BUILT_INS.get()
1923

24+
if (!require) {
25+
require = createRequire(import.meta.url)
26+
}
27+
2028
if (!exports) {
2129
exports = new Set(addDefault(Object.keys(require(name))))
2230
BUILT_INS.set(name, exports)
@@ -47,6 +55,9 @@ async function getCjsExports (url, context, parentLoad, source) {
4755
re = './'
4856
}
4957
// Resolve the re-exported module relative to the current module.
58+
if (!require) {
59+
require = createRequire(import.meta.url)
60+
}
5061
const newUrl = pathToFileURL(require.resolve(re, { paths: [dirname(fileURLToPath(url))] })).href
5162

5263
if (newUrl.endsWith('.node') || newUrl.endsWith('.json')) {
@@ -81,7 +92,7 @@ async function getCjsExports (url, context, parentLoad, source) {
8192
* Please see {@link getEsmExports} for caveats on special identifiers that may
8293
* be included in the result set.
8394
*/
84-
async function getExports (url, context, parentLoad) {
95+
export default async function getExports (url, context, parentLoad) {
8596
// `parentLoad` gives us the possibility of getting the source
8697
// from an upstream loader. This doesn't always work though,
8798
// so later on we fall back to reading it from disk.
@@ -125,5 +136,3 @@ async function getExports (url, context, parentLoad) {
125136
throw err
126137
}
127138
}
128-
129-
module.exports = getExports

test/fixtures/double-loader.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { readFile } from 'fs/promises'
2+
3+
export async function load (url, context, nextLoad) {
4+
const result = await nextLoad(url, context)
5+
if (!result.source && url.startsWith('file:')) {
6+
result.source = await readFile(new URL(url))
7+
}
8+
return result
9+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// v18.19.0 backported ESM hook execution to a separate thread,
22
// thus being equivalent to >=v20.
3-
require('./v20-get-esm-exports')
3+
import './v20-get-esm-exports.mjs'

test/get-esm-exports/v20-get-esm-exports.js renamed to test/get-esm-exports/v20-get-esm-exports.mjs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
'use strict'
22

3-
const getEsmExports = require('../../lib/get-esm-exports.js')
4-
const fs = require('fs')
5-
const assert = require('assert')
6-
const path = require('path')
3+
import getEsmExports from '../../lib/get-esm-exports.mjs'
4+
import fs from 'fs'
5+
import assert from 'assert'
6+
import path from 'path'
7+
import { fileURLToPath } from 'url'
78

8-
const fixturePath = path.join(__dirname, '../fixtures/esm-exports.txt')
9+
const dirname = path.dirname(fileURLToPath(import.meta.url))
10+
11+
const fixturePath = path.join(dirname, '../fixtures/esm-exports.txt')
912
const fixture = fs.readFileSync(fixturePath, 'utf8')
1013

1114
fixture.split('\n').forEach(line => {

test/other/double-loading.mjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
2+
//
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
4+
5+
import { execSync } from 'child_process'
6+
import { doesNotThrow } from 'assert'
7+
8+
const env = {
9+
...process.env,
10+
NODE_OPTIONS: '--no-warnings --experimental-loader ./test/fixtures/double-loader.mjs --experimental-loader ./hook.mjs'
11+
}
12+
13+
doesNotThrow(() => {
14+
execSync('node -p 0', { env })
15+
})

0 commit comments

Comments
 (0)