Skip to content

Commit

Permalink
CommonJS: resolve partials in the host directory
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjoelkemp committed Aug 23, 2015
1 parent 1b2fbb9 commit e0c343f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .deprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"root": "./"
}
26 changes: 22 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var stylusLookup = require('stylus-lookup');
var sassLookup = require('sass-lookup');
var resolveDependencyPath = require('resolve-dependency-path');

var appModulePath = require('app-module-path');

var assign = function(obj1, obj2) {
for (var prop in obj2) {
if (obj2.hasOwnProperty(prop)) {
Expand Down Expand Up @@ -44,7 +46,10 @@ module.exports = function(options) {
}

debug('found a resolver for ' + ext);
return resolver(partial, filename, directory, config);

var result = resolver(partial, filename, directory, config);
debug('resolved path: ' + result);
return result;
};

/**
Expand Down Expand Up @@ -79,7 +84,7 @@ function jsLookup(partial, filename, directory, config) {
return amdLookup(config, partial, filename, directory);
case 'commonjs':
debug('using commonjs resolver');
return commonJSLookup(partial);
return commonJSLookup(partial, directory);
case 'es6':
default:
debug('using generic resolver for es6');
Expand All @@ -90,8 +95,21 @@ function jsLookup(partial, filename, directory, config) {
/**
* @private
* @param {String} partial
* @param {String} directory
* @return {String}
*/
function commonJSLookup(partial) {
return require.resolve(partial);
function commonJSLookup(partial, directory) {
// Need to resolve partials within the directory of the module, not filing-cabinet
appModulePath.addPath(path.join(directory, 'node_modules'));

var result = '';

try {
result = require.resolve(partial);
debug('resolved path: ' + result);
} catch (e) {
debug('could not resolve ' + partial);
}

return result;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@
},
"homepage": "https://github.com/mrjoelkemp/node-filing-cabinet",
"devDependencies": {
"jscs": "~1.13.1",
"jscs": "~2.0.0",
"mocha": "~2.2.5",
"mock-fs": "~3.0.0",
"rewire": "~2.3.4",
"sinon": "~1.15.4"
},
"dependencies": {
"app-module-path": "~1.0.4",
"commander": "~2.8.1",
"debug": "~2.2.0",
"module-definition": "~2.2.2",
Expand Down
23 changes: 23 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,29 @@ describe('filing-cabinet', function() {

revert();
});

it('returns an empty string for an unresolved module', function() {
var result = cabinet({
partial: 'foobar',
filename: 'js/commonjs/foo.js',
directory: 'js/commonjs/'
});

assert.equal(result.length, 0);
});

it('adds the directory to the require resolution paths', function() {
var directory = 'js/commonjs/';
var result = cabinet({
partial: 'foobar',
filename: 'js/commonjs/foo.js',
directory: directory
});

assert.ok(require.main.paths.some(function(p) {
return p.indexOf(directory) !== -1;
}));
});
});
});

Expand Down

0 comments on commit e0c343f

Please sign in to comment.