import type { MatchPath } from 'tsconfig-paths' ; import { createMatchPath , loadConfig } from 'tsconfig-paths' ; /**
 * Jest 解析器配置
 */ interface JestResolverOptions { /**
     * 基本路径
     */ basedir : string ; /**
     * 是否是在浏览器中执行
     */ browser ? : boolean ; /**
     * 默认的解析器
     */ defaultResolver : ( request : string , options : JestResolverOptions ) => string ; /**
     * 代码文件扩展
     */ extensions ? : string [ ] ; /**
     * 模块目录
     */ moduleDirectory ? : string [ ] ; /**
     * 路径
     */ paths ? : string [ ] ; /**
     * 根目录
     */ rootDir ? : string [ ] ; } let cachedMatchPath ; /**
 * 获取或者创建匹配路径
 */ function getOrCreateMatchPath ( ) { if ( ! cachedMatchPath ) { const tsconfig = loadConfig ( ) ; if ( tsconfig . resultType === 'success' ) { cachedMatchPath = createMatchPath ( tsconfig . absoluteBaseUrl , tsconfig . paths , [ 'main' ] , tsconfig . addMatchAll ) ; } } return cachedMatchPath ; } param request param options function tsConfigPathsResolver ( request , options ) { const matchPath = getOrCreateMatchPath ( ) ; if ( matchPath ) { const pathResolveFromTsconfigPaths = matchPath ( request , undefined , undefined , [ '.ts' , '.tsx' , '.js' , '.jsx' , '.json' , '.node' ] ) ; return options . defaultResolver ( pathResolveFromTsconfigPaths || request , options ) ; } return options . defaultResolver ( request , options ) ; } module . exports = tsConfigPathsResolver ;