import { Screen , Device } from '../platform' ; import * as appCommonModule from '../application/application-common' ; import { PlatformContext , findMatch , stripQualifiers } from './qualifier-matcher' ; import { registerModulesFromFileSystem } from './non-bundle-workflow-compat' ; import { Trace } from '../trace' ; export type { PlatformContext } from './qualifier-matcher' ; export type ModuleListProvider = ( ) => string [ ] ; export class ModuleNameResolver { private _cache = { } ; constructor ( private context , private moduleListProvider = global . getRegisteredModules ) { } public resolveModuleName ( path , ext ) { const key = path + ext ; let result = this . _cache [ key ] ; if ( result === undefined ) { result = this . resolveModuleNameImpl ( path , ext ) ; this . _cache [ key ] = result ; } if ( Trace . isEnabled ( ) ) { Trace . write ( `path: '${ path }' with ext: '${ ext }' resolved: '${ result }'` , Trace . categories . ModuleNameResolver ) ; } return result ; } public clearCache ( ) { this . _cache = { } ; } private resolveModuleNameImpl ( path , ext ) { let result = null ; ext = ext ? '.' + ext : '' ; path = stripQualifiers ( path ) ; const candidates = this . getCandidates ( path , ext ) ; result = findMatch ( path , ext , candidates , this . context ) ; return result ; } private getCandidates ( path , ext ) { const candidates = this . moduleListProvider ( ) . filter ( ( moduleName ) => moduleName . startsWith ( path ) && ( ! ext || moduleName . endsWith ( ext ) ) ) ; return candidates ; } } let resolverInstance ; export function resolveModuleName ( path , ext ) { if ( global . __snapshot ) { return resolveModuleSnapshot ( path , ext ) ; } if ( ! resolverInstance ) { resolverInstance = new ModuleNameResolver ( { width : Screen . mainScreen . widthDIPs , height : Screen . mainScreen . heightDIPs , os : Device . os , deviceType : Device . deviceType , } ) ; } return resolverInstance . resolveModuleName ( path , ext ) ; } function resolveModuleSnapshot ( path , ext ) { Trace . write ( `Resolving module in SNAPSHOT context - path: '${ path }' with ext: '${ ext }'` , Trace . categories . ModuleNameResolver ) ; return new ModuleNameResolver ( { width : 400 , height : 800 , os : 'Android' , deviceType : 'Phone' , } ) . resolveModuleName ( path , ext ) ; } export function clearCache ( ) { if ( resolverInstance ) { resolverInstance . clearCache ( ) ; } } export function _setResolver ( resolver ) { resolverInstance = resolver ; } appCommonModule . on ( 'livesync' , ( args ) => clearCache ( ) ) ; appCommonModule . on ( 'orientationChanged' , ( args ) => { resolverInstance = undefined ; } ) ;