@@ -34,22 +34,51 @@ module.exports = function resolve(x, options, callback) {
34
34
35
35
opts . paths = opts . paths || [ ] ;
36
36
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
+
37
58
if ( / ^ (?: \. \. ? (?: \/ | $ ) | \/ | ( [ A - Z a - z ] : ) ? [ / \\ ] ) / . test ( x ) ) {
38
- var res = path . resolve ( y , x ) ;
59
+ var res = path . normalize ( path . join ( y , x ) ) ;
39
60
if ( x === '..' || x . slice ( - 1 ) === '/' ) res += '/' ;
40
61
if ( / \/ $ / . test ( x ) && res === y ) {
41
62
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
+ } ) ;
51
69
}
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
+ }
53
82
54
83
function onfile ( err , m , pkg ) {
55
84
if ( err ) cb ( err ) ;
0 commit comments