tap-start-to-run-workers.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.tapStartToRunWorkers = void 0;
  13. const node_abort_controller_1 = require("node-abort-controller");
  14. const files_change_1 = require("../files-change");
  15. const infrastructure_logger_1 = require("../infrastructure-logger");
  16. const plugin_hooks_1 = require("../plugin-hooks");
  17. const plugin_pools_1 = require("../plugin-pools");
  18. const intercept_done_to_get_dev_server_tap_1 = require("./intercept-done-to-get-dev-server-tap");
  19. const tap_after_compile_to_get_issues_1 = require("./tap-after-compile-to-get-issues");
  20. const tap_done_to_async_get_issues_1 = require("./tap-done-to-async-get-issues");
  21. function tapStartToRunWorkers(compiler, getIssuesWorker, getDependenciesWorker, config, state) {
  22. const hooks = (0, plugin_hooks_1.getPluginHooks)(compiler);
  23. const { log, debug } = (0, infrastructure_logger_1.getInfrastructureLogger)(compiler);
  24. compiler.hooks.run.tap('ForkTsCheckerWebpackPlugin', () => {
  25. if (!state.initialized) {
  26. debug('Initializing plugin for single run (not async).');
  27. state.initialized = true;
  28. state.watching = false;
  29. (0, tap_after_compile_to_get_issues_1.tapAfterCompileToGetIssues)(compiler, config, state);
  30. }
  31. });
  32. compiler.hooks.watchRun.tap('ForkTsCheckerWebpackPlugin', () => __awaiter(this, void 0, void 0, function* () {
  33. if (!state.initialized) {
  34. state.initialized = true;
  35. state.watching = true;
  36. if (config.async) {
  37. debug('Initializing plugin for watch run (async).');
  38. (0, tap_done_to_async_get_issues_1.tapDoneToAsyncGetIssues)(compiler, config, state);
  39. (0, intercept_done_to_get_dev_server_tap_1.interceptDoneToGetDevServerTap)(compiler, config, state);
  40. }
  41. else {
  42. debug('Initializing plugin for watch run (not async).');
  43. (0, tap_after_compile_to_get_issues_1.tapAfterCompileToGetIssues)(compiler, config, state);
  44. }
  45. }
  46. }));
  47. compiler.hooks.compilation.tap('ForkTsCheckerWebpackPlugin', (compilation) => __awaiter(this, void 0, void 0, function* () {
  48. if (compilation.compiler !== compiler) {
  49. // run only for the compiler that the plugin was registered for
  50. return;
  51. }
  52. // get current iteration number
  53. const iteration = ++state.iteration;
  54. // abort previous iteration
  55. if (state.abortController) {
  56. debug(`Aborting iteration ${iteration - 1}.`);
  57. state.abortController.abort();
  58. }
  59. // create new abort controller for the new iteration
  60. const abortController = new node_abort_controller_1.AbortController();
  61. state.abortController = abortController;
  62. let filesChange = {};
  63. if (state.watching) {
  64. filesChange = (0, files_change_1.consumeFilesChange)(compiler);
  65. log([
  66. 'Calling reporter service for incremental check.',
  67. ` Changed files: ${JSON.stringify(filesChange.changedFiles)}`,
  68. ` Deleted files: ${JSON.stringify(filesChange.deletedFiles)}`,
  69. ].join('\n'));
  70. }
  71. else {
  72. log('Calling reporter service for single check.');
  73. }
  74. filesChange = yield hooks.start.promise(filesChange, compilation);
  75. let aggregatedFilesChange = filesChange;
  76. if (state.aggregatedFilesChange) {
  77. aggregatedFilesChange = (0, files_change_1.aggregateFilesChanges)([aggregatedFilesChange, filesChange]);
  78. debug([
  79. `Aggregating with previous files change, iteration ${iteration}.`,
  80. ` Changed files: ${JSON.stringify(aggregatedFilesChange.changedFiles)}`,
  81. ` Deleted files: ${JSON.stringify(aggregatedFilesChange.deletedFiles)}`,
  82. ].join('\n'));
  83. }
  84. state.aggregatedFilesChange = aggregatedFilesChange;
  85. // submit one at a time for a single compiler
  86. state.issuesPromise = (state.issuesPromise || Promise.resolve())
  87. // resolve to undefined on error
  88. .catch(() => undefined)
  89. .then(() => {
  90. // early return
  91. if (abortController.signal.aborted) {
  92. return undefined;
  93. }
  94. debug(`Submitting the getIssuesWorker to the pool, iteration ${iteration}.`);
  95. return plugin_pools_1.issuesPool.submit(() => __awaiter(this, void 0, void 0, function* () {
  96. try {
  97. debug(`Running the getIssuesWorker, iteration ${iteration}.`);
  98. const issues = yield getIssuesWorker(aggregatedFilesChange, state.watching);
  99. if (state.aggregatedFilesChange === aggregatedFilesChange) {
  100. state.aggregatedFilesChange = undefined;
  101. }
  102. if (state.abortController === abortController) {
  103. state.abortController = undefined;
  104. }
  105. return issues;
  106. }
  107. catch (error) {
  108. hooks.error.call(error, compilation);
  109. return undefined;
  110. }
  111. finally {
  112. debug(`The getIssuesWorker finished its job, iteration ${iteration}.`);
  113. }
  114. }), abortController.signal);
  115. });
  116. debug(`Submitting the getDependenciesWorker to the pool, iteration ${iteration}.`);
  117. state.dependenciesPromise = plugin_pools_1.dependenciesPool.submit(() => __awaiter(this, void 0, void 0, function* () {
  118. try {
  119. debug(`Running the getDependenciesWorker, iteration ${iteration}.`);
  120. return yield getDependenciesWorker(filesChange);
  121. }
  122. catch (error) {
  123. hooks.error.call(error, compilation);
  124. return undefined;
  125. }
  126. finally {
  127. debug(`The getDependenciesWorker finished its job, iteration ${iteration}.`);
  128. }
  129. })); // don't pass abortController.signal because getDependencies() is blocking
  130. }));
  131. }
  132. exports.tapStartToRunWorkers = tapStartToRunWorkers;