@atlaspack/core 2.13.2-dev.3682 → 2.14.0

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.
Files changed (87) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/package.json +20 -20
  3. package/src/Atlaspack.js +3 -2
  4. package/src/atlaspack-v3/AtlaspackV3.js +24 -8
  5. package/src/loadAtlaspackPlugin.js +26 -71
  6. package/src/projectPath.js +14 -1
  7. package/src/requests/WriteBundlesRequest.js +31 -22
  8. package/LICENSE +0 -201
  9. package/lib/AssetGraph.js +0 -521
  10. package/lib/Atlaspack.js +0 -676
  11. package/lib/AtlaspackConfig.js +0 -298
  12. package/lib/AtlaspackConfig.schema.js +0 -103
  13. package/lib/BundleGraph.js +0 -1527
  14. package/lib/CommittedAsset.js +0 -155
  15. package/lib/Dependency.js +0 -136
  16. package/lib/Environment.js +0 -144
  17. package/lib/IdentifierRegistry.js +0 -36
  18. package/lib/InternalConfig.js +0 -56
  19. package/lib/PackagerRunner.js +0 -525
  20. package/lib/ReporterRunner.js +0 -151
  21. package/lib/RequestTracker.js +0 -1178
  22. package/lib/SymbolPropagation.js +0 -618
  23. package/lib/TargetDescriptor.schema.js +0 -118
  24. package/lib/Transformation.js +0 -522
  25. package/lib/UncommittedAsset.js +0 -348
  26. package/lib/Validation.js +0 -203
  27. package/lib/applyRuntimes.js +0 -355
  28. package/lib/assetUtils.js +0 -205
  29. package/lib/atlaspack-v3/AtlaspackV3.js +0 -57
  30. package/lib/atlaspack-v3/NapiWorkerPool.js +0 -71
  31. package/lib/atlaspack-v3/fs.js +0 -39
  32. package/lib/atlaspack-v3/index.js +0 -26
  33. package/lib/atlaspack-v3/jsCallable.js +0 -20
  34. package/lib/atlaspack-v3/worker/compat/asset-symbols.js +0 -197
  35. package/lib/atlaspack-v3/worker/compat/bitflags.js +0 -84
  36. package/lib/atlaspack-v3/worker/compat/dependency.js +0 -44
  37. package/lib/atlaspack-v3/worker/compat/environment.js +0 -57
  38. package/lib/atlaspack-v3/worker/compat/index.js +0 -104
  39. package/lib/atlaspack-v3/worker/compat/mutable-asset.js +0 -164
  40. package/lib/atlaspack-v3/worker/compat/plugin-config.js +0 -78
  41. package/lib/atlaspack-v3/worker/compat/plugin-logger.js +0 -29
  42. package/lib/atlaspack-v3/worker/compat/plugin-options.js +0 -113
  43. package/lib/atlaspack-v3/worker/compat/plugin-tracer.js +0 -12
  44. package/lib/atlaspack-v3/worker/compat/target.js +0 -17
  45. package/lib/atlaspack-v3/worker/index.js +0 -3
  46. package/lib/atlaspack-v3/worker/worker.js +0 -280
  47. package/lib/constants.js +0 -21
  48. package/lib/dumpGraphToGraphViz.js +0 -206
  49. package/lib/index.js +0 -70
  50. package/lib/loadAtlaspackPlugin.js +0 -136
  51. package/lib/loadDotEnv.js +0 -54
  52. package/lib/projectPath.js +0 -94
  53. package/lib/public/Asset.js +0 -259
  54. package/lib/public/Bundle.js +0 -236
  55. package/lib/public/BundleGraph.js +0 -279
  56. package/lib/public/BundleGroup.js +0 -50
  57. package/lib/public/Config.js +0 -202
  58. package/lib/public/Dependency.js +0 -131
  59. package/lib/public/Environment.js +0 -247
  60. package/lib/public/MutableBundleGraph.js +0 -204
  61. package/lib/public/PluginOptions.js +0 -71
  62. package/lib/public/Symbols.js +0 -247
  63. package/lib/public/Target.js +0 -64
  64. package/lib/registerCoreWithSerializer.js +0 -51
  65. package/lib/requests/AssetGraphRequest.js +0 -432
  66. package/lib/requests/AssetGraphRequestRust.js +0 -220
  67. package/lib/requests/AssetRequest.js +0 -132
  68. package/lib/requests/AtlaspackBuildRequest.js +0 -79
  69. package/lib/requests/AtlaspackConfigRequest.js +0 -479
  70. package/lib/requests/BundleGraphRequest.js +0 -485
  71. package/lib/requests/ConfigRequest.js +0 -203
  72. package/lib/requests/DevDepRequest.js +0 -193
  73. package/lib/requests/EntryRequest.js +0 -295
  74. package/lib/requests/PackageRequest.js +0 -88
  75. package/lib/requests/PathRequest.js +0 -357
  76. package/lib/requests/TargetRequest.js +0 -1179
  77. package/lib/requests/ValidationRequest.js +0 -66
  78. package/lib/requests/WriteBundleRequest.js +0 -252
  79. package/lib/requests/WriteBundlesRequest.js +0 -153
  80. package/lib/requests/asset-graph-diff.js +0 -128
  81. package/lib/requests/asset-graph-dot.js +0 -131
  82. package/lib/resolveOptions.js +0 -265
  83. package/lib/serializerCore.browser.js +0 -29
  84. package/lib/summarizeRequest.js +0 -55
  85. package/lib/types.js +0 -35
  86. package/lib/utils.js +0 -160
  87. package/lib/worker.js +0 -184
@@ -1,1527 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.bundleGraphEdgeTypes = void 0;
7
- function _assert() {
8
- const data = _interopRequireDefault(require("assert"));
9
- _assert = 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 _graph() {
22
- const data = require("@atlaspack/graph");
23
- _graph = function () {
24
- return data;
25
- };
26
- return data;
27
- }
28
- function _rust() {
29
- const data = require("@atlaspack/rust");
30
- _rust = function () {
31
- return data;
32
- };
33
- return data;
34
- }
35
- function _utils() {
36
- const data = require("@atlaspack/utils");
37
- _utils = function () {
38
- return data;
39
- };
40
- return data;
41
- }
42
- var _types = require("./types");
43
- var _utils2 = require("./utils");
44
- var _Environment = require("./public/Environment");
45
- var _projectPath = require("./projectPath");
46
- var _constants = require("./constants");
47
- function _featureFlags() {
48
- const data = require("@atlaspack/feature-flags");
49
- _featureFlags = function () {
50
- return data;
51
- };
52
- return data;
53
- }
54
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
55
- const bundleGraphEdgeTypes = exports.bundleGraphEdgeTypes = {
56
- // A lack of an edge type indicates to follow the edge while traversing
57
- // the bundle's contents, e.g. `bundle.traverse()` during packaging.
58
- null: 1,
59
- // Used for constant-time checks of presence of a dependency or asset in a bundle,
60
- // avoiding bundle traversal in cases like `isAssetInAncestors`
61
- contains: 2,
62
- // Connections between bundles and bundle groups, for quick traversal of the
63
- // bundle hierarchy.
64
- bundle: 3,
65
- // When dependency -> asset: Indicates that the asset a dependency references
66
- // is contained in another bundle.
67
- // When dependency -> bundle: Indicates the bundle is necessary for any bundles
68
- // with the dependency.
69
- // When bundle -> bundle: Indicates the target bundle is necessary for the
70
- // source bundle.
71
- // This type prevents referenced assets from being traversed from dependencies
72
- // along the untyped edge, and enables traversal to referenced bundles that are
73
- // not directly connected to bundle group nodes.
74
- references: 4,
75
- // Signals that the dependency is internally resolvable via the bundle's ancestry,
76
- // and that the bundle connected to the dependency is not necessary for the source bundle.
77
- internal_async: 5,
78
- // This type is used to mark an edge between a bundle and a conditional bundle.
79
- // This allows efficient discovery of conditional bundles in packaging
80
- conditional: 5
81
- };
82
- function makeReadOnlySet(set) {
83
- return new Proxy(set, {
84
- get(target, property) {
85
- if (property === 'delete' || property === 'add' || property === 'clear') {
86
- return undefined;
87
- } else {
88
- // $FlowFixMe[incompatible-type]
89
- let value = target[property];
90
- return typeof value === 'function' ? value.bind(target) : value;
91
- }
92
- }
93
- });
94
- }
95
-
96
- /**
97
- * Stores assets, dependencies, bundle groups, bundles, and the relationships between them.
98
- * The BundleGraph is passed to the bundler plugin wrapped in a MutableBundleGraph,
99
- * and is passed to packagers and optimizers wrapped in the public BundleGraph object, both
100
- * of which implement public api for this structure. This is the internal structure.
101
- */
102
- class BundleGraph {
103
- /** A set of all existing concise asset ids present in the BundleGraph */
104
-
105
- /** Maps full asset ids (currently 32-character strings) to concise ids (minimum of 5 character strings) */
106
-
107
- /**
108
- * A cache of bundle hashes by bundle id.
109
- *
110
- * TODO: These hashes are being invalidated in mutative methods, but this._graph is not a private
111
- * property so it is possible to reach in and mutate the graph without invalidating these hashes.
112
- * It needs to be exposed in BundlerRunner for now based on how applying runtimes works and the
113
- * BundlerRunner takes care of invalidating hashes when runtimes are applied, but this is not ideal.
114
- */
115
-
116
- _targetEntryRoots = new Map();
117
- /** The internal core Graph structure */
118
-
119
- _bundlePublicIds /*: Set<string> */ = new Set();
120
- _conditions /*: Map<string, Condition> */ = new Map();
121
- constructor({
122
- graph,
123
- publicIdByAssetId,
124
- assetPublicIds,
125
- bundleContentHashes,
126
- conditions
127
- }) {
128
- this._graph = graph;
129
- this._assetPublicIds = assetPublicIds;
130
- this._publicIdByAssetId = publicIdByAssetId;
131
- this._bundleContentHashes = bundleContentHashes;
132
- this._conditions = conditions;
133
- }
134
-
135
- /**
136
- * Produce a BundleGraph from an AssetGraph by removing asset groups and retargeting dependencies
137
- * based on the symbol data (resolving side-effect free reexports).
138
- */
139
- static fromAssetGraph(assetGraph, isProduction, publicIdByAssetId = new Map(), assetPublicIds = new Set()) {
140
- let graph = new (_graph().ContentGraph)();
141
- let assetGroupIds = new Map();
142
- let dependencies = new Map();
143
- let assetGraphNodeIdToBundleGraphNodeId = new Map();
144
- let conditions = new Map();
145
- let placeholderToDependency = new Map();
146
- let assetGraphRootNode = assetGraph.rootNodeId != null ? assetGraph.getNode(assetGraph.rootNodeId) : null;
147
- (0, _assert().default)(assetGraphRootNode != null && assetGraphRootNode.type === 'root');
148
- assetGraph.dfsFast(nodeId => {
149
- let node = assetGraph.getNode(nodeId);
150
- if (node != null && node.type === 'asset') {
151
- let {
152
- id: assetId
153
- } = node.value;
154
- // Generate a new, short public id for this asset to use.
155
- // If one already exists, use it.
156
- let publicId = publicIdByAssetId.get(assetId);
157
- if (publicId == null) {
158
- publicId = (0, _utils2.getPublicId)(assetId, existing => assetPublicIds.has(existing));
159
- publicIdByAssetId.set(assetId, publicId);
160
- assetPublicIds.add(publicId);
161
- }
162
- } else if (node != null && node.type === 'asset_group') {
163
- assetGroupIds.set(nodeId, assetGraph.getNodeIdsConnectedFrom(nodeId));
164
- } else if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi') && node != null && node.type === 'dependency') {
165
- var _dep$meta;
166
- // The dependency placeholders in the `importCond` calls that will be in the transformed
167
- // code need to be mapped to the "real" dependencies, so we need access to a map of placeholders
168
- // to dependencies
169
- const dep = node.value;
170
- // $FlowFixMe[incompatible-type] Meta is untyped
171
- const placeholder = (_dep$meta = dep.meta) === null || _dep$meta === void 0 ? void 0 : _dep$meta.placeholder;
172
- if (placeholder != null) {
173
- placeholderToDependency.set(placeholder, dep);
174
- }
175
- }
176
- });
177
- let walkVisited = new Set();
178
- function walk(nodeId) {
179
- if (walkVisited.has(nodeId)) return;
180
- walkVisited.add(nodeId);
181
- let node = (0, _nullthrows().default)(assetGraph.getNode(nodeId));
182
- if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi') && node.type === 'asset') {
183
- const asset = node.value;
184
- if (Array.isArray(asset.meta.conditions)) {
185
- // $FlowFixMe
186
- for (const condition of asset.meta.conditions) {
187
- // Resolve the placeholders that were attached to the asset in JSTransformer to dependencies,
188
- // as well as create a public id for the condition.
189
- const {
190
- key,
191
- ifTruePlaceholder,
192
- ifFalsePlaceholder
193
- } = condition;
194
- const condHash = (0, _rust().hashString)(`${key}:${ifTruePlaceholder}:${ifFalsePlaceholder}`);
195
- const condPublicId = (0, _utils2.getPublicId)(condHash, v => conditions.has(v));
196
- if (conditions.has(condHash)) {
197
- throw new Error('Unexpected duplicate asset');
198
- } else {
199
- conditions.set(condHash, {
200
- publicId: condPublicId,
201
- assets: new Set([asset]),
202
- key,
203
- ifTrueDependency: (0, _nullthrows().default)(placeholderToDependency.get(ifTruePlaceholder), 'ifTruePlaceholder was undefined'),
204
- ifFalseDependency: (0, _nullthrows().default)(placeholderToDependency.get(ifFalsePlaceholder), 'ifFalsePlaceholder was undefined')
205
- });
206
- }
207
- }
208
- }
209
- }
210
- if (node.type === 'dependency' && node.value.symbols != null && node.value.env.shouldScopeHoist &&
211
- // Disable in dev mode because this feature is at odds with safeToIncrementallyBundle
212
- isProduction) {
213
- let nodeValueSymbols = node.value.symbols;
214
-
215
- // asset -> symbols that should be imported directly from that asset
216
- let targets = new (_utils().DefaultMap)(() => new Map());
217
- let externalSymbols = new Set();
218
- let hasAmbiguousSymbols = false;
219
- for (let [symbol, resolvedSymbol] of node.usedSymbolsUp) {
220
- if (resolvedSymbol) {
221
- targets.get(resolvedSymbol.asset).set(symbol, resolvedSymbol.symbol ?? symbol);
222
- } else if (resolvedSymbol === null) {
223
- externalSymbols.add(symbol);
224
- } else if (resolvedSymbol === undefined) {
225
- hasAmbiguousSymbols = true;
226
- break;
227
- }
228
- }
229
- if (
230
- // Only perform retargeting when there is an imported symbol
231
- // - If the target is side-effect-free, the symbols point to the actual target and removing
232
- // the original dependency resolution is fine
233
- // - Otherwise, keep this dependency unchanged for its potential side effects
234
- node.usedSymbolsUp.size > 0 &&
235
- // Only perform retargeting if the dependency only points to a single asset (e.g. CSS modules)
236
- !hasAmbiguousSymbols &&
237
- // It doesn't make sense to retarget dependencies where `*` is used, because the
238
- // retargeting won't enable any benefits in that case (apart from potentially even more
239
- // code being generated).
240
- !node.usedSymbolsUp.has('*') &&
241
- // TODO We currently can't rename imports in async imports, e.g. from
242
- // (parcelRequire("...")).then(({ a }) => a);
243
- // to
244
- // (parcelRequire("...")).then(({ a: b }) => a);
245
- // or
246
- // (parcelRequire("...")).then((a)=>a);
247
- // if the reexporting asset did `export {a as b}` or `export * as a`
248
- node.value.priority === _types.Priority.sync &&
249
- // For every asset, no symbol is imported multiple times (with a different local name).
250
- // Don't retarget because this cannot be resolved without also changing the asset symbols
251
- // (and the asset content itself).
252
- [...targets].every(([, t]) => new Set([...t.values()]).size === t.size)) {
253
- var _nodeValueSymbols$get;
254
- let isReexportAll = ((_nodeValueSymbols$get = nodeValueSymbols.get('*')) === null || _nodeValueSymbols$get === void 0 ? void 0 : _nodeValueSymbols$get.local) === '*';
255
- let reexportAllLoc = isReexportAll ? (0, _nullthrows().default)(nodeValueSymbols.get('*')).loc : undefined;
256
-
257
- // TODO adjust sourceAssetIdNode.value.dependencies ?
258
- let deps = [
259
- // Keep the original dependency
260
- {
261
- asset: null,
262
- dep: graph.addNodeByContentKey(node.id, {
263
- ...node,
264
- value: {
265
- ...node.value,
266
- symbols: new Map([...nodeValueSymbols].filter(([k]) => externalSymbols.has(k)))
267
- },
268
- usedSymbolsUp: new Map([...node.usedSymbolsUp].filter(([k]) => externalSymbols.has(k))),
269
- usedSymbolsDown: new Set(),
270
- excluded: externalSymbols.size === 0
271
- })
272
- }, ...[...targets].map(([asset, target]) => {
273
- let newNodeId = (0, _rust().hashString)(node.id + [...target.keys()].join(','));
274
- let symbols = new Map();
275
- for (let [as, from] of target) {
276
- let existing = nodeValueSymbols.get(as);
277
- if (existing) {
278
- symbols.set(from, existing);
279
- } else {
280
- (0, _assert().default)(isReexportAll);
281
- if (as === from) {
282
- // Keep the export-all for non-renamed reexports, this still correctly models
283
- // ambiguous resolution with multiple export-alls.
284
- symbols.set('*', {
285
- isWeak: true,
286
- local: '*',
287
- loc: reexportAllLoc
288
- });
289
- } else {
290
- let local = `${node.value.id}$rewrite$${asset}$${from}`;
291
- symbols.set(from, {
292
- isWeak: true,
293
- local,
294
- loc: reexportAllLoc
295
- });
296
- if (node.value.sourceAssetId != null) {
297
- let sourceAssetId = (0, _nullthrows().default)(assetGraphNodeIdToBundleGraphNodeId.get(assetGraph.getNodeIdByContentKey(node.value.sourceAssetId)));
298
- let sourceAsset = (0, _nullthrows().default)(graph.getNode(sourceAssetId));
299
- (0, _assert().default)(sourceAsset.type === 'asset');
300
- let sourceAssetSymbols = sourceAsset.value.symbols;
301
- if (sourceAssetSymbols) {
302
- // The `as == from` case above should handle multiple export-alls causing
303
- // ambiguous resolution. So the current symbol is unambiguous and shouldn't
304
- // already exist on the importer.
305
- (0, _assert().default)(!sourceAssetSymbols.has(as));
306
- sourceAssetSymbols.set(as, {
307
- loc: reexportAllLoc,
308
- local: local
309
- });
310
- }
311
- }
312
- }
313
- }
314
- }
315
- let usedSymbolsUp = new Map([...node.usedSymbolsUp].filter(([k]) => target.has(k) || k === '*').map(([k, v]) => [target.get(k) ?? k, v]));
316
- return {
317
- asset,
318
- dep: graph.addNodeByContentKey(newNodeId, {
319
- ...node,
320
- id: newNodeId,
321
- value: {
322
- ...node.value,
323
- id: newNodeId,
324
- symbols
325
- },
326
- usedSymbolsUp,
327
- // This is only a temporary helper needed during symbol propagation and is never
328
- // read afterwards (and also not exposed through the public API).
329
- usedSymbolsDown: new Set()
330
- })
331
- };
332
- })];
333
- dependencies.set(nodeId, deps);
334
-
335
- // Jump to the dependencies that are used in this dependency
336
- for (let id of targets.keys()) {
337
- walk(assetGraph.getNodeIdByContentKey(id));
338
- }
339
- return;
340
- } else {
341
- // No special handling
342
- let bundleGraphNodeId = graph.addNodeByContentKey(node.id, node);
343
- assetGraphNodeIdToBundleGraphNodeId.set(nodeId, bundleGraphNodeId);
344
- }
345
- }
346
- // Don't copy over asset groups into the bundle graph.
347
- else if (node.type !== 'asset_group') {
348
- let nodeToAdd = node.type === 'asset' ? {
349
- ...node,
350
- value: {
351
- ...node.value,
352
- symbols: new Map(node.value.symbols)
353
- }
354
- } : node;
355
- let bundleGraphNodeId = graph.addNodeByContentKey(node.id, nodeToAdd);
356
- if (node.id === (assetGraphRootNode === null || assetGraphRootNode === void 0 ? void 0 : assetGraphRootNode.id)) {
357
- graph.setRootNodeId(bundleGraphNodeId);
358
- }
359
- assetGraphNodeIdToBundleGraphNodeId.set(nodeId, bundleGraphNodeId);
360
- }
361
- for (let id of assetGraph.getNodeIdsConnectedFrom(nodeId)) {
362
- walk(id);
363
- }
364
- }
365
- walk((0, _nullthrows().default)(assetGraph.rootNodeId));
366
- for (let edge of assetGraph.getAllEdges()) {
367
- var _dependencies$get, _assetGroupIds$get;
368
- if (assetGroupIds.has(edge.from)) {
369
- continue;
370
- }
371
- if (dependencies.has(edge.from)) {
372
- // Discard previous edge, insert outgoing edges for all split dependencies
373
- for (let {
374
- asset,
375
- dep
376
- } of (0, _nullthrows().default)(dependencies.get(edge.from))) {
377
- if (asset != null) {
378
- graph.addEdge(dep, (0, _nullthrows().default)(assetGraphNodeIdToBundleGraphNodeId.get(assetGraph.getNodeIdByContentKey(asset))));
379
- }
380
- }
381
- continue;
382
- }
383
- if (!assetGraphNodeIdToBundleGraphNodeId.has(edge.from)) {
384
- continue;
385
- }
386
- let to = ((_dependencies$get = dependencies.get(edge.to)) === null || _dependencies$get === void 0 ? void 0 : _dependencies$get.map(v => v.dep)) ?? ((_assetGroupIds$get = assetGroupIds.get(edge.to)) === null || _assetGroupIds$get === void 0 ? void 0 : _assetGroupIds$get.map(id => (0, _nullthrows().default)(assetGraphNodeIdToBundleGraphNodeId.get(id)))) ?? [(0, _nullthrows().default)(assetGraphNodeIdToBundleGraphNodeId.get(edge.to))];
387
- for (let t of to) {
388
- graph.addEdge((0, _nullthrows().default)(assetGraphNodeIdToBundleGraphNodeId.get(edge.from)), t);
389
- }
390
- }
391
- return new BundleGraph({
392
- graph,
393
- assetPublicIds,
394
- bundleContentHashes: new Map(),
395
- publicIdByAssetId,
396
- conditions
397
- });
398
- }
399
- serialize() {
400
- return {
401
- $$raw: true,
402
- graph: this._graph.serialize(),
403
- assetPublicIds: this._assetPublicIds,
404
- bundleContentHashes: this._bundleContentHashes,
405
- publicIdByAssetId: this._publicIdByAssetId,
406
- conditions: this._conditions
407
- };
408
- }
409
- static deserialize(serialized) {
410
- return new BundleGraph({
411
- graph: _graph().ContentGraph.deserialize(serialized.graph),
412
- assetPublicIds: serialized.assetPublicIds,
413
- bundleContentHashes: serialized.bundleContentHashes,
414
- publicIdByAssetId: serialized.publicIdByAssetId,
415
- conditions: serialized.conditions
416
- });
417
- }
418
- createBundle(opts) {
419
- let {
420
- entryAsset,
421
- target
422
- } = opts;
423
- let bundleId = (0, _rust().hashString)('bundle:' + (opts.entryAsset ? opts.entryAsset.id : opts.uniqueKey) + (0, _projectPath.fromProjectPathRelative)(target.distDir) + (opts.bundleBehavior ?? ''));
424
- let existing = this._graph.getNodeByContentKey(bundleId);
425
- if (existing != null) {
426
- (0, _assert().default)(existing.type === 'bundle');
427
- return existing.value;
428
- }
429
- let publicId = (0, _utils2.getPublicId)(bundleId, existing => this._bundlePublicIds.has(existing));
430
- this._bundlePublicIds.add(publicId);
431
- let isPlaceholder = false;
432
- if (entryAsset) {
433
- let entryAssetNode = this._graph.getNodeByContentKey(entryAsset.id);
434
- (0, _assert().default)((entryAssetNode === null || entryAssetNode === void 0 ? void 0 : entryAssetNode.type) === 'asset', 'Entry asset does not exist');
435
- isPlaceholder = entryAssetNode.requested === false;
436
- }
437
- let bundleNode = {
438
- type: 'bundle',
439
- id: bundleId,
440
- value: {
441
- id: bundleId,
442
- hashReference: opts.shouldContentHash ? _constants.HASH_REF_PREFIX + bundleId : bundleId.slice(-8),
443
- type: opts.entryAsset ? opts.entryAsset.type : opts.type,
444
- env: opts.env,
445
- entryAssetIds: entryAsset ? [entryAsset.id] : [],
446
- mainEntryId: entryAsset === null || entryAsset === void 0 ? void 0 : entryAsset.id,
447
- pipeline: opts.entryAsset ? opts.entryAsset.pipeline : opts.pipeline,
448
- needsStableName: opts.needsStableName,
449
- bundleBehavior: opts.bundleBehavior != null ? _types.BundleBehavior[opts.bundleBehavior] : null,
450
- isSplittable: opts.entryAsset ? opts.entryAsset.isBundleSplittable : opts.isSplittable,
451
- isPlaceholder,
452
- target,
453
- name: null,
454
- displayName: null,
455
- publicId
456
- }
457
- };
458
- let bundleNodeId = this._graph.addNodeByContentKey(bundleId, bundleNode);
459
- if (opts.entryAsset) {
460
- this._graph.addEdge(bundleNodeId, this._graph.getNodeIdByContentKey(opts.entryAsset.id));
461
- }
462
- _assert().default;
463
- return bundleNode.value;
464
- }
465
- addAssetToBundle(asset, bundle) {
466
- let bundleNodeId = this._graph.getNodeIdByContentKey(bundle.id);
467
- this._graph.addEdge(bundleNodeId, this._graph.getNodeIdByContentKey(asset.id), bundleGraphEdgeTypes.contains);
468
- this._graph.addEdge(bundleNodeId, this._graph.getNodeIdByContentKey(asset.id));
469
- let dependencies = this.getDependencies(asset);
470
- for (let dependency of dependencies) {
471
- let dependencyNodeId = this._graph.getNodeIdByContentKey(dependency.id);
472
- this._graph.addEdge(bundleNodeId, dependencyNodeId, bundleGraphEdgeTypes.contains);
473
- for (let [bundleGroupNodeId, bundleGroupNode] of this._graph.getNodeIdsConnectedFrom(dependencyNodeId).map(id => [id, (0, _nullthrows().default)(this._graph.getNode(id))]).filter(([, node]) => node.type === 'bundle_group')) {
474
- (0, _assert().default)(bundleGroupNode.type === 'bundle_group');
475
- this._graph.addEdge(bundleNodeId, bundleGroupNodeId, bundleGraphEdgeTypes.bundle);
476
- }
477
- // If the dependency references a target bundle, add a reference edge from
478
- // the source bundle to the dependency for easy traversal.
479
- // TODO: Consider bundle being created from dependency
480
- if (this._graph.getNodeIdsConnectedFrom(dependencyNodeId, bundleGraphEdgeTypes.references).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).some(node => node.type === 'bundle')) {
481
- this._graph.addEdge(bundleNodeId, dependencyNodeId, bundleGraphEdgeTypes.references);
482
- }
483
- }
484
- }
485
- addAssetGraphToBundle(asset, bundle, shouldSkipDependency = d => this.isDependencySkipped(d)) {
486
- let assetNodeId = this._graph.getNodeIdByContentKey(asset.id);
487
- let bundleNodeId = this._graph.getNodeIdByContentKey(bundle.id);
488
-
489
- // The root asset should be reached directly from the bundle in traversal.
490
- // Its children will be traversed from there.
491
- this._graph.addEdge(bundleNodeId, assetNodeId);
492
- this._graph.traverse((nodeId, _, actions) => {
493
- let node = (0, _nullthrows().default)(this._graph.getNode(nodeId));
494
- if (node.type === 'bundle_group') {
495
- actions.skipChildren();
496
- return;
497
- }
498
- if (node.type === 'dependency' && shouldSkipDependency(node.value)) {
499
- actions.skipChildren();
500
- return;
501
- }
502
- if (node.type === 'asset' || node.type === 'dependency') {
503
- this._graph.addEdge(bundleNodeId, nodeId, bundleGraphEdgeTypes.contains);
504
- }
505
- if (node.type === 'dependency') {
506
- for (let [bundleGroupNodeId, bundleGroupNode] of this._graph.getNodeIdsConnectedFrom(nodeId).map(id => [id, (0, _nullthrows().default)(this._graph.getNode(id))]).filter(([, node]) => node.type === 'bundle_group')) {
507
- (0, _assert().default)(bundleGroupNode.type === 'bundle_group');
508
- this._graph.addEdge(bundleNodeId, bundleGroupNodeId, bundleGraphEdgeTypes.bundle);
509
- }
510
-
511
- // If the dependency references a target bundle, add a reference edge from
512
- // the source bundle to the dependency for easy traversal.
513
- if (this._graph.getNodeIdsConnectedFrom(nodeId, bundleGraphEdgeTypes.references).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).some(node => node.type === 'bundle')) {
514
- this._graph.addEdge(bundleNodeId, nodeId, bundleGraphEdgeTypes.references);
515
- this.markDependencyReferenceable(node.value);
516
- //all bundles that have this dependency need to have an edge from bundle to that dependency
517
- }
518
- }
519
- }, assetNodeId);
520
- this._bundleContentHashes.delete(bundle.id);
521
- }
522
- markDependencyReferenceable(dependency) {
523
- for (let bundle of this.getBundlesWithDependency(dependency)) {
524
- this._graph.addEdge(this._graph.getNodeIdByContentKey(bundle.id), this._graph.getNodeIdByContentKey(dependency.id), bundleGraphEdgeTypes.references);
525
- }
526
- }
527
- addEntryToBundle(asset, bundle, shouldSkipDependency) {
528
- this.addAssetGraphToBundle(asset, bundle, shouldSkipDependency);
529
- if (!bundle.entryAssetIds.includes(asset.id)) {
530
- bundle.entryAssetIds.push(asset.id);
531
- }
532
- }
533
- internalizeAsyncDependency(bundle, dependency) {
534
- if (dependency.priority === _types.Priority.sync) {
535
- throw new Error('Expected an async dependency');
536
- }
537
-
538
- // It's possible for internalized async dependencies to not have
539
- // reference edges and still have untyped edges.
540
- // TODO: Maybe don't use internalized async edges at all?
541
- let dependencyNodeId = this._graph.getNodeIdByContentKey(dependency.id);
542
- let resolved = this.getResolvedAsset(dependency);
543
- if (resolved) {
544
- let resolvedNodeId = this._graph.getNodeIdByContentKey(resolved.id);
545
- if (!this._graph.hasEdge(dependencyNodeId, resolvedNodeId, bundleGraphEdgeTypes.references)) {
546
- this._graph.addEdge(dependencyNodeId, resolvedNodeId, bundleGraphEdgeTypes.references);
547
- this._graph.removeEdge(dependencyNodeId, resolvedNodeId);
548
- }
549
- }
550
- this._graph.addEdge(this._graph.getNodeIdByContentKey(bundle.id), this._graph.getNodeIdByContentKey(dependency.id), bundleGraphEdgeTypes.internal_async);
551
- this._removeExternalDependency(bundle, dependency);
552
- }
553
- isDependencySkipped(dependency) {
554
- let node = this._graph.getNodeByContentKey(dependency.id);
555
- (0, _assert().default)(node && node.type === 'dependency');
556
- return !!node.hasDeferred || node.excluded;
557
- }
558
- getParentBundlesOfBundleGroup(bundleGroup) {
559
- return this._graph.getNodeIdsConnectedTo(this._graph.getNodeIdByContentKey((0, _utils2.getBundleGroupId)(bundleGroup)), bundleGraphEdgeTypes.bundle).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).filter(node => node.type === 'bundle').map(node => {
560
- (0, _assert().default)(node.type === 'bundle');
561
- return node.value;
562
- });
563
- }
564
- resolveAsyncDependency(dependency, bundle) {
565
- let depNodeId = this._graph.getNodeIdByContentKey(dependency.id);
566
- let bundleNodeId = bundle != null ? this._graph.getNodeIdByContentKey(bundle.id) : null;
567
- if (bundleNodeId != null && this._graph.hasEdge(bundleNodeId, depNodeId, bundleGraphEdgeTypes.internal_async)) {
568
- let referencedAssetNodeIds = this._graph.getNodeIdsConnectedFrom(depNodeId, bundleGraphEdgeTypes.references);
569
- let resolved;
570
- if (referencedAssetNodeIds.length === 0) {
571
- resolved = this.getResolvedAsset(dependency, bundle);
572
- } else if (referencedAssetNodeIds.length === 1) {
573
- let referencedAssetNode = this._graph.getNode(referencedAssetNodeIds[0]);
574
- // If a referenced asset already exists, resolve this dependency to it.
575
- (0, _assert().default)((referencedAssetNode === null || referencedAssetNode === void 0 ? void 0 : referencedAssetNode.type) === 'asset');
576
- resolved = referencedAssetNode.value;
577
- } else {
578
- throw new Error('Dependencies can only reference one asset');
579
- }
580
- if (resolved == null) {
581
- return;
582
- } else {
583
- return {
584
- type: 'asset',
585
- value: resolved
586
- };
587
- }
588
- }
589
- let node = this._graph.getNodeIdsConnectedFrom(this._graph.getNodeIdByContentKey(dependency.id)).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).find(node => node.type === 'bundle_group');
590
- if (node == null) {
591
- return;
592
- }
593
- (0, _assert().default)(node.type === 'bundle_group');
594
- return {
595
- type: 'bundle_group',
596
- value: node.value
597
- };
598
- }
599
-
600
- // eslint-disable-next-line no-unused-vars
601
- getReferencedBundle(dependency, fromBundle) {
602
- let dependencyNodeId = this._graph.getNodeIdByContentKey(dependency.id);
603
-
604
- // Find an attached bundle via a reference edge (e.g. from createAssetReference).
605
- let bundleNodes = this._graph.getNodeIdsConnectedFrom(dependencyNodeId, bundleGraphEdgeTypes.references).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).filter(node => node.type === 'bundle');
606
- if (bundleNodes.length) {
607
- let bundleNode = bundleNodes.find(b => b.type === 'bundle' && b.value.type === fromBundle.type) || bundleNodes[0];
608
- (0, _assert().default)(bundleNode.type === 'bundle');
609
- return bundleNode.value;
610
- }
611
-
612
- // If this dependency is async, there will be a bundle group attached to it.
613
- let node = this._graph.getNodeIdsConnectedFrom(dependencyNodeId).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).find(node => node.type === 'bundle_group');
614
- if (node != null) {
615
- (0, _assert().default)(node.type === 'bundle_group');
616
- return this.getBundlesInBundleGroup(node.value, {
617
- includeInline: true
618
- }).find(b => {
619
- let mainEntryId = b.entryAssetIds[b.entryAssetIds.length - 1];
620
- return mainEntryId != null && node.value.entryAssetId === mainEntryId;
621
- });
622
- }
623
- }
624
- removeAssetGraphFromBundle(asset, bundle) {
625
- let bundleNodeId = this._graph.getNodeIdByContentKey(bundle.id);
626
- let assetNodeId = this._graph.getNodeIdByContentKey(asset.id);
627
-
628
- // Remove all contains edges from the bundle to the nodes in the asset's
629
- // subgraph.
630
- this._graph.traverse((nodeId, context, actions) => {
631
- let node = (0, _nullthrows().default)(this._graph.getNode(nodeId));
632
- if (node.type === 'bundle_group') {
633
- actions.skipChildren();
634
- return;
635
- }
636
- if (node.type !== 'dependency' && node.type !== 'asset') {
637
- return;
638
- }
639
- if (this._graph.hasEdge(bundleNodeId, nodeId, bundleGraphEdgeTypes.contains)) {
640
- this._graph.removeEdge(bundleNodeId, nodeId, bundleGraphEdgeTypes.contains,
641
- // Removing this contains edge should not orphan the connected node. This
642
- // is disabled for performance reasons as these edges are removed as part
643
- // of a traversal, and checking for orphans becomes quite expensive in
644
- // aggregate.
645
- false /* removeOrphans */);
646
- } else {
647
- actions.skipChildren();
648
- }
649
- if (node.type === 'asset' && this._graph.hasEdge(bundleNodeId, nodeId)) {
650
- // Remove the untyped edge from the bundle to the node (it's an entry)
651
- this._graph.removeEdge(bundleNodeId, nodeId);
652
- let entryIndex = bundle.entryAssetIds.indexOf(node.value.id);
653
- if (entryIndex >= 0) {
654
- // Shared bundles have untyped edges to their asset graphs but don't
655
- // have entry assets. For those that have entry asset ids, remove them.
656
- bundle.entryAssetIds.splice(entryIndex, 1);
657
- }
658
- }
659
- if (node.type === 'dependency') {
660
- this._removeExternalDependency(bundle, node.value);
661
- if (this._graph.hasEdge(bundleNodeId, nodeId, bundleGraphEdgeTypes.references)) {
662
- this._graph.addEdge(bundleNodeId, nodeId, bundleGraphEdgeTypes.references);
663
- this.markDependencyReferenceable(node.value);
664
- }
665
- if (this._graph.hasEdge(bundleNodeId, nodeId, bundleGraphEdgeTypes.internal_async)) {
666
- this._graph.removeEdge(bundleNodeId, nodeId, bundleGraphEdgeTypes.internal_async);
667
- }
668
- }
669
- }, assetNodeId);
670
-
671
- // Remove bundle node if it no longer has any entry assets
672
- if (this._graph.getNodeIdsConnectedFrom(bundleNodeId).length === 0) {
673
- this.removeBundle(bundle);
674
- }
675
- this._bundleContentHashes.delete(bundle.id);
676
- }
677
-
678
- /**
679
- * Remove a bundle from the bundle graph. Remove its bundle group if it is
680
- * the only bundle in the group.
681
- */
682
- removeBundle(bundle) {
683
- // Remove bundle node if it no longer has any entry assets
684
- let bundleNodeId = this._graph.getNodeIdByContentKey(bundle.id);
685
- let bundleGroupNodeIds = this._graph.getNodeIdsConnectedTo(bundleNodeId, bundleGraphEdgeTypes.bundle);
686
- this._graph.removeNode(bundleNodeId);
687
- let removedBundleGroups = new Set();
688
- // Remove bundle group node if it no longer has any bundles
689
- for (let bundleGroupNodeId of bundleGroupNodeIds) {
690
- let bundleGroupNode = (0, _nullthrows().default)(this._graph.getNode(bundleGroupNodeId));
691
- (0, _assert().default)(bundleGroupNode.type === 'bundle_group');
692
- let bundleGroup = bundleGroupNode.value;
693
- if (
694
- // If the bundle group's entry asset belongs to this bundle, the group
695
- // was created because of this bundle. Remove the group.
696
- bundle.entryAssetIds.includes(bundleGroup.entryAssetId) ||
697
- // If the bundle group is now empty, remove it.
698
- this.getBundlesInBundleGroup(bundleGroup, {
699
- includeInline: true
700
- }).length === 0) {
701
- removedBundleGroups.add(bundleGroup);
702
- this.removeBundleGroup(bundleGroup);
703
- }
704
- }
705
- this._bundleContentHashes.delete(bundle.id);
706
- return removedBundleGroups;
707
- }
708
- removeBundleGroup(bundleGroup) {
709
- let bundleGroupNode = (0, _nullthrows().default)(this._graph.getNodeByContentKey((0, _utils2.getBundleGroupId)(bundleGroup)));
710
- (0, _assert().default)(bundleGroupNode.type === 'bundle_group');
711
- let bundlesInGroup = this.getBundlesInBundleGroup(bundleGroupNode.value, {
712
- includeInline: true
713
- });
714
- for (let bundle of bundlesInGroup) {
715
- if (this.getBundleGroupsContainingBundle(bundle).length === 1) {
716
- let removedBundleGroups = this.removeBundle(bundle);
717
- if (removedBundleGroups.has(bundleGroup)) {
718
- // This function can be reentered through removeBundle above. In the case this
719
- // bundle group has already been removed, stop.
720
- return;
721
- }
722
- }
723
- }
724
-
725
- // This function can be reentered through removeBundle above. In this case,
726
- // the node may already been removed.
727
- if (this._graph.hasContentKey(bundleGroupNode.id)) {
728
- this._graph.removeNode(this._graph.getNodeIdByContentKey(bundleGroupNode.id));
729
- }
730
- (0, _assert().default)(bundlesInGroup.every(bundle => this.getBundleGroupsContainingBundle(bundle).length > 0));
731
- }
732
- _removeExternalDependency(bundle, dependency) {
733
- let bundleNodeId = this._graph.getNodeIdByContentKey(bundle.id);
734
- for (let bundleGroupNode of this._graph.getNodeIdsConnectedFrom(this._graph.getNodeIdByContentKey(dependency.id)).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).filter(node => node.type === 'bundle_group')) {
735
- let bundleGroupNodeId = this._graph.getNodeIdByContentKey(bundleGroupNode.id);
736
- if (!this._graph.hasEdge(bundleNodeId, bundleGroupNodeId, bundleGraphEdgeTypes.bundle)) {
737
- continue;
738
- }
739
- let inboundDependencies = this._graph.getNodeIdsConnectedTo(bundleGroupNodeId).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).filter(node => node.type === 'dependency').map(node => {
740
- (0, _assert().default)(node.type === 'dependency');
741
- return node.value;
742
- });
743
-
744
- // If every inbound dependency to this bundle group does not belong to this bundle,
745
- // or the dependency is internal to the bundle, then the connection between
746
- // this bundle and the group is safe to remove.
747
- if (inboundDependencies.every(dependency => dependency.specifierType !== _types.SpecifierType.url && (!this.bundleHasDependency(bundle, dependency) || this._graph.hasEdge(bundleNodeId, this._graph.getNodeIdByContentKey(dependency.id), bundleGraphEdgeTypes.internal_async)))) {
748
- this._graph.removeEdge(bundleNodeId, bundleGroupNodeId, bundleGraphEdgeTypes.bundle);
749
- }
750
- }
751
- }
752
- createAssetReference(dependency, asset, bundle) {
753
- let dependencyId = this._graph.getNodeIdByContentKey(dependency.id);
754
- let assetId = this._graph.getNodeIdByContentKey(asset.id);
755
- let bundleId = this._graph.getNodeIdByContentKey(bundle.id);
756
- this._graph.addEdge(dependencyId, assetId, bundleGraphEdgeTypes.references);
757
- this._graph.addEdge(dependencyId, bundleId, bundleGraphEdgeTypes.references);
758
- this.markDependencyReferenceable(dependency);
759
- if (this._graph.hasEdge(dependencyId, assetId)) {
760
- this._graph.removeEdge(dependencyId, assetId);
761
- }
762
- }
763
- createBundleReference(from, to) {
764
- this._graph.addEdge(this._graph.getNodeIdByContentKey(from.id), this._graph.getNodeIdByContentKey(to.id), bundleGraphEdgeTypes.references);
765
- }
766
- createBundleConditionalReference(from, to) {
767
- this._graph.addEdge(this._graph.getNodeIdByContentKey(from.id), this._graph.getNodeIdByContentKey(to.id), bundleGraphEdgeTypes.conditional);
768
- }
769
- getBundlesWithAsset(asset) {
770
- return this._graph.getNodeIdsConnectedTo(this._graph.getNodeIdByContentKey(asset.id), bundleGraphEdgeTypes.contains).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).filter(node => node.type === 'bundle').map(node => {
771
- (0, _assert().default)(node.type === 'bundle');
772
- return node.value;
773
- });
774
- }
775
- getBundlesWithDependency(dependency) {
776
- return this._graph.getNodeIdsConnectedTo((0, _nullthrows().default)(this._graph.getNodeIdByContentKey(dependency.id)), bundleGraphEdgeTypes.contains).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).filter(node => node.type === 'bundle').map(node => {
777
- (0, _assert().default)(node.type === 'bundle');
778
- return node.value;
779
- });
780
- }
781
- getDependencyAssets(dependency) {
782
- return this._graph.getNodeIdsConnectedFrom(this._graph.getNodeIdByContentKey(dependency.id)).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).filter(node => node.type === 'asset').map(node => {
783
- (0, _assert().default)(node.type === 'asset');
784
- return node.value;
785
- });
786
- }
787
- getResolvedAsset(dep, bundle) {
788
- let assets = this.getDependencyAssets(dep);
789
- let firstAsset = assets[0];
790
- let resolved =
791
- // If no bundle is specified, use the first concrete asset.
792
- bundle == null ? firstAsset :
793
- // Otherwise, find the first asset that belongs to this bundle.
794
- assets.find(asset => this.bundleHasAsset(bundle, asset)) || assets.find(a => a.type === bundle.type) || firstAsset;
795
-
796
- // If a resolution still hasn't been found, return the first referenced asset.
797
- if (resolved == null) {
798
- let potential = [];
799
- this._graph.traverse((nodeId, _, traversal) => {
800
- let node = (0, _nullthrows().default)(this._graph.getNode(nodeId));
801
- if (node.type === 'asset') {
802
- potential.push(node.value);
803
- } else if (node.id !== dep.id) {
804
- traversal.skipChildren();
805
- }
806
- }, this._graph.getNodeIdByContentKey(dep.id), bundleGraphEdgeTypes.references);
807
- if (bundle) {
808
- resolved = potential.find(a => a.type === bundle.type);
809
- }
810
- resolved ||= potential[0];
811
- }
812
- return resolved;
813
- }
814
- getDependencies(asset) {
815
- let nodeId = this._graph.getNodeIdByContentKey(asset.id);
816
- return this._graph.getNodeIdsConnectedFrom(nodeId).map(id => {
817
- let node = (0, _nullthrows().default)(this._graph.getNode(id));
818
- (0, _assert().default)(node.type === 'dependency');
819
- return node.value;
820
- });
821
- }
822
- traverseAssets(bundle, visit, startAsset) {
823
- return this.traverseBundle(bundle, (0, _graph().mapVisitor)(node => node.type === 'asset' ? node.value : null, visit), startAsset);
824
- }
825
- isAssetReferenced(bundle, asset) {
826
- // If the asset is available in multiple bundles in the same target, it's referenced.
827
- if (this.getBundlesWithAsset(asset).filter(b => b.target.name === bundle.target.name && b.target.distDir === bundle.target.distDir).length > 1) {
828
- return true;
829
- }
830
- let assetNodeId = (0, _nullthrows().default)(this._graph.getNodeIdByContentKey(asset.id));
831
- if (this._graph.getNodeIdsConnectedTo(assetNodeId, bundleGraphEdgeTypes.references).map(id => this._graph.getNode(id)).some(node => (node === null || node === void 0 ? void 0 : node.type) === 'dependency' && (node.value.priority === _types.Priority.lazy || node.value.priority === _types.Priority.conditional) && node.value.specifierType !== _types.SpecifierType.url)) {
832
- // If this asset is referenced by any async dependency, it's referenced.
833
- return true;
834
- }
835
- let dependencies = this._graph.getNodeIdsConnectedTo(assetNodeId).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).filter(node => node.type === 'dependency').map(node => {
836
- (0, _assert().default)(node.type === 'dependency');
837
- return node.value;
838
- });
839
- const bundleHasReference = bundle => {
840
- return !this.bundleHasAsset(bundle, asset) && dependencies.some(dependency => this.bundleHasDependency(bundle, dependency));
841
- };
842
- let visitedBundles = new Set();
843
- let siblingBundles = new Set(this.getBundleGroupsContainingBundle(bundle).flatMap(bundleGroup => this.getBundlesInBundleGroup(bundleGroup, {
844
- includeInline: true
845
- })));
846
-
847
- // Check if any of this bundle's descendants, referencers, bundles referenced
848
- // by referencers, or descendants of its referencers use the asset without
849
- // an explicit reference edge. This can happen if e.g. the asset has been
850
- // deduplicated.
851
- return [...siblingBundles].some(referencer => {
852
- let isReferenced = false;
853
- this.traverseBundles((descendant, _, actions) => {
854
- if (descendant.id === bundle.id) {
855
- return;
856
- }
857
- if (visitedBundles.has(descendant)) {
858
- actions.skipChildren();
859
- return;
860
- }
861
- visitedBundles.add(descendant);
862
- if (descendant.type !== bundle.type || descendant.env.context !== bundle.env.context) {
863
- actions.skipChildren();
864
- return;
865
- }
866
- if (bundleHasReference(descendant)) {
867
- isReferenced = true;
868
- actions.stop();
869
- }
870
- }, referencer);
871
- return isReferenced;
872
- });
873
- }
874
- hasParentBundleOfType(bundle, type) {
875
- let parents = this.getParentBundles(bundle);
876
- return parents.length > 0 && parents.every(parent => parent.type === type);
877
- }
878
- getParentBundles(bundle) {
879
- let parentBundles = new Set();
880
- for (let bundleGroup of this.getBundleGroupsContainingBundle(bundle)) {
881
- for (let parentBundle of this.getParentBundlesOfBundleGroup(bundleGroup)) {
882
- parentBundles.add(parentBundle);
883
- }
884
- }
885
- return [...parentBundles];
886
- }
887
- isAssetReachableFromBundle(asset, bundle) {
888
- // If a bundle's environment is isolated, it can't access assets present
889
- // in any ancestor bundles. Don't consider any assets reachable.
890
- if (_Environment.ISOLATED_ENVS.has(bundle.env.context) || !bundle.isSplittable || bundle.bundleBehavior === _types.BundleBehavior.isolated || bundle.bundleBehavior === _types.BundleBehavior.inline) {
891
- return false;
892
- }
893
-
894
- // For an asset to be reachable from a bundle, it must either exist in a sibling bundle,
895
- // or in an ancestor bundle group reachable from all parent bundles.
896
- let bundleGroups = this.getBundleGroupsContainingBundle(bundle);
897
- return bundleGroups.every(bundleGroup => {
898
- // If the asset is in any sibling bundles of the original bundle, it is reachable.
899
- let bundles = this.getBundlesInBundleGroup(bundleGroup);
900
- if (bundles.some(b => b.id !== bundle.id && b.bundleBehavior !== _types.BundleBehavior.isolated && b.bundleBehavior !== _types.BundleBehavior.inline && this.bundleHasAsset(b, asset))) {
901
- return true;
902
- }
903
-
904
- // Get a list of parent bundle nodes pointing to the bundle group
905
- let parentBundleNodes = this._graph.getNodeIdsConnectedTo(this._graph.getNodeIdByContentKey((0, _utils2.getBundleGroupId)(bundleGroup)), bundleGraphEdgeTypes.bundle);
906
-
907
- // Check that every parent bundle has a bundle group in its ancestry that contains the asset.
908
- return parentBundleNodes.every(bundleNodeId => {
909
- let bundleNode = (0, _nullthrows().default)(this._graph.getNode(bundleNodeId));
910
- if (bundleNode.type !== 'bundle' || bundleNode.value.bundleBehavior === _types.BundleBehavior.isolated || bundleNode.value.bundleBehavior === _types.BundleBehavior.inline) {
911
- return false;
912
- }
913
- let isReachable = true;
914
- this._graph.traverseAncestors(bundleNodeId, (nodeId, ctx, actions) => {
915
- let node = (0, _nullthrows().default)(this._graph.getNode(nodeId));
916
- // If we've reached the root or a context change without
917
- // finding this asset in the ancestry, it is not reachable.
918
- if (node.type === 'root' || node.type === 'bundle' && (node.value.id === bundle.id || node.value.env.context !== bundle.env.context)) {
919
- isReachable = false;
920
- actions.stop();
921
- return;
922
- }
923
- if (node.type === 'bundle_group') {
924
- let childBundles = this.getBundlesInBundleGroup(node.value);
925
- if (childBundles.some(b => b.id !== bundle.id && b.bundleBehavior !== _types.BundleBehavior.isolated && b.bundleBehavior !== _types.BundleBehavior.inline && this.bundleHasAsset(b, asset))) {
926
- actions.skipChildren();
927
- }
928
- }
929
- }, [bundleGraphEdgeTypes.references, bundleGraphEdgeTypes.bundle]);
930
- return isReachable;
931
- });
932
- });
933
- }
934
-
935
- /**
936
- * TODO: Document why this works like this & why visitor order matters
937
- * on these use-cases.
938
- */
939
- traverseBundle(bundle, visit, startAsset) {
940
- let entries = !startAsset;
941
- let bundleNodeId = this._graph.getNodeIdByContentKey(bundle.id);
942
-
943
- // A modified DFS traversal which traverses entry assets in the same order
944
- // as their ids appear in `bundle.entryAssetIds`.
945
- return this._graph.dfs({
946
- visit: (0, _graph().mapVisitor)((nodeId, actions) => {
947
- let node = (0, _nullthrows().default)(this._graph.getNode(nodeId));
948
- if (nodeId === bundleNodeId) {
949
- return;
950
- }
951
- if (node.type === 'dependency' || node.type === 'asset') {
952
- if (this._graph.hasEdge(bundleNodeId, nodeId, bundleGraphEdgeTypes.contains)) {
953
- return node;
954
- }
955
- }
956
- actions.skipChildren();
957
- }, visit),
958
- startNodeId: startAsset ? this._graph.getNodeIdByContentKey(startAsset.id) : bundleNodeId,
959
- getChildren: nodeId => {
960
- let children = this._graph.getNodeIdsConnectedFrom(nodeId).map(id => [id, (0, _nullthrows().default)(this._graph.getNode(id))]);
961
- let sorted = entries && bundle.entryAssetIds.length > 0 ? children.sort(([, a], [, b]) => {
962
- let aIndex = bundle.entryAssetIds.indexOf(a.id);
963
- let bIndex = bundle.entryAssetIds.indexOf(b.id);
964
- if (aIndex === bIndex) {
965
- // If both don't exist in the entry asset list, or
966
- // otherwise have the same index.
967
- return 0;
968
- } else if (aIndex === -1) {
969
- return 1;
970
- } else if (bIndex === -1) {
971
- return -1;
972
- }
973
- return aIndex - bIndex;
974
- }) : children;
975
- entries = false;
976
- return sorted.map(([id]) => id);
977
- }
978
- });
979
- }
980
- traverse(visit, start) {
981
- return this._graph.filteredTraverse(nodeId => {
982
- let node = (0, _nullthrows().default)(this._graph.getNode(nodeId));
983
- if (node.type === 'asset' || node.type === 'dependency') {
984
- return node;
985
- }
986
- }, visit, start ? this._graph.getNodeIdByContentKey(start.id) : undefined,
987
- // start with root
988
- _graph().ALL_EDGE_TYPES);
989
- }
990
- getChildBundles(bundle) {
991
- let siblings = new Set(this.getReferencedBundles(bundle));
992
- let bundles = [];
993
- this.traverseBundles((b, _, actions) => {
994
- if (bundle.id === b.id) {
995
- return;
996
- }
997
- if (!siblings.has(b)) {
998
- bundles.push(b);
999
- }
1000
- actions.skipChildren();
1001
- }, bundle);
1002
- return bundles;
1003
- }
1004
- traverseBundles(visit, startBundle) {
1005
- return this._graph.filteredTraverse(nodeId => {
1006
- let node = (0, _nullthrows().default)(this._graph.getNode(nodeId));
1007
- return node.type === 'bundle' ? node.value : null;
1008
- }, visit, startBundle ? this._graph.getNodeIdByContentKey(startBundle.id) : null, [bundleGraphEdgeTypes.bundle, bundleGraphEdgeTypes.references]);
1009
- }
1010
- getBundles(opts) {
1011
- let bundles = [];
1012
- this.traverseBundles(bundle => {
1013
- if (opts !== null && opts !== void 0 && opts.includeInline || bundle.bundleBehavior !== _types.BundleBehavior.inline) {
1014
- bundles.push(bundle);
1015
- }
1016
- });
1017
- return bundles;
1018
- }
1019
- getTotalSize(asset) {
1020
- let size = 0;
1021
- this._graph.traverse((nodeId, _, actions) => {
1022
- let node = (0, _nullthrows().default)(this._graph.getNode(nodeId));
1023
- if (node.type === 'bundle_group') {
1024
- actions.skipChildren();
1025
- return;
1026
- }
1027
- if (node.type === 'asset') {
1028
- size += node.value.stats.size;
1029
- }
1030
- }, this._graph.getNodeIdByContentKey(asset.id));
1031
- return size;
1032
- }
1033
- getReferencingBundles(bundle) {
1034
- let referencingBundles = new Set();
1035
- this._graph.traverseAncestors(this._graph.getNodeIdByContentKey(bundle.id), nodeId => {
1036
- let node = (0, _nullthrows().default)(this._graph.getNode(nodeId));
1037
- if (node.type === 'bundle' && node.value.id !== bundle.id) {
1038
- referencingBundles.add(node.value);
1039
- }
1040
- }, bundleGraphEdgeTypes.references);
1041
- return [...referencingBundles];
1042
- }
1043
- getBundleGroupsContainingBundle(bundle) {
1044
- let bundleGroups = new Set();
1045
- for (let currentBundle of [bundle, ...this.getReferencingBundles(bundle)]) {
1046
- for (let bundleGroup of this.getDirectParentBundleGroups(currentBundle)) {
1047
- bundleGroups.add(bundleGroup);
1048
- }
1049
- }
1050
- return [...bundleGroups];
1051
- }
1052
- getDirectParentBundleGroups(bundle) {
1053
- return this._graph.getNodeIdsConnectedTo((0, _nullthrows().default)(this._graph.getNodeIdByContentKey(bundle.id)), bundleGraphEdgeTypes.bundle).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).filter(node => node.type === 'bundle_group').map(node => {
1054
- (0, _assert().default)(node.type === 'bundle_group');
1055
- return node.value;
1056
- });
1057
- }
1058
- getBundlesInBundleGroup(bundleGroup, opts) {
1059
- let bundles = new Set();
1060
- for (let bundleNodeId of this._graph.getNodeIdsConnectedFrom(this._graph.getNodeIdByContentKey((0, _utils2.getBundleGroupId)(bundleGroup)), bundleGraphEdgeTypes.bundle)) {
1061
- let bundleNode = (0, _nullthrows().default)(this._graph.getNode(bundleNodeId));
1062
- (0, _assert().default)(bundleNode.type === 'bundle');
1063
- let bundle = bundleNode.value;
1064
- if (opts !== null && opts !== void 0 && opts.includeInline || bundle.bundleBehavior !== _types.BundleBehavior.inline) {
1065
- bundles.add(bundle);
1066
- }
1067
- for (let referencedBundle of this.getReferencedBundles(bundle, {
1068
- includeInline: opts === null || opts === void 0 ? void 0 : opts.includeInline
1069
- })) {
1070
- bundles.add(referencedBundle);
1071
- }
1072
- }
1073
- return [...bundles];
1074
- }
1075
- getReferencedBundles(bundle, opts) {
1076
- let recursive = (opts === null || opts === void 0 ? void 0 : opts.recursive) ?? true;
1077
- let includeInline = (opts === null || opts === void 0 ? void 0 : opts.includeInline) ?? false;
1078
- let referencedBundles = new Set();
1079
- this._graph.dfs({
1080
- visit: (nodeId, _, actions) => {
1081
- let node = (0, _nullthrows().default)(this._graph.getNode(nodeId));
1082
- if (node.type !== 'bundle') {
1083
- return;
1084
- }
1085
- if (node.value.id === bundle.id) {
1086
- return;
1087
- }
1088
- if (includeInline || node.value.bundleBehavior !== _types.BundleBehavior.inline) {
1089
- referencedBundles.add(node.value);
1090
- }
1091
- if (!recursive) {
1092
- actions.skipChildren();
1093
- }
1094
- },
1095
- startNodeId: this._graph.getNodeIdByContentKey(bundle.id),
1096
- getChildren: nodeId =>
1097
- // Shared bundles seem to depend on being used in the opposite order
1098
- // they were added.
1099
- // TODO: Should this be the case?
1100
- this._graph.getNodeIdsConnectedFrom(nodeId, bundleGraphEdgeTypes.references)
1101
- });
1102
- return [...referencedBundles];
1103
- }
1104
- getIncomingDependencies(asset) {
1105
- if (!this._graph.hasContentKey(asset.id)) {
1106
- return [];
1107
- }
1108
- // Dependencies can be a a parent node via an untyped edge (like in the AssetGraph but without AssetGroups)
1109
- // or they can be parent nodes via a 'references' edge
1110
- return this._graph.getNodeIdsConnectedTo(this._graph.getNodeIdByContentKey(asset.id), _graph().ALL_EDGE_TYPES).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).filter(n => n.type === 'dependency').map(n => {
1111
- (0, _assert().default)(n.type === 'dependency');
1112
- return n.value;
1113
- });
1114
- }
1115
- getAssetWithDependency(dep) {
1116
- if (!this._graph.hasContentKey(dep.id)) {
1117
- return null;
1118
- }
1119
- let res = null;
1120
- let count = 0;
1121
- this._graph.forEachNodeIdConnectedTo(this._graph.getNodeIdByContentKey(dep.id), node => {
1122
- res = node;
1123
- count += 1;
1124
- if (count > 1) {
1125
- throw new Error('Expected a single asset to be connected to a dependency');
1126
- }
1127
- }, 1);
1128
- let resNode = res != null ? this._graph.getNode(res) : null;
1129
- if ((resNode === null || resNode === void 0 ? void 0 : resNode.type) === 'asset') {
1130
- return resNode.value;
1131
- }
1132
- }
1133
- bundleHasAsset(bundle, asset) {
1134
- let bundleNodeId = this._graph.getNodeIdByContentKey(bundle.id);
1135
- let assetNodeId = this._graph.getNodeIdByContentKey(asset.id);
1136
- return this._graph.hasEdge(bundleNodeId, assetNodeId, bundleGraphEdgeTypes.contains);
1137
- }
1138
- bundleHasDependency(bundle, dependency) {
1139
- let bundleNodeId = this._graph.getNodeIdByContentKey(bundle.id);
1140
- let dependencyNodeId = this._graph.getNodeIdByContentKey(dependency.id);
1141
- return this._graph.hasEdge(bundleNodeId, dependencyNodeId, bundleGraphEdgeTypes.contains);
1142
- }
1143
- filteredTraverse(bundleNodeId, filter, visit) {
1144
- return this._graph.filteredTraverse(filter, visit, bundleNodeId);
1145
- }
1146
- getSymbolResolution(asset, symbol, boundary) {
1147
- var _asset$symbols;
1148
- let assetOutside = boundary && !this.bundleHasAsset(boundary, asset);
1149
- let identifier = (_asset$symbols = asset.symbols) === null || _asset$symbols === void 0 || (_asset$symbols = _asset$symbols.get(symbol)) === null || _asset$symbols === void 0 ? void 0 : _asset$symbols.local;
1150
- if (symbol === '*') {
1151
- var _asset$symbols2;
1152
- return {
1153
- asset,
1154
- exportSymbol: '*',
1155
- symbol: identifier ?? null,
1156
- loc: (_asset$symbols2 = asset.symbols) === null || _asset$symbols2 === void 0 || (_asset$symbols2 = _asset$symbols2.get(symbol)) === null || _asset$symbols2 === void 0 ? void 0 : _asset$symbols2.loc
1157
- };
1158
- }
1159
- let found = false;
1160
- let nonStaticDependency = false;
1161
- let skipped = false;
1162
- let deps = this.getDependencies(asset).reverse();
1163
- let potentialResults = [];
1164
- for (let dep of deps) {
1165
- var _depSymbols$get;
1166
- let depSymbols = dep.symbols;
1167
- if (!depSymbols) {
1168
- nonStaticDependency = true;
1169
- continue;
1170
- }
1171
- // If this is a re-export, find the original module.
1172
- let symbolLookup = new Map([...depSymbols].map(([key, val]) => [val.local, key]));
1173
- let depSymbol = symbolLookup.get(identifier);
1174
- if (depSymbol != null) {
1175
- let resolved = this.getResolvedAsset(dep, boundary);
1176
- if (!resolved || resolved.id === asset.id) {
1177
- var _asset$symbols3;
1178
- // External module or self-reference
1179
- return {
1180
- asset,
1181
- exportSymbol: symbol,
1182
- symbol: identifier,
1183
- loc: (_asset$symbols3 = asset.symbols) === null || _asset$symbols3 === void 0 || (_asset$symbols3 = _asset$symbols3.get(symbol)) === null || _asset$symbols3 === void 0 ? void 0 : _asset$symbols3.loc
1184
- };
1185
- }
1186
- if (assetOutside) {
1187
- // We found the symbol, but `asset` is outside, return `asset` and the original symbol
1188
- found = true;
1189
- break;
1190
- }
1191
- if (this.isDependencySkipped(dep)) {
1192
- // We found the symbol and `dep` was skipped
1193
- skipped = true;
1194
- break;
1195
- }
1196
- let {
1197
- asset: resolvedAsset,
1198
- symbol: resolvedSymbol,
1199
- exportSymbol,
1200
- loc
1201
- } = this.getSymbolResolution(resolved, depSymbol, boundary);
1202
- if (!loc) {
1203
- var _asset$symbols4;
1204
- // Remember how we got there
1205
- loc = (_asset$symbols4 = asset.symbols) === null || _asset$symbols4 === void 0 || (_asset$symbols4 = _asset$symbols4.get(symbol)) === null || _asset$symbols4 === void 0 ? void 0 : _asset$symbols4.loc;
1206
- }
1207
- return {
1208
- asset: resolvedAsset,
1209
- symbol: resolvedSymbol,
1210
- exportSymbol,
1211
- loc
1212
- };
1213
- }
1214
- // If this module exports wildcards, resolve the original module.
1215
- // Default exports are excluded from wildcard exports.
1216
- // Wildcard reexports are never listed in the reexporting asset's symbols.
1217
- if (identifier == null && ((_depSymbols$get = depSymbols.get('*')) === null || _depSymbols$get === void 0 ? void 0 : _depSymbols$get.local) === '*' && symbol !== 'default') {
1218
- let resolved = this.getResolvedAsset(dep, boundary);
1219
- if (!resolved) {
1220
- continue;
1221
- }
1222
- let result = this.getSymbolResolution(resolved, symbol, boundary);
1223
-
1224
- // We found the symbol
1225
- if (result.symbol != undefined) {
1226
- var _resolved$symbols;
1227
- if (assetOutside) {
1228
- // ..., but `asset` is outside, return `asset` and the original symbol
1229
- found = true;
1230
- break;
1231
- }
1232
- if (this.isDependencySkipped(dep)) {
1233
- // We found the symbol and `dep` was skipped
1234
- skipped = true;
1235
- break;
1236
- }
1237
- return {
1238
- asset: result.asset,
1239
- symbol: result.symbol,
1240
- exportSymbol: result.exportSymbol,
1241
- loc: (_resolved$symbols = resolved.symbols) === null || _resolved$symbols === void 0 || (_resolved$symbols = _resolved$symbols.get(symbol)) === null || _resolved$symbols === void 0 ? void 0 : _resolved$symbols.loc
1242
- };
1243
- }
1244
- if (result.symbol === null) {
1245
- found = true;
1246
- if (boundary && !this.bundleHasAsset(boundary, result.asset)) {
1247
- // If the returned asset is outside (and it's the first asset that is outside), return it.
1248
- if (!assetOutside) {
1249
- var _resolved$symbols2;
1250
- return {
1251
- asset: result.asset,
1252
- symbol: result.symbol,
1253
- exportSymbol: result.exportSymbol,
1254
- loc: (_resolved$symbols2 = resolved.symbols) === null || _resolved$symbols2 === void 0 || (_resolved$symbols2 = _resolved$symbols2.get(symbol)) === null || _resolved$symbols2 === void 0 ? void 0 : _resolved$symbols2.loc
1255
- };
1256
- } else {
1257
- // Otherwise the original asset will be returned at the end.
1258
- break;
1259
- }
1260
- } else {
1261
- var _resolved$symbols3;
1262
- // We didn't find it in this dependency, but it might still be there: bailout.
1263
- // Continue searching though, with the assumption that there are no conficting reexports
1264
- // and there might be a another (re)export (where we might statically find the symbol).
1265
- potentialResults.push({
1266
- asset: result.asset,
1267
- symbol: result.symbol,
1268
- exportSymbol: result.exportSymbol,
1269
- loc: (_resolved$symbols3 = resolved.symbols) === null || _resolved$symbols3 === void 0 || (_resolved$symbols3 = _resolved$symbols3.get(symbol)) === null || _resolved$symbols3 === void 0 ? void 0 : _resolved$symbols3.loc
1270
- });
1271
- }
1272
- }
1273
- }
1274
- }
1275
-
1276
- // We didn't find the exact symbol...
1277
- if (potentialResults.length == 1) {
1278
- // ..., but if it does exist, it has to be behind this one reexport.
1279
- return potentialResults[0];
1280
- } else {
1281
- var _asset$symbols6;
1282
- let result = identifier;
1283
- if (skipped) {
1284
- // ... and it was excluded (by symbol propagation) or deferred.
1285
- result = false;
1286
- } else {
1287
- // ... and there is no single reexport, but it might still be exported:
1288
- if (found) {
1289
- // Fallback to namespace access, because of a bundle boundary.
1290
- result = null;
1291
- } else if (result === undefined) {
1292
- var _asset$symbols5;
1293
- // If not exported explicitly by the asset (= would have to be in * or a reexport-all) ...
1294
- if (nonStaticDependency || (_asset$symbols5 = asset.symbols) !== null && _asset$symbols5 !== void 0 && _asset$symbols5.has('*')) {
1295
- // ... and if there are non-statically analyzable dependencies or it's a CJS asset,
1296
- // fallback to namespace access.
1297
- result = null;
1298
- }
1299
- // (It shouldn't be possible for the symbol to be in a reexport-all and to end up here).
1300
- // Otherwise return undefined to report that the symbol wasn't found.
1301
- }
1302
- }
1303
-
1304
- return {
1305
- asset,
1306
- exportSymbol: symbol,
1307
- symbol: result,
1308
- loc: (_asset$symbols6 = asset.symbols) === null || _asset$symbols6 === void 0 || (_asset$symbols6 = _asset$symbols6.get(symbol)) === null || _asset$symbols6 === void 0 ? void 0 : _asset$symbols6.loc
1309
- };
1310
- }
1311
- }
1312
- getAssetById(contentKey) {
1313
- let node = this._graph.getNodeByContentKey(contentKey);
1314
- if (node == null) {
1315
- throw new Error('Node not found');
1316
- } else if (node.type !== 'asset') {
1317
- throw new Error('Node was not an asset');
1318
- }
1319
- return node.value;
1320
- }
1321
- getAssetPublicId(asset) {
1322
- let publicId = this._publicIdByAssetId.get(asset.id);
1323
- if (publicId == null) {
1324
- throw new Error("Asset or it's public id not found");
1325
- }
1326
- return publicId;
1327
- }
1328
- getExportedSymbols(asset, boundary) {
1329
- if (!asset.symbols) {
1330
- return [];
1331
- }
1332
- let symbols = [];
1333
- for (let symbol of asset.symbols.keys()) {
1334
- symbols.push({
1335
- ...this.getSymbolResolution(asset, symbol, boundary),
1336
- exportAs: symbol
1337
- });
1338
- }
1339
- let deps = this.getDependencies(asset);
1340
- for (let dep of deps) {
1341
- var _depSymbols$get2;
1342
- let depSymbols = dep.symbols;
1343
- if (!depSymbols) continue;
1344
- if (((_depSymbols$get2 = depSymbols.get('*')) === null || _depSymbols$get2 === void 0 ? void 0 : _depSymbols$get2.local) === '*') {
1345
- let resolved = this.getResolvedAsset(dep, boundary);
1346
- if (!resolved) continue;
1347
- let exported = this.getExportedSymbols(resolved, boundary).filter(s => s.exportSymbol !== 'default').map(s => s.exportSymbol !== '*' ? {
1348
- ...s,
1349
- exportAs: s.exportSymbol
1350
- } : s);
1351
- symbols.push(...exported);
1352
- }
1353
- }
1354
- return symbols;
1355
- }
1356
- getContentHash(bundle) {
1357
- let existingHash = this._bundleContentHashes.get(bundle.id);
1358
- if (existingHash != null) {
1359
- return existingHash;
1360
- }
1361
- let hash = new (_rust().Hash)();
1362
- // TODO: sort??
1363
- this.traverseAssets(bundle, asset => {
1364
- {
1365
- hash.writeString([this.getAssetPublicId(asset), asset.id, asset.outputHash].join(':'));
1366
- }
1367
- });
1368
- let hashHex = hash.finish();
1369
- this._bundleContentHashes.set(bundle.id, hashHex);
1370
- return hashHex;
1371
- }
1372
- getInlineBundles(bundle) {
1373
- let bundles = [];
1374
- let seen = new Set();
1375
- let addReferencedBundles = bundle => {
1376
- if (seen.has(bundle.id)) {
1377
- return;
1378
- }
1379
- seen.add(bundle.id);
1380
- let referencedBundles = this.getReferencedBundles(bundle, {
1381
- includeInline: true
1382
- });
1383
- for (let referenced of referencedBundles) {
1384
- if (referenced.bundleBehavior === _types.BundleBehavior.inline) {
1385
- bundles.push(referenced);
1386
- addReferencedBundles(referenced);
1387
- }
1388
- }
1389
- };
1390
- addReferencedBundles(bundle);
1391
- this.traverseBundles((childBundle, _, traversal) => {
1392
- if (childBundle.bundleBehavior === _types.BundleBehavior.inline) {
1393
- bundles.push(childBundle);
1394
- } else if (childBundle.id !== bundle.id) {
1395
- traversal.skipChildren();
1396
- }
1397
- }, bundle);
1398
- return bundles;
1399
- }
1400
- getHash(bundle) {
1401
- let hash = new (_rust().Hash)();
1402
- hash.writeString(bundle.id + JSON.stringify(bundle.target) + this.getContentHash(bundle));
1403
- if (bundle.isPlaceholder) {
1404
- hash.writeString('placeholder');
1405
- }
1406
- let inlineBundles = this.getInlineBundles(bundle);
1407
- for (let inlineBundle of inlineBundles) {
1408
- hash.writeString(this.getContentHash(inlineBundle));
1409
- }
1410
- for (let referencedBundle of this.getReferencedBundles(bundle)) {
1411
- hash.writeString(referencedBundle.id);
1412
- }
1413
- hash.writeString(JSON.stringify((0, _utils().objectSortedEntriesDeep)(bundle.env)));
1414
- return hash.finish();
1415
- }
1416
- getBundleGraphHash() {
1417
- let hashes = '';
1418
- for (let bundle of this.getBundles()) {
1419
- hashes += this.getHash(bundle);
1420
- }
1421
- return (0, _rust().hashString)(hashes);
1422
- }
1423
- addBundleToBundleGroup(bundle, bundleGroup) {
1424
- let bundleGroupNodeId = this._graph.getNodeIdByContentKey((0, _utils2.getBundleGroupId)(bundleGroup));
1425
- let bundleNodeId = this._graph.getNodeIdByContentKey(bundle.id);
1426
- if (this._graph.hasEdge(bundleGroupNodeId, bundleNodeId, bundleGraphEdgeTypes.bundle)) {
1427
- // Bundle group already has bundle
1428
- return;
1429
- }
1430
- this._graph.addEdge(bundleGroupNodeId, bundleNodeId);
1431
- this._graph.addEdge(bundleGroupNodeId, bundleNodeId, bundleGraphEdgeTypes.bundle);
1432
- for (let entryAssetId of bundle.entryAssetIds) {
1433
- let entryAssetNodeId = this._graph.getNodeIdByContentKey(entryAssetId);
1434
- if (this._graph.hasEdge(bundleGroupNodeId, entryAssetNodeId)) {
1435
- this._graph.removeEdge(bundleGroupNodeId, entryAssetNodeId);
1436
- }
1437
- }
1438
- }
1439
- getUsedSymbolsAsset(asset) {
1440
- let node = this._graph.getNodeByContentKey(asset.id);
1441
- (0, _assert().default)(node && node.type === 'asset');
1442
- return node.value.symbols ? makeReadOnlySet(new Set(node.usedSymbols.keys())) : null;
1443
- }
1444
- getUsedSymbolsDependency(dep) {
1445
- let node = this._graph.getNodeByContentKey(dep.id);
1446
- (0, _assert().default)(node && node.type === 'dependency');
1447
- return node.value.symbols ? makeReadOnlySet(new Set(node.usedSymbolsUp.keys())) : null;
1448
- }
1449
- merge(other) {
1450
- let otherGraphIdToThisNodeId = new Map();
1451
- for (let [otherNodeId, otherNode] of other._graph.nodes.entries()) {
1452
- if (!otherNode) continue;
1453
- if (this._graph.hasContentKey(otherNode.id)) {
1454
- let existingNodeId = this._graph.getNodeIdByContentKey(otherNode.id);
1455
- otherGraphIdToThisNodeId.set(otherNodeId, existingNodeId);
1456
- let existingNode = (0, _nullthrows().default)(this._graph.getNode(existingNodeId));
1457
- // Merge symbols, recompute dep.excluded based on that
1458
- if (existingNode.type === 'asset') {
1459
- (0, _assert().default)(otherNode.type === 'asset');
1460
- existingNode.usedSymbols = new Set([...existingNode.usedSymbols, ...otherNode.usedSymbols]);
1461
- } else if (existingNode.type === 'dependency') {
1462
- (0, _assert().default)(otherNode.type === 'dependency');
1463
- existingNode.usedSymbolsDown = new Set([...existingNode.usedSymbolsDown, ...otherNode.usedSymbolsDown]);
1464
- existingNode.usedSymbolsUp = new Map([...existingNode.usedSymbolsUp, ...otherNode.usedSymbolsUp]);
1465
- existingNode.excluded = (existingNode.excluded || Boolean(existingNode.hasDeferred)) && (otherNode.excluded || Boolean(otherNode.hasDeferred));
1466
- }
1467
- } else {
1468
- let updateNodeId = this._graph.addNodeByContentKey(otherNode.id, otherNode);
1469
- otherGraphIdToThisNodeId.set(otherNodeId, updateNodeId);
1470
- }
1471
- }
1472
- for (let edge of other._graph.getAllEdges()) {
1473
- this._graph.addEdge((0, _nullthrows().default)(otherGraphIdToThisNodeId.get(edge.from)), (0, _nullthrows().default)(otherGraphIdToThisNodeId.get(edge.to)), edge.type);
1474
- }
1475
- }
1476
- isEntryBundleGroup(bundleGroup) {
1477
- return this._graph.getNodeIdsConnectedTo((0, _nullthrows().default)(this._graph.getNodeIdByContentKey((0, _utils2.getBundleGroupId)(bundleGroup))), bundleGraphEdgeTypes.bundle).map(id => (0, _nullthrows().default)(this._graph.getNode(id))).some(n => n.type === 'root');
1478
- }
1479
-
1480
- /**
1481
- * Update the asset in a Bundle Graph and clear the associated Bundle hash.
1482
- */
1483
- updateAsset(asset) {
1484
- this._graph.updateNode(this._graph.getNodeIdByContentKey(asset.id), asset);
1485
- let bundles = this.getBundlesWithAsset(asset.value);
1486
- for (let bundle of bundles) {
1487
- // the bundle content will change with a modified asset
1488
- this._bundleContentHashes.delete(bundle.id);
1489
- }
1490
- }
1491
- getEntryRoot(projectRoot, target) {
1492
- let cached = this._targetEntryRoots.get(target.distDir);
1493
- if (cached != null) {
1494
- return cached;
1495
- }
1496
- let entryBundleGroupIds = this._graph.getNodeIdsConnectedFrom((0, _nullthrows().default)(this._graph.rootNodeId), bundleGraphEdgeTypes.bundle);
1497
- let entries = [];
1498
- for (let bundleGroupId of entryBundleGroupIds) {
1499
- let bundleGroupNode = this._graph.getNode(bundleGroupId);
1500
- (0, _assert().default)((bundleGroupNode === null || bundleGroupNode === void 0 ? void 0 : bundleGroupNode.type) === 'bundle_group');
1501
- if (bundleGroupNode.value.target.distDir === target.distDir) {
1502
- let entryAssetNode = this._graph.getNodeByContentKey(bundleGroupNode.value.entryAssetId);
1503
- (0, _assert().default)((entryAssetNode === null || entryAssetNode === void 0 ? void 0 : entryAssetNode.type) === 'asset');
1504
- entries.push((0, _projectPath.fromProjectPath)(projectRoot, entryAssetNode.value.filePath));
1505
- }
1506
- }
1507
- let root = (0, _utils().getRootDir)(entries);
1508
- this._targetEntryRoots.set(target.distDir, root);
1509
- return root;
1510
- }
1511
- getReferencedConditionalBundles(bundle) {
1512
- let referencedBundles = new Set();
1513
- this._graph.dfs({
1514
- visit: nodeId => {
1515
- let node = (0, _nullthrows().default)(this._graph.getNode(nodeId));
1516
- if (node.type !== 'bundle' || node.value.id === bundle.id) {
1517
- return;
1518
- }
1519
- referencedBundles.add(node.value);
1520
- },
1521
- startNodeId: this._graph.getNodeIdByContentKey(bundle.id),
1522
- getChildren: nodeId => this._graph.getNodeIdsConnectedFrom(nodeId, bundleGraphEdgeTypes.conditional)
1523
- });
1524
- return [...referencedBundles];
1525
- }
1526
- }
1527
- exports.default = BundleGraph;