@atlaspack/query 2.14.5-canary.36 → 2.14.5-canary.360

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js ADDED
@@ -0,0 +1,911 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.run = run;
7
+ const path_1 = __importDefault(require("path"));
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const repl_1 = __importDefault(require("repl"));
10
+ const os_1 = __importDefault(require("os"));
11
+ const nullthrows_1 = __importDefault(require("nullthrows"));
12
+ const assert_1 = __importDefault(require("assert"));
13
+ const v8_1 = require("v8");
14
+ const table_1 = require("table");
15
+ const index_1 = require("./index");
16
+ const { BundleGraph: { bundleGraphEdgeTypes: bundleGraphEdgeTypes }, Priority, fromProjectPathRelative, } = require('./deep-imports');
17
+ async function run(input) {
18
+ let args = input;
19
+ let cacheDir = path_1.default.join(process.cwd(), '.parcel-cache');
20
+ if (args[0] === '--cache') {
21
+ cacheDir = path_1.default.resolve(process.cwd(), args[1]);
22
+ args = args.slice(2);
23
+ }
24
+ let initialCmd = args[0];
25
+ try {
26
+ fs_1.default.accessSync(cacheDir);
27
+ }
28
+ catch (e) {
29
+ console.error("Can't find cache dir", cacheDir);
30
+ process.exit(1);
31
+ }
32
+ console.log('Loading graphs...');
33
+ let { assetGraph, bundleGraph, bundleInfo, requestTracker, cacheInfo } = await (0, index_1.loadGraphs)(cacheDir);
34
+ function hasRequestTracker() {
35
+ if (requestTracker == null) {
36
+ console.error('Request Graph could not be found');
37
+ return false;
38
+ }
39
+ return true;
40
+ }
41
+ function hasBundleGraph() {
42
+ if (bundleGraph == null) {
43
+ console.error('Bundle Graph could not be found');
44
+ return false;
45
+ }
46
+ return true;
47
+ }
48
+ function hasAssetGraph() {
49
+ if (assetGraph == null) {
50
+ console.error('Asset Graph could not be found');
51
+ return false;
52
+ }
53
+ return true;
54
+ }
55
+ function hasBundleInfo() {
56
+ if (bundleInfo == null) {
57
+ console.error('Bundle Info could not be found');
58
+ return false;
59
+ }
60
+ return true;
61
+ }
62
+ // -------------------------------------------------------
63
+ function getBundleFilePath(id) {
64
+ if (!hasBundleInfo()) {
65
+ return;
66
+ }
67
+ (0, assert_1.default)(bundleInfo != null);
68
+ return fromProjectPathRelative((0, nullthrows_1.default)(bundleInfo.get(id)?.filePath));
69
+ }
70
+ function parseAssetLocator(v) {
71
+ let id = null;
72
+ if (v.length === 16) {
73
+ id = v;
74
+ }
75
+ else {
76
+ if (!hasBundleGraph()) {
77
+ return;
78
+ }
79
+ (0, assert_1.default)(bundleGraph != null);
80
+ for (let [assetId, publicId] of bundleGraph._publicIdByAssetId) {
81
+ if (publicId === v) {
82
+ id = assetId;
83
+ break;
84
+ }
85
+ }
86
+ }
87
+ if (id == null && v.length > 0) {
88
+ if (!hasAssetGraph()) {
89
+ return;
90
+ }
91
+ (0, assert_1.default)(assetGraph != null);
92
+ let assetRegex = new RegExp(v);
93
+ for (let node of assetGraph.nodes.values()) {
94
+ if (node?.type === 'asset' &&
95
+ assetRegex.test(fromProjectPathRelative(node.value.filePath))) {
96
+ id = node.id;
97
+ break;
98
+ }
99
+ }
100
+ }
101
+ return id;
102
+ }
103
+ function parseBundleLocator(v) {
104
+ if (!hasBundleGraph()) {
105
+ return;
106
+ }
107
+ (0, assert_1.default)(bundleGraph != null);
108
+ let bundleRegex = new RegExp(v);
109
+ for (let b of bundleGraph.getBundles()) {
110
+ let bundleFilePath = getBundleFilePath(b.id);
111
+ if ((bundleFilePath !== undefined && bundleRegex.test(bundleFilePath)) ||
112
+ b.id === v) {
113
+ return b.id;
114
+ }
115
+ }
116
+ }
117
+ function getAsset(v) {
118
+ let id = parseAssetLocator(v);
119
+ if (id == null) {
120
+ console.log(null);
121
+ }
122
+ else {
123
+ try {
124
+ if (!hasBundleGraph()) {
125
+ return;
126
+ }
127
+ (0, assert_1.default)(bundleGraph != null);
128
+ let asset = bundleGraph.getAssetById(id);
129
+ console.log('Public id', bundleGraph.getAssetPublicId(asset));
130
+ console.log(asset);
131
+ }
132
+ catch (e) {
133
+ if (!hasAssetGraph()) {
134
+ return;
135
+ }
136
+ (0, assert_1.default)(assetGraph != null);
137
+ let node = (0, nullthrows_1.default)(assetGraph.getNodeByContentKey(id));
138
+ (0, assert_1.default)(node.type === 'asset');
139
+ console.log(node.value);
140
+ }
141
+ }
142
+ }
143
+ function _findAssetNode(v) {
144
+ if (!hasAssetGraph()) {
145
+ return;
146
+ }
147
+ (0, assert_1.default)(assetGraph != null);
148
+ let assetRegex = new RegExp(v);
149
+ for (let node of assetGraph.nodes.values()) {
150
+ if (node?.type === 'asset' &&
151
+ assetRegex.test(fromProjectPathRelative(node.value.filePath))) {
152
+ return node;
153
+ }
154
+ }
155
+ }
156
+ function findAsset(v) {
157
+ let node = _findAssetNode(v);
158
+ if (node) {
159
+ try {
160
+ if (!hasBundleGraph()) {
161
+ return;
162
+ }
163
+ (0, assert_1.default)(bundleGraph != null);
164
+ console.log(`${bundleGraph.getAssetPublicId(bundleGraph.getAssetById(node.id))} ${fromProjectPathRelative(node.value.filePath)}`);
165
+ }
166
+ catch (e) {
167
+ console.log(fromProjectPathRelative(node.value.filePath));
168
+ }
169
+ }
170
+ }
171
+ function findAssetWithSymbol(local) {
172
+ if (!hasBundleGraph() || !hasAssetGraph()) {
173
+ return;
174
+ }
175
+ (0, assert_1.default)(bundleGraph != null);
176
+ (0, assert_1.default)(assetGraph != null);
177
+ let [, assetId, binding, ref] = (0, nullthrows_1.default)(local.match(/^\$([^$]+)\$([^$]+)\$(.*)$/), `symbol ${local} could not be resolved`);
178
+ let asset;
179
+ // Search against the id used by the JSTransformer and ScopeHoistingPackager,
180
+ // not the final asset id, as it may have changed with further transformation.
181
+ for (let node of assetGraph.nodes.values()) {
182
+ if (node?.type === 'asset' && node.value.meta.id === assetId) {
183
+ asset = node;
184
+ break;
185
+ }
186
+ }
187
+ // If the asset couldn't be found by searching for the id,
188
+ // search for the local name in asset used symbols.
189
+ if (asset == null) {
190
+ outer: for (let node of assetGraph.nodes.values()) {
191
+ if (node?.type === 'asset' && node.value.symbols) {
192
+ for (let symbol of node.value.symbols.values()) {
193
+ if (symbol.local === local) {
194
+ asset = node;
195
+ break outer;
196
+ }
197
+ }
198
+ }
199
+ }
200
+ }
201
+ (0, assert_1.default)(asset, `An asset for ${assetId} could not be found`);
202
+ (0, assert_1.default)(asset.type === 'asset', `Expected ${assetId} to be an asset, but found a ${asset.type}`);
203
+ try {
204
+ console.log(`${bundleGraph.getAssetPublicId(bundleGraph.getAssetById(asset.id))} ${fromProjectPathRelative(asset.value.filePath)}`);
205
+ }
206
+ catch (e) {
207
+ console.log(fromProjectPathRelative(asset.value.filePath));
208
+ }
209
+ if (binding === 'export' && asset.value.symbols) {
210
+ for (let [symbolName, symbol] of asset.value.symbols) {
211
+ if (symbol.local === local) {
212
+ if (symbol.loc) {
213
+ let locPath = symbol.loc.filePath;
214
+ let locAsset = _findAssetNode(String(locPath));
215
+ if (locAsset != null) {
216
+ try {
217
+ console.log(`${bundleGraph.getAssetPublicId(bundleGraph.getAssetById(locAsset.id))} ${fromProjectPathRelative(locAsset.value.filePath)}`);
218
+ }
219
+ catch (e) {
220
+ console.log(`imported as ${symbolName} from ${fromProjectPathRelative(locAsset.value.filePath)}`);
221
+ }
222
+ }
223
+ else {
224
+ console.log(`imported as ${symbolName} from ${fromProjectPathRelative(locPath)}`);
225
+ }
226
+ }
227
+ else {
228
+ console.log(`imported as ${symbolName}`);
229
+ }
230
+ }
231
+ }
232
+ }
233
+ else if (ref) {
234
+ console.log(`possibly defined as ${ref}`);
235
+ }
236
+ }
237
+ function getNodeAssetGraph(v) {
238
+ if (!hasAssetGraph()) {
239
+ return;
240
+ }
241
+ (0, assert_1.default)(assetGraph != null);
242
+ console.log(assetGraph.getNodeByContentKey(v));
243
+ }
244
+ function getNodeBundleGraph(v) {
245
+ if (!hasBundleGraph()) {
246
+ return;
247
+ }
248
+ (0, assert_1.default)(bundleGraph != null);
249
+ console.log(bundleGraph._graph.getNodeByContentKey(v));
250
+ }
251
+ class Paths {
252
+ constructor(value, label = '-', suffix = '') {
253
+ this.children = [];
254
+ this.value = value;
255
+ this.label = label;
256
+ this.suffix = suffix;
257
+ }
258
+ add(v, label, suffix) {
259
+ let next = new Paths(v, label, suffix);
260
+ this.children.push(next);
261
+ return next;
262
+ }
263
+ print(format, prefix = '') {
264
+ console.log(`${prefix}${this.label} ${format(this.value)} ${this.suffix}`);
265
+ for (let i = 0; i < this.children.length; i++) {
266
+ this.children[i].print(format, prefix + ' ');
267
+ }
268
+ }
269
+ }
270
+ function _findEntries(graph, v) {
271
+ let asset = (0, nullthrows_1.default)(parseAssetLocator(v), 'Asset not found');
272
+ let paths = new Paths(graph.getNodeIdByContentKey(asset), ' ');
273
+ let cb = (id, ctx, revisiting) => {
274
+ let { paths, lazyOutgoing } = ctx;
275
+ let node = (0, nullthrows_1.default)(graph.getNode(id));
276
+ if (node.id === asset)
277
+ return ctx;
278
+ if (node.type === 'asset') {
279
+ paths = paths.add(id, lazyOutgoing ? '<' : undefined, revisiting ? '(revisiting)' : undefined);
280
+ lazyOutgoing = false;
281
+ }
282
+ else if (node.type === 'dependency') {
283
+ if (node.value.priority === Priority.lazy) {
284
+ lazyOutgoing = true;
285
+ }
286
+ }
287
+ return { paths, lazyOutgoing };
288
+ };
289
+ // like graph.dfs, but revisiting nodes and skipping its children
290
+ let seen = new Set();
291
+ function walk(id, ctx) {
292
+ let revisiting = seen.has(id);
293
+ let newCtx = cb(id, ctx, revisiting);
294
+ if (revisiting)
295
+ return;
296
+ seen.add(id);
297
+ for (let parent of graph.getNodeIdsConnectedTo(id)) {
298
+ walk(parent, newCtx);
299
+ }
300
+ }
301
+ walk(graph.getNodeIdByContentKey(asset), { paths, lazyOutgoing: false });
302
+ paths.print((id) => {
303
+ let node = (0, nullthrows_1.default)(graph.getNode(id));
304
+ (0, assert_1.default)(node.type === 'asset');
305
+ return fromProjectPathRelative(node.value.filePath);
306
+ });
307
+ }
308
+ function findEntriesAssetGraph(v) {
309
+ if (!hasAssetGraph()) {
310
+ return;
311
+ }
312
+ (0, assert_1.default)(assetGraph != null);
313
+ _findEntries(assetGraph, v);
314
+ }
315
+ function findEntriesBundleGraph(v) {
316
+ if (!hasBundleGraph()) {
317
+ return;
318
+ }
319
+ (0, assert_1.default)(bundleGraph != null);
320
+ _findEntries(bundleGraph._graph, v);
321
+ }
322
+ function findEntries(v) {
323
+ findEntriesBundleGraph(v);
324
+ }
325
+ function getBundlesWithAsset(v) {
326
+ if (!hasBundleGraph()) {
327
+ return;
328
+ }
329
+ (0, assert_1.default)(bundleGraph != null);
330
+ let asset = (0, nullthrows_1.default)(parseAssetLocator(v), 'Asset not found');
331
+ for (let b of bundleGraph.getBundlesWithAsset(bundleGraph.getAssetById(asset))) {
332
+ let bundleFilePath = getBundleFilePath(b.id);
333
+ if (bundleFilePath !== undefined) {
334
+ console.log(`${b.id} ${bundleFilePath} ${b.mainEntryId != null ? `(main: ${b.mainEntryId})` : ''}`);
335
+ }
336
+ }
337
+ }
338
+ function getBundlesWithDependency(v) {
339
+ if (!hasBundleGraph()) {
340
+ return;
341
+ }
342
+ (0, assert_1.default)(bundleGraph != null);
343
+ let node = (0, nullthrows_1.default)(bundleGraph._graph.getNodeByContentKey(v));
344
+ (0, assert_1.default)(node.type === 'dependency');
345
+ for (let b of bundleGraph.getBundlesWithDependency(node.value)) {
346
+ let bundleFilePath = getBundleFilePath(b.id);
347
+ if (bundleFilePath !== undefined) {
348
+ console.log(`${b.id} ${bundleFilePath} ${b.mainEntryId != null ? `(main: ${b.mainEntryId})` : ''}`);
349
+ }
350
+ }
351
+ }
352
+ // eslint-disable-next-line no-unused-vars
353
+ function getBundles(_) {
354
+ if (!hasBundleGraph()) {
355
+ return;
356
+ }
357
+ (0, assert_1.default)(bundleGraph != null);
358
+ for (let b of bundleGraph.getBundles()) {
359
+ let bundleFilePath = getBundleFilePath(b.id);
360
+ if (bundleFilePath !== undefined) {
361
+ console.log(`${b.id} ${bundleFilePath} ${b.mainEntryId != null ? `(main: ${b.mainEntryId})` : ''}`);
362
+ }
363
+ }
364
+ }
365
+ function getReferencingBundles(v) {
366
+ if (!hasBundleGraph()) {
367
+ return;
368
+ }
369
+ (0, assert_1.default)(bundleGraph != null);
370
+ let bundleId = (0, nullthrows_1.default)(parseBundleLocator(v), 'Bundle not found');
371
+ let bundleNodeId = bundleGraph._graph.getNodeIdByContentKey(bundleId);
372
+ let bundleNode = (0, nullthrows_1.default)(bundleGraph._graph.getNode(bundleNodeId), 'Bundle not found');
373
+ (0, assert_1.default)(bundleNode.type === 'bundle', 'Not a bundle');
374
+ for (let b of bundleGraph.getReferencingBundles(bundleNode.value)) {
375
+ let bundleFilePath = getBundleFilePath(b.id);
376
+ if (bundleFilePath !== undefined) {
377
+ console.log(`${b.id} ${bundleFilePath} ${b.mainEntryId != null ? `(main: ${b.mainEntryId})` : ''}`);
378
+ }
379
+ }
380
+ }
381
+ function getIncomingDependenciesAssetGraph(v) {
382
+ if (!hasAssetGraph()) {
383
+ return;
384
+ }
385
+ (0, assert_1.default)(assetGraph != null);
386
+ let asset = (0, nullthrows_1.default)(parseAssetLocator(v), 'Asset not found');
387
+ let node = (0, nullthrows_1.default)(assetGraph.getNodeByContentKey(asset));
388
+ (0, assert_1.default)(node.type === 'asset');
389
+ console.log(assetGraph.getIncomingDependencies(node.value));
390
+ }
391
+ function getIncomingDependenciesBundleGraph(v) {
392
+ if (!hasBundleGraph()) {
393
+ return;
394
+ }
395
+ (0, assert_1.default)(bundleGraph != null);
396
+ let asset = (0, nullthrows_1.default)(parseAssetLocator(v), 'Asset not found');
397
+ let value = (0, nullthrows_1.default)(bundleGraph.getAssetById(asset));
398
+ console.log(bundleGraph.getIncomingDependencies(value));
399
+ }
400
+ function getIncomingDependencies(v) {
401
+ getIncomingDependenciesBundleGraph(v);
402
+ }
403
+ function getResolvedAsset(v) {
404
+ if (!hasBundleGraph()) {
405
+ return;
406
+ }
407
+ (0, assert_1.default)(bundleGraph != null);
408
+ let node = (0, nullthrows_1.default)(bundleGraph._graph.getNodeByContentKey(v), 'Dependency not found');
409
+ (0, assert_1.default)(node.type === 'dependency', 'Node is not a dependency, but a ' + node.type);
410
+ console.log(bundleGraph.getResolvedAsset(node.value));
411
+ }
412
+ function getAssetWithDependency(v) {
413
+ if (!hasBundleGraph()) {
414
+ return;
415
+ }
416
+ (0, assert_1.default)(bundleGraph != null);
417
+ let node = (0, nullthrows_1.default)(bundleGraph._graph.getNodeByContentKey(v), 'Dependency not found');
418
+ (0, assert_1.default)(node.type === 'dependency', 'Node is not a dependency, but a ' + node.type);
419
+ console.log(bundleGraph.getAssetWithDependency(node.value));
420
+ }
421
+ function traverseAssets(v) {
422
+ if (!hasBundleGraph()) {
423
+ return;
424
+ }
425
+ (0, assert_1.default)(bundleGraph != null);
426
+ let bundleId = (0, nullthrows_1.default)(parseBundleLocator(v), 'Bundle not found');
427
+ let node = (0, nullthrows_1.default)(bundleGraph._graph.getNodeByContentKey(bundleId), 'Bundle not found');
428
+ (0, assert_1.default)(node.type === 'bundle', 'Node is not a bundle, but a ' + node.type);
429
+ // @ts-expect-error TS7006
430
+ bundleGraph.traverseAssets(node.value, (asset) => {
431
+ console.log(asset.id, asset.filePath);
432
+ });
433
+ }
434
+ function traverseBundle(v) {
435
+ if (!hasBundleGraph()) {
436
+ return;
437
+ }
438
+ (0, assert_1.default)(bundleGraph != null);
439
+ let bundleId = (0, nullthrows_1.default)(parseBundleLocator(v), 'Bundle not found');
440
+ let node = (0, nullthrows_1.default)(bundleGraph._graph.getNodeByContentKey(bundleId), 'Bundle not found');
441
+ (0, assert_1.default)(node.type === 'bundle', 'Node is not a bundle, but a ' + node.type);
442
+ // @ts-expect-error TS7006
443
+ bundleGraph.traverseBundle(node.value, (node) => {
444
+ if (node.type === 'asset') {
445
+ console.log(node.id, node.value.filePath);
446
+ }
447
+ else {
448
+ console.log(node.id, node.value.sourcePath, '->', node.value.specifier, node.value.symbols
449
+ ? `(${[...node.value.symbols.keys()].join(',')})`
450
+ : '', node.excluded ? `- excluded` : '');
451
+ }
452
+ });
453
+ }
454
+ function getBundle(v) {
455
+ if (!hasBundleGraph()) {
456
+ return;
457
+ }
458
+ (0, assert_1.default)(bundleGraph != null);
459
+ let bundleRegex = new RegExp(v);
460
+ for (let b of bundleGraph.getBundles()) {
461
+ let bundleFilePath = getBundleFilePath(b.id);
462
+ if ((bundleFilePath !== undefined && bundleRegex.test(bundleFilePath)) ||
463
+ b.id === v) {
464
+ console.log(getBundleFilePath(b.id), b);
465
+ }
466
+ }
467
+ }
468
+ function findBundleReason(bundle, asset) {
469
+ if (!hasBundleGraph()) {
470
+ return;
471
+ }
472
+ (0, assert_1.default)(bundleGraph != null);
473
+ let bundleId = (0, nullthrows_1.default)(parseBundleLocator(bundle), 'Bundle not found');
474
+ let bundleNodeId = bundleGraph._graph.getNodeIdByContentKey(bundleId);
475
+ let bundleNode = (0, nullthrows_1.default)(bundleGraph._graph.getNode(bundleNodeId), 'Bundle not found');
476
+ (0, assert_1.default)(bundleNode.type === 'bundle', 'Not a bundle');
477
+ let assetId = (0, nullthrows_1.default)(parseAssetLocator(asset), 'Asset not found');
478
+ let assetNodeId = bundleGraph._graph.getNodeIdByContentKey(assetId);
479
+ let assetNode = (0, nullthrows_1.default)(bundleGraph._graph.getNode(assetNodeId), 'Asset not found');
480
+ (0, assert_1.default)(assetNode.type === 'asset', 'Not an asset');
481
+ (0, assert_1.default)(bundleGraph._graph.hasEdge(bundleNodeId, assetNodeId, bundleGraphEdgeTypes.contains), 'Asset is not part of the bundle');
482
+ console.log('# Asset is main entry of bundle:', bundleNode.value.mainEntryId === assetId);
483
+ console.log('# Asset is an entry of bundle:', bundleNode.value.entryAssetIds.includes(assetId));
484
+ console.log('# Incoming dependencies contained in the bundle:');
485
+ for (let incoming of bundleGraph._graph.getNodeIdsConnectedTo(assetNodeId)) {
486
+ if (bundleGraph._graph.hasEdge(bundleNodeId, incoming, bundleGraphEdgeTypes.contains)) {
487
+ console.log(bundleGraph._graph.getNode(incoming));
488
+ }
489
+ }
490
+ console.log('# Incoming dependencies contained in referencing bundles (using this bundle as a shared bundle)');
491
+ let referencingBundles = bundleGraph.getReferencingBundles(bundleNode.value);
492
+ for (let incoming of bundleGraph._graph.getNodeIdsConnectedTo(assetNodeId)) {
493
+ if (
494
+ // @ts-expect-error TS7006
495
+ referencingBundles.some((ref) => bundleGraph._graph.hasEdge(bundleGraph._graph.getNodeIdByContentKey(ref.id), incoming, bundleGraphEdgeTypes.contains))) {
496
+ console.log(bundleGraph._graph.getNode(incoming));
497
+ }
498
+ }
499
+ }
500
+ function _getIncomingNodeOfType(
501
+ // @ts-expect-error TS2304
502
+ bundleGraph,
503
+ // @ts-expect-error TS7006
504
+ node, type) {
505
+ if (!hasBundleGraph()) {
506
+ return;
507
+ }
508
+ (0, assert_1.default)(bundleGraph != null);
509
+ const bundleGraphNodeId = bundleGraph._graph.getNodeIdByContentKey(node.id);
510
+ return (bundleGraph._graph
511
+ .getNodeIdsConnectedTo(bundleGraphNodeId, -1)
512
+ // @ts-expect-error TS7006
513
+ .map((id) => (0, nullthrows_1.default)(bundleGraph._graph.getNode(id)))
514
+ // @ts-expect-error TS7006
515
+ .find((node) => node.type == type));
516
+ }
517
+ // We find the priority of a Bundle or BundleGroup by looking at its incoming dependencies.
518
+ // If a Bundle does not have an incoming dependency, we look for an incoming BundleGroup and its dependency
519
+ // e.g. Dep(priority = 1) -> BundleGroup -> Bundle means that the Bundle has priority 1.
520
+ // @ts-expect-error TS2304
521
+ function _getBundlePriority(bundleGraph, bundle) {
522
+ if (!hasBundleGraph()) {
523
+ return;
524
+ }
525
+ (0, assert_1.default)(bundleGraph != null);
526
+ let node = _getIncomingNodeOfType(bundleGraph, bundle, 'dependency');
527
+ if (node == null) {
528
+ node = _getIncomingNodeOfType(bundleGraph, bundle, 'bundle_group');
529
+ if (node == null)
530
+ return null;
531
+ node = _getIncomingNodeOfType(bundleGraph, node, 'dependency');
532
+ }
533
+ if (node == null)
534
+ return null;
535
+ (0, assert_1.default)(node.type === 'dependency', 'Not a dependency');
536
+ return node.value.priority;
537
+ }
538
+ // @ts-expect-error TS2304
539
+ function _findEntryBundle(bundleGraph, node) {
540
+ if (!hasBundleGraph()) {
541
+ return;
542
+ }
543
+ (0, assert_1.default)(bundleGraph != null);
544
+ const bundleGraphNodeId = bundleGraph._graph.getNodeIdByContentKey(node.id);
545
+ const entryBundleGroup = bundleGraph._graph
546
+ .getNodeIdsConnectedTo(bundleGraphNodeId, -1)
547
+ // @ts-expect-error TS7006
548
+ .map((id) => (0, nullthrows_1.default)(bundleGraph._graph.getNode(id)))
549
+ .find(
550
+ // @ts-expect-error TS7006
551
+ (node) => node.type === 'bundle_group' &&
552
+ bundleGraph.isEntryBundleGroup(node.value));
553
+ return entryBundleGroup;
554
+ }
555
+ // eslint-disable-next-line no-unused-vars
556
+ function inspectCache(_) {
557
+ // displays sizing of various entries of the cache
558
+ let table = [];
559
+ table.push([
560
+ 'Graphs',
561
+ 'Size (bytes)',
562
+ 'Deserialize (ms)',
563
+ 'Serialize (ms)',
564
+ ]);
565
+ let serialized = new Map();
566
+ serialized.set('RequestGraph', timeSerialize(requestTracker));
567
+ serialized.set('BundleGraph', timeSerialize(bundleGraph));
568
+ serialized.set('AssetGraph', timeSerialize(assetGraph));
569
+ for (let [name, info] of (0, nullthrows_1.default)(cacheInfo).entries()) {
570
+ if ((name === 'RequestGraph' && !hasRequestTracker()) ||
571
+ (name === 'BundleGraph' && !hasBundleGraph()) ||
572
+ (name === 'AssetGraph' && !hasAssetGraph())) {
573
+ continue;
574
+ }
575
+ let s = serialized.get(name);
576
+ (0, assert_1.default)(s != null);
577
+ table.push([name, ...info, s]);
578
+ }
579
+ function getColumnSum(t, col) {
580
+ if (t == null) {
581
+ return '';
582
+ }
583
+ const initialValue = 0;
584
+ let column = t.map((r) => r[col]);
585
+ column.shift();
586
+ (0, assert_1.default)(column != null);
587
+ return column.reduce(
588
+ // @ts-expect-error TS2365
589
+ (accumulator, currentValue) => accumulator + currentValue, initialValue);
590
+ }
591
+ table.push([
592
+ 'Totals',
593
+ getColumnSum(table, 1),
594
+ getColumnSum(table, 2),
595
+ getColumnSum(table, 3),
596
+ ]);
597
+ _printStatsTable('Cache Info', table);
598
+ }
599
+ function timeSerialize(
600
+ // @ts-expect-error TS2304
601
+ graph) {
602
+ let date = Date.now();
603
+ (0, v8_1.serialize)(graph);
604
+ date = Date.now() - date;
605
+ return date;
606
+ }
607
+ function _printStatsTable(header, data) {
608
+ const config = {
609
+ columnDefault: {
610
+ width: 18,
611
+ },
612
+ header: {
613
+ alignment: 'center',
614
+ content: header,
615
+ },
616
+ };
617
+ console.log((0, table_1.table)(data, config));
618
+ }
619
+ // eslint-disable-next-line no-unused-vars
620
+ function stats(_) {
621
+ let ag = {
622
+ asset: 0,
623
+ dependency: 0,
624
+ asset_group: 0,
625
+ };
626
+ if (hasAssetGraph()) {
627
+ (0, assert_1.default)(assetGraph != null);
628
+ for (let n of assetGraph.nodes) {
629
+ if (n && n.type in ag) {
630
+ // @ts-expect-error TS7053
631
+ ag[n.type]++;
632
+ }
633
+ }
634
+ _printStatsTable('# Asset Graph Node Counts', Object.entries(ag));
635
+ }
636
+ if (!hasBundleGraph()) {
637
+ return;
638
+ }
639
+ (0, assert_1.default)(bundleGraph != null);
640
+ let bg = {
641
+ dependency: 0,
642
+ bundle: 0,
643
+ bundle_group: 0,
644
+ asset_node_modules: 0,
645
+ asset_source: 0,
646
+ };
647
+ let b_type = {
648
+ entry: 0,
649
+ shared: 0,
650
+ async: 0,
651
+ parallel: 0,
652
+ sync: 0,
653
+ };
654
+ let b_ext = {};
655
+ const entries = new Set();
656
+ for (let n of bundleGraph._graph.nodes) {
657
+ if (n?.type === 'bundle_group') {
658
+ bg.bundle_group++;
659
+ }
660
+ else if (n?.type === 'bundle') {
661
+ bg.bundle++;
662
+ b_ext[n.value.type] = (b_ext[n.value.type] || 0) + 1;
663
+ const entry_group = _findEntryBundle(bundleGraph, n);
664
+ if (entry_group != null && !entries.has(entry_group.id)) {
665
+ b_type.entry++;
666
+ entries.add(entry_group.id);
667
+ }
668
+ else if (n.value.mainEntryId == null) {
669
+ // In general, !bundle.mainEntryId means that it is shared. In the case of an async and shared bundle, only count it as shared.
670
+ b_type.shared++;
671
+ }
672
+ else {
673
+ const priority = _getBundlePriority(bundleGraph, n);
674
+ if (priority == Priority.lazy) {
675
+ b_type.async++;
676
+ }
677
+ else if (priority == Priority.parallel) {
678
+ b_type.parallel++;
679
+ }
680
+ else if (priority == Priority.sync) {
681
+ b_type.sync++;
682
+ }
683
+ }
684
+ }
685
+ else if (n?.type === 'asset') {
686
+ if (fromProjectPathRelative(n.value.filePath).includes('node_modules')) {
687
+ bg.asset_node_modules++;
688
+ }
689
+ else {
690
+ bg.asset_source++;
691
+ }
692
+ }
693
+ else if (n?.type === 'dependency') {
694
+ bg.dependency++;
695
+ }
696
+ }
697
+ _printStatsTable('# Bundle Graph Node Counts', Object.entries(bg));
698
+ _printStatsTable('# Bundles By Type', Object.entries(b_type));
699
+ _printStatsTable('# Bundles By Extension', Object.entries(b_ext));
700
+ // Assert that counts for each breakdown are correct
701
+ let sum_b_ext = 0;
702
+ for (let k in b_ext) {
703
+ sum_b_ext += b_ext[k];
704
+ }
705
+ let sum_b_type = 0;
706
+ for (let k in b_type) {
707
+ // @ts-expect-error TS7053
708
+ sum_b_type += b_type[k];
709
+ }
710
+ (0, assert_1.default)(bg.bundle == sum_b_type, `Bundles by type ${sum_b_type} does not equal total ${bg.bundle}`);
711
+ (0, assert_1.default)(bg.bundle == sum_b_ext, `Bundles by extension ${sum_b_ext} does not equal total ${bg.bundle}`);
712
+ }
713
+ // -------------------------------------------------------
714
+ if (initialCmd != null) {
715
+ (async () => {
716
+ await eval(initialCmd);
717
+ process.exit(0);
718
+ })();
719
+ }
720
+ else {
721
+ console.log('See .help. The graphs can be accessed via `assetGraph`, `bundleGraph` and `requestTracker`.');
722
+ process.on('uncaughtException', function (err) {
723
+ console.error(err);
724
+ server.displayPrompt();
725
+ });
726
+ const server = repl_1.default.start({ useColors: true, useGlobal: true });
727
+ server.setupHistory(path_1.default.join(os_1.default.homedir(), '.parcel_query_history'), () => { });
728
+ server.context.bundleGraph = bundleGraph;
729
+ server.context.assetGraph = assetGraph;
730
+ server.context.requestTracker = requestTracker;
731
+ server.context.cacheInfo = cacheInfo;
732
+ for (let [name, cmd] of new Map([
733
+ [
734
+ 'getAsset',
735
+ {
736
+ help: 'args: <id | public id | filepath>',
737
+ action: getAsset,
738
+ },
739
+ ],
740
+ [
741
+ 'getNodeAssetGraph',
742
+ {
743
+ help: 'args: <content key>. Find node by content key in the asset graph',
744
+ action: getNodeAssetGraph,
745
+ },
746
+ ],
747
+ [
748
+ 'getNodeBundleGraph',
749
+ {
750
+ help: 'args: <content key>. Find node by content key in the bundle graph',
751
+ action: getNodeBundleGraph,
752
+ },
753
+ ],
754
+ [
755
+ 'findEntriesAssetGraph',
756
+ {
757
+ help: 'args: <id | public id | filepath>. List paths from an asset to entry points (in asset graph)',
758
+ action: findEntriesAssetGraph,
759
+ },
760
+ ],
761
+ [
762
+ 'findEntriesBundleGraph',
763
+ {
764
+ help: 'args: <id | public id | filepath>. List paths from an asset to entry points (in bundle graph)',
765
+ action: findEntriesBundleGraph,
766
+ },
767
+ ],
768
+ [
769
+ 'findEntries',
770
+ {
771
+ help: '= findEntriesBundleGraph',
772
+ action: findEntries,
773
+ },
774
+ ],
775
+ [
776
+ 'getBundlesWithAsset',
777
+ {
778
+ help: 'args: <id | public id | filepath>. Gets bundles containing the asset',
779
+ action: getBundlesWithAsset,
780
+ },
781
+ ],
782
+ [
783
+ 'getBundlesWithDependency',
784
+ {
785
+ help: 'args: <id>. Gets bundles containing the dependency',
786
+ action: getBundlesWithDependency,
787
+ },
788
+ ],
789
+ [
790
+ 'getIncomingDependenciesAssetGraph',
791
+ {
792
+ help: 'args: <asset: id | public id | filepath regex>',
793
+ action: getIncomingDependenciesAssetGraph,
794
+ },
795
+ ],
796
+ [
797
+ 'getIncomingDependenciesBundleGraph',
798
+ {
799
+ help: 'args: <asset: id | public id | filepath regex>',
800
+ action: getIncomingDependenciesBundleGraph,
801
+ },
802
+ ],
803
+ [
804
+ 'getIncomingDependencies',
805
+ {
806
+ help: '= getIncomingDependenciesBundleGraph',
807
+ action: getIncomingDependencies,
808
+ },
809
+ ],
810
+ [
811
+ 'getResolvedAsset',
812
+ {
813
+ help: 'args: <dependency id>. Resolve the dependency',
814
+ action: getResolvedAsset,
815
+ },
816
+ ],
817
+ [
818
+ 'getAssetWithDependency',
819
+ {
820
+ help: 'args: <dependency id>. Show which asset created the dependency',
821
+ action: getAssetWithDependency,
822
+ },
823
+ ],
824
+ [
825
+ 'traverseAssets',
826
+ {
827
+ help: 'args: <bundle id>. List assets in bundle',
828
+ action: traverseAssets,
829
+ },
830
+ ],
831
+ [
832
+ 'traverseBundle',
833
+ {
834
+ help: 'args: <bundle id>. List assets and dependencies in bundle',
835
+ action: traverseBundle,
836
+ },
837
+ ],
838
+ [
839
+ 'getBundle',
840
+ {
841
+ help: 'args: <name prefix|bundle id>. List matching bundles',
842
+ action: getBundle,
843
+ },
844
+ ],
845
+ [
846
+ 'findBundleReason',
847
+ {
848
+ help: 'args: <bundle> <asset>. Why is the asset in the bundle',
849
+ // @ts-expect-error TS2556
850
+ action: (v) => findBundleReason(...v.split(' ')),
851
+ },
852
+ ],
853
+ [
854
+ 'getBundles',
855
+ {
856
+ help: 'List all bundles',
857
+ action: getBundles,
858
+ },
859
+ ],
860
+ [
861
+ 'getReferencingBundles',
862
+ {
863
+ help: 'args: <bundle>. List bundles that reference the bundle',
864
+ action: getReferencingBundles,
865
+ },
866
+ ],
867
+ [
868
+ 'stats',
869
+ {
870
+ help: 'Statistics',
871
+ action: stats,
872
+ },
873
+ ],
874
+ [
875
+ 'findAsset',
876
+ {
877
+ help: 'args: <regex>. List assets matching the filepath regex',
878
+ action: findAsset,
879
+ },
880
+ ],
881
+ [
882
+ 'inspectCache',
883
+ {
884
+ help: 'Cache Information',
885
+ action: inspectCache,
886
+ },
887
+ ],
888
+ [
889
+ 'findAssetWithSymbol',
890
+ {
891
+ help: 'args: <local>. Get the asset that defines the symbol with the given local name',
892
+ action: findAssetWithSymbol,
893
+ },
894
+ ],
895
+ ])) {
896
+ server.context[name] = cmd.action;
897
+ server.defineCommand(name, {
898
+ help: '📦 ' + cmd.help,
899
+ action: (v) => {
900
+ server.clearBufferedCommand();
901
+ try {
902
+ cmd.action(v);
903
+ }
904
+ finally {
905
+ server.displayPrompt();
906
+ }
907
+ },
908
+ });
909
+ }
910
+ }
911
+ }