From 90b11921181c2783209e9aa31f1e20d98c11ed17 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 4 Apr 2018 22:04:03 -0700 Subject: [PATCH 1/3] [Tests] add a failing test --- test/pathfilter.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/pathfilter.js b/test/pathfilter.js index eca7793b..16519aea 100644 --- a/test/pathfilter.js +++ b/test/pathfilter.js @@ -50,16 +50,23 @@ test('#62: deep module references and the pathFilter', function (t) { }); t.test('deep/ref alt', function (st) { - st.plan(4); + st.plan(8); var pathFilter = pathFilterFactory(st); + var res = resolve.sync( + 'deep/ref', + { basedir: resolverDir, pathFilter: pathFilter } + ); + st.equal(res, path.join(resolverDir, 'node_modules/deep/alt.js')); + resolve( 'deep/ref', { basedir: resolverDir, pathFilter: pathFilter }, function (err, res, pkg) { if (err) st.fail(err); st.equal(res, path.join(resolverDir, 'node_modules/deep/alt.js')); + st.end(); } ); }); From dc23387adb93f497d67def7ee99fae48e5958fb3 Mon Sep 17 00:00:00 2001 From: Bogdan Luca Date: Wed, 29 Nov 2017 13:47:34 +0200 Subject: [PATCH 2/3] Made loadAsFileSync() work the same as async loadAsFile(). --- lib/sync.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lib/sync.js b/lib/sync.js index aa2359b3..c404d233 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -44,6 +44,16 @@ module.exports = function (x, options) { throw err; function loadAsFileSync(x) { + var pkg = loadpkg(path.dirname(x)); + + if (pkg && pkg.dir && pkg.pkg && opts.pathFilter) { + var rfile = path.relative(pkg.dir, x); + var r = opts.pathFilter(pkg.pkg, x, rfile); + if (r) { + x = path.resolve(pkg.dir, r); + } + } + if (isFile(x)) { return x; } @@ -56,6 +66,30 @@ module.exports = function (x, options) { } } + function loadpkg(dir) { + if (dir === '' || dir === '/' || (/[/\\]node_modules[/\\]*$/).test(dir)) { + return null; + } + + var pkgfile = path.join(dir, 'package.json'); + + if (!isFile(pkgfile)) { + return loadpkg(path.dirname(dir)); + } + + var body = readFileSync(pkgfile); + + try { + var pkg = JSON.parse(body); + } catch (jsonErr) {} + + if (pkg && opts.packageFilter) { + pkg = opts.packageFilter(pkg, pkgfile); + } + + return { pkg: pkg, dir: dir }; + } + function loadAsDirectorySync(x) { var pkgfile = path.join(x, '/package.json'); if (isFile(pkgfile)) { From c449d4809cf8461a3d54e458780902b95119a969 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 4 Apr 2018 19:46:10 -0700 Subject: [PATCH 3/3] Minor cleanup --- lib/sync.js | 8 +++++--- readme.markdown | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/sync.js b/lib/sync.js index c404d233..2e0d5f24 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -50,7 +50,7 @@ module.exports = function (x, options) { var rfile = path.relative(pkg.dir, x); var r = opts.pathFilter(pkg.pkg, x, rfile); if (r) { - x = path.resolve(pkg.dir, r); + x = path.resolve(pkg.dir, r); // eslint-disable-line no-param-reassign } } @@ -67,9 +67,11 @@ module.exports = function (x, options) { } function loadpkg(dir) { - if (dir === '' || dir === '/' || (/[/\\]node_modules[/\\]*$/).test(dir)) { - return null; + if (dir === '' || dir === '/') return; + if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) { + return; } + if (/[/\\]node_modules[/\\]*$/.test(dir)) return; var pkgfile = path.join(dir, 'package.json'); diff --git a/readme.markdown b/readme.markdown index a5decddd..b59f2ab9 100644 --- a/readme.markdown +++ b/readme.markdown @@ -115,6 +115,12 @@ options are: * `opts.packageFilter(pkg, pkgfile)` - transform the parsed package.json contents before looking at the "main" field +* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package + * pkg - package data + * path - the path being resolved + * relativePath - the path relative from the package.json location + * returns - a relative path that will be joined from the package.json location + * opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this) * opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`