1- import fs from 'node:fs/promises '
1+ import fs from 'node:fs'
22import module from 'node:module'
33import process from 'process'
44import esmockErr from './esmockErr.js'
@@ -25,14 +25,23 @@ const isnotfoundRe = /isfound=false/
2525const iscommonjsmoduleRe = / ^ ( c o m m o n j s | m o d u l e ) $ /
2626const isstrict3 = / s t r i c t = 3 /
2727const hashbangRe = / ^ ( # ! [ ^ \n ] * \n ) /
28+
29+ // escape '+' char, pnpm generates long pathnames serialized with these
30+ const moduleIdEsc = str =>
31+ str . indexOf ( '+' ) >= 0 ? str . replace ( / (? ! \\ ) \+ / g, '\\+' ) : str
32+
2833// returned regexp will match embedded moduleid w/ treeid
2934const moduleIdReCreate = ( moduleid , treeid ) => new RegExp (
30- `.*(${ moduleid } (\\?${ treeid } (?:(?!#-#).)*)).*` )
35+ `.*(${ moduleIdEsc ( moduleid ) } (\\?${ treeid } (?:(?!#-#).)*)).*` )
3136
3237// node v12.0-v18.x, global
3338const mockKeys = global . mockKeys = ( global . mockKeys || { } )
3439const mockKeysSource = global . mockKeysSource = ( global . mockKeysSource || { } )
3540
41+ // use fs when logging from hooks, console.log async unpredictable
42+ const log = ( ...args ) => (
43+ fs . writeSync ( 1 , JSON . stringify ( args , null , ' ' ) . slice ( 2 , - 1 ) ) )
44+
3645// node v20.0-v20.6
3746const globalPreload = ! module . register && ( ( { port } ) => (
3847 port . addEventListener ( 'message' , ev => (
@@ -170,7 +179,7 @@ const load = async (url, context, nextLoad) => {
170179 const sourceIsNullLike = (
171180 nextLoadRes . source === null || nextLoadRes . source === undefined )
172181 const source = sourceIsNullLike
173- ? String ( await fs . readFile ( new URL ( url ) ) )
182+ ? String ( fs . readFileSync ( new URL ( url ) ) )
174183 : String ( nextLoadRes . source )
175184 const hbang = ( source . match ( hashbangRe ) || [ ] ) [ 0 ] || ''
176185 const sourcesafe = hbang ? source . replace ( hashbangRe , '' ) : source
0 commit comments