Skip to content

Commit

Permalink
[FIXES browserify#125] fix when package.json main = ‘.’ or main = ‘./‘
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanpenner committed Apr 20, 2017
1 parent 452fdf9 commit 44df9a9
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,25 @@ module.exports = function (x, options) {
function loadAsDirectorySync(x) {
var pkgfile = path.join(x, '/package.json');
if (isFile(pkgfile)) {
var body = readFileSync(pkgfile, 'utf8');
try {
var body = readFileSync(pkgfile, 'UTF8');
var pkg = JSON.parse(body);

if (opts.packageFilter) {
pkg = opts.packageFilter(pkg, x);
}

if (pkg.main) {
if (pkg.main === '.' || pkg.main === './') {
pkg.main = 'index';
}
var m = loadAsFileSync(path.resolve(x, pkg.main));
if (m) return m;
var n = loadAsDirectorySync(path.resolve(x, pkg.main));
if (n) return n;
}
} catch (e) {}
} catch (e) { }
}

return loadAsFileSync(path.join(x, '/index'));
}

Expand Down
22 changes: 22 additions & 0 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,25 @@ test('async: #121 - treating an existing file as a dir when no basedir', functio

t.end();
});

test('sync dot main', function (t) {
var start = Date.now();
t.plan(3);
resolve('./resolver/dot_main', function (err, ret) {
t.notOk(err);
t.equal(ret, path.join(__dirname, 'resolver/dot_main/index.js'));
t.ok(Date.now() - start < 50, 'resolve.sync timedout');
t.end();
});
});

test('sync dot slash main', function (t) {
var start = Date.now();
t.plan(3);
resolve('./resolver/dot_slash_main', function (err, ret) {
t.notOk(err);
t.equal(ret, path.join(__dirname, 'resolver/dot_slash_main/index.js'));
t.ok(Date.now() - start < 50, 'resolve.sync timedout');
t.end();
});
});
2 changes: 2 additions & 0 deletions test/resolver/dot_main/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// this is the actual main file 'index.js', not 'wrong.js' like the package.json would indicate
module.exports = 1;
3 changes: 3 additions & 0 deletions test/resolver/dot_main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "."
}
2 changes: 2 additions & 0 deletions test/resolver/dot_slash_main/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// this is the actual main file 'index.js', not 'wrong.js' like the package.json would indicate
module.exports = 1;
3 changes: 3 additions & 0 deletions test/resolver/dot_slash_main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "./"
}
14 changes: 14 additions & 0 deletions test/resolver_sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,17 @@ test('sync: #121 - treating an existing file as a dir when no basedir', function

t.end();
});

test('sync dot main', function (t) {
var start = Date.now();
t.equal(resolve.sync('./resolver/dot_main'), path.join(__dirname, 'resolver/dot_main/index.js'));
t.ok(Date.now() - start < 50, 'resolve.sync timedout');
t.end();
});

test('sync dot slash main', function (t) {
var start = Date.now();
t.equal(resolve.sync('./resolver/dot_slash_main'), path.join(__dirname, 'resolver/dot_slash_main/index.js'));
t.ok(Date.now() - start < 50, 'resolve.sync timedout');
t.end();
});

0 comments on commit 44df9a9

Please sign in to comment.