( dependencies, dependencySourceOrderMap, onDependencyReSort )
| 559 | * @returns {void} |
| 560 | */ |
| 561 | const 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 | |
| 618 | module.exports.compareChunkGroupsByIndex = compareChunkGroupsByIndex; |
no test coverage detected