123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- "use strict";
- const { compareRuntime } = require("./runtime");
- const createCachedParameterizedComparator = fn => {
-
- const map = new WeakMap();
- return arg => {
- const cachedResult = map.get(arg);
- if (cachedResult !== undefined) return cachedResult;
-
- const result = fn.bind(null, arg);
- map.set(arg, result);
- return result;
- };
- };
- exports.compareChunksById = (a, b) => {
- return compareIds(a.id, b.id);
- };
- exports.compareModulesByIdentifier = (a, b) => {
- return compareIds(a.identifier(), b.identifier());
- };
- const compareModulesById = (chunkGraph, a, b) => {
- return compareIds(chunkGraph.getModuleId(a), chunkGraph.getModuleId(b));
- };
- exports.compareModulesById =
- createCachedParameterizedComparator(compareModulesById);
- const compareNumbers = (a, b) => {
- if (typeof a !== typeof b) {
- return typeof a < typeof b ? -1 : 1;
- }
- if (a < b) return -1;
- if (a > b) return 1;
- return 0;
- };
- exports.compareNumbers = compareNumbers;
- const compareStringsNumeric = (a, b) => {
- const partsA = a.split(/(\d+)/);
- const partsB = b.split(/(\d+)/);
- const len = Math.min(partsA.length, partsB.length);
- for (let i = 0; i < len; i++) {
- const pA = partsA[i];
- const pB = partsB[i];
- if (i % 2 === 0) {
- if (pA.length > pB.length) {
- if (pA.slice(0, pB.length) > pB) return 1;
- return -1;
- } else if (pB.length > pA.length) {
- if (pB.slice(0, pA.length) > pA) return -1;
- return 1;
- } else {
- if (pA < pB) return -1;
- if (pA > pB) return 1;
- }
- } else {
- const nA = +pA;
- const nB = +pB;
- if (nA < nB) return -1;
- if (nA > nB) return 1;
- }
- }
- if (partsB.length < partsA.length) return 1;
- if (partsB.length > partsA.length) return -1;
- return 0;
- };
- exports.compareStringsNumeric = compareStringsNumeric;
- const compareModulesByPostOrderIndexOrIdentifier = (moduleGraph, a, b) => {
- const cmp = compareNumbers(
- moduleGraph.getPostOrderIndex(a),
- moduleGraph.getPostOrderIndex(b)
- );
- if (cmp !== 0) return cmp;
- return compareIds(a.identifier(), b.identifier());
- };
- exports.compareModulesByPostOrderIndexOrIdentifier =
- createCachedParameterizedComparator(
- compareModulesByPostOrderIndexOrIdentifier
- );
- const compareModulesByPreOrderIndexOrIdentifier = (moduleGraph, a, b) => {
- const cmp = compareNumbers(
- moduleGraph.getPreOrderIndex(a),
- moduleGraph.getPreOrderIndex(b)
- );
- if (cmp !== 0) return cmp;
- return compareIds(a.identifier(), b.identifier());
- };
- exports.compareModulesByPreOrderIndexOrIdentifier =
- createCachedParameterizedComparator(
- compareModulesByPreOrderIndexOrIdentifier
- );
- const compareModulesByIdOrIdentifier = (chunkGraph, a, b) => {
- const cmp = compareIds(chunkGraph.getModuleId(a), chunkGraph.getModuleId(b));
- if (cmp !== 0) return cmp;
- return compareIds(a.identifier(), b.identifier());
- };
- exports.compareModulesByIdOrIdentifier = createCachedParameterizedComparator(
- compareModulesByIdOrIdentifier
- );
- const compareChunks = (chunkGraph, a, b) => {
- return chunkGraph.compareChunks(a, b);
- };
- exports.compareChunks = createCachedParameterizedComparator(compareChunks);
- const compareIds = (a, b) => {
- if (typeof a !== typeof b) {
- return typeof a < typeof b ? -1 : 1;
- }
- if (a < b) return -1;
- if (a > b) return 1;
- return 0;
- };
- exports.compareIds = compareIds;
- const compareStrings = (a, b) => {
- if (a < b) return -1;
- if (a > b) return 1;
- return 0;
- };
- exports.compareStrings = compareStrings;
- const compareChunkGroupsByIndex = (a, b) => {
- return a.index < b.index ? -1 : 1;
- };
- exports.compareChunkGroupsByIndex = compareChunkGroupsByIndex;
- class TwoKeyWeakMap {
- constructor() {
-
- this._map = new WeakMap();
- }
-
- get(key1, key2) {
- const childMap = this._map.get(key1);
- if (childMap === undefined) {
- return undefined;
- }
- return childMap.get(key2);
- }
-
- set(key1, key2, value) {
- let childMap = this._map.get(key1);
- if (childMap === undefined) {
- childMap = new WeakMap();
- this._map.set(key1, childMap);
- }
- childMap.set(key2, value);
- }
- }
- const concatComparatorsCache = new TwoKeyWeakMap();
- const concatComparators = (c1, c2, ...cRest) => {
- if (cRest.length > 0) {
- const [c3, ...cRest2] = cRest;
- return concatComparators(c1, concatComparators(c2, c3, ...cRest2));
- }
- const cacheEntry = (
- concatComparatorsCache.get(c1, c2)
- );
- if (cacheEntry !== undefined) return cacheEntry;
-
- const result = (a, b) => {
- const res = c1(a, b);
- if (res !== 0) return res;
- return c2(a, b);
- };
- concatComparatorsCache.set(c1, c2, result);
- return result;
- };
- exports.concatComparators = concatComparators;
- const compareSelectCache = new TwoKeyWeakMap();
- const compareSelect = (getter, comparator) => {
- const cacheEntry = compareSelectCache.get(getter, comparator);
- if (cacheEntry !== undefined) return cacheEntry;
-
- const result = (a, b) => {
- const aValue = getter(a);
- const bValue = getter(b);
- if (aValue !== undefined && aValue !== null) {
- if (bValue !== undefined && bValue !== null) {
- return comparator(aValue, bValue);
- }
- return -1;
- } else {
- if (bValue !== undefined && bValue !== null) {
- return 1;
- }
- return 0;
- }
- };
- compareSelectCache.set(getter, comparator, result);
- return result;
- };
- exports.compareSelect = compareSelect;
- const compareIteratorsCache = new WeakMap();
- const compareIterables = elementComparator => {
- const cacheEntry = compareIteratorsCache.get(elementComparator);
- if (cacheEntry !== undefined) return cacheEntry;
-
- const result = (a, b) => {
- const aI = a[Symbol.iterator]();
- const bI = b[Symbol.iterator]();
-
- while (true) {
- const aItem = aI.next();
- const bItem = bI.next();
- if (aItem.done) {
- return bItem.done ? 0 : -1;
- } else if (bItem.done) {
- return 1;
- }
- const res = elementComparator(aItem.value, bItem.value);
- if (res !== 0) return res;
- }
- };
- compareIteratorsCache.set(elementComparator, result);
- return result;
- };
- exports.compareIterables = compareIterables;
- exports.keepOriginalOrder = iterable => {
-
- const map = new Map();
- let i = 0;
- for (const item of iterable) {
- map.set(item, i++);
- }
- return (a, b) => compareNumbers(map.get(a), map.get(b));
- };
- exports.compareChunksNatural = chunkGraph => {
- const cmpFn = exports.compareModulesById(chunkGraph);
- const cmpIterableFn = compareIterables(cmpFn);
- return concatComparators(
- compareSelect(chunk => chunk.name, compareIds),
- compareSelect(chunk => chunk.runtime, compareRuntime),
- compareSelect(
-
- chunk => chunkGraph.getOrderedChunkModulesIterable(chunk, cmpFn),
- cmpIterableFn
- )
- );
- };
- exports.compareLocations = (a, b) => {
- let isObjectA = typeof a === "object" && a !== null;
- let isObjectB = typeof b === "object" && b !== null;
- if (!isObjectA || !isObjectB) {
- if (isObjectA) return 1;
- if (isObjectB) return -1;
- return 0;
- }
- if ("start" in a) {
- if ("start" in b) {
- const ap = a.start;
- const bp = b.start;
- if (ap.line < bp.line) return -1;
- if (ap.line > bp.line) return 1;
- if (ap.column < bp.column) return -1;
- if (ap.column > bp.column) return 1;
- } else return -1;
- } else if ("start" in b) return 1;
- if ("name" in a) {
- if ("name" in b) {
- if (a.name < b.name) return -1;
- if (a.name > b.name) return 1;
- } else return -1;
- } else if ("name" in b) return 1;
- if ("index" in a) {
- if ("index" in b) {
- if (a.index < b.index) return -1;
- if (a.index > b.index) return 1;
- } else return -1;
- } else if ("index" in b) return 1;
- return 0;
- };
|