-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BundleGraph.js
177 lines (155 loc) Β· 4.65 KB
/
BundleGraph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// @flow
import type {
Asset,
Bundle,
BundleGraph as IBundleGraph,
BundleGraphNode,
BundleGroup,
BundleNode,
GraphTraversalCallback
} from '@parcel/types';
import invariant from 'assert';
import Graph from './Graph';
const getBundleGroupId = (bundleGroup: BundleGroup) =>
'bundle_group:' + bundleGroup.entryAssetId;
export default class BundleGraph extends Graph<BundleGraphNode>
implements IBundleGraph {
constructor() {
super();
this.setRootNode({
type: 'root',
id: 'root',
value: null
});
}
addBundleGroup(parentBundle: ?Bundle, bundleGroup: BundleGroup) {
let node = {
id: getBundleGroupId(bundleGroup),
type: 'bundle_group',
value: bundleGroup
};
// Add a connection from the dependency to the new bundle group in all bundles
this.traverse(bundle => {
if (bundle.type === 'bundle') {
let depNode = bundle.value.assetGraph.getNode(
bundleGroup.dependency.id
);
if (depNode) {
bundle.value.assetGraph.replaceNodesConnectedTo(depNode, [node]);
}
}
});
this.addNode(node);
this.addEdge({
from: !parentBundle ? 'root' : parentBundle.id,
to: node.id
});
}
addBundle(bundleGroup: BundleGroup, bundle: Bundle) {
// Propagate target from bundle group to bundle
if (bundleGroup.target && !bundle.target) {
bundle.target = bundleGroup.target;
}
let bundleGroupId = getBundleGroupId(bundleGroup);
let bundleNode: BundleNode = {
id: bundle.id,
type: 'bundle',
value: bundle
};
this.addNode(bundleNode);
this.addEdge({
from: bundleGroupId,
to: bundleNode.id
});
this.traverse(node => {
// Replace dependencies in this bundle with bundle group references for
// already created bundles in the bundle graph. This can happen when two
// bundles point to the same dependency, which has an async import.
if (node.type === 'bundle_group') {
let {assetGraph} = bundle;
let bundleGroup: BundleGroup = node.value;
let depNode = assetGraph.getNode(bundleGroup.dependency.id);
if (depNode && !assetGraph.hasNode(node.id)) {
// $FlowFixMe Merging a graph of a subtype into a graph of the supertype
assetGraph.merge(this.getSubGraph(node));
assetGraph.replaceNodesConnectedTo(depNode, [node]);
this.addEdge({from: bundle.id, to: node.id});
}
}
// Add a connection from the bundle group to the bundle in all bundles
if (
node.type === 'bundle' &&
node.value.assetGraph.hasNode(bundleGroupId)
) {
node.value.assetGraph.addNode(bundleNode);
node.value.assetGraph.addEdge({
from: bundleGroupId,
to: bundleNode.id
});
}
});
}
getBundles(bundleGroup: BundleGroup): Array<Bundle> {
let bundleGroupId = getBundleGroupId(bundleGroup);
let node = this.getNode(bundleGroupId);
if (!node) {
return [];
}
return this.getNodesConnectedFrom(node).map(node => {
invariant(node.type === 'bundle');
return node.value;
});
}
getBundleGroups(bundle: Bundle): Array<BundleGroup> {
let node = this.getNode(bundle.id);
if (!node) {
return [];
}
return this.getNodesConnectedTo(node).map(node => {
invariant(node.type === 'bundle_group');
return node.value;
});
}
isAssetInAncestorBundle(bundle: Bundle, asset: Asset): boolean {
let bundleNode = this.getNode(bundle.id);
if (!bundleNode) {
return false;
}
let ret = null;
this.traverseAncestors(bundleNode, (node, context, traversal) => {
// Skip starting node
if (node === bundleNode) {
return;
}
// If this is the first bundle we've seen, initialize result to true
if (node.type === 'bundle' && ret === null) {
ret = true;
}
if (node.type === 'bundle' && !node.value.assetGraph.hasNode(asset.id)) {
ret = false;
traversal.stop();
}
});
return !!ret;
}
findBundlesWithAsset(asset: Asset): Array<Bundle> {
return (
Array.from(this.nodes.values())
.filter(
node =>
node.type === 'bundle' && node.value.assetGraph.hasNode(asset.id)
)
// $FlowFixMe Flow can't refine on filter https://github.com/facebook/flow/issues/1414
.map(node => node.value)
);
}
traverseBundles<TContext>(
visit: GraphTraversalCallback<Bundle, TContext>
): ?TContext {
return this.traverse((node, ...args) => {
if (node.type === 'bundle') {
return visit(node.value, ...args);
}
});
}
}