Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

cache source as well as deps

If the user wants to includeSources, make sure to cache the source along
with the deps. Without this, multiple requires of the same file will
lead to some not having the source.

fixes #7
  • Loading branch information...
commit b8748902e706e214cb4b884bde827f39f1617ee7 1 parent 7cedd30
@defunctzombie authored
View
8 bin/required
@@ -8,7 +8,11 @@ if (!path) {
return console.error('Usage: required /path/to/module.js');
}
-required(path, function(err, res) {
+var opt = {
+ includeSource: true,
+};
+
+required(path, opt, function(err, res) {
if (err) {
throw err;
}
@@ -16,4 +20,4 @@ required(path, function(err, res) {
console.dir(res);
});
-// vim ft=javascript
+// vim: ft=javascript
View
18 index.js
@@ -108,9 +108,12 @@ function from_filename(filename, parent, opt, cb) {
var cache = opt.cache;
+ // wtf is this cache?
+ // appears to be the list of dependencies for this filename
+ // what it really should be is the info
var cached = cache[filename];
if (cached) {
- return cb(null, cached);
+ return cb(null, cached.deps, cached.src);
}
fs.readFile(filename, 'utf8', function(err, content) {
@@ -119,7 +122,7 @@ function from_filename(filename, parent, opt, cb) {
}
// must be set before the compile call to handle circular references
- var result = cache[filename] = [];
+ var result = cache[filename] = { deps: [] };
try {
from_source(content, parent, opt, function(err, deps) {
@@ -127,9 +130,14 @@ function from_filename(filename, parent, opt, cb) {
return cb(err);
}
- // push onto the result set so circular references are populated
- result.push.apply(result, deps);
- return cb(err, result, content);
+ result.deps = deps;
+
+ // only cache source if caller will want the source
+ if (opt.includeSource) {
+ result.src = content;
+ }
+
+ return cb(err, deps, content);
});
} catch (err) {
err.message = filename + ': ' + err.message;
View
50 test/cache_source.js
@@ -0,0 +1,50 @@
+var assert = require('assert');
+var required = require('../');
+
+suite('include source');
+
+test('basic', function(done) {
+ var opt = {
+ includeSource: true
+ };
+
+ var entry = __dirname + '/fixtures/one_local.js';
+
+ required(entry, opt, function(err, deps) {
+ assert.ifError(err);
+ assert.equal(deps.length, 1);
+ var dep = deps[0];
+
+ assert.equal(dep.id, './none');
+ assert.deepEqual(dep.deps, []);
+ assert.equal(dep.source, '// no dependencies\n');
+
+ done();
+ });
+});
+
+test('same file twice', function(done) {
+ var opt = {
+ includeSource: true
+ };
+
+ var entry = __dirname + '/fixtures/cache_source.js';
+
+ required(entry, opt, function(err, deps) {
+ assert.ifError(err);
+ assert.equal(deps.length, 2);
+ var dep = deps[0];
+
+ assert.equal(dep.id, './one_local');
+ assert.deepEqual(dep.deps[0], deps[1]);
+ assert.equal(dep.source, '// single local dependency\nrequire(\'./none\');\n');
+
+ dep = deps[1];
+ assert.equal(dep.id, './none');
+ assert.deepEqual(dep.deps, []);
+ assert.equal(dep.source, '// no dependencies\n');
+
+ done();
+ });
+});
+
View
2  test/fixtures/cache_source.js
@@ -0,0 +1,2 @@
+require('./one_local');
+require('./none');
View
20 test/fixtures/cache_source.js.dep
@@ -0,0 +1,20 @@
+[
+ {
+ "id": "./one_local",
+ "filename": "one_local.js",
+ "deps": [
+ {
+ "id": "./none",
+ "filename": "none.js",
+ "deps": []
+ }
+ ]
+ },
+ {
+ "id": "./none",
+ "filename": "none.js",
+ "deps": {
+ "$ref": "$[0][\"deps\"][0][\"deps\"]"
+ }
+ }
+]
View
2  test/runner.js
@@ -7,6 +7,8 @@ var cycle = require('cycle');
// local
var required = require('..');
+suite('default');
+
function fix_path(key, value) {
if (key !== 'filename') {
return value;

1 comment on commit b874890

@1602

You rock. Thanks for fixing this.

Please sign in to comment.
Something went wrong with that request. Please try again.