Watching.js 4.8 KB

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