Skip to content

Commit 5f6e682

Browse files
stefanpennerljharb
authored andcommitted
[Fix] sync: fix when package.json main = ‘.’ or main = ‘./‘
Fixes #125.
1 parent f009822 commit 5f6e682

File tree

7 files changed

+49
-1
lines changed

7 files changed

+49
-1
lines changed

Diff for: lib/sync.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,18 @@ module.exports = function (x, options) {
5757
function loadAsDirectorySync(x) {
5858
var pkgfile = path.join(x, '/package.json');
5959
if (isFile(pkgfile)) {
60-
var body = readFileSync(pkgfile, 'utf8');
6160
try {
61+
var body = readFileSync(pkgfile, 'UTF8');
6262
var pkg = JSON.parse(body);
63+
6364
if (opts.packageFilter) {
6465
pkg = opts.packageFilter(pkg, x);
6566
}
6667

6768
if (pkg.main) {
69+
if (pkg.main === '.' || pkg.main === './') {
70+
pkg.main = 'index';
71+
}
6872
var m = loadAsFileSync(path.resolve(x, pkg.main));
6973
if (m) return m;
7074
var n = loadAsDirectorySync(path.resolve(x, pkg.main));

Diff for: test/resolver.js

+22
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,25 @@ test('async: #121 - treating an existing file as a dir when no basedir', functio
325325

326326
t.end();
327327
});
328+
329+
test('async dot main', function (t) {
330+
var start = new Date();
331+
t.plan(3);
332+
resolve('./resolver/dot_main', function (err, ret) {
333+
t.notOk(err);
334+
t.equal(ret, path.join(__dirname, 'resolver/dot_main/index.js'));
335+
t.ok(new Date() - start < 50, 'resolve.sync timedout');
336+
t.end();
337+
});
338+
});
339+
340+
test('async dot slash main', function (t) {
341+
var start = new Date();
342+
t.plan(3);
343+
resolve('./resolver/dot_slash_main', function (err, ret) {
344+
t.notOk(err);
345+
t.equal(ret, path.join(__dirname, 'resolver/dot_slash_main/index.js'));
346+
t.ok(new Date() - start < 50, 'resolve.sync timedout');
347+
t.end();
348+
});
349+
});

Diff for: test/resolver/dot_main/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 1;

Diff for: test/resolver/dot_main/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"main": "."
3+
}

Diff for: test/resolver/dot_slash_main/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 1;

Diff for: test/resolver/dot_slash_main/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"main": "./"
3+
}

Diff for: test/resolver_sync.js

+14
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,17 @@ test('sync: #121 - treating an existing file as a dir when no basedir', function
251251

252252
t.end();
253253
});
254+
255+
test('sync dot main', function (t) {
256+
var start = new Date();
257+
t.equal(resolve.sync('./resolver/dot_main'), path.join(__dirname, 'resolver/dot_main/index.js'));
258+
t.ok(new Date() - start < 50, 'resolve.sync timedout');
259+
t.end();
260+
});
261+
262+
test('sync dot slash main', function (t) {
263+
var start = new Date();
264+
t.equal(resolve.sync('./resolver/dot_slash_main'), path.join(__dirname, 'resolver/dot_slash_main/index.js'));
265+
t.ok(new Date() - start < 50, 'resolve.sync timedout');
266+
t.end();
267+
});

0 commit comments

Comments
 (0)