1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const asyncLib = require("neo-async");
|
8 | const util = require("util");
|
9 | const { CachedSource } = require("webpack-sources");
|
10 | const {
|
11 | Tapable,
|
12 | SyncHook,
|
13 | SyncBailHook,
|
14 | SyncWaterfallHook,
|
15 | AsyncSeriesHook
|
16 | } = require("tapable");
|
17 | const EntryModuleNotFoundError = require("./EntryModuleNotFoundError");
|
18 | const ModuleNotFoundError = require("./ModuleNotFoundError");
|
19 | const ModuleDependencyWarning = require("./ModuleDependencyWarning");
|
20 | const ModuleDependencyError = require("./ModuleDependencyError");
|
21 | const ChunkGroup = require("./ChunkGroup");
|
22 | const Chunk = require("./Chunk");
|
23 | const Entrypoint = require("./Entrypoint");
|
24 | const MainTemplate = require("./MainTemplate");
|
25 | const ChunkTemplate = require("./ChunkTemplate");
|
26 | const HotUpdateChunkTemplate = require("./HotUpdateChunkTemplate");
|
27 | const ModuleTemplate = require("./ModuleTemplate");
|
28 | const RuntimeTemplate = require("./RuntimeTemplate");
|
29 | const ChunkRenderError = require("./ChunkRenderError");
|
30 | const Stats = require("./Stats");
|
31 | const Semaphore = require("./util/Semaphore");
|
32 | const createHash = require("./util/createHash");
|
33 | const SortableSet = require("./util/SortableSet");
|
34 | const GraphHelpers = require("./GraphHelpers");
|
35 | const ModuleDependency = require("./dependencies/ModuleDependency");
|
36 | const compareLocations = require("./compareLocations");
|
37 | const { Logger, LogType } = require("./logging/Logger");
|
38 | const ErrorHelpers = require("./ErrorHelpers");
|
39 | const buildChunkGraph = require("./buildChunkGraph");
|
40 | const WebpackError = require("./WebpackError");
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | const byId = (a, b) => {
|
128 | if (typeof a.id !== typeof b.id) {
|
129 | return typeof a.id < typeof b.id ? -1 : 1;
|
130 | }
|
131 | if (a.id < b.id) return -1;
|
132 | if (a.id > b.id) return 1;
|
133 | return 0;
|
134 | };
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | const byIdOrIdentifier = (a, b) => {
|
142 | if (typeof a.id !== typeof b.id) {
|
143 | return typeof a.id < typeof b.id ? -1 : 1;
|
144 | }
|
145 | if (a.id < b.id) return -1;
|
146 | if (a.id > b.id) return 1;
|
147 | const identA = a.identifier();
|
148 | const identB = b.identifier();
|
149 | if (identA < identB) return -1;
|
150 | if (identA > identB) return 1;
|
151 | return 0;
|
152 | };
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | const byIndexOrIdentifier = (a, b) => {
|
160 | if (a.index < b.index) return -1;
|
161 | if (a.index > b.index) return 1;
|
162 | const identA = a.identifier();
|
163 | const identB = b.identifier();
|
164 | if (identA < identB) return -1;
|
165 | if (identA > identB) return 1;
|
166 | return 0;
|
167 | };
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | const byNameOrHash = (a, b) => {
|
175 | if (a.name < b.name) return -1;
|
176 | if (a.name > b.name) return 1;
|
177 | if (a.fullHash < b.fullHash) return -1;
|
178 | if (a.fullHash > b.fullHash) return 1;
|
179 | return 0;
|
180 | };
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | const iterationBlockVariable = (variables, fn) => {
|
188 | for (
|
189 | let indexVariable = 0;
|
190 | indexVariable < variables.length;
|
191 | indexVariable++
|
192 | ) {
|
193 | const varDep = variables[indexVariable].dependencies;
|
194 | for (let indexVDep = 0; indexVDep < varDep.length; indexVDep++) {
|
195 | fn(varDep[indexVDep]);
|
196 | }
|
197 | }
|
198 | };
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 | const iterationOfArrayCallback = (arr, fn) => {
|
207 | for (let index = 0; index < arr.length; index++) {
|
208 | fn(arr[index]);
|
209 | }
|
210 | };
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 | const addAllToSet = (set, otherSet) => {
|
219 | for (const item of otherSet) {
|
220 | set.add(item);
|
221 | }
|
222 | };
|
223 |
|
224 | /**
|
225 | * @param {Source} a a source
|
226 | * @param {Source} b another source
|
227 | * @returns {boolean} true, when both sources are equal
|
228 | */
|
229 | const isSourceEqual = (a, b) => {
|
230 | if (a === b) return true;
|
231 |
|
232 |
|
233 | let aSource = a.source();
|
234 |
|
235 | let bSource = b.source();
|
236 | if (aSource === bSource) return true;
|
237 | if (typeof aSource === "string" && typeof bSource === "string") return false;
|
238 | if (!Buffer.isBuffer(aSource)) aSource = Buffer.from(aSource, "utf-8");
|
239 | if (!Buffer.isBuffer(bSource)) bSource = Buffer.from(bSource, "utf-8");
|
240 | return aSource.equals(bSource);
|
241 | };
|
242 |
|
243 | class Compilation extends Tapable {
|
244 | |
245 |
|
246 |
|
247 |
|
248 | constructor(compiler) {
|
249 | super();
|
250 | this.hooks = {
|
251 |
|
252 | buildModule: new SyncHook(["module"]),
|
253 |
|
254 | rebuildModule: new SyncHook(["module"]),
|
255 |
|
256 | failedModule: new SyncHook(["module", "error"]),
|
257 |
|
258 | succeedModule: new SyncHook(["module"]),
|
259 |
|
260 |
|
261 | addEntry: new SyncHook(["entry", "name"]),
|
262 |
|
263 | failedEntry: new SyncHook(["entry", "name", "error"]),
|
264 |
|
265 | succeedEntry: new SyncHook(["entry", "name", "module"]),
|
266 |
|
267 |
|
268 | dependencyReference: new SyncWaterfallHook([
|
269 | "dependencyReference",
|
270 | "dependency",
|
271 | "module"
|
272 | ]),
|
273 |
|
274 |
|
275 | finishModules: new AsyncSeriesHook(["modules"]),
|
276 |
|
277 | finishRebuildingModule: new SyncHook(["module"]),
|
278 |
|
279 | unseal: new SyncHook([]),
|
280 |
|
281 | seal: new SyncHook([]),
|
282 |
|
283 |
|
284 | beforeChunks: new SyncHook([]),
|
285 |
|
286 | afterChunks: new SyncHook(["chunks"]),
|
287 |
|
288 |
|
289 | optimizeDependenciesBasic: new SyncBailHook(["modules"]),
|
290 |
|
291 | optimizeDependencies: new SyncBailHook(["modules"]),
|
292 |
|
293 | optimizeDependenciesAdvanced: new SyncBailHook(["modules"]),
|
294 |
|
295 | afterOptimizeDependencies: new SyncHook(["modules"]),
|
296 |
|
297 |
|
298 | optimize: new SyncHook([]),
|
299 |
|
300 | optimizeModulesBasic: new SyncBailHook(["modules"]),
|
301 |
|
302 | optimizeModules: new SyncBailHook(["modules"]),
|
303 |
|
304 | optimizeModulesAdvanced: new SyncBailHook(["modules"]),
|
305 |
|
306 | afterOptimizeModules: new SyncHook(["modules"]),
|
307 |
|
308 |
|
309 | optimizeChunksBasic: new SyncBailHook(["chunks", "chunkGroups"]),
|
310 |
|
311 | optimizeChunks: new SyncBailHook(["chunks", "chunkGroups"]),
|
312 |
|
313 | optimizeChunksAdvanced: new SyncBailHook(["chunks", "chunkGroups"]),
|
314 |
|
315 | afterOptimizeChunks: new SyncHook(["chunks", "chunkGroups"]),
|
316 |
|
317 |
|
318 | optimizeTree: new AsyncSeriesHook(["chunks", "modules"]),
|
319 |
|
320 | afterOptimizeTree: new SyncHook(["chunks", "modules"]),
|
321 |
|
322 |
|
323 | optimizeChunkModulesBasic: new SyncBailHook(["chunks", "modules"]),
|
324 |
|
325 | optimizeChunkModules: new SyncBailHook(["chunks", "modules"]),
|
326 |
|
327 | optimizeChunkModulesAdvanced: new SyncBailHook(["chunks", "modules"]),
|
328 |
|
329 | afterOptimizeChunkModules: new SyncHook(["chunks", "modules"]),
|
330 |
|
331 | shouldRecord: new SyncBailHook([]),
|
332 |
|
333 |
|
334 | reviveModules: new SyncHook(["modules", "records"]),
|
335 |
|
336 | optimizeModuleOrder: new SyncHook(["modules"]),
|
337 |
|
338 | advancedOptimizeModuleOrder: new SyncHook(["modules"]),
|
339 |
|
340 | beforeModuleIds: new SyncHook(["modules"]),
|
341 |
|
342 | moduleIds: new SyncHook(["modules"]),
|
343 |
|
344 | optimizeModuleIds: new SyncHook(["modules"]),
|
345 |
|
346 | afterOptimizeModuleIds: new SyncHook(["modules"]),
|
347 |
|
348 |
|
349 | reviveChunks: new SyncHook(["chunks", "records"]),
|
350 |
|
351 | optimizeChunkOrder: new SyncHook(["chunks"]),
|
352 |
|
353 | beforeChunkIds: new SyncHook(["chunks"]),
|
354 |
|
355 | optimizeChunkIds: new SyncHook(["chunks"]),
|
356 |
|
357 | afterOptimizeChunkIds: new SyncHook(["chunks"]),
|
358 |
|
359 |
|
360 | recordModules: new SyncHook(["modules", "records"]),
|
361 |
|
362 | recordChunks: new SyncHook(["chunks", "records"]),
|
363 |
|
364 |
|
365 | beforeHash: new SyncHook([]),
|
366 |
|
367 | contentHash: new SyncHook(["chunk"]),
|
368 |
|
369 | afterHash: new SyncHook([]),
|
370 |
|
371 | recordHash: new SyncHook(["records"]),
|
372 |
|
373 | record: new SyncHook(["compilation", "records"]),
|
374 |
|
375 |
|
376 | beforeModuleAssets: new SyncHook([]),
|
377 |
|
378 | shouldGenerateChunkAssets: new SyncBailHook([]),
|
379 |
|
380 | beforeChunkAssets: new SyncHook([]),
|
381 |
|
382 | additionalChunkAssets: new SyncHook(["chunks"]),
|
383 |
|
384 |
|
385 | additionalAssets: new AsyncSeriesHook([]),
|
386 |
|
387 | optimizeChunkAssets: new AsyncSeriesHook(["chunks"]),
|
388 |
|
389 | afterOptimizeChunkAssets: new SyncHook(["chunks"]),
|
390 |
|
391 | optimizeAssets: new AsyncSeriesHook(["assets"]),
|
392 |
|
393 | afterOptimizeAssets: new SyncHook(["assets"]),
|
394 |
|
395 |
|
396 | needAdditionalSeal: new SyncBailHook([]),
|
397 |
|
398 | afterSeal: new AsyncSeriesHook([]),
|
399 |
|
400 |
|
401 | chunkHash: new SyncHook(["chunk", "chunkHash"]),
|
402 |
|
403 | moduleAsset: new SyncHook(["module", "filename"]),
|
404 |
|
405 | chunkAsset: new SyncHook(["chunk", "filename"]),
|
406 |
|
407 |
|
408 | assetPath: new SyncWaterfallHook(["filename", "data"]),
|
409 |
|
410 |
|
411 | needAdditionalPass: new SyncBailHook([]),
|
412 |
|
413 |
|
414 | childCompiler: new SyncHook([
|
415 | "childCompiler",
|
416 | "compilerName",
|
417 | "compilerIndex"
|
418 | ]),
|
419 |
|
420 |
|
421 | log: new SyncBailHook(["origin", "logEntry"]),
|
422 |
|
423 |
|
424 |
|
425 |
|
426 | normalModuleLoader: new SyncHook(["loaderContext", "module"]),
|
427 |
|
428 |
|
429 | optimizeExtractedChunksBasic: new SyncBailHook(["chunks"]),
|
430 |
|
431 | optimizeExtractedChunks: new SyncBailHook(["chunks"]),
|
432 |
|
433 | optimizeExtractedChunksAdvanced: new SyncBailHook(["chunks"]),
|
434 |
|
435 | afterOptimizeExtractedChunks: new SyncHook(["chunks"])
|
436 | };
|
437 | this._pluginCompat.tap("Compilation", options => {
|
438 | switch (options.name) {
|
439 | case "optimize-tree":
|
440 | case "additional-assets":
|
441 | case "optimize-chunk-assets":
|
442 | case "optimize-assets":
|
443 | case "after-seal":
|
444 | options.async = true;
|
445 | break;
|
446 | }
|
447 | });
|
448 |
|
449 | this.name = undefined;
|
450 |
|
451 | this.compiler = compiler;
|
452 | this.resolverFactory = compiler.resolverFactory;
|
453 | this.inputFileSystem = compiler.inputFileSystem;
|
454 | this.requestShortener = compiler.requestShortener;
|
455 |
|
456 | const options = compiler.options;
|
457 | this.options = options;
|
458 | this.outputOptions = options && options.output;
|
459 |
|
460 | this.bail = options && options.bail;
|
461 | this.profile = options && options.profile;
|
462 | this.performance = options && options.performance;
|
463 |
|
464 | this.mainTemplate = new MainTemplate(this.outputOptions);
|
465 | this.chunkTemplate = new ChunkTemplate(this.outputOptions);
|
466 | this.hotUpdateChunkTemplate = new HotUpdateChunkTemplate(
|
467 | this.outputOptions
|
468 | );
|
469 | this.runtimeTemplate = new RuntimeTemplate(
|
470 | this.outputOptions,
|
471 | this.requestShortener
|
472 | );
|
473 | this.moduleTemplates = {
|
474 | javascript: new ModuleTemplate(this.runtimeTemplate, "javascript"),
|
475 | webassembly: new ModuleTemplate(this.runtimeTemplate, "webassembly")
|
476 | };
|
477 |
|
478 | this.semaphore = new Semaphore(options.parallelism || 100);
|
479 |
|
480 | this.entries = [];
|
481 |
|
482 | this._preparedEntrypoints = [];
|
483 |
|
484 | this.entrypoints = new Map();
|
485 |
|
486 | this.chunks = [];
|
487 |
|
488 | this.chunkGroups = [];
|
489 |
|
490 | this.namedChunkGroups = new Map();
|
491 |
|
492 | this.namedChunks = new Map();
|
493 |
|
494 | this.modules = [];
|
495 |
|
496 | this._modules = new Map();
|
497 | this.cache = null;
|
498 | this.records = null;
|
499 |
|
500 | this.additionalChunkAssets = [];
|
501 |
|
502 | this.assets = {};
|
503 |
|
504 | this.assetsInfo = new Map();
|
505 |
|
506 | this.errors = [];
|
507 |
|
508 | this.warnings = [];
|
509 |
|
510 | this.children = [];
|
511 |
|
512 | this.logging = new Map();
|
513 |
|
514 | this.dependencyFactories = new Map();
|
515 |
|
516 | this.dependencyTemplates = new Map();
|
517 |
|
518 |
|
519 | this.dependencyTemplates.set("hash", "");
|
520 | this.childrenCounters = {};
|
521 |
|
522 | this.usedChunkIds = null;
|
523 |
|
524 | this.usedModuleIds = null;
|
525 |
|
526 | this.fileTimestamps = undefined;
|
527 |
|
528 | this.contextTimestamps = undefined;
|
529 |
|
530 | this.compilationDependencies = undefined;
|
531 |
|
532 | this._buildingModules = new Map();
|
533 |
|
534 | this._rebuildingModules = new Map();
|
535 |
|
536 | this.emittedAssets = new Set();
|
537 | }
|
538 |
|
539 | getStats() {
|
540 | return new Stats(this);
|
541 | }
|
542 |
|
543 | |
544 |
|
545 |
|
546 |
|
547 | getLogger(name) {
|
548 | if (!name) {
|
549 | throw new TypeError("Compilation.getLogger(name) called without a name");
|
550 | }
|
551 |
|
552 | let logEntries;
|
553 | return new Logger((type, args) => {
|
554 | if (typeof name === "function") {
|
555 | name = name();
|
556 | if (!name) {
|
557 | throw new TypeError(
|
558 | "Compilation.getLogger(name) called with a function not returning a name"
|
559 | );
|
560 | }
|
561 | }
|
562 | let trace;
|
563 | switch (type) {
|
564 | case LogType.warn:
|
565 | case LogType.error:
|
566 | case LogType.trace:
|
567 | trace = ErrorHelpers.cutOffLoaderExecution(new Error("Trace").stack)
|
568 | .split("\n")
|
569 | .slice(3);
|
570 | break;
|
571 | }
|
572 |
|
573 | const logEntry = {
|
574 | time: Date.now(),
|
575 | type,
|
576 | args,
|
577 | trace
|
578 | };
|
579 | if (this.hooks.log.call(name, logEntry) === undefined) {
|
580 | if (logEntry.type === LogType.profileEnd) {
|
581 |
|
582 | if (typeof console.profileEnd === "function") {
|
583 |
|
584 | console.profileEnd(`[${name}] ${logEntry.args[0]}`);
|
585 | }
|
586 | }
|
587 | if (logEntries === undefined) {
|
588 | logEntries = this.logging.get(name);
|
589 | if (logEntries === undefined) {
|
590 | logEntries = [];
|
591 | this.logging.set(name, logEntries);
|
592 | }
|
593 | }
|
594 | logEntries.push(logEntry);
|
595 | if (logEntry.type === LogType.profile) {
|
596 |
|
597 | if (typeof console.profile === "function") {
|
598 |
|
599 | console.profile(`[${name}] ${logEntry.args[0]}`);
|
600 | }
|
601 | }
|
602 | }
|
603 | });
|
604 | }
|
605 |
|
606 | |
607 |
|
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 | |
615 |
|
616 |
|
617 |
|
618 |
|
619 |
|
620 | addModule(module, cacheGroup) {
|
621 | const identifier = module.identifier();
|
622 | const alreadyAddedModule = this._modules.get(identifier);
|
623 | if (alreadyAddedModule) {
|
624 | return {
|
625 | module: alreadyAddedModule,
|
626 | issuer: false,
|
627 | build: false,
|
628 | dependencies: false
|
629 | };
|
630 | }
|
631 | const cacheName = (cacheGroup || "m") + identifier;
|
632 | if (this.cache && this.cache[cacheName]) {
|
633 | const cacheModule = this.cache[cacheName];
|
634 |
|
635 | if (typeof cacheModule.updateCacheModule === "function") {
|
636 | cacheModule.updateCacheModule(module);
|
637 | }
|
638 |
|
639 | let rebuild = true;
|
640 | if (this.fileTimestamps && this.contextTimestamps) {
|
641 | rebuild = cacheModule.needRebuild(
|
642 | this.fileTimestamps,
|
643 | this.contextTimestamps
|
644 | );
|
645 | }
|
646 |
|
647 | if (!rebuild) {
|
648 | cacheModule.disconnect();
|
649 | this._modules.set(identifier, cacheModule);
|
650 | this.modules.push(cacheModule);
|
651 | for (const err of cacheModule.errors) {
|
652 | this.errors.push(err);
|
653 | }
|
654 | for (const err of cacheModule.warnings) {
|
655 | this.warnings.push(err);
|
656 | }
|
657 | return {
|
658 | module: cacheModule,
|
659 | issuer: true,
|
660 | build: false,
|
661 | dependencies: true
|
662 | };
|
663 | }
|
664 | cacheModule.unbuild();
|
665 | module = cacheModule;
|
666 | }
|
667 | this._modules.set(identifier, module);
|
668 | if (this.cache) {
|
669 | this.cache[cacheName] = module;
|
670 | }
|
671 | this.modules.push(module);
|
672 | return {
|
673 | module: module,
|
674 | issuer: true,
|
675 | build: true,
|
676 | dependencies: true
|
677 | };
|
678 | }
|
679 |
|
680 | |
681 |
|
682 |
|
683 |
|
684 |
|
685 | getModule(module) {
|
686 | const identifier = module.identifier();
|
687 | return this._modules.get(identifier);
|
688 | }
|
689 |
|
690 | |
691 |
|
692 |
|
693 |
|
694 |
|
695 | findModule(identifier) {
|
696 | return this._modules.get(identifier);
|
697 | }
|
698 |
|
699 | |
700 |
|
701 |
|
702 |
|
703 |
|
704 | waitForBuildingFinished(module, callback) {
|
705 | let callbackList = this._buildingModules.get(module);
|
706 | if (callbackList) {
|
707 | callbackList.push(() => callback());
|
708 | } else {
|
709 | process.nextTick(callback);
|
710 | }
|
711 | }
|
712 |
|
713 | |
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 | buildModule(module, optional, origin, dependencies, thisCallback) {
|
724 | let callbackList = this._buildingModules.get(module);
|
725 | if (callbackList) {
|
726 | callbackList.push(thisCallback);
|
727 | return;
|
728 | }
|
729 | this._buildingModules.set(module, (callbackList = [thisCallback]));
|
730 |
|
731 | const callback = err => {
|
732 | this._buildingModules.delete(module);
|
733 | for (const cb of callbackList) {
|
734 | cb(err);
|
735 | }
|
736 | };
|
737 |
|
738 | this.hooks.buildModule.call(module);
|
739 | module.build(
|
740 | this.options,
|
741 | this,
|
742 | this.resolverFactory.get("normal", module.resolveOptions),
|
743 | this.inputFileSystem,
|
744 | error => {
|
745 | const errors = module.errors;
|
746 | for (let indexError = 0; indexError < errors.length; indexError++) {
|
747 | const err = errors[indexError];
|
748 | err.origin = origin;
|
749 | err.dependencies = dependencies;
|
750 | if (optional) {
|
751 | this.warnings.push(err);
|
752 | } else {
|
753 | this.errors.push(err);
|
754 | }
|
755 | }
|
756 |
|
757 | const warnings = module.warnings;
|
758 | for (
|
759 | let indexWarning = 0;
|
760 | indexWarning < warnings.length;
|
761 | indexWarning++
|
762 | ) {
|
763 | const war = warnings[indexWarning];
|
764 | war.origin = origin;
|
765 | war.dependencies = dependencies;
|
766 | this.warnings.push(war);
|
767 | }
|
768 | const originalMap = module.dependencies.reduce((map, v, i) => {
|
769 | map.set(v, i);
|
770 | return map;
|
771 | }, new Map());
|
772 | module.dependencies.sort((a, b) => {
|
773 | const cmp = compareLocations(a.loc, b.loc);
|
774 | if (cmp) return cmp;
|
775 | return originalMap.get(a) - originalMap.get(b);
|
776 | });
|
777 | if (error) {
|
778 | this.hooks.failedModule.call(module, error);
|
779 | return callback(error);
|
780 | }
|
781 | this.hooks.succeedModule.call(module);
|
782 | return callback();
|
783 | }
|
784 | );
|
785 | }
|
786 |
|
787 | |
788 |
|
789 |
|
790 |
|
791 |
|
792 | processModuleDependencies(module, callback) {
|
793 | const dependencies = new Map();
|
794 |
|
795 | const addDependency = dep => {
|
796 | const resourceIdent = dep.getResourceIdentifier();
|
797 | if (resourceIdent) {
|
798 | const factory = this.dependencyFactories.get(dep.constructor);
|
799 | if (factory === undefined) {
|
800 | throw new Error(
|
801 | `No module factory available for dependency type: ${dep.constructor.name}`
|
802 | );
|
803 | }
|
804 | let innerMap = dependencies.get(factory);
|
805 | if (innerMap === undefined) {
|
806 | dependencies.set(factory, (innerMap = new Map()));
|
807 | }
|
808 | let list = innerMap.get(resourceIdent);
|
809 | if (list === undefined) innerMap.set(resourceIdent, (list = []));
|
810 | list.push(dep);
|
811 | }
|
812 | };
|
813 |
|
814 | const addDependenciesBlock = block => {
|
815 | if (block.dependencies) {
|
816 | iterationOfArrayCallback(block.dependencies, addDependency);
|
817 | }
|
818 | if (block.blocks) {
|
819 | iterationOfArrayCallback(block.blocks, addDependenciesBlock);
|
820 | }
|
821 | if (block.variables) {
|
822 | iterationBlockVariable(block.variables, addDependency);
|
823 | }
|
824 | };
|
825 |
|
826 | try {
|
827 | addDependenciesBlock(module);
|
828 | } catch (e) {
|
829 | callback(e);
|
830 | }
|
831 |
|
832 | const sortedDependencies = [];
|
833 |
|
834 | for (const pair1 of dependencies) {
|
835 | for (const pair2 of pair1[1]) {
|
836 | sortedDependencies.push({
|
837 | factory: pair1[0],
|
838 | dependencies: pair2[1]
|
839 | });
|
840 | }
|
841 | }
|
842 |
|
843 | this.addModuleDependencies(
|
844 | module,
|
845 | sortedDependencies,
|
846 | this.bail,
|
847 | null,
|
848 | true,
|
849 | callback
|
850 | );
|
851 | }
|
852 |
|
853 | |
854 |
|
855 |
|
856 |
|
857 |
|
858 |
|
859 |
|
860 |
|
861 |
|
862 | addModuleDependencies(
|
863 | module,
|
864 | dependencies,
|
865 | bail,
|
866 | cacheGroup,
|
867 | recursive,
|
868 | callback
|
869 | ) {
|
870 | const start = this.profile && Date.now();
|
871 | const currentProfile = this.profile && {};
|
872 |
|
873 | asyncLib.forEach(
|
874 | dependencies,
|
875 | (item, callback) => {
|
876 | const dependencies = item.dependencies;
|
877 |
|
878 | const errorAndCallback = err => {
|
879 | err.origin = module;
|
880 | err.dependencies = dependencies;
|
881 | this.errors.push(err);
|
882 | if (bail) {
|
883 | callback(err);
|
884 | } else {
|
885 | callback();
|
886 | }
|
887 | };
|
888 | const warningAndCallback = err => {
|
889 | err.origin = module;
|
890 | this.warnings.push(err);
|
891 | callback();
|
892 | };
|
893 |
|
894 | const semaphore = this.semaphore;
|
895 | semaphore.acquire(() => {
|
896 | const factory = item.factory;
|
897 | factory.create(
|
898 | {
|
899 | contextInfo: {
|
900 | issuer: module.nameForCondition && module.nameForCondition(),
|
901 | compiler: this.compiler.name
|
902 | },
|
903 | resolveOptions: module.resolveOptions,
|
904 | context: module.context,
|
905 | dependencies: dependencies
|
906 | },
|
907 | (err, dependentModule) => {
|
908 | let afterFactory;
|
909 |
|
910 | const isOptional = () => {
|
911 | return dependencies.every(d => d.optional);
|
912 | };
|
913 |
|
914 | const errorOrWarningAndCallback = err => {
|
915 | if (isOptional()) {
|
916 | return warningAndCallback(err);
|
917 | } else {
|
918 | return errorAndCallback(err);
|
919 | }
|
920 | };
|
921 |
|
922 | if (err) {
|
923 | semaphore.release();
|
924 | return errorOrWarningAndCallback(
|
925 | new ModuleNotFoundError(module, err)
|
926 | );
|
927 | }
|
928 | if (!dependentModule) {
|
929 | semaphore.release();
|
930 | return process.nextTick(callback);
|
931 | }
|
932 | if (currentProfile) {
|
933 | afterFactory = Date.now();
|
934 | currentProfile.factory = afterFactory - start;
|
935 | }
|
936 |
|
937 | const iterationDependencies = depend => {
|
938 | for (let index = 0; index < depend.length; index++) {
|
939 | const dep = depend[index];
|
940 | dep.module = dependentModule;
|
941 | dependentModule.addReason(module, dep);
|
942 | }
|
943 | };
|
944 |
|
945 | const addModuleResult = this.addModule(
|
946 | dependentModule,
|
947 | cacheGroup
|
948 | );
|
949 | dependentModule = addModuleResult.module;
|
950 | iterationDependencies(dependencies);
|
951 |
|
952 | const afterBuild = () => {
|
953 | if (recursive && addModuleResult.dependencies) {
|
954 | this.processModuleDependencies(dependentModule, callback);
|
955 | } else {
|
956 | return callback();
|
957 | }
|
958 | };
|
959 |
|
960 | if (addModuleResult.issuer) {
|
961 | if (currentProfile) {
|
962 | dependentModule.profile = currentProfile;
|
963 | }
|
964 |
|
965 | dependentModule.issuer = module;
|
966 | } else {
|
967 | if (this.profile) {
|
968 | if (module.profile) {
|
969 | const time = Date.now() - start;
|
970 | if (
|
971 | !module.profile.dependencies ||
|
972 | time > module.profile.dependencies
|
973 | ) {
|
974 | module.profile.dependencies = time;
|
975 | }
|
976 | }
|
977 | }
|
978 | }
|
979 |
|
980 | if (addModuleResult.build) {
|
981 | this.buildModule(
|
982 | dependentModule,
|
983 | isOptional(),
|
984 | module,
|
985 | dependencies,
|
986 | err => {
|
987 | if (err) {
|
988 | semaphore.release();
|
989 | return errorOrWarningAndCallback(err);
|
990 | }
|
991 |
|
992 | if (currentProfile) {
|
993 | const afterBuilding = Date.now();
|
994 | currentProfile.building = afterBuilding - afterFactory;
|
995 | }
|
996 |
|
997 | semaphore.release();
|
998 | afterBuild();
|
999 | }
|
1000 | );
|
1001 | } else {
|
1002 | semaphore.release();
|
1003 | this.waitForBuildingFinished(dependentModule, afterBuild);
|
1004 | }
|
1005 | }
|
1006 | );
|
1007 | });
|
1008 | },
|
1009 | err => {
|
1010 |
|
1011 |
|
1012 |
|
1013 |
|
1014 | if (err) {
|
1015 |
|
1016 | err.stack = err.stack;
|
1017 | return callback(err);
|
1018 | }
|
1019 |
|
1020 | return process.nextTick(callback);
|
1021 | }
|
1022 | );
|
1023 | }
|
1024 |
|
1025 | |
1026 |
|
1027 |
|
1028 |
|
1029 |
|
1030 |
|
1031 |
|
1032 |
|
1033 | _addModuleChain(context, dependency, onModule, callback) {
|
1034 | const start = this.profile && Date.now();
|
1035 | const currentProfile = this.profile && {};
|
1036 |
|
1037 | const errorAndCallback = this.bail
|
1038 | ? err => {
|
1039 | callback(err);
|
1040 | }
|
1041 | : err => {
|
1042 | err.dependencies = [dependency];
|
1043 | this.errors.push(err);
|
1044 | callback();
|
1045 | };
|
1046 |
|
1047 | if (
|
1048 | typeof dependency !== "object" ||
|
1049 | dependency === null ||
|
1050 | !dependency.constructor
|
1051 | ) {
|
1052 | throw new Error("Parameter 'dependency' must be a Dependency");
|
1053 | }
|
1054 | const Dep = (dependency.constructor);
|
1055 | const moduleFactory = this.dependencyFactories.get(Dep);
|
1056 | if (!moduleFactory) {
|
1057 | throw new Error(
|
1058 | `No dependency factory available for this dependency type: ${dependency.constructor.name}`
|
1059 | );
|
1060 | }
|
1061 |
|
1062 | this.semaphore.acquire(() => {
|
1063 | moduleFactory.create(
|
1064 | {
|
1065 | contextInfo: {
|
1066 | issuer: "",
|
1067 | compiler: this.compiler.name
|
1068 | },
|
1069 | context: context,
|
1070 | dependencies: [dependency]
|
1071 | },
|
1072 | (err, module) => {
|
1073 | if (err) {
|
1074 | this.semaphore.release();
|
1075 | return errorAndCallback(new EntryModuleNotFoundError(err));
|
1076 | }
|
1077 |
|
1078 | let afterFactory;
|
1079 |
|
1080 | if (currentProfile) {
|
1081 | afterFactory = Date.now();
|
1082 | currentProfile.factory = afterFactory - start;
|
1083 | }
|
1084 |
|
1085 | const addModuleResult = this.addModule(module);
|
1086 | module = addModuleResult.module;
|
1087 |
|
1088 | onModule(module);
|
1089 |
|
1090 | dependency.module = module;
|
1091 | module.addReason(null, dependency);
|
1092 |
|
1093 | const afterBuild = () => {
|
1094 | if (addModuleResult.dependencies) {
|
1095 | this.processModuleDependencies(module, err => {
|
1096 | if (err) return callback(err);
|
1097 | callback(null, module);
|
1098 | });
|
1099 | } else {
|
1100 | return callback(null, module);
|
1101 | }
|
1102 | };
|
1103 |
|
1104 | if (addModuleResult.issuer) {
|
1105 | if (currentProfile) {
|
1106 | module.profile = currentProfile;
|
1107 | }
|
1108 | }
|
1109 |
|
1110 | if (addModuleResult.build) {
|
1111 | this.buildModule(module, false, null, null, err => {
|
1112 | if (err) {
|
1113 | this.semaphore.release();
|
1114 | return errorAndCallback(err);
|
1115 | }
|
1116 |
|
1117 | if (currentProfile) {
|
1118 | const afterBuilding = Date.now();
|
1119 | currentProfile.building = afterBuilding - afterFactory;
|
1120 | }
|
1121 |
|
1122 | this.semaphore.release();
|
1123 | afterBuild();
|
1124 | });
|
1125 | } else {
|
1126 | this.semaphore.release();
|
1127 | this.waitForBuildingFinished(module, afterBuild);
|
1128 | }
|
1129 | }
|
1130 | );
|
1131 | });
|
1132 | }
|
1133 |
|
1134 | |
1135 |
|
1136 |
|
1137 |
|
1138 |
|
1139 |
|
1140 |
|
1141 |
|
1142 | addEntry(context, entry, name, callback) {
|
1143 | this.hooks.addEntry.call(entry, name);
|
1144 |
|
1145 | const slot = {
|
1146 | name: name,
|
1147 |
|
1148 | request: null,
|
1149 | module: null
|
1150 | };
|
1151 |
|
1152 | if (entry instanceof ModuleDependency) {
|
1153 | slot.request = entry.request;
|
1154 | }
|
1155 |
|
1156 |
|
1157 | const idx = this._preparedEntrypoints.findIndex(slot => slot.name === name);
|
1158 | if (idx >= 0) {
|
1159 |
|
1160 | this._preparedEntrypoints[idx] = slot;
|
1161 | } else {
|
1162 | this._preparedEntrypoints.push(slot);
|
1163 | }
|
1164 | this._addModuleChain(
|
1165 | context,
|
1166 | entry,
|
1167 | module => {
|
1168 | this.entries.push(module);
|
1169 | },
|
1170 | (err, module) => {
|
1171 | if (err) {
|
1172 | this.hooks.failedEntry.call(entry, name, err);
|
1173 | return callback(err);
|
1174 | }
|
1175 |
|
1176 | if (module) {
|
1177 | slot.module = module;
|
1178 | } else {
|
1179 | const idx = this._preparedEntrypoints.indexOf(slot);
|
1180 | if (idx >= 0) {
|
1181 | this._preparedEntrypoints.splice(idx, 1);
|
1182 | }
|
1183 | }
|
1184 | this.hooks.succeedEntry.call(entry, name, module);
|
1185 | return callback(null, module);
|
1186 | }
|
1187 | );
|
1188 | }
|
1189 |
|
1190 | |
1191 |
|
1192 |
|
1193 |
|
1194 |
|
1195 |
|
1196 | prefetch(context, dependency, callback) {
|
1197 | this._addModuleChain(
|
1198 | context,
|
1199 | dependency,
|
1200 | module => {
|
1201 | module.prefetched = true;
|
1202 | },
|
1203 | callback
|
1204 | );
|
1205 | }
|
1206 |
|
1207 | |
1208 |
|
1209 |
|
1210 |
|
1211 |
|
1212 | rebuildModule(module, thisCallback) {
|
1213 | let callbackList = this._rebuildingModules.get(module);
|
1214 | if (callbackList) {
|
1215 | callbackList.push(thisCallback);
|
1216 | return;
|
1217 | }
|
1218 | this._rebuildingModules.set(module, (callbackList = [thisCallback]));
|
1219 |
|
1220 | const callback = err => {
|
1221 | this._rebuildingModules.delete(module);
|
1222 | for (const cb of callbackList) {
|
1223 | cb(err);
|
1224 | }
|
1225 | };
|
1226 |
|
1227 | this.hooks.rebuildModule.call(module);
|
1228 | const oldDependencies = module.dependencies.slice();
|
1229 | const oldVariables = module.variables.slice();
|
1230 | const oldBlocks = module.blocks.slice();
|
1231 | module.unbuild();
|
1232 | this.buildModule(module, false, module, null, err => {
|
1233 | if (err) {
|
1234 | this.hooks.finishRebuildingModule.call(module);
|
1235 | return callback(err);
|
1236 | }
|
1237 |
|
1238 | this.processModuleDependencies(module, err => {
|
1239 | if (err) return callback(err);
|
1240 | this.removeReasonsOfDependencyBlock(module, {
|
1241 | dependencies: oldDependencies,
|
1242 | variables: oldVariables,
|
1243 | blocks: oldBlocks
|
1244 | });
|
1245 | this.hooks.finishRebuildingModule.call(module);
|
1246 | callback();
|
1247 | });
|
1248 | });
|
1249 | }
|
1250 |
|
1251 | finish(callback) {
|
1252 | const modules = this.modules;
|
1253 | this.hooks.finishModules.callAsync(modules, err => {
|
1254 | if (err) return callback(err);
|
1255 |
|
1256 | for (let index = 0; index < modules.length; index++) {
|
1257 | const module = modules[index];
|
1258 | this.reportDependencyErrorsAndWarnings(module, [module]);
|
1259 | }
|
1260 |
|
1261 | callback();
|
1262 | });
|
1263 | }
|
1264 |
|
1265 | unseal() {
|
1266 | this.hooks.unseal.call();
|
1267 | this.chunks.length = 0;
|
1268 | this.chunkGroups.length = 0;
|
1269 | this.namedChunks.clear();
|
1270 | this.namedChunkGroups.clear();
|
1271 | this.additionalChunkAssets.length = 0;
|
1272 | this.assets = {};
|
1273 | this.assetsInfo.clear();
|
1274 | for (const module of this.modules) {
|
1275 | module.unseal();
|
1276 | }
|
1277 | }
|
1278 |
|
1279 | |
1280 |
|
1281 |
|
1282 |
|
1283 | seal(callback) {
|
1284 | this.hooks.seal.call();
|
1285 |
|
1286 | while (
|
1287 | this.hooks.optimizeDependenciesBasic.call(this.modules) ||
|
1288 | this.hooks.optimizeDependencies.call(this.modules) ||
|
1289 | this.hooks.optimizeDependenciesAdvanced.call(this.modules)
|
1290 | ) {
|
1291 |
|
1292 | }
|
1293 | this.hooks.afterOptimizeDependencies.call(this.modules);
|
1294 |
|
1295 | this.hooks.beforeChunks.call();
|
1296 | for (const preparedEntrypoint of this._preparedEntrypoints) {
|
1297 | const module = preparedEntrypoint.module;
|
1298 | const name = preparedEntrypoint.name;
|
1299 | const chunk = this.addChunk(name);
|
1300 | const entrypoint = new Entrypoint(name);
|
1301 | entrypoint.setRuntimeChunk(chunk);
|
1302 | entrypoint.addOrigin(null, name, preparedEntrypoint.request);
|
1303 | this.namedChunkGroups.set(name, entrypoint);
|
1304 | this.entrypoints.set(name, entrypoint);
|
1305 | this.chunkGroups.push(entrypoint);
|
1306 |
|
1307 | GraphHelpers.connectChunkGroupAndChunk(entrypoint, chunk);
|
1308 | GraphHelpers.connectChunkAndModule(chunk, module);
|
1309 |
|
1310 | chunk.entryModule = module;
|
1311 | chunk.name = name;
|
1312 |
|
1313 | this.assignDepth(module);
|
1314 | }
|
1315 | buildChunkGraph(
|
1316 | this,
|
1317 | (this.chunkGroups.slice())
|
1318 | );
|
1319 | this.sortModules(this.modules);
|
1320 | this.hooks.afterChunks.call(this.chunks);
|
1321 |
|
1322 | this.hooks.optimize.call();
|
1323 |
|
1324 | while (
|
1325 | this.hooks.optimizeModulesBasic.call(this.modules) ||
|
1326 | this.hooks.optimizeModules.call(this.modules) ||
|
1327 | this.hooks.optimizeModulesAdvanced.call(this.modules)
|
1328 | ) {
|
1329 |
|
1330 | }
|
1331 | this.hooks.afterOptimizeModules.call(this.modules);
|
1332 |
|
1333 | while (
|
1334 | this.hooks.optimizeChunksBasic.call(this.chunks, this.chunkGroups) ||
|
1335 | this.hooks.optimizeChunks.call(this.chunks, this.chunkGroups) ||
|
1336 | this.hooks.optimizeChunksAdvanced.call(this.chunks, this.chunkGroups)
|
1337 | ) {
|
1338 |
|
1339 | }
|
1340 | this.hooks.afterOptimizeChunks.call(this.chunks, this.chunkGroups);
|
1341 |
|
1342 | this.hooks.optimizeTree.callAsync(this.chunks, this.modules, err => {
|
1343 | if (err) {
|
1344 | return callback(err);
|
1345 | }
|
1346 |
|
1347 | this.hooks.afterOptimizeTree.call(this.chunks, this.modules);
|
1348 |
|
1349 | while (
|
1350 | this.hooks.optimizeChunkModulesBasic.call(this.chunks, this.modules) ||
|
1351 | this.hooks.optimizeChunkModules.call(this.chunks, this.modules) ||
|
1352 | this.hooks.optimizeChunkModulesAdvanced.call(this.chunks, this.modules)
|
1353 | ) {
|
1354 |
|
1355 | }
|
1356 | this.hooks.afterOptimizeChunkModules.call(this.chunks, this.modules);
|
1357 |
|
1358 | const shouldRecord = this.hooks.shouldRecord.call() !== false;
|
1359 |
|
1360 | this.hooks.reviveModules.call(this.modules, this.records);
|
1361 | this.hooks.optimizeModuleOrder.call(this.modules);
|
1362 | this.hooks.advancedOptimizeModuleOrder.call(this.modules);
|
1363 | this.hooks.beforeModuleIds.call(this.modules);
|
1364 | this.hooks.moduleIds.call(this.modules);
|
1365 | this.applyModuleIds();
|
1366 | this.hooks.optimizeModuleIds.call(this.modules);
|
1367 | this.hooks.afterOptimizeModuleIds.call(this.modules);
|
1368 |
|
1369 | this.sortItemsWithModuleIds();
|
1370 |
|
1371 | this.hooks.reviveChunks.call(this.chunks, this.records);
|
1372 | this.hooks.optimizeChunkOrder.call(this.chunks);
|
1373 | this.hooks.beforeChunkIds.call(this.chunks);
|
1374 | this.applyChunkIds();
|
1375 | this.hooks.optimizeChunkIds.call(this.chunks);
|
1376 | this.hooks.afterOptimizeChunkIds.call(this.chunks);
|
1377 |
|
1378 | this.sortItemsWithChunkIds();
|
1379 |
|
1380 | if (shouldRecord) {
|
1381 | this.hooks.recordModules.call(this.modules, this.records);
|
1382 | this.hooks.recordChunks.call(this.chunks, this.records);
|
1383 | }
|
1384 |
|
1385 | this.hooks.beforeHash.call();
|
1386 | this.createHash();
|
1387 | this.hooks.afterHash.call();
|
1388 |
|
1389 | if (shouldRecord) {
|
1390 | this.hooks.recordHash.call(this.records);
|
1391 | }
|
1392 |
|
1393 | this.hooks.beforeModuleAssets.call();
|
1394 | this.createModuleAssets();
|
1395 | if (this.hooks.shouldGenerateChunkAssets.call() !== false) {
|
1396 | this.hooks.beforeChunkAssets.call();
|
1397 | this.createChunkAssets();
|
1398 | }
|
1399 | this.hooks.additionalChunkAssets.call(this.chunks);
|
1400 | this.summarizeDependencies();
|
1401 | if (shouldRecord) {
|
1402 | this.hooks.record.call(this, this.records);
|
1403 | }
|
1404 |
|
1405 | this.hooks.additionalAssets.callAsync(err => {
|
1406 | if (err) {
|
1407 | return callback(err);
|
1408 | }
|
1409 | this.hooks.optimizeChunkAssets.callAsync(this.chunks, err => {
|
1410 | if (err) {
|
1411 | return callback(err);
|
1412 | }
|
1413 | this.hooks.afterOptimizeChunkAssets.call(this.chunks);
|
1414 | this.hooks.optimizeAssets.callAsync(this.assets, err => {
|
1415 | if (err) {
|
1416 | return callback(err);
|
1417 | }
|
1418 | this.hooks.afterOptimizeAssets.call(this.assets);
|
1419 | if (this.hooks.needAdditionalSeal.call()) {
|
1420 | this.unseal();
|
1421 | return this.seal(callback);
|
1422 | }
|
1423 | return this.hooks.afterSeal.callAsync(callback);
|
1424 | });
|
1425 | });
|
1426 | });
|
1427 | });
|
1428 | }
|
1429 |
|
1430 | |
1431 |
|
1432 |
|
1433 |
|
1434 | sortModules(modules) {
|
1435 |
|
1436 |
|
1437 |
|
1438 | modules.sort(byIndexOrIdentifier);
|
1439 | }
|
1440 |
|
1441 | |
1442 |
|
1443 |
|
1444 |
|
1445 |
|
1446 | reportDependencyErrorsAndWarnings(module, blocks) {
|
1447 | for (let indexBlock = 0; indexBlock < blocks.length; indexBlock++) {
|
1448 | const block = blocks[indexBlock];
|
1449 | const dependencies = block.dependencies;
|
1450 |
|
1451 | for (let indexDep = 0; indexDep < dependencies.length; indexDep++) {
|
1452 | const d = dependencies[indexDep];
|
1453 |
|
1454 | const warnings = d.getWarnings();
|
1455 | if (warnings) {
|
1456 | for (let indexWar = 0; indexWar < warnings.length; indexWar++) {
|
1457 | const w = warnings[indexWar];
|
1458 |
|
1459 | const warning = new ModuleDependencyWarning(module, w, d.loc);
|
1460 | this.warnings.push(warning);
|
1461 | }
|
1462 | }
|
1463 | const errors = d.getErrors();
|
1464 | if (errors) {
|
1465 | for (let indexErr = 0; indexErr < errors.length; indexErr++) {
|
1466 | const e = errors[indexErr];
|
1467 |
|
1468 | const error = new ModuleDependencyError(module, e, d.loc);
|
1469 | this.errors.push(error);
|
1470 | }
|
1471 | }
|
1472 | }
|
1473 |
|
1474 | this.reportDependencyErrorsAndWarnings(module, block.blocks);
|
1475 | }
|
1476 | }
|
1477 |
|
1478 | |
1479 |
|
1480 |
|
1481 |
|
1482 |
|
1483 |
|
1484 |
|
1485 | addChunkInGroup(groupOptions, module, loc, request) {
|
1486 | if (typeof groupOptions === "string") {
|
1487 | groupOptions = { name: groupOptions };
|
1488 | }
|
1489 | const name = groupOptions.name;
|
1490 | if (name) {
|
1491 | const chunkGroup = this.namedChunkGroups.get(name);
|
1492 | if (chunkGroup !== undefined) {
|
1493 | chunkGroup.addOptions(groupOptions);
|
1494 | if (module) {
|
1495 | chunkGroup.addOrigin(module, loc, request);
|
1496 | }
|
1497 | return chunkGroup;
|
1498 | }
|
1499 | }
|
1500 | const chunkGroup = new ChunkGroup(groupOptions);
|
1501 | if (module) chunkGroup.addOrigin(module, loc, request);
|
1502 | const chunk = this.addChunk(name);
|
1503 |
|
1504 | GraphHelpers.connectChunkGroupAndChunk(chunkGroup, chunk);
|
1505 |
|
1506 | this.chunkGroups.push(chunkGroup);
|
1507 | if (name) {
|
1508 | this.namedChunkGroups.set(name, chunkGroup);
|
1509 | }
|
1510 | return chunkGroup;
|
1511 | }
|
1512 |
|
1513 | |
1514 |
|
1515 |
|
1516 |
|
1517 |
|
1518 |
|
1519 |
|
1520 | addChunk(name) {
|
1521 | if (name) {
|
1522 | const chunk = this.namedChunks.get(name);
|
1523 | if (chunk !== undefined) {
|
1524 | return chunk;
|
1525 | }
|
1526 | }
|
1527 | const chunk = new Chunk(name);
|
1528 | this.chunks.push(chunk);
|
1529 | if (name) {
|
1530 | this.namedChunks.set(name, chunk);
|
1531 | }
|
1532 | return chunk;
|
1533 | }
|
1534 |
|
1535 | |
1536 |
|
1537 |
|
1538 |
|
1539 | assignDepth(module) {
|
1540 | const queue = new Set([module]);
|
1541 | let depth;
|
1542 |
|
1543 | module.depth = 0;
|
1544 |
|
1545 | |
1546 |
|
1547 |
|
1548 |
|
1549 | const enqueueJob = module => {
|
1550 | const d = module.depth;
|
1551 | if (typeof d === "number" && d <= depth) return;
|
1552 | queue.add(module);
|
1553 | module.depth = depth;
|
1554 | };
|
1555 |
|
1556 | |
1557 |
|
1558 |
|
1559 |
|
1560 | const assignDepthToDependency = dependency => {
|
1561 | if (dependency.module) {
|
1562 | enqueueJob(dependency.module);
|
1563 | }
|
1564 | };
|
1565 |
|
1566 | |
1567 |
|
1568 |
|
1569 |
|
1570 | const assignDepthToDependencyBlock = block => {
|
1571 | if (block.variables) {
|
1572 | iterationBlockVariable(block.variables, assignDepthToDependency);
|
1573 | }
|
1574 |
|
1575 | if (block.dependencies) {
|
1576 | iterationOfArrayCallback(block.dependencies, assignDepthToDependency);
|
1577 | }
|
1578 |
|
1579 | if (block.blocks) {
|
1580 | iterationOfArrayCallback(block.blocks, assignDepthToDependencyBlock);
|
1581 | }
|
1582 | };
|
1583 |
|
1584 | for (module of queue) {
|
1585 | queue.delete(module);
|
1586 | depth = module.depth;
|
1587 |
|
1588 | depth++;
|
1589 | assignDepthToDependencyBlock(module);
|
1590 | }
|
1591 | }
|
1592 |
|
1593 | |
1594 |
|
1595 |
|
1596 |
|
1597 |
|
1598 | getDependencyReference(module, dependency) {
|
1599 |
|
1600 | if (typeof dependency.getReference !== "function") return null;
|
1601 | const ref = dependency.getReference();
|
1602 | if (!ref) return null;
|
1603 | return this.hooks.dependencyReference.call(ref, dependency, module);
|
1604 | }
|
1605 |
|
1606 | |
1607 |
|
1608 |
|
1609 |
|
1610 |
|
1611 |
|
1612 | removeReasonsOfDependencyBlock(module, block) {
|
1613 | const iteratorDependency = d => {
|
1614 | if (!d.module) {
|
1615 | return;
|
1616 | }
|
1617 | if (d.module.removeReason(module, d)) {
|
1618 | for (const chunk of d.module.chunksIterable) {
|
1619 | this.patchChunksAfterReasonRemoval(d.module, chunk);
|
1620 | }
|
1621 | }
|
1622 | };
|
1623 |
|
1624 | if (block.blocks) {
|
1625 | iterationOfArrayCallback(block.blocks, block =>
|
1626 | this.removeReasonsOfDependencyBlock(module, block)
|
1627 | );
|
1628 | }
|
1629 |
|
1630 | if (block.dependencies) {
|
1631 | iterationOfArrayCallback(block.dependencies, iteratorDependency);
|
1632 | }
|
1633 |
|
1634 | if (block.variables) {
|
1635 | iterationBlockVariable(block.variables, iteratorDependency);
|
1636 | }
|
1637 | }
|
1638 |
|
1639 | |
1640 |
|
1641 |
|
1642 |
|
1643 |
|
1644 | patchChunksAfterReasonRemoval(module, chunk) {
|
1645 | if (!module.hasReasons()) {
|
1646 | this.removeReasonsOfDependencyBlock(module, module);
|
1647 | }
|
1648 | if (!module.hasReasonForChunk(chunk)) {
|
1649 | if (module.removeChunk(chunk)) {
|
1650 | this.removeChunkFromDependencies(module, chunk);
|
1651 | }
|
1652 | }
|
1653 | }
|
1654 |
|
1655 | |
1656 |
|
1657 |
|
1658 |
|
1659 |
|
1660 |
|
1661 | removeChunkFromDependencies(block, chunk) {
|
1662 | const iteratorDependency = d => {
|
1663 | if (!d.module) {
|
1664 | return;
|
1665 | }
|
1666 | this.patchChunksAfterReasonRemoval(d.module, chunk);
|
1667 | };
|
1668 |
|
1669 | const blocks = block.blocks;
|
1670 | for (let indexBlock = 0; indexBlock < blocks.length; indexBlock++) {
|
1671 | const asyncBlock = blocks[indexBlock];
|
1672 |
|
1673 | const chunks = asyncBlock.chunkGroup.chunks;
|
1674 |
|
1675 | for (let indexChunk = 0; indexChunk < chunks.length; indexChunk++) {
|
1676 | const iteratedChunk = chunks[indexChunk];
|
1677 | asyncBlock.chunkGroup.removeChunk(iteratedChunk);
|
1678 | asyncBlock.chunkGroup.removeParent(iteratedChunk);
|
1679 |
|
1680 | this.removeChunkFromDependencies(block, iteratedChunk);
|
1681 | }
|
1682 | }
|
1683 |
|
1684 | if (block.dependencies) {
|
1685 | iterationOfArrayCallback(block.dependencies, iteratorDependency);
|
1686 | }
|
1687 |
|
1688 | if (block.variables) {
|
1689 | iterationBlockVariable(block.variables, iteratorDependency);
|
1690 | }
|
1691 | }
|
1692 |
|
1693 | applyModuleIds() {
|
1694 | const unusedIds = [];
|
1695 | let nextFreeModuleId = 0;
|
1696 | const usedIds = new Set();
|
1697 | if (this.usedModuleIds) {
|
1698 | for (const id of this.usedModuleIds) {
|
1699 | usedIds.add(id);
|
1700 | }
|
1701 | }
|
1702 |
|
1703 | const modules1 = this.modules;
|
1704 | for (let indexModule1 = 0; indexModule1 < modules1.length; indexModule1++) {
|
1705 | const module1 = modules1[indexModule1];
|
1706 | if (module1.id !== null) {
|
1707 | usedIds.add(module1.id);
|
1708 | }
|
1709 | }
|
1710 |
|
1711 | if (usedIds.size > 0) {
|
1712 | let usedIdMax = -1;
|
1713 | for (const usedIdKey of usedIds) {
|
1714 | if (typeof usedIdKey !== "number") {
|
1715 | continue;
|
1716 | }
|
1717 |
|
1718 | usedIdMax = Math.max(usedIdMax, usedIdKey);
|
1719 | }
|
1720 |
|
1721 | let lengthFreeModules = (nextFreeModuleId = usedIdMax + 1);
|
1722 |
|
1723 | while (lengthFreeModules--) {
|
1724 | if (!usedIds.has(lengthFreeModules)) {
|
1725 | unusedIds.push(lengthFreeModules);
|
1726 | }
|
1727 | }
|
1728 | }
|
1729 |
|
1730 | const modules2 = this.modules;
|
1731 | for (let indexModule2 = 0; indexModule2 < modules2.length; indexModule2++) {
|
1732 | const module2 = modules2[indexModule2];
|
1733 | if (module2.id === null) {
|
1734 | if (unusedIds.length > 0) {
|
1735 | module2.id = unusedIds.pop();
|
1736 | } else {
|
1737 | module2.id = nextFreeModuleId++;
|
1738 | }
|
1739 | }
|
1740 | }
|
1741 | }
|
1742 |
|
1743 | applyChunkIds() {
|
1744 |
|
1745 | const usedIds = new Set();
|
1746 |
|
1747 |
|
1748 | if (this.usedChunkIds) {
|
1749 | for (const id of this.usedChunkIds) {
|
1750 | if (typeof id !== "number") {
|
1751 | continue;
|
1752 | }
|
1753 |
|
1754 | usedIds.add(id);
|
1755 | }
|
1756 | }
|
1757 |
|
1758 |
|
1759 | const chunks = this.chunks;
|
1760 | for (let indexChunk = 0; indexChunk < chunks.length; indexChunk++) {
|
1761 | const chunk = chunks[indexChunk];
|
1762 | const usedIdValue = chunk.id;
|
1763 |
|
1764 | if (typeof usedIdValue !== "number") {
|
1765 | continue;
|
1766 | }
|
1767 |
|
1768 | usedIds.add(usedIdValue);
|
1769 | }
|
1770 |
|
1771 |
|
1772 | let nextFreeChunkId = -1;
|
1773 | for (const id of usedIds) {
|
1774 | nextFreeChunkId = Math.max(nextFreeChunkId, id);
|
1775 | }
|
1776 | nextFreeChunkId++;
|
1777 |
|
1778 |
|
1779 |
|
1780 | const unusedIds = [];
|
1781 | if (nextFreeChunkId > 0) {
|
1782 | let index = nextFreeChunkId;
|
1783 | while (index--) {
|
1784 | if (!usedIds.has(index)) {
|
1785 | unusedIds.push(index);
|
1786 | }
|
1787 | }
|
1788 | }
|
1789 |
|
1790 |
|
1791 | for (let indexChunk = 0; indexChunk < chunks.length; indexChunk++) {
|
1792 | const chunk = chunks[indexChunk];
|
1793 | if (chunk.id === null) {
|
1794 | if (unusedIds.length > 0) {
|
1795 | chunk.id = unusedIds.pop();
|
1796 | } else {
|
1797 | chunk.id = nextFreeChunkId++;
|
1798 | }
|
1799 | }
|
1800 | if (!chunk.ids) {
|
1801 | chunk.ids = [chunk.id];
|
1802 | }
|
1803 | }
|
1804 | }
|
1805 |
|
1806 | sortItemsWithModuleIds() {
|
1807 | this.modules.sort(byIdOrIdentifier);
|
1808 |
|
1809 | const modules = this.modules;
|
1810 | for (let indexModule = 0; indexModule < modules.length; indexModule++) {
|
1811 | modules[indexModule].sortItems(false);
|
1812 | }
|
1813 |
|
1814 | const chunks = this.chunks;
|
1815 | for (let indexChunk = 0; indexChunk < chunks.length; indexChunk++) {
|
1816 | chunks[indexChunk].sortItems();
|
1817 | }
|
1818 |
|
1819 | chunks.sort((a, b) => a.compareTo(b));
|
1820 | }
|
1821 |
|
1822 | sortItemsWithChunkIds() {
|
1823 | for (const chunkGroup of this.chunkGroups) {
|
1824 | chunkGroup.sortItems();
|
1825 | }
|
1826 |
|
1827 | this.chunks.sort(byId);
|
1828 |
|
1829 | for (
|
1830 | let indexModule = 0;
|
1831 | indexModule < this.modules.length;
|
1832 | indexModule++
|
1833 | ) {
|
1834 | this.modules[indexModule].sortItems(true);
|
1835 | }
|
1836 |
|
1837 | const chunks = this.chunks;
|
1838 | for (let indexChunk = 0; indexChunk < chunks.length; indexChunk++) {
|
1839 | chunks[indexChunk].sortItems();
|
1840 | }
|
1841 |
|
1842 | |
1843 |
|
1844 |
|
1845 |
|
1846 |
|
1847 |
|
1848 |
|
1849 |
|
1850 |
|
1851 |
|
1852 | const byMessage = (a, b) => {
|
1853 | const ma = `${a.message}`;
|
1854 | const mb = `${b.message}`;
|
1855 | if (ma < mb) return -1;
|
1856 | if (mb < ma) return 1;
|
1857 | return 0;
|
1858 | };
|
1859 |
|
1860 | this.errors.sort(byMessage);
|
1861 | this.warnings.sort(byMessage);
|
1862 | this.children.sort(byNameOrHash);
|
1863 | }
|
1864 |
|
1865 | summarizeDependencies() {
|
1866 | this.fileDependencies = new SortableSet(this.compilationDependencies);
|
1867 | this.contextDependencies = new SortableSet();
|
1868 | this.missingDependencies = new SortableSet();
|
1869 |
|
1870 | for (
|
1871 | let indexChildren = 0;
|
1872 | indexChildren < this.children.length;
|
1873 | indexChildren++
|
1874 | ) {
|
1875 | const child = this.children[indexChildren];
|
1876 |
|
1877 | addAllToSet(this.fileDependencies, child.fileDependencies);
|
1878 | addAllToSet(this.contextDependencies, child.contextDependencies);
|
1879 | addAllToSet(this.missingDependencies, child.missingDependencies);
|
1880 | }
|
1881 |
|
1882 | for (
|
1883 | let indexModule = 0;
|
1884 | indexModule < this.modules.length;
|
1885 | indexModule++
|
1886 | ) {
|
1887 | const module = this.modules[indexModule];
|
1888 |
|
1889 | if (module.buildInfo.fileDependencies) {
|
1890 | addAllToSet(this.fileDependencies, module.buildInfo.fileDependencies);
|
1891 | }
|
1892 | if (module.buildInfo.contextDependencies) {
|
1893 | addAllToSet(
|
1894 | this.contextDependencies,
|
1895 | module.buildInfo.contextDependencies
|
1896 | );
|
1897 | }
|
1898 | }
|
1899 | for (const error of this.errors) {
|
1900 | if (
|
1901 | typeof error.missing === "object" &&
|
1902 | error.missing &&
|
1903 | error.missing[Symbol.iterator]
|
1904 | ) {
|
1905 | addAllToSet(this.missingDependencies, error.missing);
|
1906 | }
|
1907 | }
|
1908 | this.fileDependencies.sort();
|
1909 | this.contextDependencies.sort();
|
1910 | this.missingDependencies.sort();
|
1911 | }
|
1912 |
|
1913 | createHash() {
|
1914 | const outputOptions = this.outputOptions;
|
1915 | const hashFunction = outputOptions.hashFunction;
|
1916 | const hashDigest = outputOptions.hashDigest;
|
1917 | const hashDigestLength = outputOptions.hashDigestLength;
|
1918 | const hash = createHash(hashFunction);
|
1919 | if (outputOptions.hashSalt) {
|
1920 | hash.update(outputOptions.hashSalt);
|
1921 | }
|
1922 | this.mainTemplate.updateHash(hash);
|
1923 | this.chunkTemplate.updateHash(hash);
|
1924 | for (const key of Object.keys(this.moduleTemplates).sort()) {
|
1925 | this.moduleTemplates[key].updateHash(hash);
|
1926 | }
|
1927 | for (const child of this.children) {
|
1928 | hash.update(child.hash);
|
1929 | }
|
1930 | for (const warning of this.warnings) {
|
1931 | hash.update(`${warning.message}`);
|
1932 | }
|
1933 | for (const error of this.errors) {
|
1934 | hash.update(`${error.message}`);
|
1935 | }
|
1936 | const modules = this.modules;
|
1937 | for (let i = 0; i < modules.length; i++) {
|
1938 | const module = modules[i];
|
1939 | const moduleHash = createHash(hashFunction);
|
1940 | module.updateHash(moduleHash);
|
1941 | module.hash = (moduleHash.digest(hashDigest));
|
1942 | module.renderedHash = module.hash.substr(0, hashDigestLength);
|
1943 | }
|
1944 |
|
1945 | const chunks = this.chunks.slice();
|
1946 | |
1947 |
|
1948 |
|
1949 |
|
1950 |
|
1951 | chunks.sort((a, b) => {
|
1952 | const aEntry = a.hasRuntime();
|
1953 | const bEntry = b.hasRuntime();
|
1954 | if (aEntry && !bEntry) return 1;
|
1955 | if (!aEntry && bEntry) return -1;
|
1956 | return byId(a, b);
|
1957 | });
|
1958 | for (let i = 0; i < chunks.length; i++) {
|
1959 | const chunk = chunks[i];
|
1960 | const chunkHash = createHash(hashFunction);
|
1961 | try {
|
1962 | if (outputOptions.hashSalt) {
|
1963 | chunkHash.update(outputOptions.hashSalt);
|
1964 | }
|
1965 | chunk.updateHash(chunkHash);
|
1966 | const template = chunk.hasRuntime()
|
1967 | ? this.mainTemplate
|
1968 | : this.chunkTemplate;
|
1969 | template.updateHashForChunk(
|
1970 | chunkHash,
|
1971 | chunk,
|
1972 | this.moduleTemplates.javascript,
|
1973 | this.dependencyTemplates
|
1974 | );
|
1975 | this.hooks.chunkHash.call(chunk, chunkHash);
|
1976 | chunk.hash = (chunkHash.digest(hashDigest));
|
1977 | hash.update(chunk.hash);
|
1978 | chunk.renderedHash = chunk.hash.substr(0, hashDigestLength);
|
1979 | this.hooks.contentHash.call(chunk);
|
1980 | } catch (err) {
|
1981 | this.errors.push(new ChunkRenderError(chunk, "", err));
|
1982 | }
|
1983 | }
|
1984 | this.fullHash = (hash.digest(hashDigest));
|
1985 | this.hash = this.fullHash.substr(0, hashDigestLength);
|
1986 | }
|
1987 |
|
1988 | |
1989 |
|
1990 |
|
1991 |
|
1992 | modifyHash(update) {
|
1993 | const outputOptions = this.outputOptions;
|
1994 | const hashFunction = outputOptions.hashFunction;
|
1995 | const hashDigest = outputOptions.hashDigest;
|
1996 | const hashDigestLength = outputOptions.hashDigestLength;
|
1997 | const hash = createHash(hashFunction);
|
1998 | hash.update(this.fullHash);
|
1999 | hash.update(update);
|
2000 | this.fullHash = (hash.digest(hashDigest));
|
2001 | this.hash = this.fullHash.substr(0, hashDigestLength);
|
2002 | }
|
2003 |
|
2004 | |
2005 |
|
2006 |
|
2007 |
|
2008 |
|
2009 |
|
2010 | emitAsset(file, source, assetInfo = {}) {
|
2011 | if (this.assets[file]) {
|
2012 | if (!isSourceEqual(this.assets[file], source)) {
|
2013 |
|
2014 | this.warnings.push(
|
2015 | new WebpackError(
|
2016 | `Conflict: Multiple assets emit different content to the same filename ${file}`
|
2017 | )
|
2018 | );
|
2019 | this.assets[file] = source;
|
2020 | this.assetsInfo.set(file, assetInfo);
|
2021 | return;
|
2022 | }
|
2023 | const oldInfo = this.assetsInfo.get(file);
|
2024 | this.assetsInfo.set(file, Object.assign({}, oldInfo, assetInfo));
|
2025 | return;
|
2026 | }
|
2027 | this.assets[file] = source;
|
2028 | this.assetsInfo.set(file, assetInfo);
|
2029 | }
|
2030 |
|
2031 | |
2032 |
|
2033 |
|
2034 |
|
2035 |
|
2036 | updateAsset(
|
2037 | file,
|
2038 | newSourceOrFunction,
|
2039 | assetInfoUpdateOrFunction = undefined
|
2040 | ) {
|
2041 | if (!this.assets[file]) {
|
2042 | throw new Error(
|
2043 | `Called Compilation.updateAsset for not existing filename ${file}`
|
2044 | );
|
2045 | }
|
2046 | if (typeof newSourceOrFunction === "function") {
|
2047 | this.assets[file] = newSourceOrFunction(this.assets[file]);
|
2048 | } else {
|
2049 | this.assets[file] = newSourceOrFunction;
|
2050 | }
|
2051 | if (assetInfoUpdateOrFunction !== undefined) {
|
2052 | const oldInfo = this.assetsInfo.get(file);
|
2053 | if (typeof assetInfoUpdateOrFunction === "function") {
|
2054 | this.assetsInfo.set(file, assetInfoUpdateOrFunction(oldInfo || {}));
|
2055 | } else {
|
2056 | this.assetsInfo.set(
|
2057 | file,
|
2058 | Object.assign({}, oldInfo, assetInfoUpdateOrFunction)
|
2059 | );
|
2060 | }
|
2061 | }
|
2062 | }
|
2063 |
|
2064 | getAssets() {
|
2065 |
|
2066 | const array = [];
|
2067 | for (const assetName of Object.keys(this.assets)) {
|
2068 | if (Object.prototype.hasOwnProperty.call(this.assets, assetName)) {
|
2069 | array.push({
|
2070 | name: assetName,
|
2071 | source: this.assets[assetName],
|
2072 | info: this.assetsInfo.get(assetName) || {}
|
2073 | });
|
2074 | }
|
2075 | }
|
2076 | return array;
|
2077 | }
|
2078 |
|
2079 | |
2080 |
|
2081 |
|
2082 |
|
2083 | getAsset(name) {
|
2084 | if (!Object.prototype.hasOwnProperty.call(this.assets, name))
|
2085 | return undefined;
|
2086 | return {
|
2087 | name,
|
2088 | source: this.assets[name],
|
2089 | info: this.assetsInfo.get(name) || {}
|
2090 | };
|
2091 | }
|
2092 |
|
2093 | createModuleAssets() {
|
2094 | for (let i = 0; i < this.modules.length; i++) {
|
2095 | const module = this.modules[i];
|
2096 | if (module.buildInfo.assets) {
|
2097 | const assetsInfo = module.buildInfo.assetsInfo;
|
2098 | for (const assetName of Object.keys(module.buildInfo.assets)) {
|
2099 | const fileName = this.getPath(assetName);
|
2100 | this.emitAsset(
|
2101 | fileName,
|
2102 | module.buildInfo.assets[assetName],
|
2103 | assetsInfo ? assetsInfo.get(assetName) : undefined
|
2104 | );
|
2105 | this.hooks.moduleAsset.call(module, fileName);
|
2106 | }
|
2107 | }
|
2108 | }
|
2109 | }
|
2110 |
|
2111 | createChunkAssets() {
|
2112 | const outputOptions = this.outputOptions;
|
2113 | const cachedSourceMap = new Map();
|
2114 |
|
2115 | const alreadyWrittenFiles = new Map();
|
2116 | for (let i = 0; i < this.chunks.length; i++) {
|
2117 | const chunk = this.chunks[i];
|
2118 | chunk.files = [];
|
2119 | let source;
|
2120 | let file;
|
2121 | let filenameTemplate;
|
2122 | try {
|
2123 | const template = chunk.hasRuntime()
|
2124 | ? this.mainTemplate
|
2125 | : this.chunkTemplate;
|
2126 | const manifest = template.getRenderManifest({
|
2127 | chunk,
|
2128 | hash: this.hash,
|
2129 | fullHash: this.fullHash,
|
2130 | outputOptions,
|
2131 | moduleTemplates: this.moduleTemplates,
|
2132 | dependencyTemplates: this.dependencyTemplates
|
2133 | });
|
2134 | for (const fileManifest of manifest) {
|
2135 | const cacheName = fileManifest.identifier;
|
2136 | const usedHash = fileManifest.hash;
|
2137 | filenameTemplate = fileManifest.filenameTemplate;
|
2138 | const pathAndInfo = this.getPathWithInfo(
|
2139 | filenameTemplate,
|
2140 | fileManifest.pathOptions
|
2141 | );
|
2142 | file = pathAndInfo.path;
|
2143 | const assetInfo = pathAndInfo.info;
|
2144 |
|
2145 |
|
2146 | const alreadyWritten = alreadyWrittenFiles.get(file);
|
2147 | if (alreadyWritten !== undefined) {
|
2148 | if (alreadyWritten.hash === usedHash) {
|
2149 | if (this.cache) {
|
2150 | this.cache[cacheName] = {
|
2151 | hash: usedHash,
|
2152 | source: alreadyWritten.source
|
2153 | };
|
2154 | }
|
2155 | chunk.files.push(file);
|
2156 | this.hooks.chunkAsset.call(chunk, file);
|
2157 | continue;
|
2158 | } else {
|
2159 | throw new Error(
|
2160 | `Conflict: Multiple chunks emit assets to the same filename ${file}` +
|
2161 | ` (chunks ${alreadyWritten.chunk.id} and ${chunk.id})`
|
2162 | );
|
2163 | }
|
2164 | }
|
2165 | if (
|
2166 | this.cache &&
|
2167 | this.cache[cacheName] &&
|
2168 | this.cache[cacheName].hash === usedHash
|
2169 | ) {
|
2170 | source = this.cache[cacheName].source;
|
2171 | } else {
|
2172 | source = fileManifest.render();
|
2173 |
|
2174 | if (!(source instanceof CachedSource)) {
|
2175 | const cacheEntry = cachedSourceMap.get(source);
|
2176 | if (cacheEntry) {
|
2177 | source = cacheEntry;
|
2178 | } else {
|
2179 | const cachedSource = new CachedSource(source);
|
2180 | cachedSourceMap.set(source, cachedSource);
|
2181 | source = cachedSource;
|
2182 | }
|
2183 | }
|
2184 | if (this.cache) {
|
2185 | this.cache[cacheName] = {
|
2186 | hash: usedHash,
|
2187 | source
|
2188 | };
|
2189 | }
|
2190 | }
|
2191 | this.emitAsset(file, source, assetInfo);
|
2192 | chunk.files.push(file);
|
2193 | this.hooks.chunkAsset.call(chunk, file);
|
2194 | alreadyWrittenFiles.set(file, {
|
2195 | hash: usedHash,
|
2196 | source,
|
2197 | chunk
|
2198 | });
|
2199 | }
|
2200 | } catch (err) {
|
2201 | this.errors.push(
|
2202 | new ChunkRenderError(chunk, file || filenameTemplate, err)
|
2203 | );
|
2204 | }
|
2205 | }
|
2206 | }
|
2207 |
|
2208 | |
2209 |
|
2210 |
|
2211 |
|
2212 |
|
2213 | getPath(filename, data) {
|
2214 | data = data || {};
|
2215 | data.hash = data.hash || this.hash;
|
2216 | return this.mainTemplate.getAssetPath(filename, data);
|
2217 | }
|
2218 |
|
2219 | |
2220 |
|
2221 |
|
2222 |
|
2223 |
|
2224 | getPathWithInfo(filename, data) {
|
2225 | data = data || {};
|
2226 | data.hash = data.hash || this.hash;
|
2227 | return this.mainTemplate.getAssetPathWithInfo(filename, data);
|
2228 | }
|
2229 |
|
2230 | |
2231 |
|
2232 |
|
2233 |
|
2234 |
|
2235 |
|
2236 |
|
2237 |
|
2238 |
|
2239 |
|
2240 | createChildCompiler(name, outputOptions, plugins) {
|
2241 | const idx = this.childrenCounters[name] || 0;
|
2242 | this.childrenCounters[name] = idx + 1;
|
2243 | return this.compiler.createChildCompiler(
|
2244 | this,
|
2245 | name,
|
2246 | idx,
|
2247 | outputOptions,
|
2248 | plugins
|
2249 | );
|
2250 | }
|
2251 |
|
2252 | checkConstraints() {
|
2253 |
|
2254 | const usedIds = new Set();
|
2255 |
|
2256 | const modules = this.modules;
|
2257 | for (let indexModule = 0; indexModule < modules.length; indexModule++) {
|
2258 | const moduleId = modules[indexModule].id;
|
2259 | if (moduleId === null) continue;
|
2260 | if (usedIds.has(moduleId)) {
|
2261 | throw new Error(`checkConstraints: duplicate module id ${moduleId}`);
|
2262 | }
|
2263 | usedIds.add(moduleId);
|
2264 | }
|
2265 |
|
2266 | const chunks = this.chunks;
|
2267 | for (let indexChunk = 0; indexChunk < chunks.length; indexChunk++) {
|
2268 | const chunk = chunks[indexChunk];
|
2269 | if (chunks.indexOf(chunk) !== indexChunk) {
|
2270 | throw new Error(
|
2271 | `checkConstraints: duplicate chunk in compilation ${chunk.debugId}`
|
2272 | );
|
2273 | }
|
2274 | }
|
2275 |
|
2276 | for (const chunkGroup of this.chunkGroups) {
|
2277 | chunkGroup.checkConstraints();
|
2278 | }
|
2279 | }
|
2280 | }
|
2281 |
|
2282 |
|
2283 | Compilation.prototype.applyPlugins = util.deprecate(
|
2284 | |
2285 |
|
2286 |
|
2287 |
|
2288 |
|
2289 |
|
2290 |
|
2291 | function(name, ...args) {
|
2292 | this.hooks[
|
2293 | name.replace(/[- ]([a-z])/g, match => match[1].toUpperCase())
|
2294 | ].call(...args);
|
2295 | },
|
2296 | "Compilation.applyPlugins is deprecated. Use new API on `.hooks` instead"
|
2297 | );
|
2298 |
|
2299 |
|
2300 | Object.defineProperty(Compilation.prototype, "moduleTemplate", {
|
2301 | configurable: false,
|
2302 | get: util.deprecate(
|
2303 | /**
|
2304 | * @deprecated
|
2305 | * @this {Compilation}
|
2306 | * @returns {TODO} module template
|
2307 | */
|
2308 | function() {
|
2309 | return this.moduleTemplates.javascript;
|
2310 | },
|
2311 | "Compilation.moduleTemplate: Use Compilation.moduleTemplates.javascript instead"
|
2312 | ),
|
2313 | set: util.deprecate(
|
2314 | /**
|
2315 | * @deprecated
|
2316 | * @param {ModuleTemplate} value Template value
|
2317 | * @this {Compilation}
|
2318 | * @returns {void}
|
2319 | */
|
2320 | function(value) {
|
2321 | this.moduleTemplates.javascript = value;
|
2322 | },
|
2323 | "Compilation.moduleTemplate: Use Compilation.moduleTemplates.javascript instead."
|
2324 | )
|
2325 | });
|
2326 |
|
2327 | module.exports = Compilation;
|