diff --git a/lib/sync.js b/lib/sync.js index 510ca256..92bc622c 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -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')); } diff --git a/test/resolver.js b/test/resolver.js index adde5444..b77a9a5a 100644 --- a/test/resolver.js +++ b/test/resolver.js @@ -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(); + }); +}); diff --git a/test/resolver/dot_main/index.js b/test/resolver/dot_main/index.js new file mode 100644 index 00000000..bc1fb0a6 --- /dev/null +++ b/test/resolver/dot_main/index.js @@ -0,0 +1,2 @@ +// this is the actual main file 'index.js', not 'wrong.js' like the package.json would indicate +module.exports = 1; diff --git a/test/resolver/dot_main/package.json b/test/resolver/dot_main/package.json new file mode 100644 index 00000000..d7f4fc80 --- /dev/null +++ b/test/resolver/dot_main/package.json @@ -0,0 +1,3 @@ +{ + "main": "." +} diff --git a/test/resolver/dot_slash_main/index.js b/test/resolver/dot_slash_main/index.js new file mode 100644 index 00000000..bc1fb0a6 --- /dev/null +++ b/test/resolver/dot_slash_main/index.js @@ -0,0 +1,2 @@ +// this is the actual main file 'index.js', not 'wrong.js' like the package.json would indicate +module.exports = 1; diff --git a/test/resolver/dot_slash_main/package.json b/test/resolver/dot_slash_main/package.json new file mode 100644 index 00000000..f51287b9 --- /dev/null +++ b/test/resolver/dot_slash_main/package.json @@ -0,0 +1,3 @@ +{ + "main": "./" +} diff --git a/test/resolver_sync.js b/test/resolver_sync.js index 2bc36102..6b57dfa2 100644 --- a/test/resolver_sync.js +++ b/test/resolver_sync.js @@ -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(); +});