@@ -40,22 +40,37 @@ module.exports = function resolve(x, options, callback) {
4040
4141 opts . paths = opts . paths || [ ] ;
4242
43- if ( ( / ^ (?: \. \. ? (?: \/ | $ ) | \/ | ( [ A - Z a - z ] : ) ? [ / \\ ] ) / ) . test ( x ) ) {
44- var res = path . resolve ( basedir , x ) ;
45- if ( x === '..' || x . slice ( - 1 ) === '/' ) res += '/' ;
46- if ( ( / \/ $ / ) . test ( x ) && res === basedir ) {
47- loadAsDirectory ( res , opts . package , onfile ) ;
48- } else loadAsFile ( res , opts . package , onfile ) ;
49- } else loadNodeModules ( x , basedir , function ( err , n , pkg ) {
50- if ( err ) cb ( err ) ;
51- else if ( n ) cb ( null , n , pkg ) ;
52- else if ( core [ x ] ) return cb ( null , x ) ;
53- else {
54- var moduleError = new Error ( "Cannot find module '" + x + "' from '" + parent + "'" ) ;
55- moduleError . code = 'MODULE_NOT_FOUND' ;
56- cb ( moduleError ) ;
57- }
58- } ) ;
43+ // ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory
44+ var absoluteStart = path . resolve ( basedir ) ;
45+
46+ if ( opts . preserveSymlinks === false ) {
47+ fs . realpath ( absoluteStart , function ( realPathErr , realStart ) {
48+ if ( realPathErr && realPathErr . code !== 'ENOENT' ) cb ( err ) ;
49+ else init ( realStart ) ;
50+ } ) ;
51+ } else {
52+ init ( absoluteStart ) ;
53+ }
54+
55+ var res ;
56+ function init ( basedir ) {
57+ if ( ( / ^ (?: \. \. ? (?: \/ | $ ) | \/ | ( [ A - Z a - z ] : ) ? [ / \\ ] ) / ) . test ( x ) ) {
58+ res = path . resolve ( basedir , x ) ;
59+ if ( x === '..' || x . slice ( - 1 ) === '/' ) res += '/' ;
60+ if ( ( / \/ $ / ) . test ( x ) && res === basedir ) {
61+ loadAsDirectory ( res , opts . package , onfile ) ;
62+ } else loadAsFile ( res , opts . package , onfile ) ;
63+ } else loadNodeModules ( x , basedir , function ( err , n , pkg ) {
64+ if ( err ) cb ( err ) ;
65+ else if ( n ) cb ( null , n , pkg ) ;
66+ else if ( core [ x ] ) return cb ( null , x ) ;
67+ else {
68+ var moduleError = new Error ( "Cannot find module '" + x + "' from '" + parent + "'" ) ;
69+ moduleError . code = 'MODULE_NOT_FOUND' ;
70+ cb ( moduleError ) ;
71+ }
72+ } ) ;
73+ }
5974
6075 function onfile ( err , m , pkg ) {
6176 if ( err ) cb ( err ) ;
0 commit comments