Skip to content

Commit 7a2edf8

Browse files
authored
Merge pull request #146 from lbogdan/fix/resolve-sync
[Fix] Make loadAsFileSync() work the same as async loadAsFile()
2 parents c3621a3 + c449d48 commit 7a2edf8

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

lib/sync.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 (/[/\\]node_modules[/\\]*$/.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)) {

readme.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ options are:
115115

116116
* `opts.packageFilter(pkg, pkgfile)` - transform the parsed package.json contents before looking at the "main" field
117117

118+
* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
119+
* pkg - package data
120+
* path - the path being resolved
121+
* relativePath - the path relative from the package.json location
122+
* returns - a relative path that will be joined from the package.json location
123+
118124
* opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)
119125

120126
* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`

test/pathfilter.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,23 @@ test('#62: deep module references and the pathFilter', function (t) {
5050
});
5151

5252
t.test('deep/ref alt', function (st) {
53-
st.plan(4);
53+
st.plan(8);
5454

5555
var pathFilter = pathFilterFactory(st);
5656

57+
var res = resolve.sync(
58+
'deep/ref',
59+
{ basedir: resolverDir, pathFilter: pathFilter }
60+
);
61+
st.equal(res, path.join(resolverDir, 'node_modules/deep/alt.js'));
62+
5763
resolve(
5864
'deep/ref',
5965
{ basedir: resolverDir, pathFilter: pathFilter },
6066
function (err, res, pkg) {
6167
if (err) st.fail(err);
6268
st.equal(res, path.join(resolverDir, 'node_modules/deep/alt.js'));
69+
st.end();
6370
}
6471
);
6572
});

0 commit comments

Comments
 (0)