source: frontend/node_modules/webpack/lib/MultiCompiler.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

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