Skip to content

Commit d8af0ba

Browse files
committed
add log function for debugging loader
1 parent 253eec5 commit d8af0ba

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# changelog
22

3+
* 2.6.8 _tbd_
4+
* add log utility for debugging loader
35
* 2.6.7 _Jul.16.2024_
46
* [add swc tests and remove swc caution from README](https://github.com/iambumblehead/esmock/pull/309) thanks @Brooooooklyn
57
* [unpin node 22.1 at test CI](https://github.com/iambumblehead/esmock/pull/309) and use latest 22.x

src/esmockLoader.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from 'node:fs/promises'
1+
import fs from 'node:fs'
22
import module from 'node:module'
33
import process from 'process'
44
import esmockErr from './esmockErr.js'
@@ -33,6 +33,10 @@ const moduleIdReCreate = (moduleid, treeid) => new RegExp(
3333
const mockKeys = global.mockKeys = (global.mockKeys || {})
3434
const mockKeysSource = global.mockKeysSource = (global.mockKeysSource || {})
3535

36+
// use fs when logging from hooks, console.log async unpredictable
37+
const log = (...args) => (
38+
fs.writeSync(1, JSON.stringify(args, null, ' ').slice(2, -1)))
39+
3640
// node v20.0-v20.6
3741
const globalPreload = !module.register && (({ port }) => (
3842
port.addEventListener('message', ev => (
@@ -170,7 +174,7 @@ const load = async (url, context, nextLoad) => {
170174
const sourceIsNullLike = (
171175
nextLoadRes.source === null || nextLoadRes.source === undefined)
172176
const source = sourceIsNullLike
173-
? String(await fs.readFile(new URL(url)))
177+
? String(fs.readFileSync(new URL(url)))
174178
: String(nextLoadRes.source)
175179
const hbang = (source.match(hashbangRe) || [])[0] || ''
176180
const sourcesafe = hbang ? source.replace(hashbangRe, '') : source

0 commit comments

Comments
 (0)