import { FileModuleRef , getMangledPackageName , PackageModuleRef , } from "@rnx-kit/tools-node" ; import path from "path" ; import ts from "typescript" ; import { ExtensionsTypeScript } from "./extension" ; import { findModuleFile } from "./module" ; import { ResolverContext } from "./types" ; param context param packageDir param modulePath param extensions returns export function resolveModule ( context , packageDir , modulePath , extensions ) { if ( modulePath ) { return findModuleFile ( context , packageDir , modulePath , extensions ) ; } const { host , log } = context ; let module ; log . log ( "Reading package.json from directory %o." , packageDir ) ; const result = host . readFile ( path . join ( packageDir , "package.json" ) ) ; if ( ! result ) { throw new Error ( `Failed to read package.json from directory '${ packageDir }'` ) ; } const { types , typings , main } = JSON . parse ( result ) ; if ( extensions . includes ( ts . Extension . Dts ) ) { if ( typeof types === "string" && types . length > 0 ) { log . log ( "Package has 'types' field '%s'." , types ) ; module = findModuleFile ( context , packageDir , types , extensions ) ; } else if ( typeof typings === "string" && typings . length > 0 ) { log . log ( "Package has 'typings' field '%s'." , typings ) ; module = findModuleFile ( context , packageDir , typings , extensions ) ; } } if ( ! module && typeof main === "string" && main . length > 0 ) { log . log ( "Package has 'main' field '%s'." , main ) ; module = findModuleFile ( context , packageDir , main , extensions ) ; } if ( ! module ) { log . log ( "Searching for index file." ) ; module = findModuleFile ( context , packageDir , "index" , extensions ) ; } return module ; } function findPackageDependencyDir ( context , ref , startDir ) { const { log , host } = context ; log . log ( "Searching for external package %o starting in %o." , ref . scope ? ref . scope + "/" + ref . name : ref . name , startDir ) ; const suffixDir = path . join ( "node_modules" , ref . scope ?? "" , ref . name ) ; let searchDir = startDir ; const { root : searchRoot } = path . parse ( searchDir ) ; while ( true ) { const p = path . join ( searchDir , suffixDir ) ; if ( host . directoryExists ( p ) ) { return p ; } if ( searchDir === searchRoot ) { return undefined ; } searchDir = path . dirname ( searchDir ) ; } } param context param moduleRef param searchDir param extensions returns export function resolvePackageModule ( context , moduleRef , searchDir , extensions ) { const { log } = context ; let module = undefined ; log . log ( "Searching for external package %o starting in %o." , moduleRef . scope ? moduleRef . scope + "/" + moduleRef . name : moduleRef . name , searchDir ) ; const pkgDir = findPackageDependencyDir ( context , moduleRef , searchDir ) ; if ( pkgDir ) { log . log ( "Loading module from external package '%s'." , pkgDir ) ; module = resolveModule ( context , pkgDir , moduleRef . path , extensions ) ; if ( ! module && moduleRef . path ) { module = resolveModule ( context , pkgDir , undefined , ExtensionsTypeScript ) ; } } if ( ! module && moduleRef . scope !== "@types" ) { const typesModuleRef = { scope : "@types" , name : getMangledPackageName ( moduleRef ) , path : moduleRef . path } ; module = resolvePackageModule ( context , typesModuleRef , searchDir , ExtensionsTypeScript ) ; } return module ; } param context param moduleRef param searchDir param extensions returns export function resolveFileModule ( context , moduleRef , searchDir , extensions ) { context . log . log ( "Loading module from directory '%s'." , searchDir ) ; return findModuleFile ( context , searchDir , moduleRef . path , extensions ) ; }