Skip to content

Commit 8bbd087

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 8bbd087

File tree

8 files changed

+42
-262
lines changed

8 files changed

+42
-262
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

Lines changed: 0 additions & 118 deletions
This file was deleted.

lib/get-exports.js

Lines changed: 0 additions & 129 deletions
This file was deleted.

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)