Skip to content
This repository has been archived by the owner on Aug 2, 2018. It is now read-only.

Support "local" directive in component.json. #372

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/duo.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ Duo.prototype.resolve = function *(dep, file, entry) {
var isRelative = './' == dep.slice(0, 2);
var isParent = '..' == dep.slice(0, 2);
var isAbsolute = '/' == dep[0];
var isLocal = (this.json.local || []).indexOf(dep) > -1;
var ret;

if (isManifest) {
Expand All @@ -711,6 +712,19 @@ Duo.prototype.resolve = function *(dep, file, entry) {
} else if (isRelative) {
// `dep` is a relative path with "./"
ret = path;
} else if (isLocal) {
// `dep` is a local dependency (as specified by component.json)
var relroot = this.findRoot(file.path);
var localPaths = (this.json.paths || []).concat([ "." ]).map(function (path) {
if ('/' === path[0]) {
return join(path, dep);
} else {
return join(relroot, path, dep);
}
});
ret = localPaths.filter(function (path) {
return fs.exists(join(path, dep));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there another way to handle this? fs.exists() is considered an anti-pattern :/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is asserting whether or not a file exists an antipattern?

If I have a list of search paths in which a file might be found, and need to return at least one path where that file was found, I'm not sure how else I'd resolve that.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephenmathieson I think that's just FUD.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephenmathieson Did you mean fs.exists specifically? I switched over to co-exists since I noticed Duo is already using that.

})[0];
} else if (yield isRelativeCSS(entry, path)) {
// Hack to support for CSS relative paths without "./"
// Example: body { background-image: url('image.jpg'); }
Expand Down
12 changes: 12 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,18 @@ describe('Duo API', function () {
assert('index' == ctx.main);
});

it('should resolve dependencies listed as local in component.json', function *() {
var js = yield build('local-path').run();
var ctx = evaluate(js);
assert('index' == ctx.main);
});

it('should resolve dependencies listed as local in component.json with search paths', function *() {
var js = yield build('local-path-with-search').run();
var ctx = evaluate(js);
assert('index' == ctx.main);
});

it('should fetch and build direct dependencies', function *() {
this.timeout(15000);
var js = yield build('simple-deps').run();
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/local-path-with-search/component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"local": [ "local-component" ],
"paths": [ "my-components" ]
}
1 change: 1 addition & 0 deletions test/fixtures/local-path-with-search/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('local-component');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'index';
3 changes: 3 additions & 0 deletions test/fixtures/local-path/component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"local": [ "local-component" ]
}
1 change: 1 addition & 0 deletions test/fixtures/local-path/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('local-component');
1 change: 1 addition & 0 deletions test/fixtures/local-path/local-component/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'index';