@@ -2,25 +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,
11
- // resolving against the process' current working directory
12
- var absoluteStart = path . resolve ( start ) ;
13
-
14
- if ( opts && opts . preserveSymlinks === false ) {
15
- try {
16
- absoluteStart = fs . realpathSync ( absoluteStart ) ;
17
- } catch ( err ) {
18
- if ( err . code !== 'ENOENT' ) {
19
- throw err ;
20
- }
21
- }
22
- }
23
-
5
+ var getNodeModulesDirs = function getNodeModulesDirs ( absoluteStart , modules ) {
24
6
var prefix = '/' ;
25
7
if ( ( / ^ ( [ A - Z a - z ] : ) / ) . test ( absoluteStart ) ) {
26
8
prefix = '' ;
@@ -35,17 +17,40 @@ module.exports = function nodeModulesPaths(start, opts, request) {
35
17
parsed = parse ( parsed . dir ) ;
36
18
}
37
19
38
- var dirs = paths . reduce ( function ( dirs , aPath ) {
20
+ return paths . reduce ( function ( dirs , aPath ) {
39
21
return dirs . concat ( modules . map ( function ( moduleDir ) {
40
22
return path . join ( prefix , aPath , moduleDir ) ;
41
23
} ) ) ;
42
24
} , [ ] ) ;
25
+ } ;
43
26
44
- if ( ! opts || ! opts . paths ) {
45
- 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 === false ) {
36
+ try {
37
+ absoluteStart = fs . realpathSync ( absoluteStart ) ;
38
+ } catch ( err ) {
39
+ if ( err . code !== 'ENOENT' ) {
40
+ throw err ;
41
+ }
42
+ }
46
43
}
47
- if ( typeof opts . paths === 'function' ) {
48
- 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
+ ) ;
49
52
}
50
- return dirs . concat ( opts . paths ) ;
53
+
54
+ var dirs = getNodeModulesDirs ( absoluteStart , modules ) ;
55
+ return opts && opts . paths ? dirs . concat ( opts . paths ) : dirs ;
51
56
} ;
0 commit comments