MultiCompiler.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const asyncLib = require("neo-async");
  7. const { SyncHook, MultiHook } = require("tapable");
  8. const ConcurrentCompilationError = require("./ConcurrentCompilationError");
  9. const MultiStats = require("./MultiStats");
  10. const MultiWatching = require("./MultiWatching");
  11. const ArrayQueue = require("./util/ArrayQueue");
  12. /** @template T @typedef {import("tapable").AsyncSeriesHook<T>} AsyncSeriesHook<T> */
  13. /** @template T @template R @typedef {import("tapable").SyncBailHook<T, R>} SyncBailHook<T, R> */
  14. /** @typedef {import("../declarations/WebpackOptions").WatchOptions} WatchOptions */
  15. /** @typedef {import("./Compiler")} Compiler */
  16. /** @typedef {import("./Stats")} Stats */
  17. /** @typedef {import("./Watching")} Watching */
  18. /** @typedef {import("./logging/Logger").Logger} Logger */
  19. /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
  20. /** @typedef {import("./util/fs").IntermediateFileSystem} IntermediateFileSystem */
  21. /** @typedef {import("./util/fs").OutputFileSystem} OutputFileSystem */
  22. /** @typedef {import("./util/fs").WatchFileSystem} WatchFileSystem */
  23. /**
  24. * @template T
  25. * @callback Callback
  26. * @param {Error | null} err
  27. * @param {T=} result
  28. */
  29. /**
  30. * @callback RunWithDependenciesHandler
  31. * @param {Compiler} compiler
  32. * @param {Callback<MultiStats>} callback
  33. */
  34. /**
  35. * @typedef {Object} MultiCompilerOptions
  36. * @property {number=} parallelism how many Compilers are allows to run at the same time in parallel
  37. */
  38. module.exports = class MultiCompiler {
  39. /**
  40. * @param {Compiler[] | Record<string, Compiler>} compilers child compilers
  41. * @param {MultiCompilerOptions} options options
  42. */
  43. constructor(compilers, options) {
  44. if (!Array.isArray(compilers)) {
  45. /** @type {Compiler[]} */
  46. compilers = Object.keys(compilers).map(name => {
  47. /** @type {Record<string, Compiler>} */
  48. (compilers)[name].name = name;
  49. return /** @type {Record<string, Compiler>} */ (compilers)[name];
  50. });
  51. }
  52. this.hooks = Object.freeze({
  53. /** @type {SyncHook<[MultiStats]>} */
  54. done: new SyncHook(["stats"]),
  55. /** @type {MultiHook<SyncHook<[string | null, number]>>} */
  56. invalid: new MultiHook(compilers.map(c => c.hooks.invalid)),
  57. /** @type {MultiHook<AsyncSeriesHook<[Compiler]>>} */
  58. run: new MultiHook(compilers.map(c => c.hooks.run)),
  59. /** @type {SyncHook<[]>} */
  60. watchClose: new SyncHook([]),
  61. /** @type {MultiHook<AsyncSeriesHook<[Compiler]>>} */
  62. watchRun: new MultiHook(compilers.map(c => c.hooks.watchRun)),
  63. /** @type {MultiHook<SyncBailHook<[string, string, any[]], true>>} */
  64. infrastructureLog: new MultiHook(
  65. compilers.map(c => c.hooks.infrastructureLog)
  66. )
  67. });
  68. this.compilers = compilers;
  69. /** @type {MultiCompilerOptions} */
  70. this._options = {
  71. parallelism: options.parallelism || Infinity
  72. };
  73. /** @type {WeakMap<Compiler, string[]>} */
  74. this.dependencies = new WeakMap();
  75. this.running = false;
  76. /** @type {(Stats | null)[]} */
  77. const compilerStats = this.compilers.map(() => null);
  78. let doneCompilers = 0;
  79. for (let index = 0; index < this.compilers.length; index++) {
  80. const compiler = this.compilers[index];
  81. const compilerIndex = index;
  82. let compilerDone = false;
  83. compiler.hooks.done.tap("MultiCompiler", stats => {
  84. if (!compilerDone) {
  85. compilerDone = true;
  86. doneCompilers++;
  87. }
  88. compilerStats[compilerIndex] = stats;
  89. if (doneCompilers === this.compilers.length) {
  90. this.hooks.done.call(
  91. new MultiStats(/** @type {Stats[]} */ (compilerStats))
  92. );
  93. }
  94. });
  95. compiler.hooks.invalid.tap("MultiCompiler", () => {
  96. if (compilerDone) {
  97. compilerDone = false;
  98. doneCompilers--;
  99. }
  100. });
  101. }
  102. }
  103. get options() {
  104. return Object.assign(
  105. this.compilers.map(c => c.options),
  106. this._options
  107. );
  108. }
  109. get outputPath() {
  110. let commonPath = this.compilers[0].outputPath;
  111. for (const compiler of this.compilers) {
  112. while (
  113. compiler.outputPath.indexOf(commonPath) !== 0 &&
  114. /[/\\]/.test(commonPath)
  115. ) {
  116. commonPath = commonPath.replace(/[/\\][^/\\]*$/, "");
  117. }
  118. }
  119. if (!commonPath && this.compilers[0].outputPath[0] === "/") return "/";
  120. return commonPath;
  121. }
  122. get inputFileSystem() {
  123. throw new Error("Cannot read inputFileSystem of a MultiCompiler");
  124. }
  125. get outputFileSystem() {
  126. throw new Error("Cannot read outputFileSystem of a MultiCompiler");
  127. }
  128. get watchFileSystem() {
  129. throw new Error("Cannot read watchFileSystem of a MultiCompiler");
  130. }
  131. get intermediateFileSystem() {
  132. throw new Error("Cannot read outputFileSystem of a MultiCompiler");
  133. }
  134. /**
  135. * @param {InputFileSystem} value the new input file system
  136. */
  137. set inputFileSystem(value) {
  138. for (const compiler of this.compilers) {
  139. compiler.inputFileSystem = value;
  140. }
  141. }
  142. /**
  143. * @param {OutputFileSystem} value the new output file system
  144. */
  145. set outputFileSystem(value) {
  146. for (const compiler of this.compilers) {
  147. compiler.outputFileSystem = value;
  148. }
  149. }
  150. /**
  151. * @param {WatchFileSystem} value the new watch file system
  152. */
  153. set watchFileSystem(value) {
  154. for (const compiler of this.compilers) {
  155. compiler.watchFileSystem = value;
  156. }
  157. }
  158. /**
  159. * @param {IntermediateFileSystem} value the new intermediate file system
  160. */
  161. set intermediateFileSystem(value) {
  162. for (const compiler of this.compilers) {
  163. compiler.intermediateFileSystem = value;
  164. }
  165. }
  166. /**
  167. * @param {string | (function(): string)} name name of the logger, or function called once to get the logger name
  168. * @returns {Logger} a logger with that name
  169. */
  170. getInfrastructureLogger(name) {
  171. return this.compilers[0].getInfrastructureLogger(name);
  172. }
  173. /**
  174. * @param {Compiler} compiler the child compiler
  175. * @param {string[]} dependencies its dependencies
  176. * @returns {void}
  177. */
  178. setDependencies(compiler, dependencies) {
  179. this.dependencies.set(compiler, dependencies);
  180. }
  181. /**
  182. * @param {Callback<MultiStats>} callback signals when the validation is complete
  183. * @returns {boolean} true if the dependencies are valid
  184. */
  185. validateDependencies(callback) {
  186. /** @type {Set<{source: Compiler, target: Compiler}>} */
  187. const edges = new Set();
  188. /** @type {string[]} */
  189. const missing = [];
  190. /**
  191. * @param {Compiler} compiler compiler
  192. * @returns {boolean} target was found
  193. */
  194. const targetFound = compiler => {
  195. for (const edge of edges) {
  196. if (edge.target === compiler) {
  197. return true;
  198. }
  199. }
  200. return false;
  201. };
  202. /**
  203. * @param {{source: Compiler, target: Compiler}} e1 edge 1
  204. * @param {{source: Compiler, target: Compiler}} e2 edge 2
  205. * @returns {number} result
  206. */
  207. const sortEdges = (e1, e2) => {
  208. return (
  209. /** @type {string} */
  210. (e1.source.name).localeCompare(
  211. /** @type {string} */ (e2.source.name)
  212. ) ||
  213. /** @type {string} */
  214. (e1.target.name).localeCompare(/** @type {string} */ (e2.target.name))
  215. );
  216. };
  217. for (const source of this.compilers) {
  218. const dependencies = this.dependencies.get(source);
  219. if (dependencies) {
  220. for (const dep of dependencies) {
  221. const target = this.compilers.find(c => c.name === dep);
  222. if (!target) {
  223. missing.push(dep);
  224. } else {
  225. edges.add({
  226. source,
  227. target
  228. });
  229. }
  230. }
  231. }
  232. }
  233. /** @type {string[]} */
  234. const errors = missing.map(m => `Compiler dependency \`${m}\` not found.`);
  235. const stack = this.compilers.filter(c => !targetFound(c));
  236. while (stack.length > 0) {
  237. const current = stack.pop();
  238. for (const edge of edges) {
  239. if (edge.source === current) {
  240. edges.delete(edge);
  241. const target = edge.target;
  242. if (!targetFound(target)) {
  243. stack.push(target);
  244. }
  245. }
  246. }
  247. }
  248. if (edges.size > 0) {
  249. /** @type {string[]} */
  250. const lines = Array.from(edges)
  251. .sort(sortEdges)
  252. .map(edge => `${edge.source.name} -> ${edge.target.name}`);
  253. lines.unshift("Circular dependency found in compiler dependencies.");
  254. errors.unshift(lines.join("\n"));
  255. }
  256. if (errors.length > 0) {
  257. const message = errors.join("\n");
  258. callback(new Error(message));
  259. return false;
  260. }
  261. return true;
  262. }
  263. // TODO webpack 6 remove
  264. /**
  265. * @deprecated This method should have been private
  266. * @param {Compiler[]} compilers the child compilers
  267. * @param {RunWithDependenciesHandler} fn a handler to run for each compiler
  268. * @param {Callback<MultiStats>} callback the compiler's handler
  269. * @returns {void}
  270. */
  271. runWithDependencies(compilers, fn, callback) {
  272. const fulfilledNames = new Set();
  273. let remainingCompilers = compilers;
  274. /**
  275. * @param {string} d dependency
  276. * @returns {boolean} when dependency was fulfilled
  277. */
  278. const isDependencyFulfilled = d => fulfilledNames.has(d);
  279. /**
  280. * @returns {Compiler[]} compilers
  281. */
  282. const getReadyCompilers = () => {
  283. let readyCompilers = [];
  284. let list = remainingCompilers;
  285. remainingCompilers = [];
  286. for (const c of list) {
  287. const dependencies = this.dependencies.get(c);
  288. const ready =
  289. !dependencies || dependencies.every(isDependencyFulfilled);
  290. if (ready) {
  291. readyCompilers.push(c);
  292. } else {
  293. remainingCompilers.push(c);
  294. }
  295. }
  296. return readyCompilers;
  297. };
  298. /**
  299. * @param {Callback<MultiStats>} callback callback
  300. * @returns {void}
  301. */
  302. const runCompilers = callback => {
  303. if (remainingCompilers.length === 0) return callback(null);
  304. asyncLib.map(
  305. getReadyCompilers(),
  306. (compiler, callback) => {
  307. fn(compiler, err => {
  308. if (err) return callback(err);
  309. fulfilledNames.add(compiler.name);
  310. runCompilers(callback);
  311. });
  312. },
  313. (err, results) => {
  314. callback(err, /** @type {TODO} */ (results));
  315. }
  316. );
  317. };
  318. runCompilers(callback);
  319. }
  320. /**
  321. * @template SetupResult
  322. * @param {function(Compiler, number, Callback<Stats>, function(): boolean, function(): void, function(): void): SetupResult} setup setup a single compiler
  323. * @param {function(Compiler, SetupResult, Callback<Stats>): void} run run/continue a single compiler
  324. * @param {Callback<MultiStats>} callback callback when all compilers are done, result includes Stats of all changed compilers
  325. * @returns {SetupResult[]} result of setup
  326. */
  327. _runGraph(setup, run, callback) {
  328. /** @typedef {{ compiler: Compiler, setupResult: undefined | SetupResult, result: undefined | Stats, state: "pending" | "blocked" | "queued" | "starting" | "running" | "running-outdated" | "done", children: Node[], parents: Node[] }} Node */
  329. // State transitions for nodes:
  330. // -> blocked (initial)
  331. // blocked -> starting [running++] (when all parents done)
  332. // queued -> starting [running++] (when processing the queue)
  333. // starting -> running (when run has been called)
  334. // running -> done [running--] (when compilation is done)
  335. // done -> pending (when invalidated from file change)
  336. // pending -> blocked [add to queue] (when invalidated from aggregated changes)
  337. // done -> blocked [add to queue] (when invalidated, from parent invalidation)
  338. // running -> running-outdated (when invalidated, either from change or parent invalidation)
  339. // running-outdated -> blocked [running--] (when compilation is done)
  340. /** @type {Node[]} */
  341. const nodes = this.compilers.map(compiler => ({
  342. compiler,
  343. setupResult: undefined,
  344. result: undefined,
  345. state: "blocked",
  346. children: [],
  347. parents: []
  348. }));
  349. /** @type {Map<string, Node>} */
  350. const compilerToNode = new Map();
  351. for (const node of nodes) {
  352. compilerToNode.set(/** @type {string} */ (node.compiler.name), node);
  353. }
  354. for (const node of nodes) {
  355. const dependencies = this.dependencies.get(node.compiler);
  356. if (!dependencies) continue;
  357. for (const dep of dependencies) {
  358. const parent = /** @type {Node} */ (compilerToNode.get(dep));
  359. node.parents.push(parent);
  360. parent.children.push(node);
  361. }
  362. }
  363. /** @type {ArrayQueue<Node>} */
  364. const queue = new ArrayQueue();
  365. for (const node of nodes) {
  366. if (node.parents.length === 0) {
  367. node.state = "queued";
  368. queue.enqueue(node);
  369. }
  370. }
  371. let errored = false;
  372. let running = 0;
  373. const parallelism = /** @type {number} */ (this._options.parallelism);
  374. /**
  375. * @param {Node} node node
  376. * @param {(Error | null)=} err error
  377. * @param {Stats=} stats result
  378. * @returns {void}
  379. */
  380. const nodeDone = (node, err, stats) => {
  381. if (errored) return;
  382. if (err) {
  383. errored = true;
  384. return asyncLib.each(
  385. nodes,
  386. (node, callback) => {
  387. if (node.compiler.watching) {
  388. node.compiler.watching.close(callback);
  389. } else {
  390. callback();
  391. }
  392. },
  393. () => callback(err)
  394. );
  395. }
  396. node.result = stats;
  397. running--;
  398. if (node.state === "running") {
  399. node.state = "done";
  400. for (const child of node.children) {
  401. if (child.state === "blocked") queue.enqueue(child);
  402. }
  403. } else if (node.state === "running-outdated") {
  404. node.state = "blocked";
  405. queue.enqueue(node);
  406. }
  407. processQueue();
  408. };
  409. /**
  410. * @param {Node} node node
  411. * @returns {void}
  412. */
  413. const nodeInvalidFromParent = node => {
  414. if (node.state === "done") {
  415. node.state = "blocked";
  416. } else if (node.state === "running") {
  417. node.state = "running-outdated";
  418. }
  419. for (const child of node.children) {
  420. nodeInvalidFromParent(child);
  421. }
  422. };
  423. /**
  424. * @param {Node} node node
  425. * @returns {void}
  426. */
  427. const nodeInvalid = node => {
  428. if (node.state === "done") {
  429. node.state = "pending";
  430. } else if (node.state === "running") {
  431. node.state = "running-outdated";
  432. }
  433. for (const child of node.children) {
  434. nodeInvalidFromParent(child);
  435. }
  436. };
  437. /**
  438. * @param {Node} node node
  439. * @returns {void}
  440. */
  441. const nodeChange = node => {
  442. nodeInvalid(node);
  443. if (node.state === "pending") {
  444. node.state = "blocked";
  445. }
  446. if (node.state === "blocked") {
  447. queue.enqueue(node);
  448. processQueue();
  449. }
  450. };
  451. /** @type {SetupResult[]} */
  452. const setupResults = [];
  453. nodes.forEach((node, i) => {
  454. setupResults.push(
  455. (node.setupResult = setup(
  456. node.compiler,
  457. i,
  458. nodeDone.bind(null, node),
  459. () => node.state !== "starting" && node.state !== "running",
  460. () => nodeChange(node),
  461. () => nodeInvalid(node)
  462. ))
  463. );
  464. });
  465. let processing = true;
  466. const processQueue = () => {
  467. if (processing) return;
  468. processing = true;
  469. process.nextTick(processQueueWorker);
  470. };
  471. const processQueueWorker = () => {
  472. while (running < parallelism && queue.length > 0 && !errored) {
  473. const node = /** @type {Node} */ (queue.dequeue());
  474. if (
  475. node.state === "queued" ||
  476. (node.state === "blocked" &&
  477. node.parents.every(p => p.state === "done"))
  478. ) {
  479. running++;
  480. node.state = "starting";
  481. run(
  482. node.compiler,
  483. /** @type {SetupResult} */ (node.setupResult),
  484. nodeDone.bind(null, node)
  485. );
  486. node.state = "running";
  487. }
  488. }
  489. processing = false;
  490. if (
  491. !errored &&
  492. running === 0 &&
  493. nodes.every(node => node.state === "done")
  494. ) {
  495. const stats = [];
  496. for (const node of nodes) {
  497. const result = node.result;
  498. if (result) {
  499. node.result = undefined;
  500. stats.push(result);
  501. }
  502. }
  503. if (stats.length > 0) {
  504. callback(null, new MultiStats(stats));
  505. }
  506. }
  507. };
  508. processQueueWorker();
  509. return setupResults;
  510. }
  511. /**
  512. * @param {WatchOptions|WatchOptions[]} watchOptions the watcher's options
  513. * @param {Callback<MultiStats>} handler signals when the call finishes
  514. * @returns {MultiWatching} a compiler watcher
  515. */
  516. watch(watchOptions, handler) {
  517. if (this.running) {
  518. return handler(new ConcurrentCompilationError());
  519. }
  520. this.running = true;
  521. if (this.validateDependencies(handler)) {
  522. const watchings = this._runGraph(
  523. (compiler, idx, callback, isBlocked, setChanged, setInvalid) => {
  524. const watching = compiler.watch(
  525. Array.isArray(watchOptions) ? watchOptions[idx] : watchOptions,
  526. callback
  527. );
  528. if (watching) {
  529. watching._onInvalid = setInvalid;
  530. watching._onChange = setChanged;
  531. watching._isBlocked = isBlocked;
  532. }
  533. return watching;
  534. },
  535. (compiler, watching, callback) => {
  536. if (compiler.watching !== watching) return;
  537. if (!watching.running) watching.invalidate();
  538. },
  539. handler
  540. );
  541. return new MultiWatching(watchings, this);
  542. }
  543. return new MultiWatching([], this);
  544. }
  545. /**
  546. * @param {Callback<MultiStats>} callback signals when the call finishes
  547. * @returns {void}
  548. */
  549. run(callback) {
  550. if (this.running) {
  551. return callback(new ConcurrentCompilationError());
  552. }
  553. this.running = true;
  554. if (this.validateDependencies(callback)) {
  555. this._runGraph(
  556. () => {},
  557. (compiler, setupResult, callback) => compiler.run(callback),
  558. (err, stats) => {
  559. this.running = false;
  560. if (callback !== undefined) {
  561. return callback(err, stats);
  562. }
  563. }
  564. );
  565. }
  566. }
  567. purgeInputFileSystem() {
  568. for (const compiler of this.compilers) {
  569. if (compiler.inputFileSystem && compiler.inputFileSystem.purge) {
  570. compiler.inputFileSystem.purge();
  571. }
  572. }
  573. }
  574. /**
  575. * @param {Callback<void>} callback signals when the compiler closes
  576. * @returns {void}
  577. */
  578. close(callback) {
  579. asyncLib.each(
  580. this.compilers,
  581. (compiler, callback) => {
  582. compiler.close(callback);
  583. },
  584. error => {
  585. callback(error);
  586. }
  587. );
  588. }
  589. };