@@ -2,25 +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,
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 ) {
246 var prefix = '/' ;
257 if ( ( / ^ ( [ A - Z a - z ] : ) / ) . test ( absoluteStart ) ) {
268 prefix = '' ;
@@ -35,17 +17,40 @@ module.exports = function nodeModulesPaths(start, opts, request) {
3517 parsed = parse ( parsed . dir ) ;
3618 }
3719
38- var dirs = paths . reduce ( function ( dirs , aPath ) {
20+ return paths . reduce ( function ( dirs , aPath ) {
3921 return dirs . concat ( modules . map ( function ( moduleDir ) {
4022 return path . join ( prefix , aPath , moduleDir ) ;
4123 } ) ) ;
4224 } , [ ] ) ;
25+ } ;
4326
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+ }
4643 }
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+ ) ;
4952 }
50- return dirs . concat ( opts . paths ) ;
53+
54+ var dirs = getNodeModulesDirs ( absoluteStart , modules ) ;
55+ return opts && opts . paths ? dirs . concat ( opts . paths ) : dirs ;
5156} ;
0 commit comments