diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e5e0ee..467a44b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/esmockLoader.js b/src/esmockLoader.js index 9770fc8..0b16977 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -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' @@ -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 => ( @@ -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