@@ -29,6 +29,7 @@ import { packageFilter } from './utils/resolve-utils';
2929import { ignoreNextCache , deleteAPICache , clearIndexedDBCache } from './cache' ;
3030import { shouldTranspile } from './transpilers/babel/check' ;
3131import { splitQueryFromPath } from './utils/query-path' ;
32+ import { measure , endMeasure } from '../utils/metrics' ;
3233
3334declare const BrowserFS : any ;
3435
@@ -598,102 +599,106 @@ export default class Manager {
598599 currentPath : string ,
599600 defaultExtensions = [ 'js' , 'jsx' , 'json' ]
600601 ) : Promise < Module > {
601- return new Promise ( promiseResolve => {
602- const dirredPath = pathUtils . dirname ( currentPath ) ;
603- if ( this . cachedPaths [ dirredPath ] === undefined ) {
604- this . cachedPaths [ dirredPath ] = { } ;
605- }
602+ const dirredPath = pathUtils . dirname ( currentPath ) ;
603+ if ( this . cachedPaths [ dirredPath ] === undefined ) {
604+ this . cachedPaths [ dirredPath ] = { } ;
605+ }
606606
607- const cachedPath = this . cachedPaths [ dirredPath ] [ path ] ;
607+ const cachedPath = this . cachedPaths [ dirredPath ] [ path ] ;
608608
609- let resolvedPath ;
609+ if ( cachedPath ) {
610+ return Promise . resolve ( this . transpiledModules [ cachedPath ] . module ) ;
611+ }
610612
611- if ( cachedPath ) {
612- resolvedPath = cachedPath ;
613- } else {
614- const presetAliasedPath = this . getPresetAliasedPath ( path ) ;
613+ const measureKey = `resolve-async- ${ path } : ${ currentPath } ` ;
614+ return new Promise ( ( promiseResolve , promiseReject ) => {
615+ measure ( measureKey ) ;
616+ const presetAliasedPath = this . getPresetAliasedPath ( path ) ;
615617
616- const aliasedPath = this . getAliasedDependencyPath (
617- presetAliasedPath ,
618- currentPath
619- ) ;
620- const shimmedPath = coreLibraries [ aliasedPath ] || aliasedPath ;
618+ const aliasedPath = this . getAliasedDependencyPath (
619+ presetAliasedPath ,
620+ currentPath
621+ ) ;
622+ const shimmedPath = coreLibraries [ aliasedPath ] || aliasedPath ;
621623
622- if ( NODE_LIBS . indexOf ( shimmedPath ) > - 1 ) {
623- this . cachedPaths [ dirredPath ] [ path ] = shimmedPath ;
624- promiseResolve ( getShimmedModuleFromPath ( currentPath , path ) ) ;
625- return ;
626- }
624+ if ( NODE_LIBS . indexOf ( shimmedPath ) > - 1 ) {
625+ this . cachedPaths [ dirredPath ] [ path ] = shimmedPath ;
626+ promiseResolve ( getShimmedModuleFromPath ( currentPath , path ) ) ;
627+ return ;
628+ }
627629
628- try {
629- resolve (
630- shimmedPath ,
631- {
632- filename : currentPath ,
633- extensions : defaultExtensions . map ( ext => '.' + ext ) ,
634- isFile : this . isFile ,
635- readFileSync : this . readFileSync ,
636- packageFilter,
637- moduleDirectory : this . getModuleDirectories ( ) ,
638- } ,
639- ( err , foundPath ) => {
640- if ( err ) {
641- throw err ;
642- }
630+ resolve (
631+ shimmedPath ,
632+ {
633+ filename : currentPath ,
634+ extensions : defaultExtensions . map ( ext => '.' + ext ) ,
635+ isFile : this . isFile ,
636+ readFileSync : this . readFileSync ,
637+ packageFilter,
638+ moduleDirectory : this . getModuleDirectories ( ) ,
639+ } ,
640+ ( err , foundPath ) => {
641+ endMeasure ( measureKey , { silent : true } ) ;
642+ if ( err ) {
643+ if (
644+ this . cachedPaths [ dirredPath ] &&
645+ this . cachedPaths [ dirredPath ] [ path ]
646+ ) {
647+ delete this . cachedPaths [ dirredPath ] [ path ] ;
648+ }
643649
644- this . cachedPaths [ dirredPath ] [ path ] = foundPath ;
650+ let connectedPath = shimmedPath ;
651+ if ( connectedPath . indexOf ( '/node_modules' ) !== 0 ) {
652+ connectedPath = / ^ ( \w | @ \w ) / . test ( shimmedPath )
653+ ? pathUtils . join ( '/node_modules' , shimmedPath )
654+ : pathUtils . join ( pathUtils . dirname ( currentPath ) , shimmedPath ) ;
655+ }
645656
646- if ( foundPath === '//empty.js' ) {
647- promiseResolve ( getShimmedModuleFromPath ( currentPath , path ) ) ;
648- return ;
649- }
657+ const isDependency = connectedPath . includes ( '/node_modules/' ) ;
650658
651- if ( ! this . transpiledModules [ foundPath ] ) {
652- this . readFileSync ( foundPath , code => {
653- this . addModule ( { path : foundPath , code } ) ;
654- promiseResolve ( this . transpiledModules [ foundPath ] . module ) ;
655- } ) ;
656- } else {
657- promiseResolve ( this . transpiledModules [ foundPath ] . module ) ;
658- }
659+ connectedPath = connectedPath . replace ( '/node_modules/' , '' ) ;
660+
661+ if ( ! isDependency ) {
662+ promiseReject (
663+ new ModuleNotFoundError ( shimmedPath , false , currentPath )
664+ ) ;
659665 }
660- ) ;
661- } catch ( e ) {
662- if (
663- this . cachedPaths [ dirredPath ] &&
664- this . cachedPaths [ dirredPath ] [ path ]
665- ) {
666- delete this . cachedPaths [ dirredPath ] [ path ] ;
667- }
668666
669- let connectedPath = shimmedPath ;
670- if ( connectedPath . indexOf ( '/node_modules' ) !== 0 ) {
671- connectedPath = / ^ ( \w | @ \w ) / . test ( shimmedPath )
672- ? pathUtils . join ( '/node_modules' , shimmedPath )
673- : pathUtils . join ( pathUtils . dirname ( currentPath ) , shimmedPath ) ;
674- }
667+ const dependencyName = getDependencyName ( connectedPath ) ;
668+
669+ if (
670+ this . manifest . dependencies . find ( d => d . name === dependencyName ) ||
671+ this . manifest . dependencyDependencies [ dependencyName ]
672+ ) {
673+ promiseReject (
674+ new ModuleNotFoundError ( connectedPath , true , currentPath )
675+ ) ;
676+ } else {
677+ promiseReject (
678+ new DependencyNotFoundError ( connectedPath , currentPath )
679+ ) ;
680+ }
675681
676- const isDependency = connectedPath . includes ( '/node_modules/' ) ;
682+ return ;
683+ }
677684
678- connectedPath = connectedPath . replace ( '/node_modules/' , '' ) ;
685+ this . cachedPaths [ dirredPath ] [ path ] = foundPath ;
679686
680- if ( ! isDependency ) {
681- throw new ModuleNotFoundError ( shimmedPath , false , currentPath ) ;
687+ if ( foundPath === '//empty.js' ) {
688+ promiseResolve ( getShimmedModuleFromPath ( currentPath , path ) ) ;
689+ return ;
682690 }
683691
684- const dependencyName = getDependencyName ( connectedPath ) ;
685-
686- if (
687- this . manifest . dependencies . find ( d => d . name === dependencyName ) ||
688- this . manifest . dependencyDependencies [ dependencyName ]
689- ) {
690- throw new ModuleNotFoundError ( connectedPath , true , currentPath ) ;
692+ if ( ! this . transpiledModules [ foundPath ] ) {
693+ this . readFileSync ( foundPath , code => {
694+ this . addModule ( { path : foundPath , code } ) ;
695+ promiseResolve ( this . transpiledModules [ foundPath ] . module ) ;
696+ } ) ;
691697 } else {
692- throw new DependencyNotFoundError ( connectedPath , currentPath ) ;
698+ promiseResolve ( this . transpiledModules [ foundPath ] . module ) ;
693699 }
694700 }
695- }
696- promiseResolve ( this . transpiledModules [ resolvedPath ] . module ) ;
701+ ) ;
697702 } ) ;
698703 }
699704
@@ -715,6 +720,8 @@ export default class Manager {
715720 if ( cachedPath && this . transpiledModules [ cachedPath ] ) {
716721 resolvedPath = cachedPath ;
717722 } else {
723+ const measureKey = `resolve-sync-${ path } :${ currentPath } ` ;
724+ measure ( measureKey ) ;
718725 const presetAliasedPath = this . getPresetAliasedPath ( path ) ;
719726
720727 const aliasedPath = this . getAliasedDependencyPath (
@@ -737,6 +744,7 @@ export default class Manager {
737744 packageFilter,
738745 moduleDirectory : this . getModuleDirectories ( ) ,
739746 } ) ;
747+ endMeasure ( measureKey , { silent : true } ) ;
740748
741749 this . cachedPaths [ dirredPath ] [ path ] = resolvedPath ;
742750
@@ -823,12 +831,11 @@ export default class Manager {
823831 const tModule =
824832 currentTModule || this . getTranspiledModule ( this . modules [ '/package.json' ] ) ; // Get arbitrary file from root
825833 try {
826- return Promise . resolve (
827- this . resolveTranspiledModule (
828- path ,
829- tModule . module . path ,
830- ignoredExtensions
831- )
834+ return this . resolveTranspiledModule (
835+ path ,
836+ tModule . module . path ,
837+ ignoredExtensions ,
838+ true
832839 ) ;
833840 } catch ( e ) {
834841 if ( e . type === 'module-not-found' && e . isDependency ) {
0 commit comments