@atlaspack/bundler-experimental 2.12.1-canary.3625 → 2.12.1-canary.3627

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.
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.EdgeContentGraph = void 0;
7
+ function _graph() {
8
+ const data = require("@atlaspack/graph");
9
+ _graph = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _nullthrows() {
15
+ const data = _interopRequireDefault(require("nullthrows"));
16
+ _nullthrows = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
+ /**
23
+ * A `ContentGraph` that also stores weights on edges.
24
+ *
25
+ * @template N The type of the node weight.
26
+ * @template EW The type of the edge weight.
27
+ */
28
+ class EdgeContentGraph extends _graph().ContentGraph {
29
+ #edgeWeights = new Map();
30
+ clone() {
31
+ const newGraph = new EdgeContentGraph();
32
+ let nodeId = 0;
33
+ for (let node of this.nodes) {
34
+ const contentKey = this._nodeIdToContentKey.get(nodeId);
35
+ if (node == null) {
36
+ // Add null node to preserve node ids
37
+ // $FlowFixMe
38
+ newGraph.addNode(null);
39
+ } else if (contentKey == null) {
40
+ newGraph.addNode(node);
41
+ } else {
42
+ newGraph.addNodeByContentKey(contentKey, node);
43
+ }
44
+ nodeId += 1;
45
+ }
46
+ for (let edge of this.getAllEdges()) {
47
+ const weight = this.getEdgeWeight(edge.from, edge.to);
48
+ newGraph.addWeightedEdge(edge.from, edge.to, edge.type, weight);
49
+ }
50
+ if (this.rootNodeId != null) {
51
+ const rootNodeContentKey = this._nodeIdToContentKey.get(this.rootNodeId);
52
+ const newGraphRootNodeId = newGraph.getNodeIdByContentKey((0, _nullthrows().default)(rootNodeContentKey));
53
+ newGraph.setRootNodeId(newGraphRootNodeId);
54
+ }
55
+ return newGraph;
56
+ }
57
+ addWeightedEdge(from, to, type, weight) {
58
+ this.addEdge(from, to, type);
59
+ if (weight != null) {
60
+ this.#edgeWeights.set([String(from), String(to)].join(','), weight);
61
+ }
62
+ }
63
+ getEdgeWeight(from, to) {
64
+ return this.#edgeWeights.get([String(from), String(to)].join(',')) ?? null;
65
+ }
66
+ }
67
+ exports.EdgeContentGraph = EdgeContentGraph;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/bundler-experimental",
3
- "version": "2.12.1-canary.3625+6c4f222b8",
3
+ "version": "2.12.1-canary.3627+2d2c23fab",
4
4
  "license": "(MIT OR Apache-2.0)",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -16,19 +16,19 @@
16
16
  "parcel": "^2.12.0"
17
17
  },
18
18
  "dependencies": {
19
- "@atlaspack/core": "2.12.1-canary.3625+6c4f222b8",
20
- "@atlaspack/diagnostic": "2.12.1-canary.3625+6c4f222b8",
21
- "@atlaspack/feature-flags": "2.12.1-canary.3625+6c4f222b8",
22
- "@atlaspack/graph": "3.2.1-canary.3625+6c4f222b8",
23
- "@atlaspack/logger": "2.12.1-canary.3625+6c4f222b8",
24
- "@atlaspack/plugin": "2.12.1-canary.3625+6c4f222b8",
25
- "@atlaspack/rust": "2.12.1-canary.3625+6c4f222b8",
26
- "@atlaspack/types": "2.12.1-canary.3625+6c4f222b8",
27
- "@atlaspack/utils": "2.12.1-canary.3625+6c4f222b8",
19
+ "@atlaspack/core": "2.12.1-canary.3627+2d2c23fab",
20
+ "@atlaspack/diagnostic": "2.12.1-canary.3627+2d2c23fab",
21
+ "@atlaspack/feature-flags": "2.12.1-canary.3627+2d2c23fab",
22
+ "@atlaspack/graph": "3.2.1-canary.3627+2d2c23fab",
23
+ "@atlaspack/logger": "2.12.1-canary.3627+2d2c23fab",
24
+ "@atlaspack/plugin": "2.12.1-canary.3627+2d2c23fab",
25
+ "@atlaspack/rust": "2.12.1-canary.3627+2d2c23fab",
26
+ "@atlaspack/types": "2.12.1-canary.3627+2d2c23fab",
27
+ "@atlaspack/utils": "2.12.1-canary.3627+2d2c23fab",
28
28
  "nullthrows": "^1.1.1"
29
29
  },
30
30
  "devDependencies": {
31
- "@atlaspack/fs": "2.12.1-canary.3625+6c4f222b8"
31
+ "@atlaspack/fs": "2.12.1-canary.3627+2d2c23fab"
32
32
  },
33
- "gitHead": "6c4f222b816a32ea2ac5cef41a1784e8d044f142"
33
+ "gitHead": "2d2c23fab90eea474aaf2ce0be4e5220a35f43cc"
34
34
  }
@@ -0,0 +1,62 @@
1
+ // @flow strict-local
2
+
3
+ import {ContentGraph, type NodeId} from '@atlaspack/graph';
4
+ import nullthrows from 'nullthrows';
5
+
6
+ /**
7
+ * A `ContentGraph` that also stores weights on edges.
8
+ *
9
+ * @template N The type of the node weight.
10
+ * @template EW The type of the edge weight.
11
+ */
12
+ export class EdgeContentGraph<N, EW> extends ContentGraph<N, number> {
13
+ #edgeWeights: Map<string, EW> = new Map();
14
+
15
+ clone(): EdgeContentGraph<N, EW> {
16
+ const newGraph: EdgeContentGraph<N, EW> = new EdgeContentGraph();
17
+ let nodeId = 0;
18
+ for (let node of this.nodes) {
19
+ const contentKey = this._nodeIdToContentKey.get(nodeId);
20
+ if (node == null) {
21
+ // Add null node to preserve node ids
22
+ // $FlowFixMe
23
+ newGraph.addNode(null);
24
+ } else if (contentKey == null) {
25
+ newGraph.addNode(node);
26
+ } else {
27
+ newGraph.addNodeByContentKey(contentKey, node);
28
+ }
29
+ nodeId += 1;
30
+ }
31
+ for (let edge of this.getAllEdges()) {
32
+ const weight = this.getEdgeWeight(edge.from, edge.to);
33
+ newGraph.addWeightedEdge(edge.from, edge.to, edge.type, weight);
34
+ }
35
+
36
+ if (this.rootNodeId != null) {
37
+ const rootNodeContentKey = this._nodeIdToContentKey.get(this.rootNodeId);
38
+ const newGraphRootNodeId = newGraph.getNodeIdByContentKey(
39
+ nullthrows(rootNodeContentKey),
40
+ );
41
+ newGraph.setRootNodeId(newGraphRootNodeId);
42
+ }
43
+
44
+ return newGraph;
45
+ }
46
+
47
+ addWeightedEdge(
48
+ from: NodeId,
49
+ to: NodeId,
50
+ type: number,
51
+ weight: EW | null,
52
+ ): void {
53
+ this.addEdge(from, to, type);
54
+ if (weight != null) {
55
+ this.#edgeWeights.set([String(from), String(to)].join(','), weight);
56
+ }
57
+ }
58
+
59
+ getEdgeWeight(from: NodeId, to: NodeId): EW | null {
60
+ return this.#edgeWeights.get([String(from), String(to)].join(',')) ?? null;
61
+ }
62
+ }
@@ -0,0 +1,58 @@
1
+ // @flow strict-local
2
+
3
+ import assert from 'assert';
4
+ import {EdgeContentGraph} from '../../src/DominatorBundler/EdgeContentGraph';
5
+
6
+ describe('EdgeContentGraph', () => {
7
+ describe('addWeightedEdge', () => {
8
+ it('creates an edge between two nodes', () => {
9
+ const graph = new EdgeContentGraph<string, number>();
10
+ const a = graph.addNode('a');
11
+ const b = graph.addNode('b');
12
+ const edgeType = 1;
13
+ const weight = 10;
14
+ graph.addWeightedEdge(a, b, edgeType, weight);
15
+ const ids = graph.getNodeIdsConnectedFrom(a);
16
+ assert.deepEqual(ids, [b]);
17
+ });
18
+
19
+ it('should add and get edge weights', () => {
20
+ const graph = new EdgeContentGraph<string, number>();
21
+ const a = graph.addNode('a');
22
+ const b = graph.addNode('b');
23
+ const c = graph.addNode('b');
24
+
25
+ const edgeType = 1;
26
+ const weight1 = 10;
27
+ const weight2 = 20;
28
+
29
+ graph.addWeightedEdge(a, b, edgeType, weight1);
30
+ graph.addWeightedEdge(b, c, edgeType, weight2);
31
+
32
+ assert.equal(graph.getEdgeWeight(a, b), weight1);
33
+ assert.equal(graph.getEdgeWeight(b, c), weight2);
34
+ });
35
+ });
36
+
37
+ describe('clone', () => {
38
+ it('clones the graph', () => {
39
+ const graph = new EdgeContentGraph<string, number>();
40
+ const a = graph.addNode('a');
41
+ const b = graph.addNode('b');
42
+ const c = graph.addNode('c');
43
+
44
+ const edgeType = 1;
45
+ const weight1 = 10;
46
+ const weight2 = 20;
47
+
48
+ graph.addWeightedEdge(a, b, edgeType, weight1);
49
+ graph.addWeightedEdge(b, c, edgeType, weight2);
50
+
51
+ const clonedGraph = graph.clone();
52
+ assert.deepEqual(clonedGraph.nodes, graph.nodes);
53
+ assert.deepEqual(clonedGraph.getAllEdges(), graph.getAllEdges());
54
+ assert.equal(clonedGraph.getEdgeWeight(a, b), weight1);
55
+ assert.equal(clonedGraph.getEdgeWeight(b, c), weight2);
56
+ });
57
+ });
58
+ });