|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -var async = require("async"); |
| 5 | +"use strict"; |
6 | 6 |
|
7 | | -function CachePlugin(cache) { |
8 | | - this.cache = cache || {}; |
9 | | - this.FS_ACCURENCY = 2000; |
10 | | -} |
11 | | -module.exports = CachePlugin; |
| 7 | +const async = require("async"); |
12 | 8 |
|
13 | | -CachePlugin.prototype.apply = function(compiler) { |
14 | | - if(Array.isArray(compiler.compilers)) { |
15 | | - compiler.compilers.forEach(function(c, idx) { |
16 | | - c.apply(new CachePlugin(this.cache[idx] = this.cache[idx] || {})); |
17 | | - }, this); |
18 | | - } else { |
19 | | - var _this = this; |
20 | | - compiler.plugin("compilation", function(compilation) { |
21 | | - if(!compilation.notCacheable) { |
22 | | - compilation.cache = _this.cache; |
23 | | - } else if(_this.watching) { |
24 | | - compilation.warnings.push( |
25 | | - new Error("CachePlugin - Cache cannot be used because of: " + compilation.notCacheable) |
26 | | - ); |
27 | | - } |
28 | | - }); |
29 | | - compiler.plugin("watch-run", function(compiler, callback) { |
30 | | - _this.watching = true; |
31 | | - callback(); |
32 | | - }); |
33 | | - compiler.plugin("run", function(compiler, callback) { |
34 | | - if(!compiler._lastCompilationFileDependencies) return callback(); |
35 | | - var fs = compiler.inputFileSystem; |
36 | | - var fileTs = compiler.fileTimestamps = {}; |
37 | | - async.forEach(compiler._lastCompilationFileDependencies, function(file, callback) { |
38 | | - fs.stat(file, function(err, stat) { |
39 | | - if(err) { |
40 | | - if(err.code === "ENOENT") return callback(); |
41 | | - return callback(err); |
42 | | - } |
| 9 | +class CachePlugin { |
| 10 | + constructor(cache) { |
| 11 | + this.cache = cache || {}; |
| 12 | + this.FS_ACCURENCY = 2000; |
| 13 | + } |
43 | 14 |
|
44 | | - if(stat.mtime) |
45 | | - _this.applyMtime(+stat.mtime); |
| 15 | + apply(compiler) { |
| 16 | + if(Array.isArray(compiler.compilers)) { |
| 17 | + compiler.compilers.forEach((c, idx) => { |
| 18 | + c.apply(new CachePlugin(this.cache[idx] = this.cache[idx] || {})); |
| 19 | + }); |
| 20 | + } else { |
| 21 | + compiler.plugin("compilation", compilation => { |
| 22 | + if(!compilation.notCacheable) { |
| 23 | + compilation.cache = this.cache; |
| 24 | + } else if(this.watching) { |
| 25 | + compilation.warnings.push( |
| 26 | + new Error(`CachePlugin - Cache cannot be used because of: ${compilation.notCacheable}`) |
| 27 | + ); |
| 28 | + } |
| 29 | + }); |
| 30 | + compiler.plugin("watch-run", (compiler, callback) => { |
| 31 | + this.watching = true; |
| 32 | + callback(); |
| 33 | + }); |
| 34 | + compiler.plugin("run", (compiler, callback) => { |
| 35 | + if(!compiler._lastCompilationFileDependencies) return callback(); |
| 36 | + const fs = compiler.inputFileSystem; |
| 37 | + const fileTs = compiler.fileTimestamps = {}; |
| 38 | + async.forEach(compiler._lastCompilationFileDependencies, (file, callback) => { |
| 39 | + fs.stat(file, (err, stat) => { |
| 40 | + if(err) { |
| 41 | + if(err.code === "ENOENT") return callback(); |
| 42 | + return callback(err); |
| 43 | + } |
46 | 44 |
|
47 | | - fileTs[file] = +stat.mtime || Infinity; |
| 45 | + if(stat.mtime) |
| 46 | + this.applyMtime(+stat.mtime); |
| 47 | + |
| 48 | + fileTs[file] = +stat.mtime || Infinity; |
| 49 | + callback(); |
| 50 | + }); |
| 51 | + }, err => { |
| 52 | + if(err) return callback(err); |
| 53 | + Object.keys(fileTs).forEach(key => { |
| 54 | + fileTs[key] += this.FS_ACCURENCY; |
| 55 | + }); |
48 | 56 | callback(); |
49 | 57 | }); |
50 | | - }, function(err) { |
51 | | - if(err) return callback(err); |
52 | | - Object.keys(fileTs).forEach(function(key) { |
53 | | - fileTs[key] += _this.FS_ACCURENCY; |
54 | | - }); |
| 58 | + }); |
| 59 | + compiler.plugin("after-compile", function(compilation, callback) { |
| 60 | + compilation.compiler._lastCompilationFileDependencies = compilation.fileDependencies; |
| 61 | + compilation.compiler._lastCompilationContextDependencies = compilation.contextDependencies; |
55 | 62 | callback(); |
56 | 63 | }); |
57 | | - }); |
58 | | - compiler.plugin("after-compile", function(compilation, callback) { |
59 | | - compilation.compiler._lastCompilationFileDependencies = compilation.fileDependencies; |
60 | | - compilation.compiler._lastCompilationContextDependencies = compilation.contextDependencies; |
61 | | - callback(); |
62 | | - }); |
| 64 | + } |
63 | 65 | } |
64 | | -}; |
65 | 66 |
|
66 | | -/* istanbul ignore next */ |
67 | | -CachePlugin.prototype.applyMtime = function applyMtime(mtime) { |
68 | | - if(this.FS_ACCURENCY > 1 && mtime % 2 !== 0) |
69 | | - this.FS_ACCURENCY = 1; |
70 | | - else if(this.FS_ACCURENCY > 10 && mtime % 20 !== 0) |
71 | | - this.FS_ACCURENCY = 10; |
72 | | - else if(this.FS_ACCURENCY > 100 && mtime % 200 !== 0) |
73 | | - this.FS_ACCURENCY = 100; |
74 | | - else if(this.FS_ACCURENCY > 1000 && mtime % 2000 !== 0) |
75 | | - this.FS_ACCURENCY = 1000; |
76 | | -}; |
| 67 | + /* istanbul ignore next */ |
| 68 | + applyMtime(mtime) { |
| 69 | + if(this.FS_ACCURENCY > 1 && mtime % 2 !== 0) |
| 70 | + this.FS_ACCURENCY = 1; |
| 71 | + else if(this.FS_ACCURENCY > 10 && mtime % 20 !== 0) |
| 72 | + this.FS_ACCURENCY = 10; |
| 73 | + else if(this.FS_ACCURENCY > 100 && mtime % 200 !== 0) |
| 74 | + this.FS_ACCURENCY = 100; |
| 75 | + else if(this.FS_ACCURENCY > 1000 && mtime % 2000 !== 0) |
| 76 | + this.FS_ACCURENCY = 1000; |
| 77 | + } |
| 78 | +} |
| 79 | +module.exports = CachePlugin; |
0 commit comments