webpack/lib/CachePlugin.js

69 lines
2.2 KiB
JavaScript
Raw Normal View History

2013-01-31 01:49:25 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var async = require("async");
function CachePlugin(cache) {
this.cache = cache || {};
this.FS_ACCURENCY = 10000;
2013-01-31 01:49:25 +08:00
}
module.exports = CachePlugin;
CachePlugin.prototype.apply = function(compiler) {
if(Array.isArray(compiler.compilers)) {
2014-06-19 05:02:33 +08:00
compiler.compilers.forEach(function(c, idx) {
c.apply(new CachePlugin(this.cache[idx] = this.cache[idx] || {}));
}, this);
} else {
var _this = this;
2014-06-19 05:02:33 +08:00
compiler.plugin("compilation", function(compilation) {
2016-02-10 05:34:10 +08:00
if(compilation.notCacheable) {
compilation.warnings.push(new Error("CachePlugin - Cache cannot be used because of: " + compilation.notCacheable))
} else {
compilation.cache = _this.cache;
}
});
2014-06-19 05:02:33 +08:00
compiler.plugin("run", function(compiler, callback) {
if(!compiler._lastCompilationFileDependencies) return callback();
2014-06-19 05:02:33 +08:00
var fs = compiler.inputFileSystem;
2015-04-24 05:55:50 +08:00
var fileTs = compiler.fileTimestamps = {};
2014-06-19 05:02:33 +08:00
async.forEach(compiler._lastCompilationFileDependencies, function(file, callback) {
fs.stat(file, function(err, stat) {
if(err) {
if(err.code === "ENOENT") return callback();
2014-06-19 05:02:33 +08:00
return callback(err);
}
2015-04-24 05:55:50 +08:00
if(stat.mtime)
_this.applyMtime(+stat.mtime);
2014-06-19 05:02:33 +08:00
fileTs[file] = stat.mtime || Infinity;
callback();
});
}, callback);
Object.keys(fileTs).forEach(function(key) {
fileTs[key] += _this.FS_ACCURENCY;
});
2015-04-24 05:55:50 +08:00
});
2014-06-19 05:02:33 +08:00
compiler.plugin("after-compile", function(compilation, callback) {
compilation.compiler._lastCompilationFileDependencies = compilation.fileDependencies;
compilation.compiler._lastCompilationContextDependencies = compilation.contextDependencies;
callback();
2015-04-24 05:55:50 +08:00
});
2014-06-19 05:02:33 +08:00
}
};
CachePlugin.prototype.applyMtime = function applyMtime(mtime) {
if(this.FS_ACCURENCY > 1 && mtime % 1 !== 0)
this.FS_ACCURENCY = 1;
else if(this.FS_ACCURENCY > 10 && mtime % 10 !== 0)
this.FS_ACCURENCY = 10;
else if(this.FS_ACCURENCY > 100 && mtime % 100 !== 0)
this.FS_ACCURENCY = 100;
else if(this.FS_ACCURENCY > 1000 && mtime % 1000 !== 0)
this.FS_ACCURENCY = 1000;
else if(this.FS_ACCURENCY > 2000 && mtime % 2000 !== 0)
this.FS_ACCURENCY = 2000;
};