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