Watching.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. /** @typedef {import("../declarations/WebpackOptions").WatchOptions} WatchOptions */
  8. /** @typedef {import("./Compilation")} Compilation */
  9. /** @typedef {import("./Compiler")} Compiler */
  10. /** @typedef {import("./FileSystemInfo").FileSystemInfoEntry} FileSystemInfoEntry */
  11. /**
  12. * @template T
  13. * @callback Callback
  14. * @param {(Error | null)=} err
  15. * @param {T=} result
  16. */
  17. class Watching {
  18. /**
  19. * @param {Compiler} compiler the compiler
  20. * @param {WatchOptions} watchOptions options
  21. * @param {Callback<Stats>} handler completion handler
  22. */
  23. constructor(compiler, watchOptions, handler) {
  24. this.startTime = null;
  25. this.invalid = false;
  26. this.handler = handler;
  27. /** @type {Callback<void>[]} */
  28. this.callbacks = [];
  29. /** @type {Callback<void>[] | undefined} */
  30. this._closeCallbacks = undefined;
  31. this.closed = false;
  32. this.suspended = false;
  33. this.blocked = false;
  34. this._isBlocked = () => false;
  35. this._onChange = () => {};
  36. this._onInvalid = () => {};
  37. if (typeof watchOptions === "number") {
  38. this.watchOptions = {
  39. aggregateTimeout: watchOptions
  40. };
  41. } else if (watchOptions && typeof watchOptions === "object") {
  42. this.watchOptions = { ...watchOptions };
  43. } else {
  44. this.watchOptions = {};
  45. }
  46. if (typeof this.watchOptions.aggregateTimeout !== "number") {
  47. this.watchOptions.aggregateTimeout = 20;
  48. }
  49. this.compiler = compiler;
  50. this.running = false;
  51. this._initial = true;
  52. this._invalidReported = true;
  53. this._needRecords = true;
  54. this.watcher = undefined;
  55. this.pausedWatcher = undefined;
  56. /** @type {Set<string>} */
  57. this._collectedChangedFiles = undefined;
  58. /** @type {Set<string>} */
  59. this._collectedRemovedFiles = undefined;
  60. this._done = this._done.bind(this);
  61. process.nextTick(() => {
  62. if (this._initial) this._invalidate();
  63. });
  64. }
  65. /**
  66. * @param {ReadonlySet<string>} changedFiles changed files
  67. * @param {ReadonlySet<string>} removedFiles removed files
  68. */
  69. _mergeWithCollected(changedFiles, removedFiles) {
  70. if (!changedFiles) return;
  71. if (!this._collectedChangedFiles) {
  72. this._collectedChangedFiles = new Set(changedFiles);
  73. this._collectedRemovedFiles = new Set(removedFiles);
  74. } else {
  75. for (const file of changedFiles) {
  76. this._collectedChangedFiles.add(file);
  77. this._collectedRemovedFiles.delete(file);
  78. }
  79. for (const file of removedFiles) {
  80. this._collectedChangedFiles.delete(file);
  81. this._collectedRemovedFiles.add(file);
  82. }
  83. }
  84. }
  85. /**
  86. * @param {ReadonlyMap<string, FileSystemInfoEntry | "ignore">=} fileTimeInfoEntries info for files
  87. * @param {ReadonlyMap<string, FileSystemInfoEntry | "ignore">=} contextTimeInfoEntries info for directories
  88. * @param {ReadonlySet<string>=} changedFiles changed files
  89. * @param {ReadonlySet<string>=} removedFiles removed files
  90. * @returns {void}
  91. */
  92. _go(fileTimeInfoEntries, contextTimeInfoEntries, changedFiles, removedFiles) {
  93. this._initial = false;
  94. if (this.startTime === null) this.startTime = Date.now();
  95. this.running = true;
  96. if (this.watcher) {
  97. this.pausedWatcher = this.watcher;
  98. this.lastWatcherStartTime = Date.now();
  99. this.watcher.pause();
  100. this.watcher = null;
  101. } else if (!this.lastWatcherStartTime) {
  102. this.lastWatcherStartTime = Date.now();
  103. }
  104. this.compiler.fsStartTime = Date.now();
  105. if (
  106. changedFiles &&
  107. removedFiles &&
  108. fileTimeInfoEntries &&
  109. contextTimeInfoEntries
  110. ) {
  111. this._mergeWithCollected(changedFiles, removedFiles);
  112. this.compiler.fileTimestamps = fileTimeInfoEntries;
  113. this.compiler.contextTimestamps = contextTimeInfoEntries;
  114. } else if (this.pausedWatcher) {
  115. if (this.pausedWatcher.getInfo) {
  116. const {
  117. changes,
  118. removals,
  119. fileTimeInfoEntries,
  120. contextTimeInfoEntries
  121. } = this.pausedWatcher.getInfo();
  122. this._mergeWithCollected(changes, removals);
  123. this.compiler.fileTimestamps = fileTimeInfoEntries;
  124. this.compiler.contextTimestamps = contextTimeInfoEntries;
  125. } else {
  126. this._mergeWithCollected(
  127. this.pausedWatcher.getAggregatedChanges &&
  128. this.pausedWatcher.getAggregatedChanges(),
  129. this.pausedWatcher.getAggregatedRemovals &&
  130. this.pausedWatcher.getAggregatedRemovals()
  131. );
  132. this.compiler.fileTimestamps =
  133. this.pausedWatcher.getFileTimeInfoEntries();
  134. this.compiler.contextTimestamps =
  135. this.pausedWatcher.getContextTimeInfoEntries();
  136. }
  137. }
  138. this.compiler.modifiedFiles = this._collectedChangedFiles;
  139. this._collectedChangedFiles = undefined;
  140. this.compiler.removedFiles = this._collectedRemovedFiles;
  141. this._collectedRemovedFiles = undefined;
  142. const run = () => {
  143. if (this.compiler.idle) {
  144. return this.compiler.cache.endIdle(err => {
  145. if (err) return this._done(err);
  146. this.compiler.idle = false;
  147. run();
  148. });
  149. }
  150. if (this._needRecords) {
  151. return this.compiler.readRecords(err => {
  152. if (err) return this._done(err);
  153. this._needRecords = false;
  154. run();
  155. });
  156. }
  157. this.invalid = false;
  158. this._invalidReported = false;
  159. this.compiler.hooks.watchRun.callAsync(this.compiler, err => {
  160. if (err) return this._done(err);
  161. const onCompiled = (err, compilation) => {
  162. if (err) return this._done(err, compilation);
  163. if (this.invalid) return this._done(null, compilation);
  164. if (this.compiler.hooks.shouldEmit.call(compilation) === false) {
  165. return this._done(null, compilation);
  166. }
  167. process.nextTick(() => {
  168. const logger = compilation.getLogger("webpack.Compiler");
  169. logger.time("emitAssets");
  170. this.compiler.emitAssets(compilation, err => {
  171. logger.timeEnd("emitAssets");
  172. if (err) return this._done(err, compilation);
  173. if (this.invalid) return this._done(null, compilation);
  174. logger.time("emitRecords");
  175. this.compiler.emitRecords(err => {
  176. logger.timeEnd("emitRecords");
  177. if (err) return this._done(err, compilation);
  178. if (compilation.hooks.needAdditionalPass.call()) {
  179. compilation.needAdditionalPass = true;
  180. compilation.startTime = this.startTime;
  181. compilation.endTime = Date.now();
  182. logger.time("done hook");
  183. const stats = new Stats(compilation);
  184. this.compiler.hooks.done.callAsync(stats, err => {
  185. logger.timeEnd("done hook");
  186. if (err) return this._done(err, compilation);
  187. this.compiler.hooks.additionalPass.callAsync(err => {
  188. if (err) return this._done(err, compilation);
  189. this.compiler.compile(onCompiled);
  190. });
  191. });
  192. return;
  193. }
  194. return this._done(null, compilation);
  195. });
  196. });
  197. });
  198. };
  199. this.compiler.compile(onCompiled);
  200. });
  201. };
  202. run();
  203. }
  204. /**
  205. * @param {Compilation} compilation the compilation
  206. * @returns {Stats} the compilation stats
  207. */
  208. _getStats(compilation) {
  209. const stats = new Stats(compilation);
  210. return stats;
  211. }
  212. /**
  213. * @param {Error=} err an optional error
  214. * @param {Compilation=} compilation the compilation
  215. * @returns {void}
  216. */
  217. _done(err, compilation) {
  218. this.running = false;
  219. const logger = compilation && compilation.getLogger("webpack.Watching");
  220. let stats = null;
  221. const handleError = (err, cbs) => {
  222. this.compiler.hooks.failed.call(err);
  223. this.compiler.cache.beginIdle();
  224. this.compiler.idle = true;
  225. this.handler(err, stats);
  226. if (!cbs) {
  227. cbs = this.callbacks;
  228. this.callbacks = [];
  229. }
  230. for (const cb of cbs) cb(err);
  231. };
  232. if (
  233. this.invalid &&
  234. !this.suspended &&
  235. !this.blocked &&
  236. !(this._isBlocked() && (this.blocked = true))
  237. ) {
  238. if (compilation) {
  239. logger.time("storeBuildDependencies");
  240. this.compiler.cache.storeBuildDependencies(
  241. compilation.buildDependencies,
  242. err => {
  243. logger.timeEnd("storeBuildDependencies");
  244. if (err) return handleError(err);
  245. this._go();
  246. }
  247. );
  248. } else {
  249. this._go();
  250. }
  251. return;
  252. }
  253. if (compilation) {
  254. compilation.startTime = this.startTime;
  255. compilation.endTime = Date.now();
  256. stats = new Stats(compilation);
  257. }
  258. this.startTime = null;
  259. if (err) return handleError(err);
  260. const cbs = this.callbacks;
  261. this.callbacks = [];
  262. logger.time("done hook");
  263. this.compiler.hooks.done.callAsync(stats, err => {
  264. logger.timeEnd("done hook");
  265. if (err) return handleError(err, cbs);
  266. this.handler(null, stats);
  267. logger.time("storeBuildDependencies");
  268. this.compiler.cache.storeBuildDependencies(
  269. compilation.buildDependencies,
  270. err => {
  271. logger.timeEnd("storeBuildDependencies");
  272. if (err) return handleError(err, cbs);
  273. logger.time("beginIdle");
  274. this.compiler.cache.beginIdle();
  275. this.compiler.idle = true;
  276. logger.timeEnd("beginIdle");
  277. process.nextTick(() => {
  278. if (!this.closed) {
  279. this.watch(
  280. compilation.fileDependencies,
  281. compilation.contextDependencies,
  282. compilation.missingDependencies
  283. );
  284. }
  285. });
  286. for (const cb of cbs) cb(null);
  287. this.compiler.hooks.afterDone.call(stats);
  288. }
  289. );
  290. });
  291. }
  292. /**
  293. * @param {Iterable<string>} files watched files
  294. * @param {Iterable<string>} dirs watched directories
  295. * @param {Iterable<string>} missing watched existence entries
  296. * @returns {void}
  297. */
  298. watch(files, dirs, missing) {
  299. this.pausedWatcher = null;
  300. this.watcher = this.compiler.watchFileSystem.watch(
  301. files,
  302. dirs,
  303. missing,
  304. this.lastWatcherStartTime,
  305. this.watchOptions,
  306. (
  307. err,
  308. fileTimeInfoEntries,
  309. contextTimeInfoEntries,
  310. changedFiles,
  311. removedFiles
  312. ) => {
  313. if (err) {
  314. this.compiler.modifiedFiles = undefined;
  315. this.compiler.removedFiles = undefined;
  316. this.compiler.fileTimestamps = undefined;
  317. this.compiler.contextTimestamps = undefined;
  318. this.compiler.fsStartTime = undefined;
  319. return this.handler(err);
  320. }
  321. this._invalidate(
  322. fileTimeInfoEntries,
  323. contextTimeInfoEntries,
  324. changedFiles,
  325. removedFiles
  326. );
  327. this._onChange();
  328. },
  329. (fileName, changeTime) => {
  330. if (!this._invalidReported) {
  331. this._invalidReported = true;
  332. this.compiler.hooks.invalid.call(fileName, changeTime);
  333. }
  334. this._onInvalid();
  335. }
  336. );
  337. }
  338. /**
  339. * @param {Callback<void>=} callback signals when the build has completed again
  340. * @returns {void}
  341. */
  342. invalidate(callback) {
  343. if (callback) {
  344. this.callbacks.push(callback);
  345. }
  346. if (!this._invalidReported) {
  347. this._invalidReported = true;
  348. this.compiler.hooks.invalid.call(null, Date.now());
  349. }
  350. this._onChange();
  351. this._invalidate();
  352. }
  353. _invalidate(
  354. fileTimeInfoEntries,
  355. contextTimeInfoEntries,
  356. changedFiles,
  357. removedFiles
  358. ) {
  359. if (this.suspended || (this._isBlocked() && (this.blocked = true))) {
  360. this._mergeWithCollected(changedFiles, removedFiles);
  361. return;
  362. }
  363. if (this.running) {
  364. this._mergeWithCollected(changedFiles, removedFiles);
  365. this.invalid = true;
  366. } else {
  367. this._go(
  368. fileTimeInfoEntries,
  369. contextTimeInfoEntries,
  370. changedFiles,
  371. removedFiles
  372. );
  373. }
  374. }
  375. suspend() {
  376. this.suspended = true;
  377. }
  378. resume() {
  379. if (this.suspended) {
  380. this.suspended = false;
  381. this._invalidate();
  382. }
  383. }
  384. /**
  385. * @param {Callback<void>} callback signals when the watcher is closed
  386. * @returns {void}
  387. */
  388. close(callback) {
  389. if (this._closeCallbacks) {
  390. if (callback) {
  391. this._closeCallbacks.push(callback);
  392. }
  393. return;
  394. }
  395. const finalCallback = (err, compilation) => {
  396. this.running = false;
  397. this.compiler.running = false;
  398. this.compiler.watching = undefined;
  399. this.compiler.watchMode = false;
  400. this.compiler.modifiedFiles = undefined;
  401. this.compiler.removedFiles = undefined;
  402. this.compiler.fileTimestamps = undefined;
  403. this.compiler.contextTimestamps = undefined;
  404. this.compiler.fsStartTime = undefined;
  405. const shutdown = err => {
  406. this.compiler.hooks.watchClose.call();
  407. const closeCallbacks = this._closeCallbacks;
  408. this._closeCallbacks = undefined;
  409. for (const cb of closeCallbacks) cb(err);
  410. };
  411. if (compilation) {
  412. const logger = compilation.getLogger("webpack.Watching");
  413. logger.time("storeBuildDependencies");
  414. this.compiler.cache.storeBuildDependencies(
  415. compilation.buildDependencies,
  416. err2 => {
  417. logger.timeEnd("storeBuildDependencies");
  418. shutdown(err || err2);
  419. }
  420. );
  421. } else {
  422. shutdown(err);
  423. }
  424. };
  425. this.closed = true;
  426. if (this.watcher) {
  427. this.watcher.close();
  428. this.watcher = null;
  429. }
  430. if (this.pausedWatcher) {
  431. this.pausedWatcher.close();
  432. this.pausedWatcher = null;
  433. }
  434. this._closeCallbacks = [];
  435. if (callback) {
  436. this._closeCallbacks.push(callback);
  437. }
  438. if (this.running) {
  439. this.invalid = true;
  440. this._done = finalCallback;
  441. } else {
  442. finalCallback();
  443. }
  444. }
  445. }
  446. module.exports = Watching;