MultiCompiler.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { Tapable, SyncHook, MultiHook } = require("tapable");
  7. const asyncLib = require("neo-async");
  8. const MultiWatching = require("./MultiWatching");
  9. const MultiStats = require("./MultiStats");
  10. const ConcurrentCompilationError = require("./ConcurrentCompilationError");
  11. module.exports = class MultiCompiler extends Tapable {
  12. constructor(compilers) {
  13. super();
  14. this.hooks = {
  15. done: new SyncHook(["stats"]),
  16. invalid: new MultiHook(compilers.map(c => c.hooks.invalid)),
  17. run: new MultiHook(compilers.map(c => c.hooks.run)),
  18. watchClose: new SyncHook([]),
  19. watchRun: new MultiHook(compilers.map(c => c.hooks.watchRun))
  20. };
  21. if (!Array.isArray(compilers)) {
  22. compilers = Object.keys(compilers).map(name => {
  23. compilers[name].name = name;
  24. return compilers[name];
  25. });
  26. }
  27. this.compilers = compilers;
  28. let doneCompilers = 0;
  29. let compilerStats = [];
  30. let index = 0;
  31. for (const compiler of this.compilers) {
  32. let compilerDone = false;
  33. const compilerIndex = index++;
  34. // eslint-disable-next-line no-loop-func
  35. compiler.hooks.done.tap("MultiCompiler", stats => {
  36. if (!compilerDone) {
  37. compilerDone = true;
  38. doneCompilers++;
  39. }
  40. compilerStats[compilerIndex] = stats;
  41. if (doneCompilers === this.compilers.length) {
  42. this.hooks.done.call(new MultiStats(compilerStats));
  43. }
  44. });
  45. // eslint-disable-next-line no-loop-func
  46. compiler.hooks.invalid.tap("MultiCompiler", () => {
  47. if (compilerDone) {
  48. compilerDone = false;
  49. doneCompilers--;
  50. }
  51. });
  52. }
  53. this.running = false;
  54. }
  55. get outputPath() {
  56. let commonPath = this.compilers[0].outputPath;
  57. for (const compiler of this.compilers) {
  58. while (
  59. compiler.outputPath.indexOf(commonPath) !== 0 &&
  60. /[/\\]/.test(commonPath)
  61. ) {
  62. commonPath = commonPath.replace(/[/\\][^/\\]*$/, "");
  63. }
  64. }
  65. if (!commonPath && this.compilers[0].outputPath[0] === "/") return "/";
  66. return commonPath;
  67. }
  68. get inputFileSystem() {
  69. throw new Error("Cannot read inputFileSystem of a MultiCompiler");
  70. }
  71. get outputFileSystem() {
  72. throw new Error("Cannot read outputFileSystem of a MultiCompiler");
  73. }
  74. set inputFileSystem(value) {
  75. for (const compiler of this.compilers) {
  76. compiler.inputFileSystem = value;
  77. }
  78. }
  79. set outputFileSystem(value) {
  80. for (const compiler of this.compilers) {
  81. compiler.outputFileSystem = value;
  82. }
  83. }
  84. validateDependencies(callback) {
  85. const edges = new Set();
  86. const missing = [];
  87. const targetFound = compiler => {
  88. for (const edge of edges) {
  89. if (edge.target === compiler) {
  90. return true;
  91. }
  92. }
  93. return false;
  94. };
  95. const sortEdges = (e1, e2) => {
  96. return (
  97. e1.source.name.localeCompare(e2.source.name) ||
  98. e1.target.name.localeCompare(e2.target.name)
  99. );
  100. };
  101. for (const source of this.compilers) {
  102. if (source.dependencies) {
  103. for (const dep of source.dependencies) {
  104. const target = this.compilers.find(c => c.name === dep);
  105. if (!target) {
  106. missing.push(dep);
  107. } else {
  108. edges.add({
  109. source,
  110. target
  111. });
  112. }
  113. }
  114. }
  115. }
  116. const errors = missing.map(m => `Compiler dependency \`${m}\` not found.`);
  117. const stack = this.compilers.filter(c => !targetFound(c));
  118. while (stack.length > 0) {
  119. const current = stack.pop();
  120. for (const edge of edges) {
  121. if (edge.source === current) {
  122. edges.delete(edge);
  123. const target = edge.target;
  124. if (!targetFound(target)) {
  125. stack.push(target);
  126. }
  127. }
  128. }
  129. }
  130. if (edges.size > 0) {
  131. const lines = Array.from(edges)
  132. .sort(sortEdges)
  133. .map(edge => `${edge.source.name} -> ${edge.target.name}`);
  134. lines.unshift("Circular dependency found in compiler dependencies.");
  135. errors.unshift(lines.join("\n"));
  136. }
  137. if (errors.length > 0) {
  138. const message = errors.join("\n");
  139. callback(new Error(message));
  140. return false;
  141. }
  142. return true;
  143. }
  144. runWithDependencies(compilers, fn, callback) {
  145. const fulfilledNames = new Set();
  146. let remainingCompilers = compilers;
  147. const isDependencyFulfilled = d => fulfilledNames.has(d);
  148. const getReadyCompilers = () => {
  149. let readyCompilers = [];
  150. let list = remainingCompilers;
  151. remainingCompilers = [];
  152. for (const c of list) {
  153. const ready =
  154. !c.dependencies || c.dependencies.every(isDependencyFulfilled);
  155. if (ready) {
  156. readyCompilers.push(c);
  157. } else {
  158. remainingCompilers.push(c);
  159. }
  160. }
  161. return readyCompilers;
  162. };
  163. const runCompilers = callback => {
  164. if (remainingCompilers.length === 0) return callback();
  165. asyncLib.map(
  166. getReadyCompilers(),
  167. (compiler, callback) => {
  168. fn(compiler, err => {
  169. if (err) return callback(err);
  170. fulfilledNames.add(compiler.name);
  171. runCompilers(callback);
  172. });
  173. },
  174. callback
  175. );
  176. };
  177. runCompilers(callback);
  178. }
  179. watch(watchOptions, handler) {
  180. if (this.running) return handler(new ConcurrentCompilationError());
  181. let watchings = [];
  182. let allStats = this.compilers.map(() => null);
  183. let compilerStatus = this.compilers.map(() => false);
  184. if (this.validateDependencies(handler)) {
  185. this.running = true;
  186. this.runWithDependencies(
  187. this.compilers,
  188. (compiler, callback) => {
  189. const compilerIdx = this.compilers.indexOf(compiler);
  190. let firstRun = true;
  191. let watching = compiler.watch(
  192. Array.isArray(watchOptions)
  193. ? watchOptions[compilerIdx]
  194. : watchOptions,
  195. (err, stats) => {
  196. if (err) handler(err);
  197. if (stats) {
  198. allStats[compilerIdx] = stats;
  199. compilerStatus[compilerIdx] = "new";
  200. if (compilerStatus.every(Boolean)) {
  201. const freshStats = allStats.filter((s, idx) => {
  202. return compilerStatus[idx] === "new";
  203. });
  204. compilerStatus.fill(true);
  205. const multiStats = new MultiStats(freshStats);
  206. handler(null, multiStats);
  207. }
  208. }
  209. if (firstRun && !err) {
  210. firstRun = false;
  211. callback();
  212. }
  213. }
  214. );
  215. watchings.push(watching);
  216. },
  217. () => {
  218. // ignore
  219. }
  220. );
  221. }
  222. return new MultiWatching(watchings, this);
  223. }
  224. run(callback) {
  225. if (this.running) {
  226. return callback(new ConcurrentCompilationError());
  227. }
  228. const finalCallback = (err, stats) => {
  229. this.running = false;
  230. if (callback !== undefined) {
  231. return callback(err, stats);
  232. }
  233. };
  234. const allStats = this.compilers.map(() => null);
  235. if (this.validateDependencies(callback)) {
  236. this.running = true;
  237. this.runWithDependencies(
  238. this.compilers,
  239. (compiler, callback) => {
  240. const compilerIdx = this.compilers.indexOf(compiler);
  241. compiler.run((err, stats) => {
  242. if (err) {
  243. return callback(err);
  244. }
  245. allStats[compilerIdx] = stats;
  246. callback();
  247. });
  248. },
  249. err => {
  250. if (err) {
  251. return finalCallback(err);
  252. }
  253. finalCallback(null, new MultiStats(allStats));
  254. }
  255. );
  256. }
  257. }
  258. purgeInputFileSystem() {
  259. for (const compiler of this.compilers) {
  260. if (compiler.inputFileSystem && compiler.inputFileSystem.purge) {
  261. compiler.inputFileSystem.purge();
  262. }
  263. }
  264. }
  265. };