1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const asyncLib = require("neo-async");
|
8 |
|
9 | class CachePlugin {
|
10 | constructor(cache) {
|
11 | this.cache = cache || {};
|
12 | this.FS_ACCURACY = 2000;
|
13 | }
|
14 |
|
15 | apply(compiler) {
|
16 | if (Array.isArray(compiler.compilers)) {
|
17 | compiler.compilers.forEach((c, idx) => {
|
18 | new CachePlugin((this.cache[idx] = this.cache[idx] || {})).apply(c);
|
19 | });
|
20 | } else {
|
21 | const registerCacheToCompiler = (compiler, cache) => {
|
22 | compiler.hooks.thisCompilation.tap("CachePlugin", compilation => {
|
23 | compilation.cache = cache;
|
24 | compilation.hooks.childCompiler.tap(
|
25 | "CachePlugin",
|
26 | (childCompiler, compilerName, compilerIndex) => {
|
27 | let childCache;
|
28 | if (!cache.children) {
|
29 | cache.children = {};
|
30 | }
|
31 | if (!cache.children[compilerName]) {
|
32 | cache.children[compilerName] = [];
|
33 | }
|
34 | if (cache.children[compilerName][compilerIndex]) {
|
35 | childCache = cache.children[compilerName][compilerIndex];
|
36 | } else {
|
37 | cache.children[compilerName].push((childCache = {}));
|
38 | }
|
39 | registerCacheToCompiler(childCompiler, childCache);
|
40 | }
|
41 | );
|
42 | });
|
43 | };
|
44 | registerCacheToCompiler(compiler, this.cache);
|
45 | compiler.hooks.watchRun.tap("CachePlugin", () => {
|
46 | this.watching = true;
|
47 | });
|
48 | compiler.hooks.run.tapAsync("CachePlugin", (compiler, callback) => {
|
49 | if (!compiler._lastCompilationFileDependencies) {
|
50 | return callback();
|
51 | }
|
52 | const fs = compiler.inputFileSystem;
|
53 | const fileTs = (compiler.fileTimestamps = new Map());
|
54 | asyncLib.forEach(
|
55 | compiler._lastCompilationFileDependencies,
|
56 | (file, callback) => {
|
57 | fs.stat(file, (err, stat) => {
|
58 | if (err) {
|
59 | if (err.code === "ENOENT") return callback();
|
60 | return callback(err);
|
61 | }
|
62 |
|
63 | if (stat.mtime) this.applyMtime(+stat.mtime);
|
64 |
|
65 | fileTs.set(file, +stat.mtime || Infinity);
|
66 |
|
67 | callback();
|
68 | });
|
69 | },
|
70 | err => {
|
71 | if (err) return callback(err);
|
72 |
|
73 | for (const [file, ts] of fileTs) {
|
74 | fileTs.set(file, ts + this.FS_ACCURACY);
|
75 | }
|
76 |
|
77 | callback();
|
78 | }
|
79 | );
|
80 | });
|
81 | compiler.hooks.afterCompile.tap("CachePlugin", compilation => {
|
82 | compilation.compiler._lastCompilationFileDependencies =
|
83 | compilation.fileDependencies;
|
84 | compilation.compiler._lastCompilationContextDependencies =
|
85 | compilation.contextDependencies;
|
86 | });
|
87 | }
|
88 | }
|
89 |
|
90 |
|
91 | applyMtime(mtime) {
|
92 | if (this.FS_ACCURACY > 1 && mtime % 2 !== 0) this.FS_ACCURACY = 1;
|
93 | else if (this.FS_ACCURACY > 10 && mtime % 20 !== 0) this.FS_ACCURACY = 10;
|
94 | else if (this.FS_ACCURACY > 100 && mtime % 200 !== 0)
|
95 | this.FS_ACCURACY = 100;
|
96 | else if (this.FS_ACCURACY > 1000 && mtime % 2000 !== 0)
|
97 | this.FS_ACCURACY = 1000;
|
98 | }
|
99 | }
|
100 | module.exports = CachePlugin;
|