Skip to content

Commit 81a2ae0

Browse files
authored
fix: use createRequire to load hook.js (#205)
1 parent 2fb1f21 commit 81a2ae0

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

hook.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
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 { createRequire } from 'node:module'
6+
const require = createRequire(import.meta.url)
7+
const { createHook } = require('./hook.js')
68

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

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+
}

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)