Skip to content

Commit

Permalink
add log function for debugging loader
Browse files Browse the repository at this point in the history
  • Loading branch information
bumblehead committed Oct 17, 2024
1 parent 253eec5 commit d8af0ba
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# changelog

* 2.6.8 _tbd_
* add log utility for debugging loader
* 2.6.7 _Jul.16.2024_
* [add swc tests and remove swc caution from README](https://github.com/iambumblehead/esmock/pull/309) thanks @Brooooooklyn
* [unpin node 22.1 at test CI](https://github.com/iambumblehead/esmock/pull/309) and use latest 22.x
Expand Down
8 changes: 6 additions & 2 deletions src/esmockLoader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'node:fs/promises'
import fs from 'node:fs'
import module from 'node:module'
import process from 'process'
import esmockErr from './esmockErr.js'
Expand Down Expand Up @@ -33,6 +33,10 @@ const moduleIdReCreate = (moduleid, treeid) => new RegExp(
const mockKeys = global.mockKeys = (global.mockKeys || {})
const mockKeysSource = global.mockKeysSource = (global.mockKeysSource || {})

// use fs when logging from hooks, console.log async unpredictable
const log = (...args) => (
fs.writeSync(1, JSON.stringify(args, null, ' ').slice(2, -1)))

// node v20.0-v20.6
const globalPreload = !module.register && (({ port }) => (
port.addEventListener('message', ev => (
Expand Down Expand Up @@ -170,7 +174,7 @@ const load = async (url, context, nextLoad) => {
const sourceIsNullLike = (
nextLoadRes.source === null || nextLoadRes.source === undefined)
const source = sourceIsNullLike
? String(await fs.readFile(new URL(url)))
? String(fs.readFileSync(new URL(url)))
: String(nextLoadRes.source)
const hbang = (source.match(hashbangRe) || [])[0] || ''
const sourcesafe = hbang ? source.replace(hashbangRe, '') : source
Expand Down

0 comments on commit d8af0ba

Please sign in to comment.