Skip to content

Commit

Permalink
Merge branch 'fix/file_emit_persistent_cache' of https://github.com/m…
Browse files Browse the repository at this point in the history
…artinheidegger/module-deps into martinheidegger-fix/file_emit_persistent_cache
  • Loading branch information
goto-bus-stop committed Feb 2, 2018
2 parents 9c3cfbc + f67af91 commit 41a126b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
28 changes: 18 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var fs = require('fs');
var path = require('path');
var relativePath = require('cached-path-relative')
var relativePath = require('cached-path-relative');

var browserResolve = require('browser-resolve');
var nodeResolve = require('resolve');
Expand Down Expand Up @@ -206,8 +206,6 @@ Deps.prototype.readFile = function (file, id, pkg) {
var rs = fs.createReadStream(file, {
encoding: 'utf8'
});
rs.on('error', function (err) { self.emit('error', err) });
this.emit('file', file, id);
return rs;
};

Expand Down Expand Up @@ -235,7 +233,9 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {

for (var i = 0; i < transforms.length; i++) (function (i) {
makeTransform(transforms[i], function (err, trs) {
if (err) return self.emit('error', err)
if (err) {
return dup.emit('error', err);
}
streams[i] = trs;
if (-- pending === 0) done();
});
Expand All @@ -247,7 +247,7 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {
middle.on('error', function (err) {
err.message += ' while parsing file: ' + file;
if (!err.filename) err.filename = file;
self.emit('error', err);
dup.emit('error', err);
});
input.pipe(middle).pipe(output);
}
Expand Down Expand Up @@ -283,7 +283,7 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {
if (err) {
params.basedir = pkg.__dirname;
return nodeResolve(id, params, function (e, r) {
nr(e, r, true)
nr(e, r, true);
});
}

Expand Down Expand Up @@ -349,6 +349,9 @@ Deps.prototype.walk = function (id, parent, cb) {
file = rec.file;

var ts = self.getTransforms(file, pkg);
ts.on('error', function (err) {
self.emit('error', err);
});
ts.pipe(concat(function (body) {
rec.source = body.toString('utf8');
fromSource(file, rec.source, pkg);
Expand All @@ -371,6 +374,9 @@ Deps.prototype.walk = function (id, parent, cb) {

if (rec.source) {
var ts = self.getTransforms(file, pkg);
ts.on('error', function (err) {
self.emit('error', err);
});
ts.pipe(concat(function (body) {
rec.source = body.toString('utf8');
fromSource(file, rec.source, pkg);
Expand All @@ -382,6 +388,7 @@ Deps.prototype.walk = function (id, parent, cb) {
if (c) return fromDeps(file, c.source, c.package, fakePath, Object.keys(c.deps));

self.persistentCache(file, id, pkg, persistentCacheFallback, function (err, c) {
self.emit('file', file, id);
if (err) {
self.emit('error', err);
return;
Expand All @@ -390,12 +397,13 @@ Deps.prototype.walk = function (id, parent, cb) {
});

function persistentCacheFallback (dataAsString, cb) {
var stream = dataAsString ? toStream(dataAsString) : self.readFile(file, id, pkg);
var stream = dataAsString ? toStream(dataAsString) : self.readFile(file, id, pkg).on('error', cb);
stream
.pipe(self.getTransforms(fakePath || file, pkg, {
builtin: builtin,
inNodeModules: parent.inNodeModules
}))
.on('error', cb)
.pipe(concat(function (body) {
var src = body.toString('utf8');
var deps = getDeps(file, src);
Expand Down Expand Up @@ -524,7 +532,7 @@ Deps.prototype.lookupPackage = function (file, cb) {
catch (err) {
return onpkg(new Error([
err + ' while parsing json file ' + pkgfile
].join('')))
].join('')));
}
pkg.__dirname = dir;

Expand All @@ -539,8 +547,8 @@ Deps.prototype.lookupPackage = function (file, cb) {
delete self.pkgFileCachePending[pkgfile];
fns.forEach(function (f) { f(err, pkg) });
}
if (err) cb(err)
else if (pkg && typeof pkg === 'object') cb(null, pkg)
if (err) cb(err);
else if (pkg && typeof pkg === 'object') cb(null, pkg);
else {
self.pkgCache[pkgfile] = false;
next();
Expand Down
49 changes: 45 additions & 4 deletions test/cache_persistent.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ test('uses persistent cache', function (t) {
var p = parser({
persistentCache: function (file, id, pkg, fallback, cb) {
if (file === files.bar) {
return fallback(null, cb)
return fallback(null, cb);
}
cb(null, {
source: 'file at ' + file + '@' + id,
package: pkg,
deps: { './bar': files.bar }
})
});
}
});
p.end({ id: 'foo', file: files.foo, entry: false });
Expand Down Expand Up @@ -48,7 +48,7 @@ test('passes persistent cache error through', function (t) {
t.plan(1);
var p = parser({
persistentCache: function (file, id, pkg, fallback, cb) {
cb(new Error('foo'))
cb(new Error('foo'));
}
});
p.end({ id: 'foo', file: files.foo, entry: false });
Expand All @@ -59,7 +59,7 @@ test('allow passing of the raw source as string', function (t) {
t.plan(1);
var p = parser({
persistentCache: function (file, id, pkg, fallback, cb) {
fallback(fs.readFileSync(files.bar, 'utf8'), cb)
fallback(fs.readFileSync(files.bar, 'utf8'), cb);
}
});
p.end({ id: 'foo', file: files.foo, entry: false });
Expand All @@ -78,4 +78,45 @@ test('allow passing of the raw source as string', function (t) {
});
});

test('send file event with persistent cache', function (t) {
t.plan(2);
var p = parser({
persistentCache: function (file, id, pkg, fallback, cb) {
cb(null, {
source: 'file at ' + file + '@' + id,
package: pkg,
deps: {}
});
}
});
p.end({ id: 'foo', file: files.foo, entry: false });
p.on('file', function (file, id) {
t.same(file, path.resolve(files.foo));
t.same(id, path.resolve(files.foo));
});
});

test('errors of transforms occur in the correct order with a persistent cache', function (t) {
t.plan(3);
var p = parser({
transform: [
path.join(__dirname, 'cache_persistent', 'error_transform')
],
persistentCache: function (file, id, pkg, fallback, cb) {
fallback(fs.readFileSync(files.foo, 'utf8'), cb);
}
});
p.end({ id: 'foo', file: files.foo, entry: false });
var order = 0;
p.on('file', function (file, id) {
t.same(order, 0);
order += 1;
});
p.on('error', function (err) {
t.same(order, 1);
t.same(err.message, 'rawr while parsing file: ' + path.resolve(files.foo));
});
});


function cmp (a, b) { return a.id < b.id ? -1 : 1 }
6 changes: 6 additions & 0 deletions test/cache_persistent/error_transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var through = require('through2');
module.exports = function (file) {
return through(function (chunk, enc, callback) {
callback(new Error('rawr'));
});
};

0 comments on commit 41a126b

Please sign in to comment.