Skip to content

Commit

Permalink
Improve require priority
Browse files Browse the repository at this point in the history
Priority: a.js > a.json > a/index.js

Close #22
  • Loading branch information
popomore committed Oct 28, 2014
1 parent a73b5f7 commit 2dda420
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ function tryFile(src, cwd) {
fileArray.push(src + '.js');
}
if (!extname(src)) {
fileArray.push(src + '/index.js');
fileArray.push(src + '.json');
fileArray.push(join(src, 'index.js'));
}

for (var i in fileArray) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/spm/require-directory/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require('./lib');
require('./lib/');
1 change: 1 addition & 0 deletions test/fixtures/spm/require-priority/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'a.js';
1 change: 1 addition & 0 deletions test/fixtures/spm/require-priority/a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"a":"json"}
1 change: 1 addition & 0 deletions test/fixtures/spm/require-priority/b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"b":"json"}
1 change: 1 addition & 0 deletions test/fixtures/spm/require-priority/b/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'b/index.js';
2 changes: 2 additions & 0 deletions test/fixtures/spm/require-priority/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('./a');
require('./b');
4 changes: 4 additions & 0 deletions test/fixtures/spm/require-priority/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "require-priority",
"version": "1.0.0"
}
8 changes: 8 additions & 0 deletions test/spm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ describe('Father.SpmPackage', function() {
pkg.files['index.js'].dependencies.should.eql(['./lib.js']);
});

it('should require priority a.js > a.json > a/index.js', function() {
var pkg = getPackage('require-priority');
should.exist(pkg.files['a.js']);
should.not.exist(pkg.files['a.json']);
should.exist(pkg.files['b.json']);
should.not.exist(pkg.files['b/index.js']);
});

it('should support relative main', function() {
var pkg = getPackage('relative-main');
Object.keys(pkg.files).length.should.eql(1);
Expand Down

0 comments on commit 2dda420

Please sign in to comment.