MCPcopy
hub / github.com/webpack/webpack / sortWithSourceOrder

Function sortWithSourceOrder

lib/util/comparators.js:561–616  ·  view source on GitHub ↗
(
	dependencies,
	dependencySourceOrderMap,
	onDependencyReSort
)

Source from the content-addressed store, hash-verified

559 * @returns {void}
560 */
561const sortWithSourceOrder = (
562 dependencies,
563 dependencySourceOrderMap,
564 onDependencyReSort
565) => {
566 /** @type {{ dep: Dependency, main: number, sub: number }[]} */
567 const withSourceOrder = [];
568 /** @type {number[]} */
569 const positions = [];
570
571 for (let i = 0; i < dependencies.length; i++) {
572 const dep = dependencies[i];
573 const cached = dependencySourceOrderMap.get(dep);
574
575 if (cached) {
576 positions.push(i);
577 withSourceOrder.push({
578 dep,
579 main: cached.main,
580 sub: cached.sub
581 });
582 } else {
583 const sourceOrder = /** @type {number | undefined} */ (
584 /** @type {ModuleDependency} */ (dep).sourceOrder
585 );
586 if (typeof sourceOrder === "number") {
587 positions.push(i);
588 withSourceOrder.push({
589 dep,
590 main: sourceOrder,
591 sub: 0
592 });
593 }
594 }
595 }
596
597 if (withSourceOrder.length <= 1) {
598 return;
599 }
600
601 withSourceOrder.sort((a, b) => {
602 if (a.main !== b.main) {
603 return compareNumbers(a.main, b.main);
604 }
605 return compareNumbers(a.sub, b.sub);
606 });
607
608 // Second pass: place sorted deps back to original positions
609 for (let i = 0; i < positions.length; i++) {
610 const depIndex = positions[i];
611 dependencies[depIndex] = withSourceOrder[i].dep;
612 if (onDependencyReSort) {
613 onDependencyReSort(dependencies[depIndex], depIndex);
614 }
615 }
616};
617
618module.exports.compareChunkGroupsByIndex = compareChunkGroupsByIndex;

Callers 3

finishUpdateParentMethod · 0.85
handleParseResultMethod · 0.85

Calls 4

sortMethod · 0.80
compareNumbersFunction · 0.70
getMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected