Skip to content

Commit

Permalink
Support all require sub-properties
Browse files Browse the repository at this point in the history
  • Loading branch information
felixge committed Jun 25, 2011
1 parent 8447d94 commit bd49a8a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/require-like.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function requireLike(path, uncached) {
var parentModule = new Module(path);
parentModule.filename = path;

return function(file) {
function requireLike(file) {
var cache = Module._cache;
if (uncached) {
Module._cache = {};
Expand All @@ -15,4 +15,16 @@ module.exports = function requireLike(path, uncached) {

return exports;
};


requireLike.resolve = function(request) {
return Module._resolveFilename(request, parentModule)[1];
}

requireLike.paths = require.paths;
requireLike.main = process.mainModule;
requireLike.extensions = require.extensions;
requireLike.cache = require.cache;

return requireLike;
};
27 changes: 27 additions & 0 deletions test/integration/test-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,30 @@ var requireLike = require(common.dir.lib + '/require-like');
assert.notStrictEqual(myFoo, foo);
assert.deepEqual(myFoo, foo);
})();

(function testResolve() {
var myRequire = requireLike(common.dir.fixture + '/bar.js');
var fooPath = myRequire.resolve('./foo');

assert.strictEqual(fooPath, common.dir.fixture + '/foo.js');
})();

(function testPaths() {
var myRequire = requireLike(common.dir.fixture + '/bar.js');
assert.strictEqual(myRequire.paths, require.paths);
})();

(function testMain() {
var myRequire = requireLike(common.dir.fixture + '/bar.js');
assert.strictEqual(myRequire.main, process.mainModule);
})();

(function testExtensions() {
var myRequire = requireLike(common.dir.fixture + '/bar.js');
assert.strictEqual(myRequire.extensions, require.extensions);
})();

(function testCache() {
var myRequire = requireLike(common.dir.fixture + '/bar.js');
assert.strictEqual(myRequire.cache, require.cache);
})();

0 comments on commit bd49a8a

Please sign in to comment.