import path from "path" ; import url from "url" ; const formattedImport = async ( file ) => { if ( path . isAbsolute ( file ) ) { try { return await import ( url . pathToFileURL ( file ) . toString ( ) ) ; } catch ( error ) { if ( error instanceof SyntaxError && error . message && error . stack && ! error . stack . includes ( file ) ) { const newErrorWithFilename = new SyntaxError ( error . message ) ; newErrorWithFilename . stack = error . stack . replace ( /^SyntaxError/ , `SyntaxError[ @${ file } ]` ) ; throw newErrorWithFilename ; } throw error ; } } return import ( file ) ; } ; const hasStableEsmImplementation = ( ( ) => { const [ major , minor ] = process . version . split ( "." ) ; const majorNumber = parseInt ( major . slice ( 1 ) , 10 ) ; const minorNumber = parseInt ( minor , 10 ) ; return majorNumber > 12 || ( majorNumber === 12 && minorNumber >= 22 ) ; } ) ( ) ; const implementationOfRequireOrImportForUnstableEsm = async ( file ) => { if ( path . extname ( file ) === ".mjs" ) { return formattedImport ( file ) ; } try { return require ( file ) ; } catch ( error ) { if ( error . code === "ERR_REQUIRE_ESM" ) { return formattedImport ( file ) ; } else { throw error ; } } } ; const dealWithExports = ( module ) => { if ( module . default ) { return module . default ; } else { return { ... module , default : undefined } ; } } ; export const requireOrImport = hasStableEsmImplementation ? async ( file ) => { if ( path . extname ( file ) === ".mjs" ) { return formattedImport ( file ) ; } try { return dealWithExports ( await formattedImport ( file ) ) ; } catch ( err ) { if ( err . code === "ERR_MODULE_NOT_FOUND" || err . code === "ERR_UNKNOWN_FILE_EXTENSION" || err . code === "ERR_UNSUPPORTED_DIR_IMPORT" ) { try { return require ( file ) ; } catch ( requireErr ) { if ( requireErr . code === "ERR_REQUIRE_ESM" ) { throw err ; } else { throw requireErr ; } } } else { throw err ; } } } : implementationOfRequireOrImportForUnstableEsm ; export const loadFilesAsync = async ( files , preLoadFunc , postLoadFunc ) => { for ( const file of files ) { preLoadFunc ( file ) ; const result = await exports . requireOrImport ( path . resolve ( file ) ) ; postLoadFunc ( file , result ) ; } } ;