1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { compareRuntime } = require("./runtime");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | const createCachedParameterizedComparator = fn => {
|
27 |
|
28 | const map = new WeakMap();
|
29 | return arg => {
|
30 | const cachedResult = map.get(arg);
|
31 | if (cachedResult !== undefined) return cachedResult;
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 | const result = fn.bind(null, arg);
|
38 | map.set(arg, result);
|
39 | return result;
|
40 | };
|
41 | };
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | exports.compareChunksById = (a, b) => {
|
49 | return compareIds(a.id, b.id);
|
50 | };
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | exports.compareModulesByIdentifier = (a, b) => {
|
58 | return compareIds(a.identifier(), b.identifier());
|
59 | };
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | const compareModulesById = (chunkGraph, a, b) => {
|
68 | return compareIds(chunkGraph.getModuleId(a), chunkGraph.getModuleId(b));
|
69 | };
|
70 |
|
71 | exports.compareModulesById =
|
72 | createCachedParameterizedComparator(compareModulesById);
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | const compareNumbers = (a, b) => {
|
80 | if (typeof a !== typeof b) {
|
81 | return typeof a < typeof b ? -1 : 1;
|
82 | }
|
83 | if (a < b) return -1;
|
84 | if (a > b) return 1;
|
85 | return 0;
|
86 | };
|
87 | exports.compareNumbers = compareNumbers;
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | const compareStringsNumeric = (a, b) => {
|
95 | const partsA = a.split(/(\d+)/);
|
96 | const partsB = b.split(/(\d+)/);
|
97 | const len = Math.min(partsA.length, partsB.length);
|
98 | for (let i = 0; i < len; i++) {
|
99 | const pA = partsA[i];
|
100 | const pB = partsB[i];
|
101 | if (i % 2 === 0) {
|
102 | if (pA.length > pB.length) {
|
103 | if (pA.slice(0, pB.length) > pB) return 1;
|
104 | return -1;
|
105 | } else if (pB.length > pA.length) {
|
106 | if (pB.slice(0, pA.length) > pA) return -1;
|
107 | return 1;
|
108 | } else {
|
109 | if (pA < pB) return -1;
|
110 | if (pA > pB) return 1;
|
111 | }
|
112 | } else {
|
113 | const nA = +pA;
|
114 | const nB = +pB;
|
115 | if (nA < nB) return -1;
|
116 | if (nA > nB) return 1;
|
117 | }
|
118 | }
|
119 | if (partsB.length < partsA.length) return 1;
|
120 | if (partsB.length > partsA.length) return -1;
|
121 | return 0;
|
122 | };
|
123 | exports.compareStringsNumeric = compareStringsNumeric;
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | const compareModulesByPostOrderIndexOrIdentifier = (moduleGraph, a, b) => {
|
132 | const cmp = compareNumbers(
|
133 | moduleGraph.getPostOrderIndex(a),
|
134 | moduleGraph.getPostOrderIndex(b)
|
135 | );
|
136 | if (cmp !== 0) return cmp;
|
137 | return compareIds(a.identifier(), b.identifier());
|
138 | };
|
139 |
|
140 | exports.compareModulesByPostOrderIndexOrIdentifier =
|
141 | createCachedParameterizedComparator(
|
142 | compareModulesByPostOrderIndexOrIdentifier
|
143 | );
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | const compareModulesByPreOrderIndexOrIdentifier = (moduleGraph, a, b) => {
|
152 | const cmp = compareNumbers(
|
153 | moduleGraph.getPreOrderIndex(a),
|
154 | moduleGraph.getPreOrderIndex(b)
|
155 | );
|
156 | if (cmp !== 0) return cmp;
|
157 | return compareIds(a.identifier(), b.identifier());
|
158 | };
|
159 |
|
160 | exports.compareModulesByPreOrderIndexOrIdentifier =
|
161 | createCachedParameterizedComparator(
|
162 | compareModulesByPreOrderIndexOrIdentifier
|
163 | );
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | const compareModulesByIdOrIdentifier = (chunkGraph, a, b) => {
|
172 | const cmp = compareIds(chunkGraph.getModuleId(a), chunkGraph.getModuleId(b));
|
173 | if (cmp !== 0) return cmp;
|
174 | return compareIds(a.identifier(), b.identifier());
|
175 | };
|
176 |
|
177 | exports.compareModulesByIdOrIdentifier = createCachedParameterizedComparator(
|
178 | compareModulesByIdOrIdentifier
|
179 | );
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | const compareChunks = (chunkGraph, a, b) => {
|
188 | return chunkGraph.compareChunks(a, b);
|
189 | };
|
190 |
|
191 | exports.compareChunks = createCachedParameterizedComparator(compareChunks);
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | const compareIds = (a, b) => {
|
199 | if (typeof a !== typeof b) {
|
200 | return typeof a < typeof b ? -1 : 1;
|
201 | }
|
202 | if (a < b) return -1;
|
203 | if (a > b) return 1;
|
204 | return 0;
|
205 | };
|
206 |
|
207 | exports.compareIds = compareIds;
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | const compareStrings = (a, b) => {
|
215 | if (a < b) return -1;
|
216 | if (a > b) return 1;
|
217 | return 0;
|
218 | };
|
219 |
|
220 | exports.compareStrings = compareStrings;
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | const compareChunkGroupsByIndex = (a, b) => {
|
228 | return a.index < b.index ? -1 : 1;
|
229 | };
|
230 |
|
231 | exports.compareChunkGroupsByIndex = compareChunkGroupsByIndex;
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | class TwoKeyWeakMap {
|
239 | constructor() {
|
240 |
|
241 | this._map = new WeakMap();
|
242 | }
|
243 |
|
244 | |
245 |
|
246 |
|
247 |
|
248 |
|
249 | get(key1, key2) {
|
250 | const childMap = this._map.get(key1);
|
251 | if (childMap === undefined) {
|
252 | return undefined;
|
253 | }
|
254 | return childMap.get(key2);
|
255 | }
|
256 |
|
257 | |
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | set(key1, key2, value) {
|
264 | let childMap = this._map.get(key1);
|
265 | if (childMap === undefined) {
|
266 | childMap = new WeakMap();
|
267 | this._map.set(key1, childMap);
|
268 | }
|
269 | childMap.set(key2, value);
|
270 | }
|
271 | }
|
272 |
|
273 |
|
274 | const concatComparatorsCache = new TwoKeyWeakMap();
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | const concatComparators = (c1, c2, ...cRest) => {
|
284 | if (cRest.length > 0) {
|
285 | const [c3, ...cRest2] = cRest;
|
286 | return concatComparators(c1, concatComparators(c2, c3, ...cRest2));
|
287 | }
|
288 | const cacheEntry = (
|
289 | concatComparatorsCache.get(c1, c2)
|
290 | );
|
291 | if (cacheEntry !== undefined) return cacheEntry;
|
292 | |
293 |
|
294 |
|
295 |
|
296 |
|
297 | const result = (a, b) => {
|
298 | const res = c1(a, b);
|
299 | if (res !== 0) return res;
|
300 | return c2(a, b);
|
301 | };
|
302 | concatComparatorsCache.set(c1, c2, result);
|
303 | return result;
|
304 | };
|
305 | exports.concatComparators = concatComparators;
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | const compareSelectCache = new TwoKeyWeakMap();
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 | const compareSelect = (getter, comparator) => {
|
320 | const cacheEntry = compareSelectCache.get(getter, comparator);
|
321 | if (cacheEntry !== undefined) return cacheEntry;
|
322 | |
323 |
|
324 |
|
325 |
|
326 |
|
327 | const result = (a, b) => {
|
328 | const aValue = getter(a);
|
329 | const bValue = getter(b);
|
330 | if (aValue !== undefined && aValue !== null) {
|
331 | if (bValue !== undefined && bValue !== null) {
|
332 | return comparator(aValue, bValue);
|
333 | }
|
334 | return -1;
|
335 | } else {
|
336 | if (bValue !== undefined && bValue !== null) {
|
337 | return 1;
|
338 | }
|
339 | return 0;
|
340 | }
|
341 | };
|
342 | compareSelectCache.set(getter, comparator, result);
|
343 | return result;
|
344 | };
|
345 | exports.compareSelect = compareSelect;
|
346 |
|
347 |
|
348 | const compareIteratorsCache = new WeakMap();
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 | const compareIterables = elementComparator => {
|
356 | const cacheEntry = compareIteratorsCache.get(elementComparator);
|
357 | if (cacheEntry !== undefined) return cacheEntry;
|
358 | |
359 |
|
360 |
|
361 |
|
362 |
|
363 | const result = (a, b) => {
|
364 | const aI = a[Symbol.iterator]();
|
365 | const bI = b[Symbol.iterator]();
|
366 |
|
367 | while (true) {
|
368 | const aItem = aI.next();
|
369 | const bItem = bI.next();
|
370 | if (aItem.done) {
|
371 | return bItem.done ? 0 : -1;
|
372 | } else if (bItem.done) {
|
373 | return 1;
|
374 | }
|
375 | const res = elementComparator(aItem.value, bItem.value);
|
376 | if (res !== 0) return res;
|
377 | }
|
378 | };
|
379 | compareIteratorsCache.set(elementComparator, result);
|
380 | return result;
|
381 | };
|
382 | exports.compareIterables = compareIterables;
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 | exports.keepOriginalOrder = iterable => {
|
392 |
|
393 | const map = new Map();
|
394 | let i = 0;
|
395 | for (const item of iterable) {
|
396 | map.set(item, i++);
|
397 | }
|
398 | return (a, b) => compareNumbers(map.get(a), map.get(b));
|
399 | };
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 | exports.compareChunksNatural = chunkGraph => {
|
406 | const cmpFn = exports.compareModulesById(chunkGraph);
|
407 | const cmpIterableFn = compareIterables(cmpFn);
|
408 | return concatComparators(
|
409 | compareSelect(chunk => chunk.name, compareIds),
|
410 | compareSelect(chunk => chunk.runtime, compareRuntime),
|
411 | compareSelect(
|
412 | |
413 |
|
414 |
|
415 |
|
416 | chunk => chunkGraph.getOrderedChunkModulesIterable(chunk, cmpFn),
|
417 | cmpIterableFn
|
418 | )
|
419 | );
|
420 | };
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 | exports.compareLocations = (a, b) => {
|
429 | let isObjectA = typeof a === "object" && a !== null;
|
430 | let isObjectB = typeof b === "object" && b !== null;
|
431 | if (!isObjectA || !isObjectB) {
|
432 | if (isObjectA) return 1;
|
433 | if (isObjectB) return -1;
|
434 | return 0;
|
435 | }
|
436 | if ("start" in a) {
|
437 | if ("start" in b) {
|
438 | const ap = a.start;
|
439 | const bp = b.start;
|
440 | if (ap.line < bp.line) return -1;
|
441 | if (ap.line > bp.line) return 1;
|
442 | if (ap.column < bp.column) return -1;
|
443 | if (ap.column > bp.column) return 1;
|
444 | } else return -1;
|
445 | } else if ("start" in b) return 1;
|
446 | if ("name" in a) {
|
447 | if ("name" in b) {
|
448 | if (a.name < b.name) return -1;
|
449 | if (a.name > b.name) return 1;
|
450 | } else return -1;
|
451 | } else if ("name" in b) return 1;
|
452 | if ("index" in a) {
|
453 | if ("index" in b) {
|
454 | if (a.index < b.index) return -1;
|
455 | if (a.index > b.index) return 1;
|
456 | } else return -1;
|
457 | } else if ("index" in b) return 1;
|
458 | return 0;
|
459 | };
|