@@ -2,24 +2,7 @@ var path = require('path');
2
2
var fs = require ( 'fs' ) ;
3
3
var parse = path . parse || require ( 'path-parse' ) ;
4
4
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 ) {
23
6
var prefix = '/' ;
24
7
if ( ( / ^ ( [ A - Z a - z ] : ) / ) . test ( absoluteStart ) ) {
25
8
prefix = '' ;
@@ -34,17 +17,40 @@ module.exports = function nodeModulesPaths(start, opts, request) {
34
17
parsed = parse ( parsed . dir ) ;
35
18
}
36
19
37
- var dirs = paths . reduce ( function ( dirs , aPath ) {
20
+ return paths . reduce ( function ( dirs , aPath ) {
38
21
return dirs . concat ( modules . map ( function ( moduleDir ) {
39
22
return path . join ( prefix , aPath , moduleDir ) ;
40
23
} ) ) ;
41
24
} , [ ] ) ;
25
+ } ;
42
26
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
+ }
45
43
}
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
+ ) ;
48
52
}
49
- return dirs . concat ( opts . paths ) ;
53
+
54
+ var dirs = getNodeModulesDirs ( absoluteStart , modules ) ;
55
+ return opts && opts . paths ? dirs . concat ( opts . paths ) : dirs ;
50
56
} ;
0 commit comments