import { builtinModules } from 'module' ; import { loadConfig , createMatchPath } from 'tsconfig-paths' ; const noOp = ( ) => { } ; export function registerTsconfigPaths ( ) { const configLoaderResult = loadConfig ( ) ; if ( configLoaderResult . resultType === 'failed' ) { return noOp ; } const matchPath = createMatchPath ( configLoaderResult . absoluteBaseUrl , configLoaderResult . paths , configLoaderResult . mainFields , configLoaderResult . addMatchAll ) ; const Module = require ( 'module' ) ; const originalResolveFilename = Module . _resolveFilename ; Module . _resolveFilename = function ( request , _parent ) : string { const isCoreModule = builtinModules . includes ( 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 ; } ; }