@atlaspack/core 2.12.1-canary.3354

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 (215) hide show
  1. package/LICENSE +201 -0
  2. package/index.d.ts +22 -0
  3. package/lib/AssetGraph.js +518 -0
  4. package/lib/Atlaspack.js +587 -0
  5. package/lib/AtlaspackConfig.js +298 -0
  6. package/lib/AtlaspackConfig.schema.js +125 -0
  7. package/lib/BundleGraph.js +1445 -0
  8. package/lib/CommittedAsset.js +133 -0
  9. package/lib/Dependency.js +88 -0
  10. package/lib/Environment.js +128 -0
  11. package/lib/InternalConfig.js +48 -0
  12. package/lib/PackagerRunner.js +519 -0
  13. package/lib/ReporterRunner.js +151 -0
  14. package/lib/RequestTracker.js +1089 -0
  15. package/lib/SymbolPropagation.js +625 -0
  16. package/lib/TargetDescriptor.schema.js +115 -0
  17. package/lib/Transformation.js +536 -0
  18. package/lib/UncommittedAsset.js +342 -0
  19. package/lib/Validation.js +215 -0
  20. package/lib/applyRuntimes.js +279 -0
  21. package/lib/assetUtils.js +189 -0
  22. package/lib/atlaspack-v3/AtlaspackV3.js +74 -0
  23. package/lib/atlaspack-v3/fs.js +30 -0
  24. package/lib/atlaspack-v3/index.js +19 -0
  25. package/lib/atlaspack-v3/jsCallable.js +15 -0
  26. package/lib/atlaspack-v3/plugins/Resolver.js +12 -0
  27. package/lib/atlaspack-v3/plugins/index.js +16 -0
  28. package/lib/atlaspack-v3/worker/index.js +3 -0
  29. package/lib/atlaspack-v3/worker/worker.js +30 -0
  30. package/lib/buildCache.js +18 -0
  31. package/lib/constants.js +21 -0
  32. package/lib/dumpGraphToGraphViz.js +206 -0
  33. package/lib/index.js +106 -0
  34. package/lib/loadAtlaspackPlugin.js +148 -0
  35. package/lib/loadDotEnv.js +54 -0
  36. package/lib/projectPath.js +86 -0
  37. package/lib/public/Asset.js +259 -0
  38. package/lib/public/Bundle.js +231 -0
  39. package/lib/public/BundleGraph.js +193 -0
  40. package/lib/public/BundleGroup.js +44 -0
  41. package/lib/public/Config.js +202 -0
  42. package/lib/public/Dependency.js +131 -0
  43. package/lib/public/Environment.js +244 -0
  44. package/lib/public/MutableBundleGraph.js +184 -0
  45. package/lib/public/PluginOptions.js +71 -0
  46. package/lib/public/Symbols.js +247 -0
  47. package/lib/public/Target.js +64 -0
  48. package/lib/registerCoreWithSerializer.js +45 -0
  49. package/lib/requests/AssetGraphRequest.js +427 -0
  50. package/lib/requests/AssetGraphRequestRust.js +187 -0
  51. package/lib/requests/AssetRequest.js +137 -0
  52. package/lib/requests/AtlaspackBuildRequest.js +78 -0
  53. package/lib/requests/AtlaspackConfigRequest.js +473 -0
  54. package/lib/requests/BundleGraphRequest.js +419 -0
  55. package/lib/requests/ConfigRequest.js +198 -0
  56. package/lib/requests/DevDepRequest.js +166 -0
  57. package/lib/requests/EntryRequest.js +295 -0
  58. package/lib/requests/PackageRequest.js +88 -0
  59. package/lib/requests/PathRequest.js +349 -0
  60. package/lib/requests/TargetRequest.js +1177 -0
  61. package/lib/requests/ValidationRequest.js +66 -0
  62. package/lib/requests/WriteBundleRequest.js +252 -0
  63. package/lib/requests/WriteBundlesRequest.js +153 -0
  64. package/lib/resolveOptions.js +234 -0
  65. package/lib/serializer.js +216 -0
  66. package/lib/serializerCore.browser.js +29 -0
  67. package/lib/serializerCore.js +16 -0
  68. package/lib/summarizeRequest.js +55 -0
  69. package/lib/types.js +35 -0
  70. package/lib/utils.js +160 -0
  71. package/lib/worker.js +170 -0
  72. package/package.json +59 -0
  73. package/src/AssetGraph.js +646 -0
  74. package/src/Atlaspack.js +632 -0
  75. package/src/AtlaspackConfig.js +490 -0
  76. package/src/AtlaspackConfig.schema.js +141 -0
  77. package/src/BundleGraph.js +2193 -0
  78. package/src/CommittedAsset.js +143 -0
  79. package/src/Dependency.js +142 -0
  80. package/src/Environment.js +151 -0
  81. package/src/InternalConfig.js +72 -0
  82. package/src/PackagerRunner.js +790 -0
  83. package/src/ReporterRunner.js +158 -0
  84. package/src/RequestTracker.js +1697 -0
  85. package/src/SymbolPropagation.js +794 -0
  86. package/src/TargetDescriptor.schema.js +136 -0
  87. package/src/Transformation.js +752 -0
  88. package/src/UncommittedAsset.js +426 -0
  89. package/src/Validation.js +223 -0
  90. package/src/applyRuntimes.js +341 -0
  91. package/src/assetUtils.js +254 -0
  92. package/src/atlaspack-v3/AtlaspackV3.js +73 -0
  93. package/src/atlaspack-v3/fs.js +36 -0
  94. package/src/atlaspack-v3/index.js +5 -0
  95. package/src/atlaspack-v3/jsCallable.js +13 -0
  96. package/src/atlaspack-v3/plugins/Resolver.js +9 -0
  97. package/src/atlaspack-v3/plugins/index.js +3 -0
  98. package/src/atlaspack-v3/worker/index.js +8 -0
  99. package/src/atlaspack-v3/worker/worker.js +14 -0
  100. package/src/buildCache.js +15 -0
  101. package/src/constants.js +22 -0
  102. package/src/dumpGraphToGraphViz.js +244 -0
  103. package/src/index.js +22 -0
  104. package/src/loadAtlaspackPlugin.js +239 -0
  105. package/src/loadDotEnv.js +56 -0
  106. package/src/projectPath.js +87 -0
  107. package/src/public/Asset.js +359 -0
  108. package/src/public/Bundle.js +337 -0
  109. package/src/public/BundleGraph.js +335 -0
  110. package/src/public/BundleGroup.js +55 -0
  111. package/src/public/Config.js +261 -0
  112. package/src/public/Dependency.js +176 -0
  113. package/src/public/Environment.js +316 -0
  114. package/src/public/MutableBundleGraph.js +320 -0
  115. package/src/public/PluginOptions.js +99 -0
  116. package/src/public/Symbols.js +315 -0
  117. package/src/public/Target.js +71 -0
  118. package/src/registerCoreWithSerializer.js +34 -0
  119. package/src/requests/AssetGraphRequest.js +577 -0
  120. package/src/requests/AssetGraphRequestRust.js +222 -0
  121. package/src/requests/AssetRequest.js +179 -0
  122. package/src/requests/AtlaspackBuildRequest.js +109 -0
  123. package/src/requests/AtlaspackConfigRequest.js +709 -0
  124. package/src/requests/BundleGraphRequest.js +547 -0
  125. package/src/requests/ConfigRequest.js +287 -0
  126. package/src/requests/DevDepRequest.js +246 -0
  127. package/src/requests/EntryRequest.js +385 -0
  128. package/src/requests/PackageRequest.js +105 -0
  129. package/src/requests/PathRequest.js +454 -0
  130. package/src/requests/TargetRequest.js +1632 -0
  131. package/src/requests/ValidationRequest.js +79 -0
  132. package/src/requests/WriteBundleRequest.js +362 -0
  133. package/src/requests/WriteBundlesRequest.js +209 -0
  134. package/src/resolveOptions.js +278 -0
  135. package/src/serializer.js +255 -0
  136. package/src/serializerCore.browser.js +8 -0
  137. package/src/serializerCore.js +5 -0
  138. package/src/summarizeRequest.js +47 -0
  139. package/src/types.js +581 -0
  140. package/src/utils.js +211 -0
  141. package/src/worker.js +189 -0
  142. package/test/AssetGraph.test.js +679 -0
  143. package/test/Atlaspack.test.js +142 -0
  144. package/test/AtlaspackConfig.test.js +405 -0
  145. package/test/AtlaspackConfigRequest.test.js +993 -0
  146. package/test/BundleGraph.test.js +121 -0
  147. package/test/EntryRequest.test.js +215 -0
  148. package/test/Environment.test.js +99 -0
  149. package/test/InternalAsset.test.js +76 -0
  150. package/test/PackagerRunner.test.js +27 -0
  151. package/test/PublicAsset.test.js +40 -0
  152. package/test/PublicBundle.test.js +68 -0
  153. package/test/PublicDependency.test.js +22 -0
  154. package/test/PublicEnvironment.test.js +27 -0
  155. package/test/PublicMutableBundleGraph.test.js +192 -0
  156. package/test/RequestTracker.test.js +448 -0
  157. package/test/SymbolPropagation.test.js +716 -0
  158. package/test/TargetRequest.test.js +1715 -0
  159. package/test/fixtures/application-targets/package.json +3 -0
  160. package/test/fixtures/atlaspack/index.js +1 -0
  161. package/test/fixtures/atlaspack/other.js +3 -0
  162. package/test/fixtures/atlaspack/package.json +1 -0
  163. package/test/fixtures/atlaspack/yarn.lock +0 -0
  164. package/test/fixtures/bundle.js +10 -0
  165. package/test/fixtures/common-targets/package.json +24 -0
  166. package/test/fixtures/common-targets-ignore/package.json +13 -0
  167. package/test/fixtures/config/.atlaspackrc +6 -0
  168. package/test/fixtures/config/subfolder/.atlaspackrc +6 -0
  169. package/test/fixtures/config-extends-not-found/.atlaspackrc +3 -0
  170. package/test/fixtures/config-extends-not-found/.atlaspackrc-json5 +3 -0
  171. package/test/fixtures/config-extends-not-found/.atlaspackrc-multiple +3 -0
  172. package/test/fixtures/config-extends-not-found/.atlaspackrc-node-modules +3 -0
  173. package/test/fixtures/config-malformed/.atlaspackrc +3 -0
  174. package/test/fixtures/config-node-pipeline/.atlaspackrc +6 -0
  175. package/test/fixtures/config-plugin-not-found/.atlaspackrc +6 -0
  176. package/test/fixtures/context/package.json +8 -0
  177. package/test/fixtures/custom-format-infer-ext/package.json +6 -0
  178. package/test/fixtures/custom-format-infer-type/package.json +7 -0
  179. package/test/fixtures/custom-format-mismatch/package.json +8 -0
  180. package/test/fixtures/custom-targets/package.json +20 -0
  181. package/test/fixtures/custom-targets-distdir/package.json +11 -0
  182. package/test/fixtures/duplicate-targets/package.json +4 -0
  183. package/test/fixtures/glob-like/[entry].js +0 -0
  184. package/test/fixtures/invalid-distpath/package.json +11 -0
  185. package/test/fixtures/invalid-engines/package.json +12 -0
  186. package/test/fixtures/invalid-source-missing/package.json +5 -0
  187. package/test/fixtures/invalid-source-not-file/package.json +5 -0
  188. package/test/fixtures/invalid-source-not-file/src/index.js +1 -0
  189. package/test/fixtures/invalid-target-source-missing/package.json +9 -0
  190. package/test/fixtures/invalid-target-source-not-file/package.json +9 -0
  191. package/test/fixtures/invalid-target-source-not-file/src/index.js +1 -0
  192. package/test/fixtures/invalid-targets/package.json +19 -0
  193. package/test/fixtures/library-custom-scopehoist/package.json +9 -0
  194. package/test/fixtures/library-scopehoist/package.json +8 -0
  195. package/test/fixtures/local-plugin-config-pkg/.atlaspackrc +3 -0
  196. package/test/fixtures/local-plugin-config-pkg/node_modules/atlaspack-config-local/index.json +8 -0
  197. package/test/fixtures/local-plugin-config-pkg/node_modules/atlaspack-config-local/local-plugin.js +7 -0
  198. package/test/fixtures/local-plugin-config-pkg/node_modules/atlaspack-config-local/package.json +7 -0
  199. package/test/fixtures/main-format-mismatch/package.json +8 -0
  200. package/test/fixtures/main-global/package.json +8 -0
  201. package/test/fixtures/main-mjs/package.json +8 -0
  202. package/test/fixtures/module-a.js +1 -0
  203. package/test/fixtures/module-b.js +1 -0
  204. package/test/fixtures/plugins/local-plugin.js +7 -0
  205. package/test/fixtures/plugins/node_modules/atlaspack-transformer-bad-engines/index.js +7 -0
  206. package/test/fixtures/plugins/node_modules/atlaspack-transformer-bad-engines/package.json +7 -0
  207. package/test/fixtures/plugins/node_modules/atlaspack-transformer-no-engines/index.js +7 -0
  208. package/test/fixtures/plugins/node_modules/atlaspack-transformer-no-engines/package.json +4 -0
  209. package/test/fixtures/targets-default-distdir-none/package.json +5 -0
  210. package/test/fixtures/targets-default-distdir-one/package.json +8 -0
  211. package/test/fixtures/targets-default-distdir-two/package.json +18 -0
  212. package/test/requests/ConfigRequest.test.js +254 -0
  213. package/test/serializer.test.js +302 -0
  214. package/test/test-utils.js +80 -0
  215. package/test/utils.test.js +43 -0
@@ -0,0 +1,337 @@
1
+ // @flow strict-local
2
+
3
+ import type {
4
+ Bundle as InternalBundle,
5
+ AtlaspackOptions,
6
+ PackagedBundleInfo,
7
+ } from '../types';
8
+ import type {
9
+ Asset as IAsset,
10
+ Bundle as IBundle,
11
+ BundleTraversable,
12
+ Dependency as IDependency,
13
+ Environment as IEnvironment,
14
+ GraphVisitor,
15
+ NamedBundle as INamedBundle,
16
+ PackagedBundle as IPackagedBundle,
17
+ Stats,
18
+ Target as ITarget,
19
+ BundleBehavior,
20
+ } from '@atlaspack/types';
21
+ import type BundleGraph from '../BundleGraph';
22
+
23
+ import invariant from 'assert';
24
+ import nullthrows from 'nullthrows';
25
+ import {DefaultWeakMap} from '@atlaspack/utils';
26
+
27
+ import {assetToAssetValue, assetFromValue} from './Asset';
28
+ import {mapVisitor} from '@atlaspack/graph';
29
+ import Environment from './Environment';
30
+ import {
31
+ dependencyToInternalDependency,
32
+ getPublicDependency,
33
+ } from './Dependency';
34
+ import Target from './Target';
35
+ import {BundleBehaviorNames} from '../types';
36
+ import {fromProjectPath} from '../projectPath';
37
+
38
+ const internalBundleToBundle: DefaultWeakMap<
39
+ AtlaspackOptions,
40
+ DefaultWeakMap<BundleGraph, WeakMap<InternalBundle, Bundle>>,
41
+ > = new DefaultWeakMap(() => new DefaultWeakMap(() => new WeakMap()));
42
+ const internalBundleToNamedBundle: DefaultWeakMap<
43
+ AtlaspackOptions,
44
+ DefaultWeakMap<BundleGraph, WeakMap<InternalBundle, NamedBundle>>,
45
+ > = new DefaultWeakMap(() => new DefaultWeakMap(() => new WeakMap()));
46
+ const internalBundleToPackagedBundle: DefaultWeakMap<
47
+ AtlaspackOptions,
48
+ DefaultWeakMap<BundleGraph, WeakMap<InternalBundle, PackagedBundle>>,
49
+ > = new DefaultWeakMap(() => new DefaultWeakMap(() => new WeakMap()));
50
+
51
+ // Friendly access for other modules within this package that need access
52
+ // to the internal bundle.
53
+ const _bundleToInternalBundle: WeakMap<IBundle, InternalBundle> = new WeakMap();
54
+ export function bundleToInternalBundle(bundle: IBundle): InternalBundle {
55
+ return nullthrows(_bundleToInternalBundle.get(bundle));
56
+ }
57
+ const _bundleToInternalBundleGraph: WeakMap<IBundle, BundleGraph> =
58
+ new WeakMap();
59
+ export function bundleToInternalBundleGraph(bundle: IBundle): BundleGraph {
60
+ return nullthrows(_bundleToInternalBundleGraph.get(bundle));
61
+ }
62
+
63
+ // Require this private object to be present when invoking these constructors,
64
+ // preventing others from using them. They should use the static `get` method.
65
+ let _private = {};
66
+
67
+ export class Bundle implements IBundle {
68
+ #bundle /*: InternalBundle */;
69
+ #bundleGraph /*: BundleGraph */;
70
+ #options /*: AtlaspackOptions */;
71
+
72
+ constructor(
73
+ sentinel: mixed,
74
+ bundle: InternalBundle,
75
+ bundleGraph: BundleGraph,
76
+ options: AtlaspackOptions,
77
+ ) {
78
+ if (sentinel !== _private) {
79
+ throw new Error('Unexpected public usage');
80
+ }
81
+
82
+ this.#bundle = bundle;
83
+ this.#bundleGraph = bundleGraph;
84
+ this.#options = options;
85
+ }
86
+
87
+ static get(
88
+ internalBundle: InternalBundle,
89
+ bundleGraph: BundleGraph,
90
+ options: AtlaspackOptions,
91
+ ): Bundle {
92
+ let existingMap = internalBundleToBundle.get(options).get(bundleGraph);
93
+ let existing = existingMap.get(internalBundle);
94
+ if (existing != null) {
95
+ return existing;
96
+ }
97
+
98
+ let bundle = new Bundle(_private, internalBundle, bundleGraph, options);
99
+ _bundleToInternalBundle.set(bundle, internalBundle);
100
+ _bundleToInternalBundleGraph.set(bundle, bundleGraph);
101
+ existingMap.set(internalBundle, bundle);
102
+
103
+ return bundle;
104
+ }
105
+
106
+ get id(): string {
107
+ return this.#bundle.id;
108
+ }
109
+
110
+ get hashReference(): string {
111
+ return this.#bundle.hashReference;
112
+ }
113
+
114
+ get type(): string {
115
+ return this.#bundle.type;
116
+ }
117
+
118
+ get env(): IEnvironment {
119
+ return new Environment(this.#bundle.env, this.#options);
120
+ }
121
+
122
+ get needsStableName(): ?boolean {
123
+ return this.#bundle.needsStableName;
124
+ }
125
+
126
+ get bundleBehavior(): ?BundleBehavior {
127
+ let bundleBehavior = this.#bundle.bundleBehavior;
128
+ return bundleBehavior != null ? BundleBehaviorNames[bundleBehavior] : null;
129
+ }
130
+
131
+ get isSplittable(): ?boolean {
132
+ return this.#bundle.isSplittable;
133
+ }
134
+
135
+ get manualSharedBundle(): ?string {
136
+ return this.#bundle.manualSharedBundle;
137
+ }
138
+
139
+ get target(): ITarget {
140
+ return new Target(this.#bundle.target, this.#options);
141
+ }
142
+
143
+ hasAsset(asset: IAsset): boolean {
144
+ return this.#bundleGraph.bundleHasAsset(
145
+ this.#bundle,
146
+ assetToAssetValue(asset),
147
+ );
148
+ }
149
+
150
+ hasDependency(dep: IDependency): boolean {
151
+ return this.#bundleGraph.bundleHasDependency(
152
+ this.#bundle,
153
+ dependencyToInternalDependency(dep),
154
+ );
155
+ }
156
+
157
+ getEntryAssets(): Array<IAsset> {
158
+ return this.#bundle.entryAssetIds.map(id => {
159
+ let assetNode = this.#bundleGraph._graph.getNodeByContentKey(id);
160
+ invariant(assetNode != null && assetNode.type === 'asset');
161
+ return assetFromValue(assetNode.value, this.#options);
162
+ });
163
+ }
164
+
165
+ getMainEntry(): ?IAsset {
166
+ if (this.#bundle.mainEntryId != null) {
167
+ let assetNode = this.#bundleGraph._graph.getNodeByContentKey(
168
+ this.#bundle.mainEntryId,
169
+ );
170
+ invariant(assetNode != null && assetNode.type === 'asset');
171
+ return assetFromValue(assetNode.value, this.#options);
172
+ }
173
+ }
174
+
175
+ traverse<TContext>(
176
+ visit: GraphVisitor<BundleTraversable, TContext>,
177
+ ): ?TContext {
178
+ return this.#bundleGraph.traverseBundle(
179
+ this.#bundle,
180
+ mapVisitor(node => {
181
+ if (node.type === 'asset') {
182
+ return {
183
+ type: 'asset',
184
+ value: assetFromValue(node.value, this.#options),
185
+ };
186
+ } else if (node.type === 'dependency') {
187
+ return {
188
+ type: 'dependency',
189
+ value: getPublicDependency(node.value, this.#options),
190
+ };
191
+ }
192
+ }, visit),
193
+ );
194
+ }
195
+
196
+ traverseAssets<TContext>(
197
+ visit: GraphVisitor<IAsset, TContext>,
198
+ startAsset?: IAsset,
199
+ ): ?TContext {
200
+ return this.#bundleGraph.traverseAssets(
201
+ this.#bundle,
202
+ mapVisitor(asset => assetFromValue(asset, this.#options), visit),
203
+ startAsset ? assetToAssetValue(startAsset) : undefined,
204
+ );
205
+ }
206
+ }
207
+
208
+ export class NamedBundle extends Bundle implements INamedBundle {
209
+ #bundle /*: InternalBundle */;
210
+ #bundleGraph /*: BundleGraph */;
211
+ #options /*: AtlaspackOptions */;
212
+
213
+ constructor(
214
+ sentinel: mixed,
215
+ bundle: InternalBundle,
216
+ bundleGraph: BundleGraph,
217
+ options: AtlaspackOptions,
218
+ ) {
219
+ super(sentinel, bundle, bundleGraph, options);
220
+ this.#bundle = bundle; // Repeating for flow
221
+ this.#bundleGraph = bundleGraph; // Repeating for flow
222
+ this.#options = options;
223
+ }
224
+
225
+ static get(
226
+ internalBundle: InternalBundle,
227
+ bundleGraph: BundleGraph,
228
+ options: AtlaspackOptions,
229
+ ): NamedBundle {
230
+ let existingMap = internalBundleToNamedBundle.get(options).get(bundleGraph);
231
+ let existing = existingMap.get(internalBundle);
232
+ if (existing != null) {
233
+ return existing;
234
+ }
235
+
236
+ let namedBundle = new NamedBundle(
237
+ _private,
238
+ internalBundle,
239
+ bundleGraph,
240
+ options,
241
+ );
242
+ _bundleToInternalBundle.set(namedBundle, internalBundle);
243
+ _bundleToInternalBundleGraph.set(namedBundle, bundleGraph);
244
+ existingMap.set(internalBundle, namedBundle);
245
+
246
+ return namedBundle;
247
+ }
248
+
249
+ get name(): string {
250
+ return nullthrows(this.#bundle.name);
251
+ }
252
+
253
+ get displayName(): string {
254
+ return nullthrows(this.#bundle.displayName);
255
+ }
256
+
257
+ get publicId(): string {
258
+ return nullthrows(this.#bundle.publicId);
259
+ }
260
+ }
261
+
262
+ export class PackagedBundle extends NamedBundle implements IPackagedBundle {
263
+ #bundle /*: InternalBundle */;
264
+ #bundleGraph /*: BundleGraph */;
265
+ #options /*: AtlaspackOptions */;
266
+ #bundleInfo /*: ?PackagedBundleInfo */;
267
+
268
+ constructor(
269
+ sentinel: mixed,
270
+ bundle: InternalBundle,
271
+ bundleGraph: BundleGraph,
272
+ options: AtlaspackOptions,
273
+ ) {
274
+ super(sentinel, bundle, bundleGraph, options);
275
+ this.#bundle = bundle; // Repeating for flow
276
+ this.#bundleGraph = bundleGraph; // Repeating for flow
277
+ this.#options = options; // Repeating for flow
278
+ }
279
+
280
+ static get(
281
+ internalBundle: InternalBundle,
282
+ bundleGraph: BundleGraph,
283
+ options: AtlaspackOptions,
284
+ ): PackagedBundle {
285
+ let existingMap = internalBundleToPackagedBundle
286
+ .get(options)
287
+ .get(bundleGraph);
288
+ let existing = existingMap.get(internalBundle);
289
+ if (existing != null) {
290
+ return existing;
291
+ }
292
+
293
+ let packagedBundle = new PackagedBundle(
294
+ _private,
295
+ internalBundle,
296
+ bundleGraph,
297
+ options,
298
+ );
299
+ _bundleToInternalBundle.set(packagedBundle, internalBundle);
300
+ _bundleToInternalBundleGraph.set(packagedBundle, bundleGraph);
301
+ existingMap.set(internalBundle, packagedBundle);
302
+
303
+ return packagedBundle;
304
+ }
305
+
306
+ static getWithInfo(
307
+ internalBundle: InternalBundle,
308
+ bundleGraph: BundleGraph,
309
+ options: AtlaspackOptions,
310
+ bundleInfo: ?PackagedBundleInfo,
311
+ ): PackagedBundle {
312
+ let packagedBundle = PackagedBundle.get(
313
+ internalBundle,
314
+ bundleGraph,
315
+ options,
316
+ );
317
+ packagedBundle.#bundleInfo = bundleInfo;
318
+ return packagedBundle;
319
+ }
320
+
321
+ get filePath(): string {
322
+ return fromProjectPath(
323
+ this.#options.projectRoot,
324
+ nullthrows(this.#bundleInfo).filePath,
325
+ );
326
+ }
327
+
328
+ get type(): string {
329
+ // The bundle type may be overridden in the packager.
330
+ // However, inline bundles will not have a bundleInfo here since they are not written to the filesystem.
331
+ return this.#bundleInfo ? this.#bundleInfo.type : this.#bundle.type;
332
+ }
333
+
334
+ get stats(): Stats {
335
+ return nullthrows(this.#bundleInfo).stats;
336
+ }
337
+ }
@@ -0,0 +1,335 @@
1
+ // @flow strict-local
2
+
3
+ import type {
4
+ Asset as IAsset,
5
+ Bundle as IBundle,
6
+ BundleGraph as IBundleGraph,
7
+ BundleGraphTraversable,
8
+ BundleGroup as IBundleGroup,
9
+ Dependency as IDependency,
10
+ ExportSymbolResolution,
11
+ FilePath,
12
+ GraphVisitor,
13
+ Symbol,
14
+ SymbolResolution,
15
+ Target,
16
+ } from '@atlaspack/types';
17
+ import type {Bundle as InternalBundle, AtlaspackOptions} from '../types';
18
+ import type InternalBundleGraph from '../BundleGraph';
19
+
20
+ import invariant from 'assert';
21
+ import nullthrows from 'nullthrows';
22
+
23
+ import {mapVisitor} from '@atlaspack/graph';
24
+ import {assetFromValue, assetToAssetValue, Asset} from './Asset';
25
+ import {bundleToInternalBundle} from './Bundle';
26
+ import Dependency, {
27
+ dependencyToInternalDependency,
28
+ getPublicDependency,
29
+ } from './Dependency';
30
+ import {targetToInternalTarget} from './Target';
31
+ import {fromInternalSourceLocation} from '../utils';
32
+ import BundleGroup, {bundleGroupToInternalBundleGroup} from './BundleGroup';
33
+
34
+ // Friendly access for other modules within this package that need access
35
+ // to the internal bundle.
36
+ const _bundleGraphToInternalBundleGraph: WeakMap<
37
+ IBundleGraph<IBundle>,
38
+ InternalBundleGraph,
39
+ > = new WeakMap();
40
+ export function bundleGraphToInternalBundleGraph(
41
+ bundleGraph: IBundleGraph<IBundle>,
42
+ ): InternalBundleGraph {
43
+ return nullthrows(_bundleGraphToInternalBundleGraph.get(bundleGraph));
44
+ }
45
+
46
+ type BundleFactory<TBundle: IBundle> = (
47
+ InternalBundle,
48
+ InternalBundleGraph,
49
+ AtlaspackOptions,
50
+ ) => TBundle;
51
+
52
+ export default class BundleGraph<TBundle: IBundle>
53
+ implements IBundleGraph<TBundle>
54
+ {
55
+ #graph: InternalBundleGraph;
56
+ #options: AtlaspackOptions;
57
+ #createBundle: BundleFactory<TBundle>;
58
+
59
+ constructor(
60
+ graph: InternalBundleGraph,
61
+ createBundle: BundleFactory<TBundle>,
62
+ options: AtlaspackOptions,
63
+ ) {
64
+ this.#graph = graph;
65
+ this.#options = options;
66
+ this.#createBundle = createBundle;
67
+ // $FlowFixMe
68
+ _bundleGraphToInternalBundleGraph.set(this, graph);
69
+ }
70
+
71
+ getAssetById(id: string): Asset {
72
+ return assetFromValue(this.#graph.getAssetById(id), this.#options);
73
+ }
74
+
75
+ getAssetPublicId(asset: IAsset): string {
76
+ return this.#graph.getAssetPublicId(assetToAssetValue(asset));
77
+ }
78
+
79
+ isDependencySkipped(dep: IDependency): boolean {
80
+ return this.#graph.isDependencySkipped(dependencyToInternalDependency(dep));
81
+ }
82
+
83
+ getResolvedAsset(dep: IDependency, bundle: ?IBundle): ?IAsset {
84
+ let resolution = this.#graph.getResolvedAsset(
85
+ dependencyToInternalDependency(dep),
86
+ bundle && bundleToInternalBundle(bundle),
87
+ );
88
+ if (resolution) {
89
+ return assetFromValue(resolution, this.#options);
90
+ }
91
+ }
92
+
93
+ getIncomingDependencies(asset: IAsset): Array<IDependency> {
94
+ return this.#graph
95
+ .getIncomingDependencies(assetToAssetValue(asset))
96
+ .map(dep => getPublicDependency(dep, this.#options));
97
+ }
98
+
99
+ getAssetWithDependency(dep: IDependency): ?IAsset {
100
+ let asset = this.#graph.getAssetWithDependency(
101
+ dependencyToInternalDependency(dep),
102
+ );
103
+ if (asset) {
104
+ return assetFromValue(asset, this.#options);
105
+ }
106
+ }
107
+
108
+ getBundleGroupsContainingBundle(bundle: IBundle): Array<IBundleGroup> {
109
+ return this.#graph
110
+ .getBundleGroupsContainingBundle(bundleToInternalBundle(bundle))
111
+ .map(bundleGroup => new BundleGroup(bundleGroup, this.#options));
112
+ }
113
+
114
+ getReferencedBundles(
115
+ bundle: IBundle,
116
+ opts?: {|recursive?: boolean, includeInline?: boolean|},
117
+ ): Array<TBundle> {
118
+ return this.#graph
119
+ .getReferencedBundles(bundleToInternalBundle(bundle), opts)
120
+ .map(bundle => this.#createBundle(bundle, this.#graph, this.#options));
121
+ }
122
+
123
+ getReferencingBundles(bundle: IBundle): Array<TBundle> {
124
+ return this.#graph
125
+ .getReferencingBundles(bundleToInternalBundle(bundle))
126
+ .map(bundle => this.#createBundle(bundle, this.#graph, this.#options));
127
+ }
128
+
129
+ resolveAsyncDependency(
130
+ dependency: IDependency,
131
+ bundle: ?IBundle,
132
+ ): ?(
133
+ | {|type: 'bundle_group', value: IBundleGroup|}
134
+ | {|type: 'asset', value: IAsset|}
135
+ ) {
136
+ let resolved = this.#graph.resolveAsyncDependency(
137
+ dependencyToInternalDependency(dependency),
138
+ bundle && bundleToInternalBundle(bundle),
139
+ );
140
+
141
+ if (resolved == null) {
142
+ return;
143
+ } else if (resolved.type === 'bundle_group') {
144
+ return {
145
+ type: 'bundle_group',
146
+ value: new BundleGroup(resolved.value, this.#options),
147
+ };
148
+ }
149
+
150
+ return {
151
+ type: 'asset',
152
+ value: assetFromValue(resolved.value, this.#options),
153
+ };
154
+ }
155
+
156
+ getReferencedBundle(dependency: IDependency, bundle: IBundle): ?TBundle {
157
+ let result = this.#graph.getReferencedBundle(
158
+ dependencyToInternalDependency(dependency),
159
+ bundleToInternalBundle(bundle),
160
+ );
161
+
162
+ if (result != null) {
163
+ return this.#createBundle(result, this.#graph, this.#options);
164
+ }
165
+ }
166
+
167
+ getDependencies(asset: IAsset): Array<IDependency> {
168
+ return this.#graph
169
+ .getDependencies(assetToAssetValue(asset))
170
+ .map(dep => getPublicDependency(dep, this.#options));
171
+ }
172
+
173
+ isAssetReachableFromBundle(asset: IAsset, bundle: IBundle): boolean {
174
+ return this.#graph.isAssetReachableFromBundle(
175
+ assetToAssetValue(asset),
176
+ bundleToInternalBundle(bundle),
177
+ );
178
+ }
179
+
180
+ isAssetReferenced(bundle: IBundle, asset: IAsset): boolean {
181
+ return this.#graph.isAssetReferenced(
182
+ bundleToInternalBundle(bundle),
183
+ assetToAssetValue(asset),
184
+ );
185
+ }
186
+
187
+ hasParentBundleOfType(bundle: IBundle, type: string): boolean {
188
+ return this.#graph.hasParentBundleOfType(
189
+ bundleToInternalBundle(bundle),
190
+ type,
191
+ );
192
+ }
193
+
194
+ getBundlesInBundleGroup(
195
+ bundleGroup: IBundleGroup,
196
+ opts?: {|includeInline: boolean|},
197
+ ): Array<TBundle> {
198
+ return this.#graph
199
+ .getBundlesInBundleGroup(
200
+ bundleGroupToInternalBundleGroup(bundleGroup),
201
+ opts,
202
+ )
203
+ .map(bundle => this.#createBundle(bundle, this.#graph, this.#options));
204
+ }
205
+
206
+ getBundles(opts?: {|includeInline: boolean|}): Array<TBundle> {
207
+ return this.#graph
208
+ .getBundles(opts)
209
+ .map(bundle => this.#createBundle(bundle, this.#graph, this.#options));
210
+ }
211
+
212
+ isEntryBundleGroup(bundleGroup: IBundleGroup): boolean {
213
+ return this.#graph.isEntryBundleGroup(
214
+ bundleGroupToInternalBundleGroup(bundleGroup),
215
+ );
216
+ }
217
+
218
+ getChildBundles(bundle: IBundle): Array<TBundle> {
219
+ return this.#graph
220
+ .getChildBundles(bundleToInternalBundle(bundle))
221
+ .map(bundle => this.#createBundle(bundle, this.#graph, this.#options));
222
+ }
223
+
224
+ getParentBundles(bundle: IBundle): Array<TBundle> {
225
+ return this.#graph
226
+ .getParentBundles(bundleToInternalBundle(bundle))
227
+ .map(bundle => this.#createBundle(bundle, this.#graph, this.#options));
228
+ }
229
+
230
+ getSymbolResolution(
231
+ asset: IAsset,
232
+ symbol: Symbol,
233
+ boundary: ?IBundle,
234
+ ): SymbolResolution {
235
+ let res = this.#graph.getSymbolResolution(
236
+ assetToAssetValue(asset),
237
+ symbol,
238
+ boundary ? bundleToInternalBundle(boundary) : null,
239
+ );
240
+ return {
241
+ asset: assetFromValue(res.asset, this.#options),
242
+ exportSymbol: res.exportSymbol,
243
+ symbol: res.symbol,
244
+ loc: fromInternalSourceLocation(this.#options.projectRoot, res.loc),
245
+ };
246
+ }
247
+
248
+ getExportedSymbols(
249
+ asset: IAsset,
250
+ boundary: ?IBundle,
251
+ ): Array<ExportSymbolResolution> {
252
+ let res = this.#graph.getExportedSymbols(
253
+ assetToAssetValue(asset),
254
+ boundary ? bundleToInternalBundle(boundary) : null,
255
+ );
256
+ return res.map(e => ({
257
+ asset: assetFromValue(e.asset, this.#options),
258
+ exportSymbol: e.exportSymbol,
259
+ symbol: e.symbol,
260
+ loc: fromInternalSourceLocation(this.#options.projectRoot, e.loc),
261
+ exportAs: e.exportAs,
262
+ }));
263
+ }
264
+
265
+ traverse<TContext>(
266
+ visit: GraphVisitor<BundleGraphTraversable, TContext>,
267
+ start?: ?IAsset,
268
+ opts?: ?{|skipUnusedDependencies?: boolean|},
269
+ ): ?TContext {
270
+ return this.#graph.traverse(
271
+ mapVisitor((node, actions) => {
272
+ // Skipping unused dependencies here is faster than doing an isDependencySkipped check inside the visitor
273
+ // because the node needs to be re-looked up by id from the hashmap.
274
+ if (
275
+ opts?.skipUnusedDependencies &&
276
+ node.type === 'dependency' &&
277
+ (node.hasDeferred || node.excluded)
278
+ ) {
279
+ actions.skipChildren();
280
+ return null;
281
+ }
282
+ return node.type === 'asset'
283
+ ? {type: 'asset', value: assetFromValue(node.value, this.#options)}
284
+ : {
285
+ type: 'dependency',
286
+ value: getPublicDependency(node.value, this.#options),
287
+ };
288
+ }, visit),
289
+ start ? assetToAssetValue(start) : undefined,
290
+ );
291
+ }
292
+
293
+ traverseBundles<TContext>(
294
+ visit: GraphVisitor<TBundle, TContext>,
295
+ startBundle: ?IBundle,
296
+ ): ?TContext {
297
+ return this.#graph.traverseBundles(
298
+ mapVisitor(
299
+ bundle => this.#createBundle(bundle, this.#graph, this.#options),
300
+ visit,
301
+ ),
302
+ startBundle == null ? undefined : bundleToInternalBundle(startBundle),
303
+ );
304
+ }
305
+
306
+ getBundlesWithAsset(asset: IAsset): Array<TBundle> {
307
+ return this.#graph
308
+ .getBundlesWithAsset(assetToAssetValue(asset))
309
+ .map(bundle => this.#createBundle(bundle, this.#graph, this.#options));
310
+ }
311
+
312
+ getBundlesWithDependency(dependency: IDependency): Array<TBundle> {
313
+ return this.#graph
314
+ .getBundlesWithDependency(dependencyToInternalDependency(dependency))
315
+ .map(bundle => this.#createBundle(bundle, this.#graph, this.#options));
316
+ }
317
+
318
+ getUsedSymbols(v: IAsset | IDependency): ?$ReadOnlySet<Symbol> {
319
+ if (v instanceof Asset) {
320
+ return this.#graph.getUsedSymbolsAsset(assetToAssetValue(v));
321
+ } else {
322
+ invariant(v instanceof Dependency);
323
+ return this.#graph.getUsedSymbolsDependency(
324
+ dependencyToInternalDependency(v),
325
+ );
326
+ }
327
+ }
328
+
329
+ getEntryRoot(target: Target): FilePath {
330
+ return this.#graph.getEntryRoot(
331
+ this.#options.projectRoot,
332
+ targetToInternalTarget(target),
333
+ );
334
+ }
335
+ }