Watching.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Stats = require("./Stats");
  7. class Watching {
  8. constructor(compiler, watchOptions, handler) {
  9. this.startTime = null;
  10. this.invalid = false;
  11. this.handler = handler;
  12. this.callbacks = [];
  13. this.closed = false;
  14. if (typeof watchOptions === "number") {
  15. this.watchOptions = {
  16. aggregateTimeout: watchOptions
  17. };
  18. } else if (watchOptions && typeof watchOptions === "object") {
  19. this.watchOptions = Object.assign({}, watchOptions);
  20. } else {
  21. this.watchOptions = {};
  22. }
  23. this.watchOptions.aggregateTimeout =
  24. this.watchOptions.aggregateTimeout || 200;
  25. this.compiler = compiler;
  26. this.running = true;
  27. this.compiler.readRecords(err => {
  28. if (err) return this._done(err);
  29. this._go();
  30. });
  31. }
  32. _go() {
  33. this.startTime = Date.now();
  34. this.running = true;
  35. this.invalid = false;
  36. this.compiler.hooks.watchRun.callAsync(this.compiler, err => {
  37. if (err) return this._done(err);
  38. const onCompiled = (err, compilation) => {
  39. if (err) return this._done(err);
  40. if (this.invalid) return this._done();
  41. if (this.compiler.hooks.shouldEmit.call(compilation) === false) {
  42. return this._done(null, compilation);
  43. }
  44. this.compiler.emitAssets(compilation, err => {
  45. if (err) return this._done(err);
  46. if (this.invalid) return this._done();
  47. this.compiler.emitRecords(err => {
  48. if (err) return this._done(err);
  49. if (compilation.hooks.needAdditionalPass.call()) {
  50. compilation.needAdditionalPass = true;
  51. const stats = new Stats(compilation);
  52. stats.startTime = this.startTime;
  53. stats.endTime = Date.now();
  54. this.compiler.hooks.done.callAsync(stats, err => {
  55. if (err) return this._done(err);
  56. this.compiler.hooks.additionalPass.callAsync(err => {
  57. if (err) return this._done(err);
  58. this.compiler.compile(onCompiled);
  59. });
  60. });
  61. return;
  62. }
  63. return this._done(null, compilation);
  64. });
  65. });
  66. };
  67. this.compiler.compile(onCompiled);
  68. });
  69. }
  70. _getStats(compilation) {
  71. const stats = new Stats(compilation);
  72. stats.startTime = this.startTime;
  73. stats.endTime = Date.now();
  74. return stats;
  75. }
  76. _done(err, compilation) {
  77. this.running = false;
  78. if (this.invalid) return this._go();
  79. const stats = compilation ? this._getStats(compilation) : null;
  80. if (err) {
  81. this.compiler.hooks.failed.call(err);
  82. this.handler(err, stats);
  83. return;
  84. }
  85. this.compiler.hooks.done.callAsync(stats, () => {
  86. this.handler(null, stats);
  87. if (!this.closed) {
  88. this.watch(
  89. Array.from(compilation.fileDependencies),
  90. Array.from(compilation.contextDependencies),
  91. Array.from(compilation.missingDependencies)
  92. );
  93. }
  94. for (const cb of this.callbacks) cb();
  95. this.callbacks.length = 0;
  96. });
  97. }
  98. watch(files, dirs, missing) {
  99. this.pausedWatcher = null;
  100. this.watcher = this.compiler.watchFileSystem.watch(
  101. files,
  102. dirs,
  103. missing,
  104. this.startTime,
  105. this.watchOptions,
  106. (
  107. err,
  108. filesModified,
  109. contextModified,
  110. missingModified,
  111. fileTimestamps,
  112. contextTimestamps,
  113. removedFiles
  114. ) => {
  115. this.pausedWatcher = this.watcher;
  116. this.watcher = null;
  117. if (err) {
  118. return this.handler(err);
  119. }
  120. this.compiler.fileTimestamps = fileTimestamps;
  121. this.compiler.contextTimestamps = contextTimestamps;
  122. this.compiler.removedFiles = removedFiles;
  123. this._invalidate();
  124. },
  125. (fileName, changeTime) => {
  126. this.compiler.hooks.invalid.call(fileName, changeTime);
  127. }
  128. );
  129. }
  130. invalidate(callback) {
  131. if (callback) {
  132. this.callbacks.push(callback);
  133. }
  134. if (this.watcher) {
  135. this.compiler.fileTimestamps = this.watcher.getFileTimestamps();
  136. this.compiler.contextTimestamps = this.watcher.getContextTimestamps();
  137. }
  138. return this._invalidate();
  139. }
  140. _invalidate() {
  141. if (this.watcher) {
  142. this.pausedWatcher = this.watcher;
  143. this.watcher.pause();
  144. this.watcher = null;
  145. }
  146. if (this.running) {
  147. this.invalid = true;
  148. return false;
  149. } else {
  150. this._go();
  151. }
  152. }
  153. close(callback) {
  154. const finalCallback = () => {
  155. this.compiler.hooks.watchClose.call();
  156. this.compiler.running = false;
  157. this.compiler.watchMode = false;
  158. if (callback !== undefined) callback();
  159. };
  160. this.closed = true;
  161. if (this.watcher) {
  162. this.watcher.close();
  163. this.watcher = null;
  164. }
  165. if (this.pausedWatcher) {
  166. this.pausedWatcher.close();
  167. this.pausedWatcher = null;
  168. }
  169. if (this.running) {
  170. this.invalid = true;
  171. this._done = finalCallback;
  172. } else {
  173. finalCallback();
  174. }
  175. }
  176. }
  177. module.exports = Watching;