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

Commit

Permalink
fetch default hook to include caching
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed May 19, 2016
1 parent 8316062 commit 61d7540
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions lib/builder.js
Expand Up @@ -176,26 +176,21 @@ Builder.prototype.reset = function(baseLoader) {
loader.fetch = function(load) {
var self = this;
return Promise.resolve((builder.fetch || loaderFetch).call(this, load, function(load) {
return loaderFetch.call(self, load);
}))
.then(function(source) {
// custom fetch hook has to handle custom statting too
// by setting load.metadata.timestamp itself
if (builder.fetch)
return 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 loaderFetch.call(self, 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;
throw err;
});
})
}, 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;
});
})
}))
.then(function (source) {
var metadata = load.metadata;
if (metadata.sourceMap)
Expand Down

0 comments on commit 61d7540

Please sign in to comment.