import { createMatchPath } from "./match-path-sync" ; import { configLoader , ExplicitParams } from "./config-loader" ; import { options } from "./options" ; const noOp = ( ) : void => void 0 ; function getCoreModules ( builtinModules ) { builtinModules = builtinModules || [ "assert" , "buffer" , "child_process" , "cluster" , "crypto" , "dgram" , "dns" , "domain" , "events" , "fs" , "http" , "https" , "net" , "os" , "path" , "punycode" , "querystring" , "readline" , "stream" , "string_decoder" , "tls" , "tty" , "url" , "util" , "v8" , "vm" , "zlib" , ] ; const coreModules = { } ; for ( let module of builtinModules ) { coreModules [ module ] = true ; } return coreModules ; } /**
 * Installs a custom module load function that can adhere to paths in tsconfig.
 * Returns a function to undo paths registration.
 */ export function register ( explicitParams ) { const configLoaderResult = configLoader ( { cwd : options . cwd , explicitParams , } ) ; if ( configLoaderResult . resultType === "failed" ) { console . warn ( `${ configLoaderResult . message }. tsconfig-paths will be skipped` ) ; return noOp ; } const matchPath = createMatchPath ( configLoaderResult . absoluteBaseUrl , configLoaderResult . paths , configLoaderResult . mainFields , configLoaderResult . addMatchAll ) ; const Module = require ( "module" ) ; const originalResolveFilename = Module . _resolveFilename ; const coreModules = getCoreModules ( Module . builtinModules ) ; Module . _resolveFilename = function ( request , _parent ) : string { const isCoreModule = coreModules . hasOwnProperty ( request ) ; if ( ! isCoreModule ) { const found = matchPath ( request ) ; if ( found ) { const modifiedArguments = [ found , ... [ ] . slice . call ( arguments , 1 ) ] ; return originalResolveFilename . apply ( this , modifiedArguments ) ; } } return originalResolveFilename . apply ( this , arguments ) ; } ; return ( ) => { Module . _resolveFilename = originalResolveFilename ; } ; }