Permalink
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...
1 parent 7cedd30 commit b8748902e706e214cb4b884bde827f39f1617ee7 @defunctzombie committed Feb 24, 2013
Showing with 93 additions and 7 deletions.
  1. +6 −2 bin/required
  2. +13 −5 index.js
  3. +50 −0 test/cache_source.js
  4. +2 −0 test/fixtures/cache_source.js
  5. +20 −0 test/fixtures/cache_source.js.dep
  6. +2 −0 test/runner.js
View
@@ -8,12 +8,16 @@ 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;
}
console.dir(res);
});
-// vim ft=javascript
+// vim: ft=javascript
View
@@ -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,17 +122,22 @@ 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) {
if (err) {
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
@@ -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();
+ });
+});
+
@@ -0,0 +1,2 @@
+require('./one_local');
+require('./none');
@@ -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
@@ -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 commented on b874890 Feb 24, 2013

You rock. Thanks for fixing this.

Please sign in to comment.