[Pkg-javascript-commits] [node-module-deps] 393/444: Add fileCache.

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 15 09:48:16 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-module-deps.

commit 4693f5ee5f89a137089f566ae433f3c83c100cdf
Author: James Pike <jpike at chilon.net>
Date:   Mon Jul 27 02:49:01 2015 +0100

    Add fileCache.
---
 index.js           |  7 +++++--
 readme.markdown    |  3 +++
 test/file_cache.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 2 deletions(-)

diff --git a/index.js b/index.js
index 53fe143..179a967 100644
--- a/index.js
+++ b/index.js
@@ -27,6 +27,7 @@ function Deps (opts) {
     
     this.basedir = opts.basedir || process.cwd();
     this.cache = opts.cache;
+    this.fileCache = opts.fileCache;
     this.pkgCache = opts.packageCache || {};
     this.pkgFileCache = {};
     this.pkgFileCachePending = {};
@@ -181,8 +182,10 @@ Deps.prototype.resolve = function (id, parent, cb) {
 Deps.prototype.readFile = function (file, id, pkg) {
     var self = this;
     var tr = through();
-    if (this.cache && this.cache[file]) {
-        tr.push(this.cache[file].source);
+
+    var inputSource = this.fileCache && this.fileCache[file];
+    if (inputSource) {
+        tr.push(inputSource);
         tr.push(null);
         return tr;
     }
diff --git a/readme.markdown b/readme.markdown
index f82743e..b9a5c79 100644
--- a/readme.markdown
+++ b/readme.markdown
@@ -91,6 +91,9 @@ this for large dependencies like jquery or threejs which take forever to parse.
 * `opts.packageCache` - an object mapping filenames to their parent package.json
 contents for browser fields, main entries, and transforms
 
+* `opts.fileCache` - an object mapping filenames to raw source to avoid reading
+from disk.
+
 * `opts.paths` - array of global paths to search. Defaults to splitting on `':'`
 in `process.env.NODE_PATH`
 
diff --git a/test/file_cache.js b/test/file_cache.js
new file mode 100644
index 0000000..0a03c87
--- /dev/null
+++ b/test/file_cache.js
@@ -0,0 +1,58 @@
+var mdeps = require('../');
+var test = require('tap').test;
+var path = require('path');
+var through = require('through2');
+
+var files = {
+    foo: path.join(__dirname, '/files/foo.js'),
+    bar: path.join(__dirname, '/files/bar.js')
+};
+
+var sources = {
+    foo: 'require("./bar"); var tongs;',
+    bar: 'notreal tongs'
+};
+
+var fileCache = {};
+fileCache[files.foo] = sources.foo;
+fileCache[files.bar] = sources.bar;
+
+var specialReplace = function(input) {
+    return input.replace(/tongs/g, 'tangs');
+};
+
+test('uses file cache', function (t) {
+    t.plan(1);
+    var p = mdeps({
+        fileCache: fileCache,
+        transform: function (file) {
+            return through(function (buf, enc, next) {
+                this.push(specialReplace(String(buf)));
+                next();
+            });
+        },
+        transformKey: [ 'browserify', 'transform' ]
+    });
+    p.end({ id: 'foo', file: files.foo, entry: false });
+
+    var rows = [];
+    p.on('data', function (row) { rows.push(row) });
+    p.on('end', function () {
+        t.same(rows.sort(cmp), [
+            {
+                id: 'foo',
+                file: files.foo,
+                source: specialReplace(sources.foo),
+                deps: { './bar': files.bar }
+            },
+            {
+                id: files.bar,
+                file: files.bar,
+                source: specialReplace(sources.bar),
+                deps: {}
+            }
+        ].sort(cmp));
+    });
+});
+
+function cmp (a, b) { return a.id < b.id ? -1 : 1 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-module-deps.git



More information about the Pkg-javascript-commits mailing list