@@ -2,24 +2,7 @@ var path = require('path');
22var fs = require ( 'fs' ) ;
33var parse = path . parse || require ( 'path-parse' ) ;
44
5- module . exports = function nodeModulesPaths ( start , opts , request ) {
6- var modules = opts && opts . moduleDirectory
7- ? [ ] . concat ( opts . moduleDirectory )
8- : [ 'node_modules' ] ;
9-
10- // ensure that `start` is an absolute path at this point, resolving against the process' current working directory
11- var absoluteStart = path . resolve ( start ) ;
12-
13- if ( ! opts || ! opts . preserveSymlinks ) {
14- try {
15- absoluteStart = fs . realpathSync ( absoluteStart ) ;
16- } catch ( err ) {
17- if ( err . code !== 'ENOENT' ) {
18- throw err ;
19- }
20- }
21- }
22-
5+ var getNodeModulesDirs = function getNodeModulesDirs ( absoluteStart , modules ) {
236 var prefix = '/' ;
247 if ( ( / ^ ( [ A - Z a - z ] : ) / ) . test ( absoluteStart ) ) {
258 prefix = '' ;
@@ -34,17 +17,40 @@ module.exports = function nodeModulesPaths(start, opts, request) {
3417 parsed = parse ( parsed . dir ) ;
3518 }
3619
37- var dirs = paths . reduce ( function ( dirs , aPath ) {
20+ return paths . reduce ( function ( dirs , aPath ) {
3821 return dirs . concat ( modules . map ( function ( moduleDir ) {
3922 return path . join ( prefix , aPath , moduleDir ) ;
4023 } ) ) ;
4124 } , [ ] ) ;
25+ } ;
4226
43- if ( ! opts || ! opts . paths ) {
44- return dirs ;
27+ module . exports = function nodeModulesPaths ( start , opts , request ) {
28+ var modules = opts && opts . moduleDirectory
29+ ? [ ] . concat ( opts . moduleDirectory )
30+ : [ 'node_modules' ] ;
31+
32+ // ensure that `start` is an absolute path at this point, resolving against the process' current working directory
33+ var absoluteStart = path . resolve ( start ) ;
34+
35+ if ( ! opts || ! opts . preserveSymlinks ) {
36+ try {
37+ absoluteStart = fs . realpathSync ( absoluteStart ) ;
38+ } catch ( err ) {
39+ if ( err . code !== 'ENOENT' ) {
40+ throw err ;
41+ }
42+ }
4543 }
46- if ( typeof opts . paths === 'function' ) {
47- return dirs . concat ( opts . paths ( request , absoluteStart , opts ) ) ;
44+
45+ if ( opts && typeof opts . paths === 'function' ) {
46+ return opts . paths (
47+ request ,
48+ absoluteStart ,
49+ function ( ) { return getNodeModulesDirs ( absoluteStart , modules ) ; } ,
50+ opts
51+ ) ;
4852 }
49- return dirs . concat ( opts . paths ) ;
53+
54+ var dirs = getNodeModulesDirs ( absoluteStart , modules ) ;
55+ return opts && opts . paths ? dirs . concat ( opts . paths ) : dirs ;
5056} ;
0 commit comments