1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const ChunkGraph = require("./ChunkGraph");
|
9 | const Entrypoint = require("./Entrypoint");
|
10 | const { intersect } = require("./util/SetHelpers");
|
11 | const SortableSet = require("./util/SortableSet");
|
12 | const StringXor = require("./util/StringXor");
|
13 | const {
|
14 | compareModulesByIdentifier,
|
15 | compareChunkGroupsByIndex,
|
16 | compareModulesById
|
17 | } = require("./util/comparators");
|
18 | const { createArrayToSetDeprecationSet } = require("./util/deprecation");
|
19 | const { mergeRuntime } = require("./util/runtime");
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | const ChunkFilesSet = createArrayToSetDeprecationSet("chunk.files");
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | let debugId = 1000;
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | class Chunk {
|
64 | |
65 |
|
66 |
|
67 | constructor(name) {
|
68 |
|
69 | this.id = null;
|
70 |
|
71 | this.ids = null;
|
72 |
|
73 | this.debugId = debugId++;
|
74 |
|
75 | this.name = name;
|
76 |
|
77 | this.idNameHints = new SortableSet();
|
78 |
|
79 | this.preventIntegration = false;
|
80 |
|
81 | this.filenameTemplate = undefined;
|
82 |
|
83 | this._groups = new SortableSet(undefined, compareChunkGroupsByIndex);
|
84 |
|
85 | this.runtime = undefined;
|
86 |
|
87 | this.files = new ChunkFilesSet();
|
88 |
|
89 | this.auxiliaryFiles = new Set();
|
90 |
|
91 | this.rendered = false;
|
92 |
|
93 | this.hash = undefined;
|
94 |
|
95 | this.contentHash = Object.create(null);
|
96 |
|
97 | this.renderedHash = undefined;
|
98 |
|
99 | this.chunkReason = undefined;
|
100 |
|
101 | this.extraAsync = false;
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 | get entryModule() {
|
107 | const entryModules = Array.from(
|
108 | ChunkGraph.getChunkGraphForChunk(
|
109 | this,
|
110 | "Chunk.entryModule",
|
111 | "DEP_WEBPACK_CHUNK_ENTRY_MODULE"
|
112 | ).getChunkEntryModulesIterable(this)
|
113 | );
|
114 | if (entryModules.length === 0) {
|
115 | return undefined;
|
116 | } else if (entryModules.length === 1) {
|
117 | return entryModules[0];
|
118 | } else {
|
119 | throw new Error(
|
120 | "Module.entryModule: Multiple entry modules are not supported by the deprecated API (Use the new ChunkGroup API)"
|
121 | );
|
122 | }
|
123 | }
|
124 |
|
125 | |
126 |
|
127 |
|
128 | hasEntryModule() {
|
129 | return (
|
130 | ChunkGraph.getChunkGraphForChunk(
|
131 | this,
|
132 | "Chunk.hasEntryModule",
|
133 | "DEP_WEBPACK_CHUNK_HAS_ENTRY_MODULE"
|
134 | ).getNumberOfEntryModules(this) > 0
|
135 | );
|
136 | }
|
137 |
|
138 | |
139 |
|
140 |
|
141 |
|
142 | addModule(module) {
|
143 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
144 | this,
|
145 | "Chunk.addModule",
|
146 | "DEP_WEBPACK_CHUNK_ADD_MODULE"
|
147 | );
|
148 | if (chunkGraph.isModuleInChunk(module, this)) return false;
|
149 | chunkGraph.connectChunkAndModule(this, module);
|
150 | return true;
|
151 | }
|
152 |
|
153 | |
154 |
|
155 |
|
156 |
|
157 | removeModule(module) {
|
158 | ChunkGraph.getChunkGraphForChunk(
|
159 | this,
|
160 | "Chunk.removeModule",
|
161 | "DEP_WEBPACK_CHUNK_REMOVE_MODULE"
|
162 | ).disconnectChunkAndModule(this, module);
|
163 | }
|
164 |
|
165 | |
166 |
|
167 |
|
168 | getNumberOfModules() {
|
169 | return ChunkGraph.getChunkGraphForChunk(
|
170 | this,
|
171 | "Chunk.getNumberOfModules",
|
172 | "DEP_WEBPACK_CHUNK_GET_NUMBER_OF_MODULES"
|
173 | ).getNumberOfChunkModules(this);
|
174 | }
|
175 |
|
176 | get modulesIterable() {
|
177 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
178 | this,
|
179 | "Chunk.modulesIterable",
|
180 | "DEP_WEBPACK_CHUNK_MODULES_ITERABLE"
|
181 | );
|
182 | return chunkGraph.getOrderedChunkModulesIterable(
|
183 | this,
|
184 | compareModulesByIdentifier
|
185 | );
|
186 | }
|
187 |
|
188 | |
189 |
|
190 |
|
191 |
|
192 | compareTo(otherChunk) {
|
193 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
194 | this,
|
195 | "Chunk.compareTo",
|
196 | "DEP_WEBPACK_CHUNK_COMPARE_TO"
|
197 | );
|
198 | return chunkGraph.compareChunks(this, otherChunk);
|
199 | }
|
200 |
|
201 | |
202 |
|
203 |
|
204 |
|
205 | containsModule(module) {
|
206 | return ChunkGraph.getChunkGraphForChunk(
|
207 | this,
|
208 | "Chunk.containsModule",
|
209 | "DEP_WEBPACK_CHUNK_CONTAINS_MODULE"
|
210 | ).isModuleInChunk(module, this);
|
211 | }
|
212 |
|
213 | |
214 |
|
215 |
|
216 | getModules() {
|
217 | return ChunkGraph.getChunkGraphForChunk(
|
218 | this,
|
219 | "Chunk.getModules",
|
220 | "DEP_WEBPACK_CHUNK_GET_MODULES"
|
221 | ).getChunkModules(this);
|
222 | }
|
223 |
|
224 | |
225 |
|
226 |
|
227 | remove() {
|
228 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
229 | this,
|
230 | "Chunk.remove",
|
231 | "DEP_WEBPACK_CHUNK_REMOVE"
|
232 | );
|
233 | chunkGraph.disconnectChunk(this);
|
234 | this.disconnectFromGroups();
|
235 | }
|
236 |
|
237 | |
238 |
|
239 |
|
240 |
|
241 |
|
242 | moveModule(module, otherChunk) {
|
243 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
244 | this,
|
245 | "Chunk.moveModule",
|
246 | "DEP_WEBPACK_CHUNK_MOVE_MODULE"
|
247 | );
|
248 | chunkGraph.disconnectChunkAndModule(this, module);
|
249 | chunkGraph.connectChunkAndModule(otherChunk, module);
|
250 | }
|
251 |
|
252 | |
253 |
|
254 |
|
255 |
|
256 | integrate(otherChunk) {
|
257 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
258 | this,
|
259 | "Chunk.integrate",
|
260 | "DEP_WEBPACK_CHUNK_INTEGRATE"
|
261 | );
|
262 | if (chunkGraph.canChunksBeIntegrated(this, otherChunk)) {
|
263 | chunkGraph.integrateChunks(this, otherChunk);
|
264 | return true;
|
265 | } else {
|
266 | return false;
|
267 | }
|
268 | }
|
269 |
|
270 | |
271 |
|
272 |
|
273 |
|
274 | canBeIntegrated(otherChunk) {
|
275 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
276 | this,
|
277 | "Chunk.canBeIntegrated",
|
278 | "DEP_WEBPACK_CHUNK_CAN_BE_INTEGRATED"
|
279 | );
|
280 | return chunkGraph.canChunksBeIntegrated(this, otherChunk);
|
281 | }
|
282 |
|
283 | |
284 |
|
285 |
|
286 | isEmpty() {
|
287 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
288 | this,
|
289 | "Chunk.isEmpty",
|
290 | "DEP_WEBPACK_CHUNK_IS_EMPTY"
|
291 | );
|
292 | return chunkGraph.getNumberOfChunkModules(this) === 0;
|
293 | }
|
294 |
|
295 | |
296 |
|
297 |
|
298 | modulesSize() {
|
299 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
300 | this,
|
301 | "Chunk.modulesSize",
|
302 | "DEP_WEBPACK_CHUNK_MODULES_SIZE"
|
303 | );
|
304 | return chunkGraph.getChunkModulesSize(this);
|
305 | }
|
306 |
|
307 | |
308 |
|
309 |
|
310 |
|
311 | size(options = {}) {
|
312 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
313 | this,
|
314 | "Chunk.size",
|
315 | "DEP_WEBPACK_CHUNK_SIZE"
|
316 | );
|
317 | return chunkGraph.getChunkSize(this, options);
|
318 | }
|
319 |
|
320 | |
321 |
|
322 |
|
323 |
|
324 |
|
325 | integratedSize(otherChunk, options) {
|
326 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
327 | this,
|
328 | "Chunk.integratedSize",
|
329 | "DEP_WEBPACK_CHUNK_INTEGRATED_SIZE"
|
330 | );
|
331 | return chunkGraph.getIntegratedChunksSize(this, otherChunk, options);
|
332 | }
|
333 |
|
334 | |
335 |
|
336 |
|
337 |
|
338 | getChunkModuleMaps(filterFn) {
|
339 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
340 | this,
|
341 | "Chunk.getChunkModuleMaps",
|
342 | "DEP_WEBPACK_CHUNK_GET_CHUNK_MODULE_MAPS"
|
343 | );
|
344 |
|
345 | const chunkModuleIdMap = Object.create(null);
|
346 |
|
347 | const chunkModuleHashMap = Object.create(null);
|
348 |
|
349 | for (const asyncChunk of this.getAllAsyncChunks()) {
|
350 |
|
351 | let array;
|
352 | for (const module of chunkGraph.getOrderedChunkModulesIterable(
|
353 | asyncChunk,
|
354 | compareModulesById(chunkGraph)
|
355 | )) {
|
356 | if (filterFn(module)) {
|
357 | if (array === undefined) {
|
358 | array = [];
|
359 | chunkModuleIdMap[asyncChunk.id] = array;
|
360 | }
|
361 | const moduleId = chunkGraph.getModuleId(module);
|
362 | array.push(moduleId);
|
363 | chunkModuleHashMap[moduleId] = chunkGraph.getRenderedModuleHash(
|
364 | module,
|
365 | undefined
|
366 | );
|
367 | }
|
368 | }
|
369 | }
|
370 |
|
371 | return {
|
372 | id: chunkModuleIdMap,
|
373 | hash: chunkModuleHashMap
|
374 | };
|
375 | }
|
376 |
|
377 | |
378 |
|
379 |
|
380 |
|
381 |
|
382 | hasModuleInGraph(filterFn, filterChunkFn) {
|
383 | const chunkGraph = ChunkGraph.getChunkGraphForChunk(
|
384 | this,
|
385 | "Chunk.hasModuleInGraph",
|
386 | "DEP_WEBPACK_CHUNK_HAS_MODULE_IN_GRAPH"
|
387 | );
|
388 | return chunkGraph.hasModuleInGraph(this, filterFn, filterChunkFn);
|
389 | }
|
390 |
|
391 | |
392 |
|
393 |
|
394 |
|
395 |
|
396 | getChunkMaps(realHash) {
|
397 |
|
398 | const chunkHashMap = Object.create(null);
|
399 |
|
400 | const chunkContentHashMap = Object.create(null);
|
401 |
|
402 | const chunkNameMap = Object.create(null);
|
403 |
|
404 | for (const chunk of this.getAllAsyncChunks()) {
|
405 | chunkHashMap[chunk.id] = realHash ? chunk.hash : chunk.renderedHash;
|
406 | for (const key of Object.keys(chunk.contentHash)) {
|
407 | if (!chunkContentHashMap[key]) {
|
408 | chunkContentHashMap[key] = Object.create(null);
|
409 | }
|
410 | chunkContentHashMap[key][chunk.id] = chunk.contentHash[key];
|
411 | }
|
412 | if (chunk.name) {
|
413 | chunkNameMap[chunk.id] = chunk.name;
|
414 | }
|
415 | }
|
416 |
|
417 | return {
|
418 | hash: chunkHashMap,
|
419 | contentHash: chunkContentHashMap,
|
420 | name: chunkNameMap
|
421 | };
|
422 | }
|
423 |
|
424 |
|
425 | |
426 |
|
427 |
|
428 | hasRuntime() {
|
429 | for (const chunkGroup of this._groups) {
|
430 | if (
|
431 | chunkGroup instanceof Entrypoint &&
|
432 | chunkGroup.getRuntimeChunk() === this
|
433 | ) {
|
434 | return true;
|
435 | }
|
436 | }
|
437 | return false;
|
438 | }
|
439 |
|
440 | |
441 |
|
442 |
|
443 | canBeInitial() {
|
444 | for (const chunkGroup of this._groups) {
|
445 | if (chunkGroup.isInitial()) return true;
|
446 | }
|
447 | return false;
|
448 | }
|
449 |
|
450 | |
451 |
|
452 |
|
453 | isOnlyInitial() {
|
454 | if (this._groups.size <= 0) return false;
|
455 | for (const chunkGroup of this._groups) {
|
456 | if (!chunkGroup.isInitial()) return false;
|
457 | }
|
458 | return true;
|
459 | }
|
460 |
|
461 | |
462 |
|
463 |
|
464 | getEntryOptions() {
|
465 | for (const chunkGroup of this._groups) {
|
466 | if (chunkGroup instanceof Entrypoint) {
|
467 | return chunkGroup.options;
|
468 | }
|
469 | }
|
470 | return undefined;
|
471 | }
|
472 |
|
473 | |
474 |
|
475 |
|
476 |
|
477 | addGroup(chunkGroup) {
|
478 | this._groups.add(chunkGroup);
|
479 | }
|
480 |
|
481 | |
482 |
|
483 |
|
484 |
|
485 | removeGroup(chunkGroup) {
|
486 | this._groups.delete(chunkGroup);
|
487 | }
|
488 |
|
489 | |
490 |
|
491 |
|
492 |
|
493 | isInGroup(chunkGroup) {
|
494 | return this._groups.has(chunkGroup);
|
495 | }
|
496 |
|
497 | |
498 |
|
499 |
|
500 | getNumberOfGroups() {
|
501 | return this._groups.size;
|
502 | }
|
503 |
|
504 | |
505 |
|
506 |
|
507 | get groupsIterable() {
|
508 | this._groups.sort();
|
509 | return this._groups;
|
510 | }
|
511 |
|
512 | |
513 |
|
514 |
|
515 | disconnectFromGroups() {
|
516 | for (const chunkGroup of this._groups) {
|
517 | chunkGroup.removeChunk(this);
|
518 | }
|
519 | }
|
520 |
|
521 | |
522 |
|
523 |
|
524 |
|
525 | split(newChunk) {
|
526 | for (const chunkGroup of this._groups) {
|
527 | chunkGroup.insertChunk(newChunk, this);
|
528 | newChunk.addGroup(chunkGroup);
|
529 | }
|
530 | for (const idHint of this.idNameHints) {
|
531 | newChunk.idNameHints.add(idHint);
|
532 | }
|
533 | newChunk.runtime = mergeRuntime(newChunk.runtime, this.runtime);
|
534 | }
|
535 |
|
536 | |
537 |
|
538 |
|
539 |
|
540 |
|
541 | updateHash(hash, chunkGraph) {
|
542 | hash.update(`${this.id} `);
|
543 | hash.update(this.ids ? this.ids.join(",") : "");
|
544 | hash.update(`${this.name || ""} `);
|
545 | const xor = new StringXor();
|
546 | for (const m of chunkGraph.getChunkModulesIterable(this)) {
|
547 | xor.add(chunkGraph.getModuleHash(m, this.runtime));
|
548 | }
|
549 | xor.updateHash(hash);
|
550 | const entryModules = chunkGraph.getChunkEntryModulesWithChunkGroupIterable(
|
551 | this
|
552 | );
|
553 | for (const [m, chunkGroup] of entryModules) {
|
554 | hash.update("entry");
|
555 | hash.update(`${chunkGraph.getModuleId(m)}`);
|
556 | hash.update(chunkGroup.id);
|
557 | }
|
558 | }
|
559 |
|
560 | |
561 |
|
562 |
|
563 | getAllAsyncChunks() {
|
564 | const queue = new Set();
|
565 | const chunks = new Set();
|
566 |
|
567 | const initialChunks = intersect(
|
568 | Array.from(this.groupsIterable, g => new Set(g.chunks))
|
569 | );
|
570 |
|
571 | for (const chunkGroup of this.groupsIterable) {
|
572 | for (const child of chunkGroup.childrenIterable) {
|
573 | queue.add(child);
|
574 | }
|
575 | }
|
576 |
|
577 | for (const chunkGroup of queue) {
|
578 | for (const chunk of chunkGroup.chunks) {
|
579 | if (!initialChunks.has(chunk)) {
|
580 | chunks.add(chunk);
|
581 | }
|
582 | }
|
583 | for (const child of chunkGroup.childrenIterable) {
|
584 | queue.add(child);
|
585 | }
|
586 | }
|
587 |
|
588 | return chunks;
|
589 | }
|
590 |
|
591 | |
592 |
|
593 |
|
594 | getAllInitialChunks() {
|
595 | return intersect(Array.from(this.groupsIterable, g => new Set(g.chunks)));
|
596 | }
|
597 |
|
598 | |
599 |
|
600 |
|
601 | getAllReferencedChunks() {
|
602 | const queue = new Set(this.groupsIterable);
|
603 | const chunks = new Set();
|
604 |
|
605 | for (const chunkGroup of queue) {
|
606 | for (const chunk of chunkGroup.chunks) {
|
607 | chunks.add(chunk);
|
608 | }
|
609 | for (const child of chunkGroup.childrenIterable) {
|
610 | queue.add(child);
|
611 | }
|
612 | }
|
613 |
|
614 | return chunks;
|
615 | }
|
616 |
|
617 | |
618 |
|
619 |
|
620 | getAllReferencedAsyncEntrypoints() {
|
621 | const queue = new Set(this.groupsIterable);
|
622 | const entrypoints = new Set();
|
623 |
|
624 | for (const chunkGroup of queue) {
|
625 | for (const entrypoint of chunkGroup.asyncEntrypointsIterable) {
|
626 | entrypoints.add(entrypoint);
|
627 | }
|
628 | for (const child of chunkGroup.childrenIterable) {
|
629 | queue.add(child);
|
630 | }
|
631 | }
|
632 |
|
633 | return entrypoints;
|
634 | }
|
635 |
|
636 | |
637 |
|
638 |
|
639 | hasAsyncChunks() {
|
640 | const queue = new Set();
|
641 |
|
642 | const initialChunks = intersect(
|
643 | Array.from(this.groupsIterable, g => new Set(g.chunks))
|
644 | );
|
645 |
|
646 | for (const chunkGroup of this.groupsIterable) {
|
647 | for (const child of chunkGroup.childrenIterable) {
|
648 | queue.add(child);
|
649 | }
|
650 | }
|
651 |
|
652 | for (const chunkGroup of queue) {
|
653 | for (const chunk of chunkGroup.chunks) {
|
654 | if (!initialChunks.has(chunk)) {
|
655 | return true;
|
656 | }
|
657 | }
|
658 | for (const child of chunkGroup.childrenIterable) {
|
659 | queue.add(child);
|
660 | }
|
661 | }
|
662 |
|
663 | return false;
|
664 | }
|
665 |
|
666 | |
667 |
|
668 |
|
669 |
|
670 |
|
671 | getChildIdsByOrders(chunkGraph, filterFn) {
|
672 |
|
673 | const lists = new Map();
|
674 | for (const group of this.groupsIterable) {
|
675 | if (group.chunks[group.chunks.length - 1] === this) {
|
676 | for (const childGroup of group.childrenIterable) {
|
677 | for (const key of Object.keys(childGroup.options)) {
|
678 | if (key.endsWith("Order")) {
|
679 | const name = key.substr(0, key.length - "Order".length);
|
680 | let list = lists.get(name);
|
681 | if (list === undefined) {
|
682 | list = [];
|
683 | lists.set(name, list);
|
684 | }
|
685 | list.push({
|
686 | order: childGroup.options[key],
|
687 | group: childGroup
|
688 | });
|
689 | }
|
690 | }
|
691 | }
|
692 | }
|
693 | }
|
694 |
|
695 | const result = Object.create(null);
|
696 | for (const [name, list] of lists) {
|
697 | list.sort((a, b) => {
|
698 | const cmp = b.order - a.order;
|
699 | if (cmp !== 0) return cmp;
|
700 | return a.group.compareTo(chunkGraph, b.group);
|
701 | });
|
702 |
|
703 | const chunkIdSet = new Set();
|
704 | for (const item of list) {
|
705 | for (const chunk of item.group.chunks) {
|
706 | if (filterFn && !filterFn(chunk, chunkGraph)) continue;
|
707 | chunkIdSet.add(chunk.id);
|
708 | }
|
709 | }
|
710 | if (chunkIdSet.size > 0) {
|
711 | result[name] = Array.from(chunkIdSet);
|
712 | }
|
713 | }
|
714 | return result;
|
715 | }
|
716 |
|
717 | |
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 | getChildIdsByOrdersMap(chunkGraph, includeDirectChildren, filterFn) {
|
724 |
|
725 | const chunkMaps = Object.create(null);
|
726 |
|
727 | |
728 |
|
729 |
|
730 |
|
731 | const addChildIdsByOrdersToMap = chunk => {
|
732 | const data = chunk.getChildIdsByOrders(chunkGraph, filterFn);
|
733 | for (const key of Object.keys(data)) {
|
734 | let chunkMap = chunkMaps[key];
|
735 | if (chunkMap === undefined) {
|
736 | chunkMaps[key] = chunkMap = Object.create(null);
|
737 | }
|
738 | chunkMap[chunk.id] = data[key];
|
739 | }
|
740 | };
|
741 |
|
742 | if (includeDirectChildren) {
|
743 |
|
744 | const chunks = new Set();
|
745 | for (const chunkGroup of this.groupsIterable) {
|
746 | for (const chunk of chunkGroup.chunks) {
|
747 | chunks.add(chunk);
|
748 | }
|
749 | }
|
750 | for (const chunk of chunks) {
|
751 | addChildIdsByOrdersToMap(chunk);
|
752 | }
|
753 | }
|
754 |
|
755 | for (const chunk of this.getAllAsyncChunks()) {
|
756 | addChildIdsByOrdersToMap(chunk);
|
757 | }
|
758 |
|
759 | return chunkMaps;
|
760 | }
|
761 | }
|
762 |
|
763 | module.exports = Chunk;
|