Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
updated with PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
frankwallis committed Jul 25, 2016
1 parent 32a6ead commit 1a6721d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
34 changes: 20 additions & 14 deletions lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,28 @@ Builder.prototype.reset = function(baseLoader) {

// allow a custom fetch hook
var loaderFetch = loader.fetch;
loader.fetch = function(load) {
var self = this;
return Promise.resolve((builder.fetch || loaderFetch).call(this, load))
.then(function(source) {
// calling default fs.readFile fetch -> set timestamp as well for cache invalidation
return asp(fs.stat)(fromFileURL(load.address))
.then(function(stats) {
load.metadata.timestamp = stats.mtime.getTime();
var cachedFetch = function(load) {
return Promise.resolve(loaderFetch.call(this, load))
.then(function(source) {
// calling default fs.readFile fetch -> set timestamp as well for cache invalidation
return asp(fs.stat)(fromFileURL(load.address))
.then(function(stats) {
load.metadata.timestamp = stats.mtime.getTime();
return source;
}, function(err) {
// if the stat fails on a plugin, it may not be linked to the file itself
if (err.code == 'ENOENT' && load.metadata.loader)
return source;
}, function(err) {
// if the stat fails on a plugin, it may not be linked to the file itself
if (err.code == 'ENOENT' && load.metadata.loader)
return source;
throw err;
});
throw err;
});
});
};

loader.fetch = function(load) {
if (builder.fetch)
return Promise.resolve(builder.fetch.call(this, load, cachedFetch));
else
return cachedFetch.call(this, load);
};

// this allows us to normalize package conditionals into package conditionals
Expand Down
2 changes: 1 addition & 1 deletion test/test-build-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ suite('Test compiler cache', function() {
var builder = new Builder('test/output');
fs.writeFileSync('./test/output/timestamp-module.js', source);
var address = builder.loader.normalizeSync('./test/output/timestamp-module.js');
var load = { name: address, address, metadata: {} };
var load = { name: address, address: address, metadata: {} };
return builder.loader.fetch(load)
.then(function(text) {
//console.log(JSON.stringify(load));
Expand Down

0 comments on commit 1a6721d

Please sign in to comment.