Skip to content

Commit 4693f5e

Browse files
committed
Add fileCache.
1 parent 0e290cd commit 4693f5e

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function Deps (opts) {
2727

2828
this.basedir = opts.basedir || process.cwd();
2929
this.cache = opts.cache;
30+
this.fileCache = opts.fileCache;
3031
this.pkgCache = opts.packageCache || {};
3132
this.pkgFileCache = {};
3233
this.pkgFileCachePending = {};
@@ -181,8 +182,10 @@ Deps.prototype.resolve = function (id, parent, cb) {
181182
Deps.prototype.readFile = function (file, id, pkg) {
182183
var self = this;
183184
var tr = through();
184-
if (this.cache && this.cache[file]) {
185-
tr.push(this.cache[file].source);
185+
186+
var inputSource = this.fileCache && this.fileCache[file];
187+
if (inputSource) {
188+
tr.push(inputSource);
186189
tr.push(null);
187190
return tr;
188191
}

readme.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ this for large dependencies like jquery or threejs which take forever to parse.
9191
* `opts.packageCache` - an object mapping filenames to their parent package.json
9292
contents for browser fields, main entries, and transforms
9393

94+
* `opts.fileCache` - an object mapping filenames to raw source to avoid reading
95+
from disk.
96+
9497
* `opts.paths` - array of global paths to search. Defaults to splitting on `':'`
9598
in `process.env.NODE_PATH`
9699

test/file_cache.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var mdeps = require('../');
2+
var test = require('tap').test;
3+
var path = require('path');
4+
var through = require('through2');
5+
6+
var files = {
7+
foo: path.join(__dirname, '/files/foo.js'),
8+
bar: path.join(__dirname, '/files/bar.js')
9+
};
10+
11+
var sources = {
12+
foo: 'require("./bar"); var tongs;',
13+
bar: 'notreal tongs'
14+
};
15+
16+
var fileCache = {};
17+
fileCache[files.foo] = sources.foo;
18+
fileCache[files.bar] = sources.bar;
19+
20+
var specialReplace = function(input) {
21+
return input.replace(/tongs/g, 'tangs');
22+
};
23+
24+
test('uses file cache', function (t) {
25+
t.plan(1);
26+
var p = mdeps({
27+
fileCache: fileCache,
28+
transform: function (file) {
29+
return through(function (buf, enc, next) {
30+
this.push(specialReplace(String(buf)));
31+
next();
32+
});
33+
},
34+
transformKey: [ 'browserify', 'transform' ]
35+
});
36+
p.end({ id: 'foo', file: files.foo, entry: false });
37+
38+
var rows = [];
39+
p.on('data', function (row) { rows.push(row) });
40+
p.on('end', function () {
41+
t.same(rows.sort(cmp), [
42+
{
43+
id: 'foo',
44+
file: files.foo,
45+
source: specialReplace(sources.foo),
46+
deps: { './bar': files.bar }
47+
},
48+
{
49+
id: files.bar,
50+
file: files.bar,
51+
source: specialReplace(sources.bar),
52+
deps: {}
53+
}
54+
].sort(cmp));
55+
});
56+
});
57+
58+
function cmp (a, b) { return a.id < b.id ? -1 : 1 }

0 commit comments

Comments
 (0)