@@ -44,6 +44,16 @@ module.exports = function (x, options) {
4444 throw err ;
4545
4646 function loadAsFileSync ( x ) {
47+ var pkg = loadpkg ( path . dirname ( x ) ) ;
48+
49+ if ( pkg && pkg . dir && pkg . pkg && opts . pathFilter ) {
50+ var rfile = path . relative ( pkg . dir , x ) ;
51+ var r = opts . pathFilter ( pkg . pkg , x , rfile ) ;
52+ if ( r ) {
53+ x = path . resolve ( pkg . dir , r ) ; // eslint-disable-line no-param-reassign
54+ }
55+ }
56+
4757 if ( isFile ( x ) ) {
4858 return x ;
4959 }
@@ -56,6 +66,32 @@ module.exports = function (x, options) {
5666 }
5767 }
5868
69+ function loadpkg ( dir ) {
70+ if ( dir === '' || dir === '/' ) return ;
71+ if ( process . platform === 'win32' && ( / ^ \w : [ / \\ ] * $ / ) . test ( dir ) ) {
72+ return ;
73+ }
74+ if ( / [ / \\ ] n o d e _ m o d u l e s [ / \\ ] * $ / . test ( dir ) ) return ;
75+
76+ var pkgfile = path . join ( dir , 'package.json' ) ;
77+
78+ if ( ! isFile ( pkgfile ) ) {
79+ return loadpkg ( path . dirname ( dir ) ) ;
80+ }
81+
82+ var body = readFileSync ( pkgfile ) ;
83+
84+ try {
85+ var pkg = JSON . parse ( body ) ;
86+ } catch ( jsonErr ) { }
87+
88+ if ( pkg && opts . packageFilter ) {
89+ pkg = opts . packageFilter ( pkg , pkgfile ) ;
90+ }
91+
92+ return { pkg : pkg , dir : dir } ;
93+ }
94+
5995 function loadAsDirectorySync ( x ) {
6096 var pkgfile = path . join ( x , '/package.json' ) ;
6197 if ( isFile ( pkgfile ) ) {
0 commit comments