@@ -34,22 +34,51 @@ module.exports = function resolve(x, options, callback) {
3434
3535 opts . paths = opts . paths || [ ] ;
3636
37+ function isFileWithExtensions ( file , cb , extensionIndex ) {
38+ var newExtensionIndex = 0 ;
39+ var filename = file ;
40+ if ( typeof extensionIndex === 'number' ) {
41+ if ( extensionIndex >= extensions . length ) {
42+ return cb ( null , false ) ;
43+ }
44+
45+ newExtensionIndex = extensionIndex + 1 ;
46+ filename = file + extensions [ extensionIndex ] ;
47+ }
48+
49+ isFile ( filename , function ( err , filenameIsFile ) {
50+ if ( err ) { return cb ( err ) ; }
51+ if ( filenameIsFile ) {
52+ return cb ( null , filenameIsFile ) ;
53+ }
54+ isFileWithExtensions ( file , cb , newExtensionIndex ) ;
55+ } ) ;
56+ }
57+
3758 if ( / ^ (?: \. \. ? (?: \/ | $ ) | \/ | ( [ A - Z a - z ] : ) ? [ / \\ ] ) / . test ( x ) ) {
38- var res = path . resolve ( y , x ) ;
59+ var res = path . normalize ( path . join ( y , x ) ) ;
3960 if ( x === '..' || x . slice ( - 1 ) === '/' ) res += '/' ;
4061 if ( / \/ $ / . test ( x ) && res === y ) {
4162 loadAsDirectory ( res , opts . package , onfile ) ;
42- } else loadAsFile ( res , opts . package , onfile ) ;
43- } else loadNodeModules ( x , y , function ( err , n , pkg ) {
44- if ( err ) cb ( err ) ;
45- else if ( n ) cb ( null , n , pkg ) ;
46- else if ( core [ x ] ) return cb ( null , x ) ;
47- else {
48- var moduleError = new Error ( "Cannot find module '" + x + "' from '" + y + "'" ) ;
49- moduleError . code = 'MODULE_NOT_FOUND' ;
50- cb ( moduleError ) ;
63+ } else {
64+ isFileWithExtensions ( res , function ( err , resIsFile ) {
65+ if ( err ) { return onfile ( err ) ; }
66+ if ( resIsFile ) { return loadAsFile ( res , opts . package , onfile ) ; }
67+ return loadAsDirectory ( res , opts . package , onfile ) ;
68+ } ) ;
5169 }
52- } ) ;
70+ } else {
71+ loadNodeModules ( x , y , function ( err , n , pkg ) {
72+ if ( err ) cb ( err ) ;
73+ else if ( n ) cb ( null , n , pkg ) ;
74+ else if ( core [ x ] ) return cb ( null , x ) ;
75+ else {
76+ var moduleError = new Error ( "Cannot find module '" + x + "' from '" + y + "'" ) ;
77+ moduleError . code = 'MODULE_NOT_FOUND' ;
78+ cb ( moduleError ) ;
79+ }
80+ } ) ;
81+ }
5382
5483 function onfile ( err , m , pkg ) {
5584 if ( err ) cb ( err ) ;
0 commit comments