CachePlugin.js 2.8 KB

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