From 04a89fec9b63819215c9a754e0d60cd1e5fb4a05 Mon Sep 17 00:00:00 2001 From: thebriando Date: Mon, 12 Apr 2021 16:19:57 -0700 Subject: [PATCH 1/8] change edgetypes to numbers --- packages/core/core/src/BundleGraph.js | 211 +++++++++++++----- packages/core/core/src/Graph.js | 41 ++-- packages/core/core/src/RequestTracker.js | 194 ++++++++++++---- packages/core/core/src/applyRuntimes.js | 8 +- .../core/src/public/MutableBundleGraph.js | 14 +- packages/core/core/src/types.js | 2 +- packages/core/core/test/AssetGraph.test.js | 12 +- packages/core/core/test/Graph.test.js | 32 +-- 8 files changed, 374 insertions(+), 140 deletions(-) diff --git a/packages/core/core/src/BundleGraph.js b/packages/core/core/src/BundleGraph.js index 26619c55b2f..a8a2b325893 100644 --- a/packages/core/core/src/BundleGraph.js +++ b/packages/core/core/src/BundleGraph.js @@ -29,16 +29,16 @@ import {getBundleGroupId, getPublicId} from './utils'; import Graph, {ALL_EDGE_TYPES, mapVisitor, type GraphOpts} from './Graph'; import Environment from './public/Environment'; -type BundleGraphEdgeTypes = +export const BundleGraphEdgeTypes = { // A lack of an edge type indicates to follow the edge while traversing // the bundle's contents, e.g. `bundle.traverse()` during packaging. - | null + null: 0, // Used for constant-time checks of presence of a dependency or asset in a bundle, // avoiding bundle traversal in cases like `isAssetInAncestors` - | 'contains' + contains: 1, // Connections between bundles and bundle groups, for quick traversal of the // bundle hierarchy. - | 'bundle' + bundle: 2, // When dependency -> asset: Indicates that the asset a dependency references // is contained in another bundle. // When dependency -> bundle: Indicates the bundle is necessary for any bundles @@ -48,10 +48,37 @@ type BundleGraphEdgeTypes = // This type prevents referenced assets from being traversed from dependencies // along the untyped edge, and enables traversal to referenced bundles that are // not directly connected to bundle group nodes. - | 'references' + references: 3, // Signals that the dependency is internally resolvable via the bundle's ancestry, // and that the bundle connected to the dependency is not necessary for the source bundle. - | 'internal_async'; + internal_async: 4, +}; + +type BundleGraphEdgeType = $Values; + +// type BundleGraphEdgeTypes = +// // A lack of an edge type indicates to follow the edge while traversing +// // the bundle's contents, e.g. `bundle.traverse()` during packaging. +// | null +// // Used for constant-time checks of presence of a dependency or asset in a bundle, +// // avoiding bundle traversal in cases like `isAssetInAncestors` +// | 'contains' +// // Connections between bundles and bundle groups, for quick traversal of the +// // bundle hierarchy. +// | 'bundle' +// // When dependency -> asset: Indicates that the asset a dependency references +// // is contained in another bundle. +// // When dependency -> bundle: Indicates the bundle is necessary for any bundles +// // with the dependency. +// // When bundle -> bundle: Indicates the target bundle is necessary for the +// // source bundle. +// // This type prevents referenced assets from being traversed from dependencies +// // along the untyped edge, and enables traversal to referenced bundles that are +// // not directly connected to bundle group nodes. +// | 'references' +// // Signals that the dependency is internally resolvable via the bundle's ancestry, +// // and that the bundle connected to the dependency is not necessary for the source bundle. +// | BundleGraphEdgeTypeIds.internal_async; type InternalSymbolResolution = {| asset: Asset, @@ -67,7 +94,7 @@ type InternalExportSymbolResolution = {| type SerializedBundleGraph = {| $$raw: true, - graph: GraphOpts, + graph: GraphOpts, bundleContentHashes: Map, assetPublicIds: Set, publicIdByAssetId: Map, @@ -95,7 +122,7 @@ export default class BundleGraph { // It needs to be exposed in BundlerRunner for now based on how applying runtimes works and the // BundlerRunner takes care of invalidating hashes when runtimes are applied, but this is not ideal. _bundleContentHashes: Map; - _graph: Graph; + _graph: Graph; constructor({ graph, @@ -103,7 +130,7 @@ export default class BundleGraph { assetPublicIds, bundleContentHashes, }: {| - graph: Graph, + graph: Graph, publicIdByAssetId: Map, assetPublicIds: Set, bundleContentHashes: Map, @@ -119,7 +146,7 @@ export default class BundleGraph { publicIdByAssetId: Map = new Map(), assetPublicIds: Set = new Set(), ): BundleGraph { - let graph = new Graph(); + let graph = new Graph(); let rootNode = assetGraph.getRootNode(); invariant(rootNode != null && rootNode.type === 'root'); @@ -152,14 +179,22 @@ export default class BundleGraph { for (let edge of assetGraph.getAllEdges()) { let fromIds; if (assetGroupIds.has(edge.from)) { - fromIds = [...assetGraph.inboundEdges.getEdges(edge.from, null)]; + fromIds = [ + ...assetGraph.inboundEdges.getEdges( + edge.from, + BundleGraphEdgeTypes.null, + ), + ]; } else { fromIds = [edge.from]; } for (let from of fromIds) { if (assetGroupIds.has(edge.to)) { - for (let to of assetGraph.outboundEdges.getEdges(edge.to, null)) { + for (let to of assetGraph.outboundEdges.getEdges( + edge.to, + BundleGraphEdgeTypes.null, + )) { graph.addEdge(from, to); } } else { @@ -220,7 +255,7 @@ export default class BundleGraph { } if (node.type === 'asset' || node.type === 'dependency') { - this._graph.addEdge(bundle.id, node.id, 'contains'); + this._graph.addEdge(bundle.id, node.id, BundleGraphEdgeTypes.contains); } if (node.type === 'dependency') { @@ -228,17 +263,25 @@ export default class BundleGraph { .getNodesConnectedFrom(node) .filter(node => node.type === 'bundle_group')) { invariant(bundleGroupNode.type === 'bundle_group'); - this._graph.addEdge(bundle.id, bundleGroupNode.id, 'bundle'); + this._graph.addEdge( + bundle.id, + bundleGroupNode.id, + BundleGraphEdgeTypes.bundle, + ); } // If the dependency references a target bundle, add a reference edge from // the source bundle to the dependency for easy traversal. if ( this._graph - .getNodesConnectedFrom(node, 'references') + .getNodesConnectedFrom(node, BundleGraphEdgeTypes.references) .some(node => node.type === 'bundle') ) { - this._graph.addEdge(bundle.id, node.id, 'references'); + this._graph.addEdge( + bundle.id, + node.id, + BundleGraphEdgeTypes.references, + ); } } }, nullthrows(this._graph.getNode(asset.id))); @@ -261,7 +304,11 @@ export default class BundleGraph { throw new Error('Expected an async dependency'); } - this._graph.addEdge(bundle.id, dependency.id, 'internal_async'); + this._graph.addEdge( + bundle.id, + dependency.id, + BundleGraphEdgeTypes.internal_async, + ); this.removeExternalDependency(bundle, dependency); } @@ -275,7 +322,7 @@ export default class BundleGraph { return this._graph .getNodesConnectedTo( nullthrows(this._graph.getNode(getBundleGroupId(bundleGroup))), - 'bundle', + BundleGraphEdgeTypes.bundle, ) .filter(node => node.type === 'bundle') .map(node => { @@ -295,11 +342,15 @@ export default class BundleGraph { if ( bundle != null && - this._graph.hasEdge(bundle.id, depNode.id, 'internal_async') + this._graph.hasEdge( + bundle.id, + depNode.id, + BundleGraphEdgeTypes.internal_async, + ) ) { let referencedAssetNode = this._graph.getNodesConnectedFrom( depNode, - 'references', + BundleGraphEdgeTypes.references, )[0]; let resolved; @@ -356,7 +407,7 @@ export default class BundleGraph { let referenced = this._graph .getNodesConnectedFrom( nullthrows(this._graph.getNode(dependency.id)), - 'references', + BundleGraphEdgeTypes.references, ) .find(node => node.type === 'asset'); @@ -381,11 +432,13 @@ export default class BundleGraph { return; } - if (this._graph.hasEdge(bundle.id, node.id, 'contains')) { + if ( + this._graph.hasEdge(bundle.id, node.id, BundleGraphEdgeTypes.contains) + ) { this._graph.removeEdge( bundle.id, node.id, - 'contains', + BundleGraphEdgeTypes.contains, // Removing this contains edge should not orphan the connected node. This // is disabled for performance reasons as these edges are removed as part // of a traversal, and checking for orphans becomes quite expensive in @@ -414,8 +467,18 @@ export default class BundleGraph { if (node.type === 'dependency') { this.removeExternalDependency(bundle, node.value); - if (this._graph.hasEdge(bundle.id, node.id, 'references')) { - this._graph.removeEdge(bundle.id, node.id, 'references'); + if ( + this._graph.hasEdge( + bundle.id, + node.id, + BundleGraphEdgeTypes.references, + ) + ) { + this._graph.removeEdge( + bundle.id, + node.id, + BundleGraphEdgeTypes.references, + ); } } }, nullthrows(this._graph.getNode(asset.id))); @@ -435,7 +498,7 @@ export default class BundleGraph { let bundleGroupNodes = this._graph.getNodesConnectedTo( bundleNode, - 'bundle', + BundleGraphEdgeTypes.bundle, ); this._graph.removeNode(bundleNode); @@ -491,7 +554,13 @@ export default class BundleGraph { for (let bundleGroupNode of this._graph .getNodesConnectedFrom(nullthrows(this._graph.getNode(dependency.id))) .filter(node => node.type === 'bundle_group')) { - if (!this._graph.hasEdge(bundle.id, bundleGroupNode.id, 'bundle')) { + if ( + !this._graph.hasEdge( + bundle.id, + bundleGroupNode.id, + BundleGraphEdgeTypes.bundle, + ) + ) { continue; } @@ -510,10 +579,18 @@ export default class BundleGraph { inboundDependencies.every( dependency => !this.bundleHasDependency(bundle, dependency) || - this._graph.hasEdge(bundle.id, dependency.id, 'internal_async'), + this._graph.hasEdge( + bundle.id, + dependency.id, + BundleGraphEdgeTypes.internal_async, + ), ) ) { - this._graph.removeEdge(bundle.id, bundleGroupNode.id, 'bundle'); + this._graph.removeEdge( + bundle.id, + bundleGroupNode.id, + BundleGraphEdgeTypes.bundle, + ); } } } @@ -523,22 +600,30 @@ export default class BundleGraph { asset: Asset, bundle: Bundle, ): void { - this._graph.addEdge(dependency.id, asset.id, 'references'); - this._graph.addEdge(dependency.id, bundle.id, 'references'); + this._graph.addEdge( + dependency.id, + asset.id, + BundleGraphEdgeTypes.references, + ); + this._graph.addEdge( + dependency.id, + bundle.id, + BundleGraphEdgeTypes.references, + ); if (this._graph.hasEdge(dependency.id, asset.id)) { this._graph.removeEdge(dependency.id, asset.id); } } createBundleReference(from: Bundle, to: Bundle): void { - this._graph.addEdge(from.id, to.id, 'references'); + this._graph.addEdge(from.id, to.id, BundleGraphEdgeTypes.references); } findBundlesWithAsset(asset: Asset): Array { return this._graph .getNodesConnectedTo( nullthrows(this._graph.getNode(asset.id)), - 'contains', + BundleGraphEdgeTypes.contains, ) .filter(node => node.type === 'bundle') .map(node => { @@ -551,7 +636,7 @@ export default class BundleGraph { return this._graph .getNodesConnectedTo( nullthrows(this._graph.getNode(dependency.id)), - 'contains', + BundleGraphEdgeTypes.contains, ) .filter(node => node.type === 'bundle') .map(node => { @@ -599,7 +684,7 @@ export default class BundleGraph { } }, depNode, - 'references', + BundleGraphEdgeTypes.references, ); } @@ -643,7 +728,7 @@ export default class BundleGraph { // referenced. let asyncInternalReferencingBundles = new Set( this._graph - .getNodesConnectedTo(assetNode, 'references') + .getNodesConnectedTo(assetNode, BundleGraphEdgeTypes.references) .filter(node => node.type === 'dependency') .map(node => { invariant(node.type === 'dependency'); @@ -651,7 +736,10 @@ export default class BundleGraph { }) .flatMap(dependencyNode => this._graph - .getNodesConnectedTo(dependencyNode, 'internal_async') + .getNodesConnectedTo( + dependencyNode, + BundleGraphEdgeTypes.internal_async, + ) .map(node => { invariant(node.type === 'bundle'); return node.value; @@ -754,7 +842,7 @@ export default class BundleGraph { // Get a list of parent bundle nodes pointing to the bundle group let parentBundleNodes = this._graph.getNodesConnectedTo( nullthrows(this._graph.getNode(getBundleGroupId(bundleGroup))), - 'bundle', + BundleGraphEdgeTypes.bundle, ); // Check that every parent bundle has a bundle group in its ancestry that contains the asset. @@ -791,7 +879,7 @@ export default class BundleGraph { } } }, - ['references', 'bundle'], + [BundleGraphEdgeTypes.references, BundleGraphEdgeTypes.bundle], ); return isReachable; @@ -843,7 +931,7 @@ export default class BundleGraph { actions.skipChildren(); } }, - ['references', 'bundle'], + [BundleGraphEdgeTypes.references, BundleGraphEdgeTypes.bundle], ); if (res != null) { @@ -868,7 +956,13 @@ export default class BundleGraph { } if (node.type === 'dependency' || node.type === 'asset') { - if (this._graph.hasEdge(bundle.id, node.id, 'contains')) { + if ( + this._graph.hasEdge( + bundle.id, + node.id, + BundleGraphEdgeTypes.contains, + ) + ) { return node; } } @@ -939,7 +1033,7 @@ export default class BundleGraph { node => (node.type === 'bundle' ? node.value : null), visit, startBundle ? nullthrows(this._graph.getNode(startBundle.id)) : null, - ['bundle', 'references'], + [BundleGraphEdgeTypes.bundle, BundleGraphEdgeTypes.references], ); } @@ -978,7 +1072,7 @@ export default class BundleGraph { referencingBundles.add(node.value); } }, - 'references', + BundleGraphEdgeTypes.references, ); return [...referencingBundles]; @@ -998,7 +1092,10 @@ export default class BundleGraph { getDirectParentBundleGroups(bundle: Bundle): Array { return this._graph - .getNodesConnectedTo(nullthrows(this._graph.getNode(bundle.id)), 'bundle') + .getNodesConnectedTo( + nullthrows(this._graph.getNode(bundle.id)), + BundleGraphEdgeTypes.bundle, + ) .filter(node => node.type === 'bundle_group') .map(node => { invariant(node.type === 'bundle_group'); @@ -1013,7 +1110,7 @@ export default class BundleGraph { ); for (let bundleNode of this._graph.getNodesConnectedFrom( bundleGroupNode, - 'bundle', + BundleGraphEdgeTypes.bundle, )) { invariant(bundleNode.type === 'bundle'); let bundle = bundleNode.value; @@ -1054,7 +1151,9 @@ export default class BundleGraph { // Shared bundles seem to depend on being used in the opposite order // they were added. // TODO: Should this be the case? - this._graph.getNodesConnectedFrom(node, 'references').reverse(), + this._graph + .getNodesConnectedFrom(node, BundleGraphEdgeTypes.references) + .reverse(), }); return [...referencedBundles]; @@ -1097,11 +1196,19 @@ export default class BundleGraph { } bundleHasAsset(bundle: Bundle, asset: Asset): boolean { - return this._graph.hasEdge(bundle.id, asset.id, 'contains'); + return this._graph.hasEdge( + bundle.id, + asset.id, + BundleGraphEdgeTypes.contains, + ); } bundleHasDependency(bundle: Bundle, dependency: Dependency): boolean { - return this._graph.hasEdge(bundle.id, dependency.id, 'contains'); + return this._graph.hasEdge( + bundle.id, + dependency.id, + BundleGraphEdgeTypes.contains, + ); } filteredTraverse( @@ -1409,13 +1516,15 @@ export default class BundleGraph { addBundleToBundleGroup(bundle: Bundle, bundleGroup: BundleGroup) { let bundleGroupId = getBundleGroupId(bundleGroup); - if (this._graph.hasEdge(bundleGroupId, bundle.id, 'bundle')) { + if ( + this._graph.hasEdge(bundleGroupId, bundle.id, BundleGraphEdgeTypes.bundle) + ) { // Bundle group already has bundle return; } this._graph.addEdge(bundleGroupId, bundle.id); - this._graph.addEdge(bundleGroupId, bundle.id, 'bundle'); + this._graph.addEdge(bundleGroupId, bundle.id, BundleGraphEdgeTypes.bundle); for (let entryAssetId of bundle.entryAssetIds) { if (this._graph.hasEdge(bundleGroupId, entryAssetId)) { @@ -1476,7 +1585,7 @@ export default class BundleGraph { return this._graph .getNodesConnectedTo( nullthrows(this._graph.getNode(getBundleGroupId(bundleGroup))), - 'bundle', + BundleGraphEdgeTypes.bundle, ) .some(n => n.type === 'root'); } diff --git a/packages/core/core/src/Graph.js b/packages/core/core/src/Graph.js index 68bfc48beb7..91263822de3 100644 --- a/packages/core/core/src/Graph.js +++ b/packages/core/core/src/Graph.js @@ -6,21 +6,22 @@ import type {TraversalActions, GraphVisitor} from '@parcel/types'; import assert from 'assert'; import nullthrows from 'nullthrows'; -export type GraphOpts = {| +type NullEdgeType = 0; +export type GraphOpts = {| nodes?: Map, edges?: {| - inboundEdges: AdjacencyListMap, - outboundEdges: AdjacencyListMap, + inboundEdges: AdjacencyListMap, + outboundEdges: AdjacencyListMap, |}, rootNodeId?: ?NodeId, |}; export const ALL_EDGE_TYPES = '@@all_edge_types'; -export default class Graph { +export default class Graph { nodes: Map; - inboundEdges: AdjacencyList; - outboundEdges: AdjacencyList; + inboundEdges: AdjacencyList; + outboundEdges: AdjacencyList; rootNodeId: ?NodeId; constructor(opts: GraphOpts = ({}: any)) { @@ -60,7 +61,7 @@ export default class Graph { // Returns a list of all edges in the graph. This can be large, so iterating // the complete list can be costly in large graphs. Used when merging graphs. - getAllEdges(): Array> { + getAllEdges(): Array> { let edges = []; for (let [from, edgeList] of this.outboundEdges.getListMap()) { for (let [type, toNodes] of edgeList) { @@ -98,7 +99,7 @@ export default class Graph { return this.rootNodeId ? this.getNode(this.rootNodeId) : null; } - addEdge(from: NodeId, to: NodeId, type: TEdgeType | null = null): void { + addEdge(from: NodeId, to: NodeId, type: TEdgeType | NullEdgeType = 0): void { if (!this.getNode(from)) { throw new Error(`"from" node '${from}' not found`); } @@ -111,13 +112,17 @@ export default class Graph { this.inboundEdges.addEdge(to, from, type); } - hasEdge(from: NodeId, to: NodeId, type?: TEdgeType | null = null): boolean { + hasEdge( + from: NodeId, + to: NodeId, + type?: TEdgeType | NullEdgeType = 0, + ): boolean { return this.outboundEdges.hasEdge(from, to, type); } getNodesConnectedTo( node: TNode, - type: TEdgeType | null | Array = null, + type: TEdgeType | NullEdgeType | Array = 0, ): Array { assertHasNode(this, node); @@ -150,7 +155,7 @@ export default class Graph { getNodesConnectedFrom( node: TNode, - type: TEdgeType | null | Array = null, + type: TEdgeType | NullEdgeType | Array = 0, ): Array { assertHasNode(this, node); @@ -215,7 +220,7 @@ export default class Graph { this.removeNode(node); } - removeEdges(node: TNode, type: TEdgeType | null = null) { + removeEdges(node: TNode, type: TEdgeType | NullEdgeType = 0) { assertHasNode(this, node); for (let to of this.outboundEdges.getEdges(node.id, type)) { @@ -227,7 +232,7 @@ export default class Graph { removeEdge( from: NodeId, to: NodeId, - type: TEdgeType | null = null, + type: TEdgeType | NullEdgeType = 0, removeOrphans: boolean = true, ) { if (!this.outboundEdges.hasEdge(from, to, type)) { @@ -291,7 +296,7 @@ export default class Graph { replaceNode( fromNode: TNode, toNode: TNode, - type: TEdgeType | null = null, + type: TEdgeType | NullEdgeType = 0, ): void { assertHasNode(this, fromNode); @@ -310,7 +315,7 @@ export default class Graph { fromNode: TNode, toNodes: $ReadOnlyArray, replaceFilter?: null | (TNode => boolean), - type?: TEdgeType | null = null, + type?: TEdgeType | NullEdgeType = 0, ): void { assertHasNode(this, fromNode); @@ -339,7 +344,7 @@ export default class Graph { traverse( visit: GraphVisitor, startNode: ?TNode, - type: TEdgeType | null | Array = null, + type: TEdgeType | NullEdgeType | Array = 0, ): ?TContext { return this.dfs({ visit, @@ -352,7 +357,7 @@ export default class Graph { filter: (TNode, TraversalActions) => ?TValue, visit: GraphVisitor, startNode: ?TNode, - type?: TEdgeType | null | Array, + type?: TEdgeType | Array, ): ?TContext { return this.traverse(mapVisitor(filter, visit), startNode, type); } @@ -360,7 +365,7 @@ export default class Graph { traverseAncestors( startNode: TNode, visit: GraphVisitor, - type: TEdgeType | null | Array = null, + type: TEdgeType | NullEdgeType | Array = 0, ): ?TContext { return this.dfs({ visit, diff --git a/packages/core/core/src/RequestTracker.js b/packages/core/core/src/RequestTracker.js index 7c124aa0de3..f1b85214303 100644 --- a/packages/core/core/src/RequestTracker.js +++ b/packages/core/core/src/RequestTracker.js @@ -95,14 +95,22 @@ type RequestGraphNode = | EnvNode | OptionNode; -type RequestGraphEdgeType = - | 'subrequest' - | 'invalidated_by_update' - | 'invalidated_by_delete' - | 'invalidated_by_create' - | 'invalidated_by_create_above' - | 'dirname'; - +// type RequestGraphEdgeType = +// | 'subrequest' +// | 'invalidated_by_update' +// | 'invalidated_by_delete' +// | 'invalidated_by_create' +// | 'invalidated_by_create_above' +// | 'dirname'; + +export const RequestGraphEdgeType = { + subrequest: 1, + invalidated_by_update: 2, + invalidated_by_delete: 3, + invalidated_by_create: 4, + invalidated_by_create_above: 5, + dirname: 6, +}; export type RunAPI = {| invalidateOnFileCreate: FileCreateInvalidation => void, invalidateOnFileDelete: FilePath => void, @@ -273,7 +281,7 @@ export class RequestGraph extends Graph< requestNode, subrequestNodes, null, - 'subrequest', + RequestGraphEdgeType.subrequest, ); } @@ -283,7 +291,10 @@ export class RequestGraph extends Graph< node.invalidateReason |= reason; this.invalidNodeIds.add(node.id); - let parentNodes = this.getNodesConnectedTo(node, 'subrequest'); + let parentNodes = this.getNodesConnectedTo( + node, + RequestGraphEdgeType.subrequest, + ); for (let parentNode of parentNodes) { this.invalidateNode(parentNode, reason); } @@ -305,7 +316,7 @@ export class RequestGraph extends Graph< if (env[node.value.key] !== node.value.value) { let parentNodes = this.getNodesConnectedTo( node, - 'invalidated_by_update', + RequestGraphEdgeType.invalidated_by_update, ); for (let parentNode of parentNodes) { this.invalidateNode(parentNode, ENV_CHANGE); @@ -321,7 +332,7 @@ export class RequestGraph extends Graph< if (hashFromOption(options[node.value.key]) !== node.value.hash) { let parentNodes = this.getNodesConnectedTo( node, - 'invalidated_by_update', + RequestGraphEdgeType.invalidated_by_update, ); for (let parentNode of parentNodes) { this.invalidateNode(parentNode, OPTION_CHANGE); @@ -337,8 +348,18 @@ export class RequestGraph extends Graph< this.addNode(fileNode); } - if (!this.hasEdge(requestNode.id, fileNode.id, 'invalidated_by_update')) { - this.addEdge(requestNode.id, fileNode.id, 'invalidated_by_update'); + if ( + !this.hasEdge( + requestNode.id, + fileNode.id, + RequestGraphEdgeType.invalidated_by_update, + ) + ) { + this.addEdge( + requestNode.id, + fileNode.id, + RequestGraphEdgeType.invalidated_by_update, + ); } } @@ -349,8 +370,18 @@ export class RequestGraph extends Graph< this.addNode(fileNode); } - if (!this.hasEdge(requestNode.id, fileNode.id, 'invalidated_by_delete')) { - this.addEdge(requestNode.id, fileNode.id, 'invalidated_by_delete'); + if ( + !this.hasEdge( + requestNode.id, + fileNode.id, + RequestGraphEdgeType.invalidated_by_delete, + ) + ) { + this.addEdge( + requestNode.id, + fileNode.id, + RequestGraphEdgeType.invalidated_by_delete, + ); } } @@ -377,9 +408,9 @@ export class RequestGraph extends Graph< if ( last != null && - !this.hasEdge(last.id, fileNameNode.id, 'dirname') + !this.hasEdge(last.id, fileNameNode.id, RequestGraphEdgeType.dirname) ) { - this.addEdge(last.id, fileNameNode.id, 'dirname'); + this.addEdge(last.id, fileNameNode.id, RequestGraphEdgeType.dirname); } last = fileNameNode; @@ -402,13 +433,33 @@ export class RequestGraph extends Graph< // This indicates a complete match, and any requests attached to the `aboveFilePath` // node will be invalidated. let firstId = 'file_name:' + parts[0]; - if (!this.hasEdge(node.id, firstId, 'invalidated_by_create_above')) { - this.addEdge(node.id, firstId, 'invalidated_by_create_above'); + if ( + !this.hasEdge( + node.id, + firstId, + RequestGraphEdgeType.invalidated_by_create_above, + ) + ) { + this.addEdge( + node.id, + firstId, + RequestGraphEdgeType.invalidated_by_create_above, + ); } invariant(last != null); - if (!this.hasEdge(last.id, node.id, 'invalidated_by_create_above')) { - this.addEdge(last.id, node.id, 'invalidated_by_create_above'); + if ( + !this.hasEdge( + last.id, + node.id, + RequestGraphEdgeType.invalidated_by_create_above, + ) + ) { + this.addEdge( + last.id, + node.id, + RequestGraphEdgeType.invalidated_by_create_above, + ); } } else if (input.filePath != null) { node = nodeFromFilePath(input.filePath); @@ -420,8 +471,18 @@ export class RequestGraph extends Graph< this.addNode(node); } - if (!this.hasEdge(requestNode.id, node.id, 'invalidated_by_create')) { - this.addEdge(requestNode.id, node.id, 'invalidated_by_create'); + if ( + !this.hasEdge( + requestNode.id, + node.id, + RequestGraphEdgeType.invalidated_by_create, + ) + ) { + this.addEdge( + requestNode.id, + node.id, + RequestGraphEdgeType.invalidated_by_create, + ); } } @@ -437,8 +498,18 @@ export class RequestGraph extends Graph< this.addNode(envNode); } - if (!this.hasEdge(requestNode.id, envNode.id, 'invalidated_by_update')) { - this.addEdge(requestNode.id, envNode.id, 'invalidated_by_update'); + if ( + !this.hasEdge( + requestNode.id, + envNode.id, + RequestGraphEdgeType.invalidated_by_update, + ) + ) { + this.addEdge( + requestNode.id, + envNode.id, + RequestGraphEdgeType.invalidated_by_update, + ); } } @@ -449,16 +520,41 @@ export class RequestGraph extends Graph< this.addNode(optionNode); } - if (!this.hasEdge(requestNode.id, optionNode.id, 'invalidated_by_update')) { - this.addEdge(requestNode.id, optionNode.id, 'invalidated_by_update'); + if ( + !this.hasEdge( + requestNode.id, + optionNode.id, + RequestGraphEdgeType.invalidated_by_update, + ) + ) { + this.addEdge( + requestNode.id, + optionNode.id, + RequestGraphEdgeType.invalidated_by_update, + ); } } clearInvalidations(node: RequestNode) { this.unpredicatableNodeIds.delete(node.id); - this.replaceNodesConnectedTo(node, [], null, 'invalidated_by_update'); - this.replaceNodesConnectedTo(node, [], null, 'invalidated_by_delete'); - this.replaceNodesConnectedTo(node, [], null, 'invalidated_by_create'); + this.replaceNodesConnectedTo( + node, + [], + null, + RequestGraphEdgeType.invalidated_by_update, + ); + this.replaceNodesConnectedTo( + node, + [], + null, + RequestGraphEdgeType.invalidated_by_delete, + ); + this.replaceNodesConnectedTo( + node, + [], + null, + RequestGraphEdgeType.invalidated_by_create, + ); } getInvalidations(requestId: string): Array { @@ -470,7 +566,7 @@ export class RequestGraph extends Graph< let requestNode = this.getRequestNode(requestId); let invalidations = this.getNodesConnectedFrom( requestNode, - 'invalidated_by_update', + RequestGraphEdgeType.invalidated_by_update, ); return invalidations .map(node => { @@ -492,7 +588,10 @@ export class RequestGraph extends Graph< } let requestNode = this.getRequestNode(requestId); - let subRequests = this.getNodesConnectedFrom(requestNode, 'subrequest'); + let subRequests = this.getNodesConnectedFrom( + requestNode, + RequestGraphEdgeType.subrequest, + ); return subRequests.map(node => { invariant(node.type === 'request'); @@ -511,12 +610,16 @@ export class RequestGraph extends Graph< let dirname = path.dirname(filePath); for (let matchNode of matchNodes) { if ( - this.hasEdge(node.id, matchNode.id, 'invalidated_by_create_above') && + this.hasEdge( + node.id, + matchNode.id, + RequestGraphEdgeType.invalidated_by_create_above, + ) && isDirectoryInside(path.dirname(matchNode.value.filePath), dirname) ) { let connectedNodes = this.getNodesConnectedTo( matchNode, - 'invalidated_by_create', + RequestGraphEdgeType.invalidated_by_create, ); for (let connectedNode of connectedNodes) { this.invalidateNode(connectedNode, FILE_CREATE); @@ -528,7 +631,10 @@ export class RequestGraph extends Graph< // recursively invalidate connected requests as described above. let basename = path.basename(dirname); let parent = this.getNode('file_name:' + basename); - if (parent != null && this.hasEdge(node.id, parent.id, 'dirname')) { + if ( + parent != null && + this.hasEdge(node.id, parent.id, RequestGraphEdgeType.dirname) + ) { invariant(parent.type === 'file_name'); this.invalidateFileNameNode(parent, dirname, matchNodes); } @@ -543,14 +649,20 @@ export class RequestGraph extends Graph< // if it was a create event, but the file already exists in the graph, // then also invalidate nodes connected by invalidated_by_update edges. if (node && (type === 'create' || type === 'update')) { - let nodes = this.getNodesConnectedTo(node, 'invalidated_by_update'); + let nodes = this.getNodesConnectedTo( + node, + RequestGraphEdgeType.invalidated_by_update, + ); for (let connectedNode of nodes) { didInvalidate = true; this.invalidateNode(connectedNode, FILE_UPDATE); } if (type === 'create') { - let nodes = this.getNodesConnectedTo(node, 'invalidated_by_create'); + let nodes = this.getNodesConnectedTo( + node, + RequestGraphEdgeType.invalidated_by_create, + ); for (let connectedNode of nodes) { didInvalidate = true; this.invalidateNode(connectedNode, FILE_CREATE); @@ -563,7 +675,7 @@ export class RequestGraph extends Graph< // Find potential file nodes to be invalidated if this file name pattern matches let above = this.getNodesConnectedTo( fileNameNode, - 'invalidated_by_create_above', + RequestGraphEdgeType.invalidated_by_create_above, ).map(node => { invariant(node.type === 'file'); return node; @@ -582,7 +694,7 @@ export class RequestGraph extends Graph< if (isGlobMatch(filePath, globNode.value)) { let connectedNodes = this.getNodesConnectedTo( globNode, - 'invalidated_by_create', + RequestGraphEdgeType.invalidated_by_create, ); for (let connectedNode of connectedNodes) { didInvalidate = true; @@ -593,7 +705,7 @@ export class RequestGraph extends Graph< } else if (node && type === 'delete') { for (let connectedNode of this.getNodesConnectedTo( node, - 'invalidated_by_delete', + RequestGraphEdgeType.invalidated_by_delete, )) { didInvalidate = true; this.invalidateNode(connectedNode, FILE_DELETE); diff --git a/packages/core/core/src/applyRuntimes.js b/packages/core/core/src/applyRuntimes.js index ff7045eee26..3609f8798e8 100644 --- a/packages/core/core/src/applyRuntimes.js +++ b/packages/core/core/src/applyRuntimes.js @@ -19,7 +19,7 @@ import invariant from 'assert'; import nullthrows from 'nullthrows'; import AssetGraph, {nodeFromAssetGroup} from './AssetGraph'; import BundleGraph from './public/BundleGraph'; -import InternalBundleGraph from './BundleGraph'; +import InternalBundleGraph, {BundleGraphEdgeTypes} from './BundleGraph'; import {NamedBundle} from './public/Bundle'; import {PluginLogger} from '@parcel/logger'; import ThrowableDiagnostic, {errorToDiagnostic} from '@parcel/diagnostic'; @@ -187,7 +187,11 @@ export default async function applyRuntimes({ return; } - bundleGraph._graph.addEdge(bundle.id, node.id, 'contains'); + bundleGraph._graph.addEdge( + bundle.id, + node.id, + BundleGraphEdgeTypes.contains, + ); } }, runtimeNode); diff --git a/packages/core/core/src/public/MutableBundleGraph.js b/packages/core/core/src/public/MutableBundleGraph.js index 7942c574ca0..a190cfff7a7 100644 --- a/packages/core/core/src/public/MutableBundleGraph.js +++ b/packages/core/core/src/public/MutableBundleGraph.js @@ -18,7 +18,7 @@ import path from 'path'; import nullthrows from 'nullthrows'; import {md5FromString} from '@parcel/utils'; import BundleGraph from './BundleGraph'; -import InternalBundleGraph from '../BundleGraph'; +import InternalBundleGraph, {BundleGraphEdgeTypes} from '../BundleGraph'; import {Bundle, bundleToInternalBundle} from './Bundle'; import {mapVisitor, ALL_EDGE_TYPES} from '../Graph'; import {assetFromValue, assetToAssetValue} from './Asset'; @@ -98,26 +98,30 @@ export default class MutableBundleGraph extends BundleGraph let assetNodes = this.#graph._graph.getNodesConnectedFrom(dependencyNode); this.#graph._graph.addEdge(dependencyNode.id, bundleGroupNode.id); this.#graph._graph.replaceNodesConnectedTo(bundleGroupNode, assetNodes); - this.#graph._graph.addEdge(dependencyNode.id, resolved.id, 'references'); + this.#graph._graph.addEdge( + dependencyNode.id, + resolved.id, + BundleGraphEdgeTypes.references, + ); this.#graph._graph.removeEdge(dependencyNode.id, resolved.id); if (dependency.isEntry) { this.#graph._graph.addEdge( nullthrows(this.#graph._graph.getRootNode()).id, bundleGroupNode.id, - 'bundle', + BundleGraphEdgeTypes.bundle, ); } else { let inboundBundleNodes = this.#graph._graph.getNodesConnectedTo( dependencyNode, - 'contains', + BundleGraphEdgeTypes.contains, ); for (let inboundBundleNode of inboundBundleNodes) { invariant(inboundBundleNode.type === 'bundle'); this.#graph._graph.addEdge( inboundBundleNode.id, bundleGroupNode.id, - 'bundle', + BundleGraphEdgeTypes.bundle, ); } } diff --git a/packages/core/core/src/types.js b/packages/core/core/src/types.js index 2856f7609c6..df24a85007d 100644 --- a/packages/core/core/src/types.js +++ b/packages/core/core/src/types.js @@ -215,7 +215,7 @@ export type ParcelOptions = {| export type NodeId = string; -export type Edge = {| +export type Edge = {| from: NodeId, to: NodeId, type: TEdgeType, diff --git a/packages/core/core/test/AssetGraph.test.js b/packages/core/core/test/AssetGraph.test.js index cfe36fc2681..fe83d8b3f51 100644 --- a/packages/core/core/test/AssetGraph.test.js +++ b/packages/core/core/test/AssetGraph.test.js @@ -123,12 +123,12 @@ describe('AssetGraph', () => { { from: '@@root', to: 'entry_specifier:/path/to/index1', - type: null, + type: 0, }, { from: '@@root', to: 'entry_specifier:/path/to/index2', - type: null, + type: 0, }, { from: 'entry_specifier:/path/to/index1', @@ -136,7 +136,7 @@ describe('AssetGraph', () => { filePath: '/path/to/index1/src/main.js', packagePath: '/path/to/index1', }).id, - type: null, + type: 0, }, { from: 'entry_specifier:/path/to/index2', @@ -144,7 +144,7 @@ describe('AssetGraph', () => { filePath: '/path/to/index2/src/main.js', packagePath: '/path/to/index2', }).id, - type: null, + type: 0, }, { from: nodeFromEntryFile({ @@ -156,7 +156,7 @@ describe('AssetGraph', () => { target: DEFAULT_TARGETS[0], env: DEFAULT_ENV, }).id, - type: null, + type: 0, }, { from: nodeFromEntryFile({ @@ -168,7 +168,7 @@ describe('AssetGraph', () => { target: DEFAULT_TARGETS[0], env: DEFAULT_ENV, }).id, - type: null, + type: 0, }, ]); }); diff --git a/packages/core/core/test/Graph.test.js b/packages/core/core/test/Graph.test.js index d149e1d11f4..d0ee8010e45 100644 --- a/packages/core/core/test/Graph.test.js +++ b/packages/core/core/test/Graph.test.js @@ -94,7 +94,7 @@ describe('Graph', () => { graph.addNode(nodeB); graph.addNode(nodeC); graph.addEdge('a', 'b'); - graph.addEdge('a', 'c', 'edgetype'); + graph.addEdge('a', 'c', 1); assert(graph.isOrphanedNode(nodeA)); assert(!graph.isOrphanedNode(nodeB)); assert(!graph.isOrphanedNode(nodeC)); @@ -121,7 +121,7 @@ describe('Graph', () => { assert(graph.nodes.has('d')); assert(!graph.nodes.has('b')); assert(!graph.nodes.has('c')); - assert.deepEqual(graph.getAllEdges(), [{from: 'a', to: 'd', type: null}]); + assert.deepEqual(graph.getAllEdges(), [{from: 'a', to: 'd', type: 0}]); }); it('removing a node recursively deletes orphaned nodes', () => { @@ -165,8 +165,8 @@ describe('Graph', () => { ['a', 'c', 'f'], ); assert.deepEqual(graph.getAllEdges(), [ - {from: 'a', to: 'c', type: null}, - {from: 'c', to: 'f', type: null}, + {from: 'a', to: 'c', type: 0}, + {from: 'c', to: 'f', type: 0}, ]); }); @@ -212,8 +212,8 @@ describe('Graph', () => { ['a', 'c', 'f'], ); assert.deepEqual(graph.getAllEdges(), [ - {from: 'a', to: 'c', type: null}, - {from: 'c', to: 'f', type: null}, + {from: 'a', to: 'c', type: 0}, + {from: 'c', to: 'f', type: 0}, ]); }); @@ -246,11 +246,11 @@ describe('Graph', () => { assert.deepEqual(nodesBefore, getNodeIds()); assert.deepEqual(graph.getAllEdges(), [ - {from: 'a', to: 'b', type: null}, - {from: 'b', to: 'c', type: null}, - {from: 'b', to: 'd', type: null}, - {from: 'd', to: 'e', type: null}, - {from: 'e', to: 'b', type: null}, + {from: 'a', to: 'b', type: 0}, + {from: 'b', to: 'c', type: 0}, + {from: 'b', to: 'd', type: 0}, + {from: 'd', to: 'e', type: 0}, + {from: 'e', to: 'b', type: 0}, ]); }); @@ -287,8 +287,8 @@ describe('Graph', () => { assert(!graph.nodes.has('c')); assert(graph.nodes.has('d')); assert.deepEqual(graph.getAllEdges(), [ - {from: 'a', to: 'b', type: null}, - {from: 'a', to: 'd', type: null}, + {from: 'a', to: 'b', type: 0}, + {from: 'a', to: 'd', type: 0}, ]); }); @@ -299,10 +299,10 @@ describe('Graph', () => { graph.addNode({id: 'c', type: 'mynode', value: 'c'}); graph.addNode({id: 'd', type: 'mynode', value: 'd'}); - graph.addEdge('a', 'b', 'edgetype'); + graph.addEdge('a', 'b', 1); graph.addEdge('a', 'd'); graph.addEdge('b', 'c'); - graph.addEdge('b', 'd', 'edgetype'); + graph.addEdge('b', 'd', 1); graph.rootNodeId = 'a'; @@ -312,7 +312,7 @@ describe('Graph', () => { visited.push(node.id); }, null, // use root as startNode - 'edgetype', + 1, ); assert.deepEqual(visited, ['a', 'b', 'd']); }); From c09a85ec44101e0e132e63fa952eb5e8b8de4e37 Mon Sep 17 00:00:00 2001 From: thebriando Date: Tue, 13 Apr 2021 12:43:43 -0700 Subject: [PATCH 2/8] clean up comments --- packages/core/core/src/BundleGraph.js | 24 ------------------------ packages/core/core/src/RequestTracker.js | 8 -------- 2 files changed, 32 deletions(-) diff --git a/packages/core/core/src/BundleGraph.js b/packages/core/core/src/BundleGraph.js index a8a2b325893..7d727393598 100644 --- a/packages/core/core/src/BundleGraph.js +++ b/packages/core/core/src/BundleGraph.js @@ -56,30 +56,6 @@ export const BundleGraphEdgeTypes = { type BundleGraphEdgeType = $Values; -// type BundleGraphEdgeTypes = -// // A lack of an edge type indicates to follow the edge while traversing -// // the bundle's contents, e.g. `bundle.traverse()` during packaging. -// | null -// // Used for constant-time checks of presence of a dependency or asset in a bundle, -// // avoiding bundle traversal in cases like `isAssetInAncestors` -// | 'contains' -// // Connections between bundles and bundle groups, for quick traversal of the -// // bundle hierarchy. -// | 'bundle' -// // When dependency -> asset: Indicates that the asset a dependency references -// // is contained in another bundle. -// // When dependency -> bundle: Indicates the bundle is necessary for any bundles -// // with the dependency. -// // When bundle -> bundle: Indicates the target bundle is necessary for the -// // source bundle. -// // This type prevents referenced assets from being traversed from dependencies -// // along the untyped edge, and enables traversal to referenced bundles that are -// // not directly connected to bundle group nodes. -// | 'references' -// // Signals that the dependency is internally resolvable via the bundle's ancestry, -// // and that the bundle connected to the dependency is not necessary for the source bundle. -// | BundleGraphEdgeTypeIds.internal_async; - type InternalSymbolResolution = {| asset: Asset, exportSymbol: string, diff --git a/packages/core/core/src/RequestTracker.js b/packages/core/core/src/RequestTracker.js index f8f7b569949..82125099b66 100644 --- a/packages/core/core/src/RequestTracker.js +++ b/packages/core/core/src/RequestTracker.js @@ -105,14 +105,6 @@ type RequestGraphNode = | EnvNode | OptionNode; -// type RequestGraphEdgeType = -// | 'subrequest' -// | 'invalidated_by_update' -// | 'invalidated_by_delete' -// | 'invalidated_by_create' -// | 'invalidated_by_create_above' -// | 'dirname'; - export type RunAPI = {| invalidateOnFileCreate: FileCreateInvalidation => void, invalidateOnFileDelete: FilePath => void, From 4fd4898d8a5f3dc8f20f48dab50ce6a1add716f8 Mon Sep 17 00:00:00 2001 From: thebriando Date: Wed, 14 Apr 2021 16:56:50 -0700 Subject: [PATCH 3/8] fix edge colors in graphviz --- packages/core/core/src/Parcel.js | 20 ++++++++--- packages/core/core/src/dumpGraphToGraphViz.js | 9 ++++- .../core/src/requests/BundleGraphRequest.js | 34 ++++++++++++++----- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/packages/core/core/src/Parcel.js b/packages/core/core/src/Parcel.js index 793025e7c22..c7ee48a508c 100644 --- a/packages/core/core/src/Parcel.js +++ b/packages/core/core/src/Parcel.js @@ -33,7 +33,11 @@ import {AbortController} from 'abortcontroller-polyfill/dist/cjs-ponyfill'; import {PromiseQueue} from '@parcel/utils'; import ParcelConfig from './ParcelConfig'; import logger from '@parcel/logger'; -import RequestTracker, {getWatcherOptions} from './RequestTracker'; +import {BundleGraphEdgeTypes} from './BundleGraph'; +import RequestTracker, { + getWatcherOptions, + RequestGraphEdgeTypes, +} from './RequestTracker'; import createAssetGraphRequest from './requests/AssetGraphRequest'; import createValidationRequest from './requests/ValidationRequest'; import createBundleGraphRequest from './requests/BundleGraphRequest'; @@ -295,14 +299,22 @@ export default class Parcel { bundleGraphRequest, ); - // $FlowFixMe Added in Flow 0.121.0 upgrade in #4381 (Windows only) - dumpGraphToGraphViz(bundleGraph._graph, 'BundleGraph'); + dumpGraphToGraphViz( + // $FlowFixMe Added in Flow 0.121.0 upgrade in #4381 (Windows only) + bundleGraph._graph, + 'BundleGraph', + BundleGraphEdgeTypes, + ); await this.#packagerRunner.writeBundles(bundleGraph); assertSignalNotAborted(signal); // $FlowFixMe - dumpGraphToGraphViz(this.#requestTracker.graph, 'RequestGraph'); + dumpGraphToGraphViz( + this.#requestTracker.graph, + 'RequestGraph', + RequestGraphEdgeTypes, + ); let event = { type: 'buildSuccess', diff --git a/packages/core/core/src/dumpGraphToGraphViz.js b/packages/core/core/src/dumpGraphToGraphViz.js index a9bcc050697..69491ce7fa1 100644 --- a/packages/core/core/src/dumpGraphToGraphViz.js +++ b/packages/core/core/src/dumpGraphToGraphViz.js @@ -32,6 +32,7 @@ export default async function dumpGraphToGraphViz( // $FlowFixMe graph: Graph | Graph, name: string, + edgeTypes?: any, ): Promise { if ( process.env.PARCEL_BUILD_ENV === 'production' || @@ -128,9 +129,15 @@ export default async function dumpGraphToGraphViz( } n.set('label', label); } + let edgeNames; + if (edgeTypes) { + edgeNames = Object.fromEntries( + Object.entries(edgeTypes).map(([k, v]) => [v, k]), + ); + } for (let edge of graph.getAllEdges()) { let gEdge = g.addEdge(edge.from, edge.to); - let color = edge.type != null ? TYPE_COLORS[edge.type] : null; + let color = edgeNames ? TYPE_COLORS[edgeNames[edge.type]] : null; if (color != null) { gEdge.set('color', color); } diff --git a/packages/core/core/src/requests/BundleGraphRequest.js b/packages/core/core/src/requests/BundleGraphRequest.js index 45e6f7ab58e..d62234413fe 100644 --- a/packages/core/core/src/requests/BundleGraphRequest.js +++ b/packages/core/core/src/requests/BundleGraphRequest.js @@ -19,7 +19,7 @@ import {PluginLogger} from '@parcel/logger'; import ThrowableDiagnostic, {errorToDiagnostic} from '@parcel/diagnostic'; import AssetGraph from '../AssetGraph'; import BundleGraph from '../public/BundleGraph'; -import InternalBundleGraph from '../BundleGraph'; +import InternalBundleGraph, {BundleGraphEdgeTypes} from '../BundleGraph'; import MutableBundleGraph from '../public/MutableBundleGraph'; import {Bundle, NamedBundle} from '../public/Bundle'; import {report} from '../ReporterRunner'; @@ -196,8 +196,12 @@ class BundlerRunner { } let internalBundleGraph = InternalBundleGraph.fromAssetGraph(graph); - // $FlowFixMe - await dumpGraphToGraphViz(internalBundleGraph._graph, 'before_bundle'); + await dumpGraphToGraphViz( + // $FlowFixMe + internalBundleGraph._graph, + 'before_bundle', + BundleGraphEdgeTypes, + ); let mutableBundleGraph = new MutableBundleGraph( internalBundleGraph, this.options, @@ -219,8 +223,12 @@ class BundlerRunner { }), }); } finally { - // $FlowFixMe[incompatible-call] - await dumpGraphToGraphViz(internalBundleGraph._graph, 'after_bundle'); + await dumpGraphToGraphViz( + // $FlowFixMe[incompatible-call] + internalBundleGraph._graph, + 'after_bundle', + BundleGraphEdgeTypes, + ); } if (this.pluginOptions.mode === 'production') { @@ -238,8 +246,12 @@ class BundlerRunner { }), }); } finally { - // $FlowFixMe[incompatible-call] - await dumpGraphToGraphViz(internalBundleGraph._graph, 'after_optimize'); + await dumpGraphToGraphViz( + // $FlowFixMe[incompatible-call] + internalBundleGraph._graph, + 'after_optimize', + BundleGraphEdgeTypes, + ); } } @@ -270,8 +282,12 @@ class BundlerRunner { configs: this.configs, }); - // $FlowFixMe - await dumpGraphToGraphViz(internalBundleGraph._graph, 'after_runtimes'); + await dumpGraphToGraphViz( + // $FlowFixMe + internalBundleGraph._graph, + 'after_runtimes', + BundleGraphEdgeTypes, + ); // Store the serialized bundle graph in an in memory cache so that we avoid serializing it // many times to send to each worker, and in build mode, when writing to cache on shutdown. From bb58ee8226d6f6f5f49c23037f1ad434b4155e96 Mon Sep 17 00:00:00 2001 From: thebriando Date: Wed, 14 Apr 2021 22:55:49 -0700 Subject: [PATCH 4/8] camelCase edge type objects --- packages/core/core/src/BundleGraph.js | 86 +++++++++---------- packages/core/core/src/Parcel.js | 10 +-- packages/core/core/src/RequestTracker.js | 70 +++++++-------- packages/core/core/src/applyRuntimes.js | 4 +- .../core/src/public/MutableBundleGraph.js | 10 +-- .../core/src/requests/BundleGraphRequest.js | 10 +-- 6 files changed, 95 insertions(+), 95 deletions(-) diff --git a/packages/core/core/src/BundleGraph.js b/packages/core/core/src/BundleGraph.js index 7d727393598..51f92c4f17e 100644 --- a/packages/core/core/src/BundleGraph.js +++ b/packages/core/core/src/BundleGraph.js @@ -29,7 +29,7 @@ import {getBundleGroupId, getPublicId} from './utils'; import Graph, {ALL_EDGE_TYPES, mapVisitor, type GraphOpts} from './Graph'; import Environment from './public/Environment'; -export const BundleGraphEdgeTypes = { +export const bundleGraphEdgeTypes = { // A lack of an edge type indicates to follow the edge while traversing // the bundle's contents, e.g. `bundle.traverse()` during packaging. null: 0, @@ -54,7 +54,7 @@ export const BundleGraphEdgeTypes = { internal_async: 4, }; -type BundleGraphEdgeType = $Values; +type BundleGraphEdgeType = $Values; type InternalSymbolResolution = {| asset: Asset, @@ -158,7 +158,7 @@ export default class BundleGraph { fromIds = [ ...assetGraph.inboundEdges.getEdges( edge.from, - BundleGraphEdgeTypes.null, + bundleGraphEdgeTypes.null, ), ]; } else { @@ -169,7 +169,7 @@ export default class BundleGraph { if (assetGroupIds.has(edge.to)) { for (let to of assetGraph.outboundEdges.getEdges( edge.to, - BundleGraphEdgeTypes.null, + bundleGraphEdgeTypes.null, )) { graph.addEdge(from, to); } @@ -231,7 +231,7 @@ export default class BundleGraph { } if (node.type === 'asset' || node.type === 'dependency') { - this._graph.addEdge(bundle.id, node.id, BundleGraphEdgeTypes.contains); + this._graph.addEdge(bundle.id, node.id, bundleGraphEdgeTypes.contains); } if (node.type === 'dependency') { @@ -242,7 +242,7 @@ export default class BundleGraph { this._graph.addEdge( bundle.id, bundleGroupNode.id, - BundleGraphEdgeTypes.bundle, + bundleGraphEdgeTypes.bundle, ); } @@ -250,13 +250,13 @@ export default class BundleGraph { // the source bundle to the dependency for easy traversal. if ( this._graph - .getNodesConnectedFrom(node, BundleGraphEdgeTypes.references) + .getNodesConnectedFrom(node, bundleGraphEdgeTypes.references) .some(node => node.type === 'bundle') ) { this._graph.addEdge( bundle.id, node.id, - BundleGraphEdgeTypes.references, + bundleGraphEdgeTypes.references, ); } } @@ -283,7 +283,7 @@ export default class BundleGraph { this._graph.addEdge( bundle.id, dependency.id, - BundleGraphEdgeTypes.internal_async, + bundleGraphEdgeTypes.internal_async, ); this.removeExternalDependency(bundle, dependency); } @@ -298,7 +298,7 @@ export default class BundleGraph { return this._graph .getNodesConnectedTo( nullthrows(this._graph.getNode(getBundleGroupId(bundleGroup))), - BundleGraphEdgeTypes.bundle, + bundleGraphEdgeTypes.bundle, ) .filter(node => node.type === 'bundle') .map(node => { @@ -321,12 +321,12 @@ export default class BundleGraph { this._graph.hasEdge( bundle.id, depNode.id, - BundleGraphEdgeTypes.internal_async, + bundleGraphEdgeTypes.internal_async, ) ) { let referencedAssetNode = this._graph.getNodesConnectedFrom( depNode, - BundleGraphEdgeTypes.references, + bundleGraphEdgeTypes.references, )[0]; let resolved; @@ -383,7 +383,7 @@ export default class BundleGraph { let referenced = this._graph .getNodesConnectedFrom( nullthrows(this._graph.getNode(dependency.id)), - BundleGraphEdgeTypes.references, + bundleGraphEdgeTypes.references, ) .find(node => node.type === 'asset'); @@ -409,12 +409,12 @@ export default class BundleGraph { } if ( - this._graph.hasEdge(bundle.id, node.id, BundleGraphEdgeTypes.contains) + this._graph.hasEdge(bundle.id, node.id, bundleGraphEdgeTypes.contains) ) { this._graph.removeEdge( bundle.id, node.id, - BundleGraphEdgeTypes.contains, + bundleGraphEdgeTypes.contains, // Removing this contains edge should not orphan the connected node. This // is disabled for performance reasons as these edges are removed as part // of a traversal, and checking for orphans becomes quite expensive in @@ -447,13 +447,13 @@ export default class BundleGraph { this._graph.hasEdge( bundle.id, node.id, - BundleGraphEdgeTypes.references, + bundleGraphEdgeTypes.references, ) ) { this._graph.removeEdge( bundle.id, node.id, - BundleGraphEdgeTypes.references, + bundleGraphEdgeTypes.references, ); } } @@ -474,7 +474,7 @@ export default class BundleGraph { let bundleGroupNodes = this._graph.getNodesConnectedTo( bundleNode, - BundleGraphEdgeTypes.bundle, + bundleGraphEdgeTypes.bundle, ); this._graph.removeNode(bundleNode); @@ -534,7 +534,7 @@ export default class BundleGraph { !this._graph.hasEdge( bundle.id, bundleGroupNode.id, - BundleGraphEdgeTypes.bundle, + bundleGraphEdgeTypes.bundle, ) ) { continue; @@ -558,14 +558,14 @@ export default class BundleGraph { this._graph.hasEdge( bundle.id, dependency.id, - BundleGraphEdgeTypes.internal_async, + bundleGraphEdgeTypes.internal_async, ), ) ) { this._graph.removeEdge( bundle.id, bundleGroupNode.id, - BundleGraphEdgeTypes.bundle, + bundleGraphEdgeTypes.bundle, ); } } @@ -579,12 +579,12 @@ export default class BundleGraph { this._graph.addEdge( dependency.id, asset.id, - BundleGraphEdgeTypes.references, + bundleGraphEdgeTypes.references, ); this._graph.addEdge( dependency.id, bundle.id, - BundleGraphEdgeTypes.references, + bundleGraphEdgeTypes.references, ); if (this._graph.hasEdge(dependency.id, asset.id)) { this._graph.removeEdge(dependency.id, asset.id); @@ -592,14 +592,14 @@ export default class BundleGraph { } createBundleReference(from: Bundle, to: Bundle): void { - this._graph.addEdge(from.id, to.id, BundleGraphEdgeTypes.references); + this._graph.addEdge(from.id, to.id, bundleGraphEdgeTypes.references); } findBundlesWithAsset(asset: Asset): Array { return this._graph .getNodesConnectedTo( nullthrows(this._graph.getNode(asset.id)), - BundleGraphEdgeTypes.contains, + bundleGraphEdgeTypes.contains, ) .filter(node => node.type === 'bundle') .map(node => { @@ -612,7 +612,7 @@ export default class BundleGraph { return this._graph .getNodesConnectedTo( nullthrows(this._graph.getNode(dependency.id)), - BundleGraphEdgeTypes.contains, + bundleGraphEdgeTypes.contains, ) .filter(node => node.type === 'bundle') .map(node => { @@ -660,7 +660,7 @@ export default class BundleGraph { } }, depNode, - BundleGraphEdgeTypes.references, + bundleGraphEdgeTypes.references, ); } @@ -704,7 +704,7 @@ export default class BundleGraph { // referenced. let asyncInternalReferencingBundles = new Set( this._graph - .getNodesConnectedTo(assetNode, BundleGraphEdgeTypes.references) + .getNodesConnectedTo(assetNode, bundleGraphEdgeTypes.references) .filter(node => node.type === 'dependency') .map(node => { invariant(node.type === 'dependency'); @@ -714,7 +714,7 @@ export default class BundleGraph { this._graph .getNodesConnectedTo( dependencyNode, - BundleGraphEdgeTypes.internal_async, + bundleGraphEdgeTypes.internal_async, ) .map(node => { invariant(node.type === 'bundle'); @@ -818,7 +818,7 @@ export default class BundleGraph { // Get a list of parent bundle nodes pointing to the bundle group let parentBundleNodes = this._graph.getNodesConnectedTo( nullthrows(this._graph.getNode(getBundleGroupId(bundleGroup))), - BundleGraphEdgeTypes.bundle, + bundleGraphEdgeTypes.bundle, ); // Check that every parent bundle has a bundle group in its ancestry that contains the asset. @@ -855,7 +855,7 @@ export default class BundleGraph { } } }, - [BundleGraphEdgeTypes.references, BundleGraphEdgeTypes.bundle], + [bundleGraphEdgeTypes.references, bundleGraphEdgeTypes.bundle], ); return isReachable; @@ -907,7 +907,7 @@ export default class BundleGraph { actions.skipChildren(); } }, - [BundleGraphEdgeTypes.references, BundleGraphEdgeTypes.bundle], + [bundleGraphEdgeTypes.references, bundleGraphEdgeTypes.bundle], ); if (res != null) { @@ -936,7 +936,7 @@ export default class BundleGraph { this._graph.hasEdge( bundle.id, node.id, - BundleGraphEdgeTypes.contains, + bundleGraphEdgeTypes.contains, ) ) { return node; @@ -1009,7 +1009,7 @@ export default class BundleGraph { node => (node.type === 'bundle' ? node.value : null), visit, startBundle ? nullthrows(this._graph.getNode(startBundle.id)) : null, - [BundleGraphEdgeTypes.bundle, BundleGraphEdgeTypes.references], + [bundleGraphEdgeTypes.bundle, bundleGraphEdgeTypes.references], ); } @@ -1048,7 +1048,7 @@ export default class BundleGraph { referencingBundles.add(node.value); } }, - BundleGraphEdgeTypes.references, + bundleGraphEdgeTypes.references, ); return [...referencingBundles]; @@ -1070,7 +1070,7 @@ export default class BundleGraph { return this._graph .getNodesConnectedTo( nullthrows(this._graph.getNode(bundle.id)), - BundleGraphEdgeTypes.bundle, + bundleGraphEdgeTypes.bundle, ) .filter(node => node.type === 'bundle_group') .map(node => { @@ -1086,7 +1086,7 @@ export default class BundleGraph { ); for (let bundleNode of this._graph.getNodesConnectedFrom( bundleGroupNode, - BundleGraphEdgeTypes.bundle, + bundleGraphEdgeTypes.bundle, )) { invariant(bundleNode.type === 'bundle'); let bundle = bundleNode.value; @@ -1128,7 +1128,7 @@ export default class BundleGraph { // they were added. // TODO: Should this be the case? this._graph - .getNodesConnectedFrom(node, BundleGraphEdgeTypes.references) + .getNodesConnectedFrom(node, bundleGraphEdgeTypes.references) .reverse(), }); @@ -1175,7 +1175,7 @@ export default class BundleGraph { return this._graph.hasEdge( bundle.id, asset.id, - BundleGraphEdgeTypes.contains, + bundleGraphEdgeTypes.contains, ); } @@ -1183,7 +1183,7 @@ export default class BundleGraph { return this._graph.hasEdge( bundle.id, dependency.id, - BundleGraphEdgeTypes.contains, + bundleGraphEdgeTypes.contains, ); } @@ -1493,14 +1493,14 @@ export default class BundleGraph { addBundleToBundleGroup(bundle: Bundle, bundleGroup: BundleGroup) { let bundleGroupId = getBundleGroupId(bundleGroup); if ( - this._graph.hasEdge(bundleGroupId, bundle.id, BundleGraphEdgeTypes.bundle) + this._graph.hasEdge(bundleGroupId, bundle.id, bundleGraphEdgeTypes.bundle) ) { // Bundle group already has bundle return; } this._graph.addEdge(bundleGroupId, bundle.id); - this._graph.addEdge(bundleGroupId, bundle.id, BundleGraphEdgeTypes.bundle); + this._graph.addEdge(bundleGroupId, bundle.id, bundleGraphEdgeTypes.bundle); for (let entryAssetId of bundle.entryAssetIds) { if (this._graph.hasEdge(bundleGroupId, entryAssetId)) { @@ -1561,7 +1561,7 @@ export default class BundleGraph { return this._graph .getNodesConnectedTo( nullthrows(this._graph.getNode(getBundleGroupId(bundleGroup))), - BundleGraphEdgeTypes.bundle, + bundleGraphEdgeTypes.bundle, ) .some(n => n.type === 'root'); } diff --git a/packages/core/core/src/Parcel.js b/packages/core/core/src/Parcel.js index c7ee48a508c..5f36a2fc0d0 100644 --- a/packages/core/core/src/Parcel.js +++ b/packages/core/core/src/Parcel.js @@ -33,10 +33,10 @@ import {AbortController} from 'abortcontroller-polyfill/dist/cjs-ponyfill'; import {PromiseQueue} from '@parcel/utils'; import ParcelConfig from './ParcelConfig'; import logger from '@parcel/logger'; -import {BundleGraphEdgeTypes} from './BundleGraph'; +import {bundleGraphEdgeTypes} from './BundleGraph'; import RequestTracker, { getWatcherOptions, - RequestGraphEdgeTypes, + requestGraphEdgeTypes, } from './RequestTracker'; import createAssetGraphRequest from './requests/AssetGraphRequest'; import createValidationRequest from './requests/ValidationRequest'; @@ -303,17 +303,17 @@ export default class Parcel { // $FlowFixMe Added in Flow 0.121.0 upgrade in #4381 (Windows only) bundleGraph._graph, 'BundleGraph', - BundleGraphEdgeTypes, + bundleGraphEdgeTypes, ); await this.#packagerRunner.writeBundles(bundleGraph); assertSignalNotAborted(signal); - // $FlowFixMe dumpGraphToGraphViz( + // $FlowFixMe this.#requestTracker.graph, 'RequestGraph', - RequestGraphEdgeTypes, + requestGraphEdgeTypes, ); let event = { diff --git a/packages/core/core/src/RequestTracker.js b/packages/core/core/src/RequestTracker.js index 82125099b66..f13efe61bbe 100644 --- a/packages/core/core/src/RequestTracker.js +++ b/packages/core/core/src/RequestTracker.js @@ -37,7 +37,7 @@ import { ERROR, } from './constants'; -export const RequestGraphEdgeTypes = { +export const requestGraphEdgeTypes = { subrequest: 1, invalidated_by_update: 2, invalidated_by_delete: 3, @@ -46,7 +46,7 @@ export const RequestGraphEdgeTypes = { dirname: 6, }; -type RequestGraphEdgeType = $Values; +type RequestGraphEdgeType = $Values; type SerializedRequestGraph = {| ...GraphOpts, invalidNodeIds: Set, @@ -275,7 +275,7 @@ export class RequestGraph extends Graph< requestNode, subrequestNodes, null, - RequestGraphEdgeTypes.subrequest, + requestGraphEdgeTypes.subrequest, ); } @@ -287,7 +287,7 @@ export class RequestGraph extends Graph< let parentNodes = this.getNodesConnectedTo( node, - RequestGraphEdgeTypes.subrequest, + requestGraphEdgeTypes.subrequest, ); for (let parentNode of parentNodes) { this.invalidateNode(parentNode, reason); @@ -310,7 +310,7 @@ export class RequestGraph extends Graph< if (env[node.value.key] !== node.value.value) { let parentNodes = this.getNodesConnectedTo( node, - RequestGraphEdgeTypes.invalidated_by_update, + requestGraphEdgeTypes.invalidated_by_update, ); for (let parentNode of parentNodes) { this.invalidateNode(parentNode, ENV_CHANGE); @@ -326,7 +326,7 @@ export class RequestGraph extends Graph< if (hashFromOption(options[node.value.key]) !== node.value.hash) { let parentNodes = this.getNodesConnectedTo( node, - RequestGraphEdgeTypes.invalidated_by_update, + requestGraphEdgeTypes.invalidated_by_update, ); for (let parentNode of parentNodes) { this.invalidateNode(parentNode, OPTION_CHANGE); @@ -346,13 +346,13 @@ export class RequestGraph extends Graph< !this.hasEdge( requestNode.id, fileNode.id, - RequestGraphEdgeTypes.invalidated_by_update, + requestGraphEdgeTypes.invalidated_by_update, ) ) { this.addEdge( requestNode.id, fileNode.id, - RequestGraphEdgeTypes.invalidated_by_update, + requestGraphEdgeTypes.invalidated_by_update, ); } } @@ -368,13 +368,13 @@ export class RequestGraph extends Graph< !this.hasEdge( requestNode.id, fileNode.id, - RequestGraphEdgeTypes.invalidated_by_delete, + requestGraphEdgeTypes.invalidated_by_delete, ) ) { this.addEdge( requestNode.id, fileNode.id, - RequestGraphEdgeTypes.invalidated_by_delete, + requestGraphEdgeTypes.invalidated_by_delete, ); } } @@ -402,9 +402,9 @@ export class RequestGraph extends Graph< if ( last != null && - !this.hasEdge(last.id, fileNameNode.id, RequestGraphEdgeTypes.dirname) + !this.hasEdge(last.id, fileNameNode.id, requestGraphEdgeTypes.dirname) ) { - this.addEdge(last.id, fileNameNode.id, RequestGraphEdgeTypes.dirname); + this.addEdge(last.id, fileNameNode.id, requestGraphEdgeTypes.dirname); } last = fileNameNode; @@ -431,13 +431,13 @@ export class RequestGraph extends Graph< !this.hasEdge( node.id, firstId, - RequestGraphEdgeTypes.invalidated_by_create_above, + requestGraphEdgeTypes.invalidated_by_create_above, ) ) { this.addEdge( node.id, firstId, - RequestGraphEdgeTypes.invalidated_by_create_above, + requestGraphEdgeTypes.invalidated_by_create_above, ); } @@ -446,13 +446,13 @@ export class RequestGraph extends Graph< !this.hasEdge( last.id, node.id, - RequestGraphEdgeTypes.invalidated_by_create_above, + requestGraphEdgeTypes.invalidated_by_create_above, ) ) { this.addEdge( last.id, node.id, - RequestGraphEdgeTypes.invalidated_by_create_above, + requestGraphEdgeTypes.invalidated_by_create_above, ); } } else if (input.filePath != null) { @@ -469,13 +469,13 @@ export class RequestGraph extends Graph< !this.hasEdge( requestNode.id, node.id, - RequestGraphEdgeTypes.invalidated_by_create, + requestGraphEdgeTypes.invalidated_by_create, ) ) { this.addEdge( requestNode.id, node.id, - RequestGraphEdgeTypes.invalidated_by_create, + requestGraphEdgeTypes.invalidated_by_create, ); } } @@ -496,13 +496,13 @@ export class RequestGraph extends Graph< !this.hasEdge( requestNode.id, envNode.id, - RequestGraphEdgeTypes.invalidated_by_update, + requestGraphEdgeTypes.invalidated_by_update, ) ) { this.addEdge( requestNode.id, envNode.id, - RequestGraphEdgeTypes.invalidated_by_update, + requestGraphEdgeTypes.invalidated_by_update, ); } } @@ -518,13 +518,13 @@ export class RequestGraph extends Graph< !this.hasEdge( requestNode.id, optionNode.id, - RequestGraphEdgeTypes.invalidated_by_update, + requestGraphEdgeTypes.invalidated_by_update, ) ) { this.addEdge( requestNode.id, optionNode.id, - RequestGraphEdgeTypes.invalidated_by_update, + requestGraphEdgeTypes.invalidated_by_update, ); } } @@ -535,19 +535,19 @@ export class RequestGraph extends Graph< node, [], null, - RequestGraphEdgeTypes.invalidated_by_update, + requestGraphEdgeTypes.invalidated_by_update, ); this.replaceNodesConnectedTo( node, [], null, - RequestGraphEdgeTypes.invalidated_by_delete, + requestGraphEdgeTypes.invalidated_by_delete, ); this.replaceNodesConnectedTo( node, [], null, - RequestGraphEdgeTypes.invalidated_by_create, + requestGraphEdgeTypes.invalidated_by_create, ); } @@ -560,7 +560,7 @@ export class RequestGraph extends Graph< let requestNode = this.getRequestNode(requestId); let invalidations = this.getNodesConnectedFrom( requestNode, - RequestGraphEdgeTypes.invalidated_by_update, + requestGraphEdgeTypes.invalidated_by_update, ); return invalidations .map(node => { @@ -584,7 +584,7 @@ export class RequestGraph extends Graph< let requestNode = this.getRequestNode(requestId); let subRequests = this.getNodesConnectedFrom( requestNode, - RequestGraphEdgeTypes.subrequest, + requestGraphEdgeTypes.subrequest, ); return subRequests.map(node => { @@ -607,13 +607,13 @@ export class RequestGraph extends Graph< this.hasEdge( node.id, matchNode.id, - RequestGraphEdgeTypes.invalidated_by_create_above, + requestGraphEdgeTypes.invalidated_by_create_above, ) && isDirectoryInside(path.dirname(matchNode.value.filePath), dirname) ) { let connectedNodes = this.getNodesConnectedTo( matchNode, - RequestGraphEdgeTypes.invalidated_by_create, + requestGraphEdgeTypes.invalidated_by_create, ); for (let connectedNode of connectedNodes) { this.invalidateNode(connectedNode, FILE_CREATE); @@ -627,7 +627,7 @@ export class RequestGraph extends Graph< let parent = this.getNode('file_name:' + basename); if ( parent != null && - this.hasEdge(node.id, parent.id, RequestGraphEdgeTypes.dirname) + this.hasEdge(node.id, parent.id, requestGraphEdgeTypes.dirname) ) { invariant(parent.type === 'file_name'); this.invalidateFileNameNode(parent, dirname, matchNodes); @@ -645,7 +645,7 @@ export class RequestGraph extends Graph< if (node && (type === 'create' || type === 'update')) { let nodes = this.getNodesConnectedTo( node, - RequestGraphEdgeTypes.invalidated_by_update, + requestGraphEdgeTypes.invalidated_by_update, ); for (let connectedNode of nodes) { didInvalidate = true; @@ -655,7 +655,7 @@ export class RequestGraph extends Graph< if (type === 'create') { let nodes = this.getNodesConnectedTo( node, - RequestGraphEdgeTypes.invalidated_by_create, + requestGraphEdgeTypes.invalidated_by_create, ); for (let connectedNode of nodes) { didInvalidate = true; @@ -669,7 +669,7 @@ export class RequestGraph extends Graph< // Find potential file nodes to be invalidated if this file name pattern matches let above = this.getNodesConnectedTo( fileNameNode, - RequestGraphEdgeTypes.invalidated_by_create_above, + requestGraphEdgeTypes.invalidated_by_create_above, ).map(node => { invariant(node.type === 'file'); return node; @@ -688,7 +688,7 @@ export class RequestGraph extends Graph< if (isGlobMatch(filePath, globNode.value)) { let connectedNodes = this.getNodesConnectedTo( globNode, - RequestGraphEdgeTypes.invalidated_by_create, + requestGraphEdgeTypes.invalidated_by_create, ); for (let connectedNode of connectedNodes) { didInvalidate = true; @@ -699,7 +699,7 @@ export class RequestGraph extends Graph< } else if (node && type === 'delete') { for (let connectedNode of this.getNodesConnectedTo( node, - RequestGraphEdgeTypes.invalidated_by_delete, + requestGraphEdgeTypes.invalidated_by_delete, )) { didInvalidate = true; this.invalidateNode(connectedNode, FILE_DELETE); diff --git a/packages/core/core/src/applyRuntimes.js b/packages/core/core/src/applyRuntimes.js index 3609f8798e8..97daaaf5d2a 100644 --- a/packages/core/core/src/applyRuntimes.js +++ b/packages/core/core/src/applyRuntimes.js @@ -19,7 +19,7 @@ import invariant from 'assert'; import nullthrows from 'nullthrows'; import AssetGraph, {nodeFromAssetGroup} from './AssetGraph'; import BundleGraph from './public/BundleGraph'; -import InternalBundleGraph, {BundleGraphEdgeTypes} from './BundleGraph'; +import InternalBundleGraph, {bundleGraphEdgeTypes} from './BundleGraph'; import {NamedBundle} from './public/Bundle'; import {PluginLogger} from '@parcel/logger'; import ThrowableDiagnostic, {errorToDiagnostic} from '@parcel/diagnostic'; @@ -190,7 +190,7 @@ export default async function applyRuntimes({ bundleGraph._graph.addEdge( bundle.id, node.id, - BundleGraphEdgeTypes.contains, + bundleGraphEdgeTypes.contains, ); } }, runtimeNode); diff --git a/packages/core/core/src/public/MutableBundleGraph.js b/packages/core/core/src/public/MutableBundleGraph.js index a190cfff7a7..413f300db56 100644 --- a/packages/core/core/src/public/MutableBundleGraph.js +++ b/packages/core/core/src/public/MutableBundleGraph.js @@ -18,7 +18,7 @@ import path from 'path'; import nullthrows from 'nullthrows'; import {md5FromString} from '@parcel/utils'; import BundleGraph from './BundleGraph'; -import InternalBundleGraph, {BundleGraphEdgeTypes} from '../BundleGraph'; +import InternalBundleGraph, {bundleGraphEdgeTypes} from '../BundleGraph'; import {Bundle, bundleToInternalBundle} from './Bundle'; import {mapVisitor, ALL_EDGE_TYPES} from '../Graph'; import {assetFromValue, assetToAssetValue} from './Asset'; @@ -101,7 +101,7 @@ export default class MutableBundleGraph extends BundleGraph this.#graph._graph.addEdge( dependencyNode.id, resolved.id, - BundleGraphEdgeTypes.references, + bundleGraphEdgeTypes.references, ); this.#graph._graph.removeEdge(dependencyNode.id, resolved.id); @@ -109,19 +109,19 @@ export default class MutableBundleGraph extends BundleGraph this.#graph._graph.addEdge( nullthrows(this.#graph._graph.getRootNode()).id, bundleGroupNode.id, - BundleGraphEdgeTypes.bundle, + bundleGraphEdgeTypes.bundle, ); } else { let inboundBundleNodes = this.#graph._graph.getNodesConnectedTo( dependencyNode, - BundleGraphEdgeTypes.contains, + bundleGraphEdgeTypes.contains, ); for (let inboundBundleNode of inboundBundleNodes) { invariant(inboundBundleNode.type === 'bundle'); this.#graph._graph.addEdge( inboundBundleNode.id, bundleGroupNode.id, - BundleGraphEdgeTypes.bundle, + bundleGraphEdgeTypes.bundle, ); } } diff --git a/packages/core/core/src/requests/BundleGraphRequest.js b/packages/core/core/src/requests/BundleGraphRequest.js index d62234413fe..52f61e69895 100644 --- a/packages/core/core/src/requests/BundleGraphRequest.js +++ b/packages/core/core/src/requests/BundleGraphRequest.js @@ -19,7 +19,7 @@ import {PluginLogger} from '@parcel/logger'; import ThrowableDiagnostic, {errorToDiagnostic} from '@parcel/diagnostic'; import AssetGraph from '../AssetGraph'; import BundleGraph from '../public/BundleGraph'; -import InternalBundleGraph, {BundleGraphEdgeTypes} from '../BundleGraph'; +import InternalBundleGraph, {bundleGraphEdgeTypes} from '../BundleGraph'; import MutableBundleGraph from '../public/MutableBundleGraph'; import {Bundle, NamedBundle} from '../public/Bundle'; import {report} from '../ReporterRunner'; @@ -200,7 +200,7 @@ class BundlerRunner { // $FlowFixMe internalBundleGraph._graph, 'before_bundle', - BundleGraphEdgeTypes, + bundleGraphEdgeTypes, ); let mutableBundleGraph = new MutableBundleGraph( internalBundleGraph, @@ -227,7 +227,7 @@ class BundlerRunner { // $FlowFixMe[incompatible-call] internalBundleGraph._graph, 'after_bundle', - BundleGraphEdgeTypes, + bundleGraphEdgeTypes, ); } @@ -250,7 +250,7 @@ class BundlerRunner { // $FlowFixMe[incompatible-call] internalBundleGraph._graph, 'after_optimize', - BundleGraphEdgeTypes, + bundleGraphEdgeTypes, ); } } @@ -286,7 +286,7 @@ class BundlerRunner { // $FlowFixMe internalBundleGraph._graph, 'after_runtimes', - BundleGraphEdgeTypes, + bundleGraphEdgeTypes, ); // Store the serialized bundle graph in an in memory cache so that we avoid serializing it From 1a07b1efcd1f5e93dd6e53286435d6744445474e Mon Sep 17 00:00:00 2001 From: thebriando Date: Wed, 14 Apr 2021 22:59:03 -0700 Subject: [PATCH 5/8] add NullEdgeType to generic in Array edge types --- packages/core/core/src/Graph.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/core/core/src/Graph.js b/packages/core/core/src/Graph.js index c465f7dfa7a..cf07afd3bc2 100644 --- a/packages/core/core/src/Graph.js +++ b/packages/core/core/src/Graph.js @@ -122,7 +122,7 @@ export default class Graph { getNodesConnectedTo( node: TNode, - type: TEdgeType | NullEdgeType | Array = 0, + type: TEdgeType | NullEdgeType | Array = 0, ): Array { assertHasNode(this, node); @@ -155,7 +155,7 @@ export default class Graph { getNodesConnectedFrom( node: TNode, - type: TEdgeType | NullEdgeType | Array = 0, + type: TEdgeType | NullEdgeType | Array = 0, ): Array { assertHasNode(this, node); @@ -344,7 +344,7 @@ export default class Graph { traverse( visit: GraphVisitor, startNode: ?TNode, - type: TEdgeType | NullEdgeType | Array = 0, + type: TEdgeType | NullEdgeType | Array = 0, ): ?TContext { return this.dfs({ visit, @@ -357,7 +357,7 @@ export default class Graph { filter: (TNode, TraversalActions) => ?TValue, visit: GraphVisitor, startNode: ?TNode, - type?: TEdgeType | Array, + type?: TEdgeType | Array, ): ?TContext { return this.traverse(mapVisitor(filter, visit), startNode, type); } @@ -365,7 +365,7 @@ export default class Graph { traverseAncestors( startNode: TNode, visit: GraphVisitor, - type: TEdgeType | NullEdgeType | Array = 0, + type: TEdgeType | NullEdgeType | Array = 0, ): ?TContext { return this.dfs({ visit, From e85fc92ea85dcd9daad50e2a6961f616329bb750 Mon Sep 17 00:00:00 2001 From: thebriando Date: Thu, 5 Aug 2021 11:28:28 -0700 Subject: [PATCH 6/8] Change NullEdgeType to 1 --- packages/core/core/src/BundleGraph.js | 10 ++++----- packages/core/core/src/ContentGraph.js | 4 ++-- packages/core/core/src/Graph.js | 28 ++++++++++++------------ packages/core/core/src/RequestTracker.js | 12 +++++----- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/packages/core/core/src/BundleGraph.js b/packages/core/core/src/BundleGraph.js index b6b04e43ecf..ab5a129f823 100644 --- a/packages/core/core/src/BundleGraph.js +++ b/packages/core/core/src/BundleGraph.js @@ -39,13 +39,13 @@ import {fromProjectPath} from './projectPath'; export const bundleGraphEdgeTypes = { // A lack of an edge type indicates to follow the edge while traversing // the bundle's contents, e.g. `bundle.traverse()` during packaging. - null: 0, + null: 1, // Used for constant-time checks of presence of a dependency or asset in a bundle, // avoiding bundle traversal in cases like `isAssetInAncestors` - contains: 1, + contains: 2, // Connections between bundles and bundle groups, for quick traversal of the // bundle hierarchy. - bundle: 2, + bundle: 3, // When dependency -> asset: Indicates that the asset a dependency references // is contained in another bundle. // When dependency -> bundle: Indicates the bundle is necessary for any bundles @@ -55,10 +55,10 @@ export const bundleGraphEdgeTypes = { // This type prevents referenced assets from being traversed from dependencies // along the untyped edge, and enables traversal to referenced bundles that are // not directly connected to bundle group nodes. - references: 3, + references: 4, // Signals that the dependency is internally resolvable via the bundle's ancestry, // and that the bundle connected to the dependency is not necessary for the source bundle. - internal_async: 4, + internal_async: 5, }; type BundleGraphEdgeType = $Values; diff --git a/packages/core/core/src/ContentGraph.js b/packages/core/core/src/ContentGraph.js index 44ecc2e9d68..e43da92cddc 100644 --- a/packages/core/core/src/ContentGraph.js +++ b/packages/core/core/src/ContentGraph.js @@ -4,13 +4,13 @@ import type {ContentKey, NodeId} from './types'; import Graph, {type GraphOpts} from './Graph'; import nullthrows from 'nullthrows'; -export type SerializedContentGraph = {| +export type SerializedContentGraph = {| ...GraphOpts, _contentKeyToNodeId: Map, _nodeIdToContentKey: Map, |}; -export default class ContentGraph extends Graph< +export default class ContentGraph extends Graph< TNode, TEdgeType, > { diff --git a/packages/core/core/src/Graph.js b/packages/core/core/src/Graph.js index aa2487c3ee7..d68ced59065 100644 --- a/packages/core/core/src/Graph.js +++ b/packages/core/core/src/Graph.js @@ -7,8 +7,8 @@ import type {TraversalActions, GraphVisitor} from '@parcel/types'; import assert from 'assert'; import nullthrows from 'nullthrows'; -type NullEdgeType = 0; -export type GraphOpts = {| +type NullEdgeType = 1; +export type GraphOpts = {| nodes?: Map, edges?: AdjacencyListMap, rootNodeId?: ?NodeId, @@ -17,12 +17,12 @@ export type GraphOpts = {| export const ALL_EDGE_TYPES = '@@all_edge_types'; -export default class Graph { +export default class Graph { nodes: Map; inboundEdges: AdjacencyList; outboundEdges: AdjacencyList; rootNodeId: ?NodeId; - nextNodeId: number = 0; + nextNodeId: number = 1; constructor(opts: ?GraphOpts) { this.nodes = opts?.nodes || new Map(); @@ -98,7 +98,7 @@ export default class Graph { return this.nodes.get(id); } - addEdge(from: NodeId, to: NodeId, type: TEdgeType | NullEdgeType = 0): void { + addEdge(from: NodeId, to: NodeId, type: TEdgeType | NullEdgeType = 1): void { if (!this.getNode(from)) { throw new Error(`"from" node '${fromNodeId(from)}' not found`); } @@ -114,14 +114,14 @@ export default class Graph { hasEdge( from: NodeId, to: NodeId, - type?: TEdgeType | NullEdgeType = 0, + type?: TEdgeType | NullEdgeType = 1, ): boolean { return this.outboundEdges.hasEdge(from, to, type); } getNodeIdsConnectedTo( nodeId: NodeId, - type: TEdgeType | NullEdgeType | Array = 0, + type: TEdgeType | NullEdgeType | Array = 1, ): Array { this._assertHasNodeId(nodeId); @@ -154,7 +154,7 @@ export default class Graph { getNodeIdsConnectedFrom( nodeId: NodeId, - type: TEdgeType | NullEdgeType | Array = 0, + type: TEdgeType | NullEdgeType | Array = 1, ): Array { this._assertHasNodeId(nodeId); let outboundByType = this.outboundEdges.getEdgesByType(nodeId); @@ -211,7 +211,7 @@ export default class Graph { assert(wasRemoved); } - removeEdges(nodeId: NodeId, type: TEdgeType | NullEdgeType = 0) { + removeEdges(nodeId: NodeId, type: TEdgeType | NullEdgeType = 1) { this._assertHasNodeId(nodeId); for (let to of this.outboundEdges.getEdges(nodeId, type)) { @@ -223,7 +223,7 @@ export default class Graph { removeEdge( from: NodeId, to: NodeId, - type: TEdgeType | NullEdgeType = 0, + type: TEdgeType | NullEdgeType = 1, removeOrphans: boolean = true, ) { if (!this.outboundEdges.hasEdge(from, to, type)) { @@ -295,7 +295,7 @@ export default class Graph { replaceNode( fromNodeId: NodeId, toNodeId: NodeId, - type: TEdgeType | NullEdgeType = 0, + type: TEdgeType | NullEdgeType = 1, ): void { this._assertHasNodeId(fromNodeId); for (let parent of this.inboundEdges.getEdges(fromNodeId, type)) { @@ -310,7 +310,7 @@ export default class Graph { fromNodeId: NodeId, toNodeIds: $ReadOnlyArray, replaceFilter?: null | (NodeId => boolean), - type?: TEdgeType | NullEdgeType = 0, + type?: TEdgeType | NullEdgeType = 1, ): void { this._assertHasNodeId(fromNodeId); @@ -336,7 +336,7 @@ export default class Graph { traverse( visit: GraphVisitor, startNodeId: ?NodeId, - type: TEdgeType | NullEdgeType | Array = 0, + type: TEdgeType | NullEdgeType | Array = 1, ): ?TContext { return this.dfs({ visit, @@ -357,7 +357,7 @@ export default class Graph { traverseAncestors( startNodeId: ?NodeId, visit: GraphVisitor, - type: TEdgeType | NullEdgeType | Array = 0, + type: TEdgeType | NullEdgeType | Array = 1, ): ?TContext { return this.dfs({ visit, diff --git a/packages/core/core/src/RequestTracker.js b/packages/core/core/src/RequestTracker.js index f0be777d813..de5e8c126c0 100644 --- a/packages/core/core/src/RequestTracker.js +++ b/packages/core/core/src/RequestTracker.js @@ -47,12 +47,12 @@ import { } from './constants'; export const requestGraphEdgeTypes = { - subrequest: 1, - invalidated_by_update: 2, - invalidated_by_delete: 3, - invalidated_by_create: 4, - invalidated_by_create_above: 5, - dirname: 6, + subrequest: 2, + invalidated_by_update: 3, + invalidated_by_delete: 4, + invalidated_by_create: 5, + invalidated_by_create_above: 6, + dirname: 7, }; type RequestGraphEdgeType = $Values; From 414a3f26fbf51c4aa979ba4b83ebae646f2ed6e3 Mon Sep 17 00:00:00 2001 From: thebriando Date: Tue, 14 Sep 2021 18:23:21 -0700 Subject: [PATCH 7/8] Fix colors in graphviz --- packages/core/core/src/BundleGraph.js | 2 +- packages/core/core/src/Parcel.js | 13 ++++++++++--- packages/core/core/src/RequestTracker.js | 2 +- packages/core/core/src/dumpGraphToGraphViz.js | 11 +++++++++-- .../core/core/src/requests/ParcelBuildRequest.js | 3 ++- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/core/core/src/BundleGraph.js b/packages/core/core/src/BundleGraph.js index 7977f0aeb5f..6436d994b51 100644 --- a/packages/core/core/src/BundleGraph.js +++ b/packages/core/core/src/BundleGraph.js @@ -60,7 +60,7 @@ export const bundleGraphEdgeTypes = { internal_async: 5, }; -type BundleGraphEdgeType = $Values; +export type BundleGraphEdgeType = $Values; type InternalSymbolResolution = {| asset: Asset, diff --git a/packages/core/core/src/Parcel.js b/packages/core/core/src/Parcel.js index 9aa0d713faa..70b3b24d79f 100644 --- a/packages/core/core/src/Parcel.js +++ b/packages/core/core/src/Parcel.js @@ -31,7 +31,10 @@ import {AbortController} from 'abortcontroller-polyfill/dist/cjs-ponyfill'; import {PromiseQueue} from '@parcel/utils'; import ParcelConfig from './ParcelConfig'; import logger from '@parcel/logger'; -import RequestTracker, {getWatcherOptions} from './RequestTracker'; +import RequestTracker, { + getWatcherOptions, + requestGraphEdgeTypes, +} from './RequestTracker'; import createValidationRequest from './requests/ValidationRequest'; import createParcelBuildRequest from './requests/ParcelBuildRequest'; import {Disposable} from '@parcel/events'; @@ -274,8 +277,12 @@ export default class Parcel { this.#requestedAssetIds.clear(); - // $FlowFixMe - dumpGraphToGraphViz(this.#requestTracker.graph, 'RequestGraph'); + dumpGraphToGraphViz( + // $FlowFixMe + this.#requestTracker.graph, + 'RequestGraph', + requestGraphEdgeTypes, + ); let event = { type: 'buildSuccess', diff --git a/packages/core/core/src/RequestTracker.js b/packages/core/core/src/RequestTracker.js index 531515385dc..6fedb3cf09a 100644 --- a/packages/core/core/src/RequestTracker.js +++ b/packages/core/core/src/RequestTracker.js @@ -54,7 +54,7 @@ export const requestGraphEdgeTypes = { dirname: 7, }; -type RequestGraphEdgeType = $Values; +export type RequestGraphEdgeType = $Values; type SerializedRequestGraph = {| ...SerializedContentGraph, invalidNodeIds: Set, diff --git a/packages/core/core/src/dumpGraphToGraphViz.js b/packages/core/core/src/dumpGraphToGraphViz.js index 1441e5ca3e3..02ed0def369 100644 --- a/packages/core/core/src/dumpGraphToGraphViz.js +++ b/packages/core/core/src/dumpGraphToGraphViz.js @@ -2,6 +2,8 @@ import type {Graph} from '@parcel/graph'; import type {AssetGraphNode, BundleGraphNode, Environment} from './types'; +import type {BundleGraphEdgeType} from './BundleGraph'; +import type {RequestGraphEdgeType} from './RequestTracker'; import path from 'path'; import {fromProjectPathRelative} from './projectPath'; @@ -32,7 +34,7 @@ export default async function dumpGraphToGraphViz( // $FlowFixMe graph: Graph | Graph, name: string, - edgeTypes?: any, + edgeTypes?: BundleGraphEdgeType | RequestGraphEdgeType, ): Promise { if ( process.env.PARCEL_BUILD_ENV === 'production' || @@ -130,15 +132,20 @@ export default async function dumpGraphToGraphViz( } n.set('label', label); } + let edgeNames; if (edgeTypes) { edgeNames = Object.fromEntries( Object.entries(edgeTypes).map(([k, v]) => [v, k]), ); } + for (let edge of graph.getAllEdges()) { let gEdge = g.addEdge(nodeId(edge.from), nodeId(edge.to)); - let color = edge.type != null ? TYPE_COLORS[edge.type] : null; + let color = null; + if (edge.type != 1 && edgeNames) { + color = TYPE_COLORS[edgeNames[edge.type]]; + } if (color != null) { gEdge.set('color', color); } diff --git a/packages/core/core/src/requests/ParcelBuildRequest.js b/packages/core/core/src/requests/ParcelBuildRequest.js index 56b690afe78..ea5a2a00215 100644 --- a/packages/core/core/src/requests/ParcelBuildRequest.js +++ b/packages/core/core/src/requests/ParcelBuildRequest.js @@ -14,6 +14,7 @@ import createBundleGraphRequest from './BundleGraphRequest'; import createWriteBundlesRequest from './WriteBundlesRequest'; import {assertSignalNotAborted} from '../utils'; import dumpGraphToGraphViz from '../dumpGraphToGraphViz'; +import {bundleGraphEdgeTypes} from '../BundleGraph'; type ParcelBuildRequestInput = {| optionsRef: SharedReference, @@ -75,7 +76,7 @@ async function run({input, api, options}: RunInput) { let bundleGraph = await api.runRequest(bundleGraphRequest); // $FlowFixMe Added in Flow 0.121.0 upgrade in #4381 (Windows only) - dumpGraphToGraphViz(bundleGraph._graph, 'BundleGraph'); + dumpGraphToGraphViz(bundleGraph._graph, 'BundleGraph', bundleGraphEdgeTypes); let writeBundlesRequest = createWriteBundlesRequest({ bundleGraph, From 2951a01eefdb1c1c1c39bfd62527e7457c15bf01 Mon Sep 17 00:00:00 2001 From: thebriando Date: Tue, 14 Sep 2021 22:07:27 -0700 Subject: [PATCH 8/8] Fix types --- packages/core/core/src/dumpGraphToGraphViz.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/core/src/dumpGraphToGraphViz.js b/packages/core/core/src/dumpGraphToGraphViz.js index 02ed0def369..65ec13025cd 100644 --- a/packages/core/core/src/dumpGraphToGraphViz.js +++ b/packages/core/core/src/dumpGraphToGraphViz.js @@ -2,8 +2,8 @@ import type {Graph} from '@parcel/graph'; import type {AssetGraphNode, BundleGraphNode, Environment} from './types'; -import type {BundleGraphEdgeType} from './BundleGraph'; -import type {RequestGraphEdgeType} from './RequestTracker'; +import {bundleGraphEdgeTypes} from './BundleGraph'; +import {requestGraphEdgeTypes} from './RequestTracker'; import path from 'path'; import {fromProjectPathRelative} from './projectPath'; @@ -34,7 +34,7 @@ export default async function dumpGraphToGraphViz( // $FlowFixMe graph: Graph | Graph, name: string, - edgeTypes?: BundleGraphEdgeType | RequestGraphEdgeType, + edgeTypes?: typeof bundleGraphEdgeTypes | typeof requestGraphEdgeTypes, ): Promise { if ( process.env.PARCEL_BUILD_ENV === 'production' ||