Watching.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. ) => {
  114. this.pausedWatcher = this.watcher;
  115. this.watcher = null;
  116. if (err) {
  117. return this.handler(err);
  118. }
  119. this.compiler.fileTimestamps = fileTimestamps;
  120. this.compiler.contextTimestamps = contextTimestamps;
  121. this._invalidate();
  122. },
  123. (fileName, changeTime) => {
  124. this.compiler.hooks.invalid.call(fileName, changeTime);
  125. }
  126. );
  127. }
  128. invalidate(callback) {
  129. if (callback) {
  130. this.callbacks.push(callback);
  131. }
  132. if (this.watcher) {
  133. this.compiler.fileTimestamps = this.watcher.getFileTimestamps();
  134. this.compiler.contextTimestamps = this.watcher.getContextTimestamps();
  135. }
  136. return this._invalidate();
  137. }
  138. _invalidate() {
  139. if (this.watcher) {
  140. this.pausedWatcher = this.watcher;
  141. this.watcher.pause();
  142. this.watcher = null;
  143. }
  144. if (this.running) {
  145. this.invalid = true;
  146. return false;
  147. } else {
  148. this._go();
  149. }
  150. }
  151. close(callback) {
  152. const finalCallback = () => {
  153. this.compiler.hooks.watchClose.call();
  154. this.compiler.running = false;
  155. if (callback !== undefined) callback();
  156. };
  157. this.closed = true;
  158. if (this.watcher) {
  159. this.watcher.close();
  160. this.watcher = null;
  161. }
  162. if (this.pausedWatcher) {
  163. this.pausedWatcher.close();
  164. this.pausedWatcher = null;
  165. }
  166. if (this.running) {
  167. this.invalid = true;
  168. this._done = finalCallback;
  169. } else {
  170. finalCallback();
  171. }
  172. }
  173. }
  174. module.exports = Watching;