Skip to content

Commit

Permalink
Fix dynamic caching to prevent rebuilding the whole bundle on every l…
Browse files Browse the repository at this point in the history
…oad.
  • Loading branch information
scottbrady committed Feb 11, 2015
1 parent d9e1e81 commit e2521f9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/compile.js
Expand Up @@ -65,7 +65,8 @@ function compile(path, options, cb, cacheUpdated) {
basedir: options.basedir,
debug: options.debug,
standalone: options.standalone || false,
cache: cache ? cache.getCache() : undefined
cache: cache ? cache.getCache() : undefined,
fullPaths: cache ? true : false
});
if (options.plugins) {
var plugins = options.plugins; // in the format options.plugins = [{plugin: plugin, options: options}, {plugin: plugin, options: options}, ... ]
Expand Down
17 changes: 13 additions & 4 deletions lib/dynamic-cache.js
Expand Up @@ -39,8 +39,9 @@ DynamicCache.prototype.update = function (cb) {
if (err || stats.mtime.getTime() !== this._time[filename]) {
var deps = getDeps(filename);
for (var i = 0; i < deps.length; i++) {
if (this._cache[deps]) {
delete this._cache[deps];
var dep = deps[i];
if (this._cache[dep]) {
delete this._cache[dep];
}
}
if (filename in this._files) {
Expand All @@ -59,11 +60,19 @@ DynamicCache.prototype.populate = function (bundle) {
this._files = {};
this._time = {};

var getDeps = function (dep) {
var deps = [];
for (var key in dep.deps) {
deps.push(dep.deps[key]);
}
return deps;
}

bundle.on('dep', function (dep) {
fs.stat(dep.file, function (err, stats) {
if (err) return;
this._cache[dep.id] = dep;
this._files[dep.file] = (this._files[dep.file] || []).concat(dep.id);
this._files[dep.file] = getDeps(dep);
this._time[dep.file] = stats.mtime.getTime();
}.bind(this));
}.bind(this));
Expand All @@ -72,7 +81,7 @@ DynamicCache.prototype.populate = function (bundle) {
tr.on('file', function (dependency) {
fs.stat(dependency, function (err, stats) {
if (err) return;
this._files[dependency] = (this._files[dependency] || []).concat(file);
this._files[dependency] = getDeps(dependency);
this._time[dependency] = stats.mtime.getTime();
}.bind(this));
}.bind(this));
Expand Down

0 comments on commit e2521f9

Please sign in to comment.