@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,316 @@
1
+ // @flow strict-local
2
+ import type {
3
+ Environment as IEnvironment,
4
+ EnvironmentContext,
5
+ EnvironmentFeature,
6
+ Engines,
7
+ OutputFormat,
8
+ PackageName,
9
+ VersionMap,
10
+ SourceLocation,
11
+ SourceType,
12
+ TargetSourceMapOptions,
13
+ } from '@atlaspack/types';
14
+ import type {
15
+ Environment as InternalEnvironment,
16
+ AtlaspackOptions,
17
+ } from '../types';
18
+ import nullthrows from 'nullthrows';
19
+ import browserslist from 'browserslist';
20
+ import semver from 'semver';
21
+ import {fromInternalSourceLocation} from '../utils';
22
+
23
+ const inspect = Symbol.for('nodejs.util.inspect.custom');
24
+
25
+ export const BROWSER_ENVS: Set<string> = new Set<string>([
26
+ 'browser',
27
+ 'web-worker',
28
+ 'service-worker',
29
+ 'worklet',
30
+ 'electron-renderer',
31
+ ]);
32
+ const ELECTRON_ENVS = new Set(['electron-main', 'electron-renderer']);
33
+ const NODE_ENVS = new Set(['node', ...ELECTRON_ENVS]);
34
+ const WORKER_ENVS = new Set(['web-worker', 'service-worker']);
35
+ export const ISOLATED_ENVS: Set<string> = new Set([...WORKER_ENVS, 'worklet']);
36
+
37
+ const ALL_BROWSERS = [
38
+ 'chrome',
39
+ 'and_chr',
40
+ 'edge',
41
+ 'firefox',
42
+ 'and_ff',
43
+ 'safari',
44
+ 'ios',
45
+ 'samsung',
46
+ 'opera',
47
+ 'ie',
48
+ 'op_mini',
49
+ 'blackberry',
50
+ 'op_mob',
51
+ 'ie_mob',
52
+ 'and_uc',
53
+ 'and_qq',
54
+ 'baidu',
55
+ 'kaios',
56
+ ];
57
+
58
+ // See require("caniuse-api").getSupport(<feature name>)
59
+ const supportData = {
60
+ esmodules: {
61
+ edge: '16',
62
+ firefox: '60',
63
+ chrome: '61',
64
+ safari: '10.1',
65
+ opera: '48',
66
+ ios: '10.3',
67
+ android: '76',
68
+ and_chr: '76',
69
+ and_ff: '68',
70
+ samsung: '8.2',
71
+ and_qq: '10.4',
72
+ op_mob: '64',
73
+ },
74
+ 'dynamic-import': {
75
+ edge: '76',
76
+ firefox: '67',
77
+ chrome: '63',
78
+ safari: '11.1',
79
+ opera: '50',
80
+ ios: '11.3',
81
+ android: '63',
82
+ and_chr: '63',
83
+ and_ff: '67',
84
+ samsung: '8',
85
+ and_qq: '10.4',
86
+ op_mob: '64',
87
+ },
88
+ 'worker-module': {
89
+ edge: '80',
90
+ chrome: '80',
91
+ opera: '67',
92
+ android: '81',
93
+ and_chr: '86',
94
+ },
95
+ 'service-worker-module': {
96
+ // TODO: Safari 14.1??
97
+ },
98
+ 'import-meta-url': {
99
+ edge: '79',
100
+ firefox: '62',
101
+ chrome: '64',
102
+ safari: '11.1',
103
+ opera: '51',
104
+ ios: '12',
105
+ android: '64',
106
+ and_chr: '64',
107
+ and_ff: '62',
108
+ samsung: '9.2',
109
+ and_qq: '10.4',
110
+ op_mob: '64',
111
+ },
112
+ 'arrow-functions': {
113
+ chrome: '47',
114
+ opera: '34',
115
+ edge: '13',
116
+ firefox: '45',
117
+ safari: '10',
118
+ node: '6',
119
+ ios: '10',
120
+ samsung: '5',
121
+ electron: '0.36',
122
+ android: '50',
123
+ qq: '10.4',
124
+ baidu: '7.12',
125
+ kaios: '2.5',
126
+ and_chr: '50',
127
+ and_qq: '12.12',
128
+ op_mob: '64',
129
+ },
130
+ 'global-this': {
131
+ chrome: '75',
132
+ edge: '79',
133
+ safari: '12.1',
134
+ firefox: '65',
135
+ opera: '58',
136
+ node: '12',
137
+ and_chr: '71',
138
+ ios: '12.2',
139
+ android: '71',
140
+ samsung: '10.1',
141
+ },
142
+ };
143
+
144
+ const internalEnvironmentToEnvironment: WeakMap<
145
+ InternalEnvironment,
146
+ Environment,
147
+ > = new WeakMap();
148
+ const _environmentToInternalEnvironment: WeakMap<
149
+ IEnvironment,
150
+ InternalEnvironment,
151
+ > = new WeakMap();
152
+ export function environmentToInternalEnvironment(
153
+ environment: IEnvironment,
154
+ ): InternalEnvironment {
155
+ return nullthrows(_environmentToInternalEnvironment.get(environment));
156
+ }
157
+
158
+ export default class Environment implements IEnvironment {
159
+ #environment /*: InternalEnvironment */;
160
+ #options /*: AtlaspackOptions */;
161
+
162
+ constructor(
163
+ env: InternalEnvironment,
164
+ options: AtlaspackOptions,
165
+ ): Environment {
166
+ let existing = internalEnvironmentToEnvironment.get(env);
167
+ if (existing != null) {
168
+ return existing;
169
+ }
170
+
171
+ this.#environment = env;
172
+ this.#options = options;
173
+ _environmentToInternalEnvironment.set(this, env);
174
+ internalEnvironmentToEnvironment.set(env, this);
175
+ return this;
176
+ }
177
+
178
+ get id(): string {
179
+ return this.#environment.id;
180
+ }
181
+
182
+ get context(): EnvironmentContext {
183
+ return this.#environment.context;
184
+ }
185
+
186
+ get engines(): Engines {
187
+ return this.#environment.engines;
188
+ }
189
+
190
+ get includeNodeModules():
191
+ | boolean
192
+ | Array<PackageName>
193
+ | {[PackageName]: boolean, ...} {
194
+ return this.#environment.includeNodeModules;
195
+ }
196
+
197
+ get outputFormat(): OutputFormat {
198
+ return this.#environment.outputFormat;
199
+ }
200
+
201
+ get sourceType(): SourceType {
202
+ return this.#environment.sourceType;
203
+ }
204
+
205
+ get isLibrary(): boolean {
206
+ return this.#environment.isLibrary;
207
+ }
208
+
209
+ get shouldOptimize(): boolean {
210
+ return this.#environment.shouldOptimize;
211
+ }
212
+
213
+ get shouldScopeHoist(): boolean {
214
+ return this.#environment.shouldScopeHoist;
215
+ }
216
+
217
+ get sourceMap(): ?TargetSourceMapOptions {
218
+ return this.#environment.sourceMap;
219
+ }
220
+
221
+ get loc(): ?SourceLocation {
222
+ return fromInternalSourceLocation(
223
+ this.#options.projectRoot,
224
+ this.#environment.loc,
225
+ );
226
+ }
227
+
228
+ // $FlowFixMe[unsupported-syntax]
229
+ [inspect](): string {
230
+ return `Env(${this.#environment.context})`;
231
+ }
232
+
233
+ isBrowser(): boolean {
234
+ return BROWSER_ENVS.has(this.#environment.context);
235
+ }
236
+
237
+ isNode(): boolean {
238
+ return NODE_ENVS.has(this.#environment.context);
239
+ }
240
+
241
+ isElectron(): boolean {
242
+ return ELECTRON_ENVS.has(this.#environment.context);
243
+ }
244
+
245
+ isIsolated(): boolean {
246
+ return ISOLATED_ENVS.has(this.#environment.context);
247
+ }
248
+
249
+ isWorker(): boolean {
250
+ return WORKER_ENVS.has(this.#environment.context);
251
+ }
252
+
253
+ isWorklet(): boolean {
254
+ return this.#environment.context === 'worklet';
255
+ }
256
+
257
+ matchesEngines(
258
+ minVersions: VersionMap,
259
+ defaultValue?: boolean = false,
260
+ ): boolean {
261
+ // Determine if the environment matches some minimum version requirements.
262
+ // For browsers, we run a browserslist query with and without the minimum
263
+ // required browsers and compare the lists. For node, we just check semver.
264
+ if (this.isBrowser() && this.engines.browsers != null) {
265
+ let targetBrowsers = this.engines.browsers;
266
+ let browsers =
267
+ targetBrowsers != null && !Array.isArray(targetBrowsers)
268
+ ? [targetBrowsers]
269
+ : targetBrowsers;
270
+
271
+ // If outputting esmodules, exclude browsers without support.
272
+ if (this.outputFormat === 'esmodule') {
273
+ browsers = [...browsers, ...getExcludedBrowsers(supportData.esmodules)];
274
+ }
275
+
276
+ let matchedBrowsers = browserslist(browsers);
277
+ if (matchedBrowsers.length === 0) {
278
+ return false;
279
+ }
280
+
281
+ let minBrowsers = getExcludedBrowsers(minVersions);
282
+ let withoutMinBrowsers = browserslist([...browsers, ...minBrowsers]);
283
+ return matchedBrowsers.length === withoutMinBrowsers.length;
284
+ } else if (this.isNode() && this.engines.node != null) {
285
+ return (
286
+ minVersions.node != null &&
287
+ !semver.intersects(`< ${minVersions.node}`, this.engines.node)
288
+ );
289
+ }
290
+
291
+ return defaultValue;
292
+ }
293
+
294
+ supports(feature: EnvironmentFeature, defaultValue?: boolean): boolean {
295
+ let engines = supportData[feature];
296
+ if (!engines) {
297
+ throw new Error('Unknown environment feature: ' + feature);
298
+ }
299
+
300
+ return this.matchesEngines(engines, defaultValue);
301
+ }
302
+ }
303
+
304
+ function getExcludedBrowsers(minVersions: VersionMap) {
305
+ let browsers = [];
306
+ for (let browser of ALL_BROWSERS) {
307
+ let version = minVersions[browser];
308
+ if (version) {
309
+ browsers.push(`not ${browser} < ${version}`);
310
+ } else {
311
+ browsers.push(`not ${browser} > 0`);
312
+ }
313
+ }
314
+
315
+ return browsers;
316
+ }
@@ -0,0 +1,320 @@
1
+ // @flow strict-local
2
+
3
+ import type {
4
+ Asset as IAsset,
5
+ Bundle as IBundle,
6
+ BundleGroup as IBundleGroup,
7
+ CreateBundleOpts,
8
+ Dependency as IDependency,
9
+ MutableBundleGraph as IMutableBundleGraph,
10
+ Target,
11
+ } from '@atlaspack/types';
12
+ import type {
13
+ AtlaspackOptions,
14
+ BundleGroup as InternalBundleGroup,
15
+ BundleNode,
16
+ } from '../types';
17
+
18
+ import invariant from 'assert';
19
+ import nullthrows from 'nullthrows';
20
+ import {hashString} from '@atlaspack/rust';
21
+ import BundleGraph from './BundleGraph';
22
+ import InternalBundleGraph, {bundleGraphEdgeTypes} from '../BundleGraph';
23
+ import {Bundle, bundleToInternalBundle} from './Bundle';
24
+ import {assetFromValue, assetToAssetValue} from './Asset';
25
+ import {getBundleGroupId, getPublicId} from '../utils';
26
+ import Dependency, {dependencyToInternalDependency} from './Dependency';
27
+ import {environmentToInternalEnvironment} from './Environment';
28
+ import {targetToInternalTarget} from './Target';
29
+ import {HASH_REF_PREFIX} from '../constants';
30
+ import {fromProjectPathRelative} from '../projectPath';
31
+ import {BundleBehavior} from '../types';
32
+ import BundleGroup, {bundleGroupToInternalBundleGroup} from './BundleGroup';
33
+
34
+ export default class MutableBundleGraph
35
+ extends BundleGraph<IBundle>
36
+ implements IMutableBundleGraph
37
+ {
38
+ #graph /*: InternalBundleGraph */;
39
+ #options /*: AtlaspackOptions */;
40
+ #bundlePublicIds /*: Set<string> */ = new Set<string>();
41
+
42
+ constructor(graph: InternalBundleGraph, options: AtlaspackOptions) {
43
+ super(graph, Bundle.get.bind(Bundle), options);
44
+ this.#graph = graph;
45
+ this.#options = options;
46
+ }
47
+
48
+ addAssetToBundle(asset: IAsset, bundle: IBundle) {
49
+ this.#graph.addAssetToBundle(
50
+ assetToAssetValue(asset),
51
+ bundleToInternalBundle(bundle),
52
+ );
53
+ }
54
+
55
+ addAssetGraphToBundle(
56
+ asset: IAsset,
57
+ bundle: IBundle,
58
+ shouldSkipDependency?: IDependency => boolean,
59
+ ) {
60
+ this.#graph.addAssetGraphToBundle(
61
+ assetToAssetValue(asset),
62
+ bundleToInternalBundle(bundle),
63
+ shouldSkipDependency
64
+ ? d => shouldSkipDependency(new Dependency(d, this.#options))
65
+ : undefined,
66
+ );
67
+ }
68
+
69
+ addEntryToBundle(
70
+ asset: IAsset,
71
+ bundle: IBundle,
72
+ shouldSkipDependency?: IDependency => boolean,
73
+ ) {
74
+ this.#graph.addEntryToBundle(
75
+ assetToAssetValue(asset),
76
+ bundleToInternalBundle(bundle),
77
+ shouldSkipDependency
78
+ ? d => shouldSkipDependency(new Dependency(d, this.#options))
79
+ : undefined,
80
+ );
81
+ }
82
+
83
+ createBundleGroup(dependency: IDependency, target: Target): IBundleGroup {
84
+ let dependencyNode = this.#graph._graph.getNodeByContentKey(dependency.id);
85
+ if (!dependencyNode) {
86
+ throw new Error('Dependency not found');
87
+ }
88
+
89
+ invariant(dependencyNode.type === 'dependency');
90
+
91
+ let resolved = this.#graph.getResolvedAsset(
92
+ dependencyToInternalDependency(dependency),
93
+ );
94
+ if (!resolved) {
95
+ throw new Error(
96
+ 'Dependency did not resolve to an asset ' + dependency.id,
97
+ );
98
+ }
99
+
100
+ let bundleGroup: InternalBundleGroup = {
101
+ target: targetToInternalTarget(target),
102
+ entryAssetId: resolved.id,
103
+ };
104
+
105
+ let bundleGroupKey = getBundleGroupId(bundleGroup);
106
+ let bundleGroupNodeId = this.#graph._graph.hasContentKey(bundleGroupKey)
107
+ ? this.#graph._graph.getNodeIdByContentKey(bundleGroupKey)
108
+ : this.#graph._graph.addNodeByContentKey(bundleGroupKey, {
109
+ id: bundleGroupKey,
110
+ type: 'bundle_group',
111
+ value: bundleGroup,
112
+ });
113
+
114
+ let dependencyNodeId = this.#graph._graph.getNodeIdByContentKey(
115
+ dependencyNode.id,
116
+ );
117
+ let resolvedNodeId = this.#graph._graph.getNodeIdByContentKey(resolved.id);
118
+ let assetNodes =
119
+ this.#graph._graph.getNodeIdsConnectedFrom(dependencyNodeId);
120
+ this.#graph._graph.addEdge(dependencyNodeId, bundleGroupNodeId);
121
+ this.#graph._graph.replaceNodeIdsConnectedTo(bundleGroupNodeId, assetNodes);
122
+ this.#graph._graph.addEdge(
123
+ dependencyNodeId,
124
+ resolvedNodeId,
125
+ bundleGraphEdgeTypes.references,
126
+ );
127
+ if (
128
+ // This check is needed for multiple targets, when we go over the same nodes twice
129
+ this.#graph._graph.hasEdge(
130
+ dependencyNodeId,
131
+ resolvedNodeId,
132
+ bundleGraphEdgeTypes.null,
133
+ )
134
+ ) {
135
+ //nullEdgeType
136
+ this.#graph._graph.removeEdge(dependencyNodeId, resolvedNodeId);
137
+ }
138
+
139
+ if (dependency.isEntry) {
140
+ this.#graph._graph.addEdge(
141
+ nullthrows(this.#graph._graph.rootNodeId),
142
+ bundleGroupNodeId,
143
+ bundleGraphEdgeTypes.bundle,
144
+ );
145
+ } else {
146
+ let inboundBundleNodeIds = this.#graph._graph.getNodeIdsConnectedTo(
147
+ dependencyNodeId,
148
+ bundleGraphEdgeTypes.contains,
149
+ );
150
+ for (let inboundBundleNodeId of inboundBundleNodeIds) {
151
+ invariant(
152
+ this.#graph._graph.getNode(inboundBundleNodeId)?.type === 'bundle',
153
+ );
154
+ this.#graph._graph.addEdge(
155
+ inboundBundleNodeId,
156
+ bundleGroupNodeId,
157
+ bundleGraphEdgeTypes.bundle,
158
+ );
159
+ }
160
+ }
161
+
162
+ return new BundleGroup(bundleGroup, this.#options);
163
+ }
164
+
165
+ removeBundleGroup(bundleGroup: IBundleGroup): void {
166
+ this.#graph.removeBundleGroup(
167
+ bundleGroupToInternalBundleGroup(bundleGroup),
168
+ );
169
+ }
170
+
171
+ internalizeAsyncDependency(bundle: IBundle, dependency: IDependency): void {
172
+ this.#graph.internalizeAsyncDependency(
173
+ bundleToInternalBundle(bundle),
174
+ dependencyToInternalDependency(dependency),
175
+ );
176
+ }
177
+
178
+ createBundle(opts: CreateBundleOpts): Bundle {
179
+ let entryAsset = opts.entryAsset
180
+ ? assetToAssetValue(opts.entryAsset)
181
+ : null;
182
+
183
+ let target = targetToInternalTarget(opts.target);
184
+ let bundleId = hashString(
185
+ 'bundle:' +
186
+ (opts.entryAsset ? opts.entryAsset.id : opts.uniqueKey) +
187
+ fromProjectPathRelative(target.distDir) +
188
+ (opts.bundleBehavior ?? ''),
189
+ );
190
+
191
+ let existing = this.#graph._graph.getNodeByContentKey(bundleId);
192
+ if (existing != null) {
193
+ invariant(existing.type === 'bundle');
194
+ return Bundle.get(existing.value, this.#graph, this.#options);
195
+ }
196
+
197
+ let publicId = getPublicId(bundleId, existing =>
198
+ this.#bundlePublicIds.has(existing),
199
+ );
200
+ this.#bundlePublicIds.add(publicId);
201
+
202
+ let isPlaceholder = false;
203
+ if (entryAsset) {
204
+ let entryAssetNode = this.#graph._graph.getNodeByContentKey(
205
+ entryAsset.id,
206
+ );
207
+ invariant(entryAssetNode?.type === 'asset', 'Entry asset does not exist');
208
+ isPlaceholder = entryAssetNode.requested === false;
209
+ }
210
+
211
+ let bundleNode: BundleNode = {
212
+ type: 'bundle',
213
+ id: bundleId,
214
+ value: {
215
+ id: bundleId,
216
+ hashReference: this.#options.shouldContentHash
217
+ ? HASH_REF_PREFIX + bundleId
218
+ : bundleId.slice(-8),
219
+ type: opts.entryAsset ? opts.entryAsset.type : opts.type,
220
+ env: opts.env
221
+ ? environmentToInternalEnvironment(opts.env)
222
+ : nullthrows(entryAsset).env,
223
+ entryAssetIds: entryAsset ? [entryAsset.id] : [],
224
+ mainEntryId: entryAsset?.id,
225
+ pipeline: opts.entryAsset ? opts.entryAsset.pipeline : opts.pipeline,
226
+ needsStableName: opts.needsStableName,
227
+ bundleBehavior:
228
+ opts.bundleBehavior != null
229
+ ? BundleBehavior[opts.bundleBehavior]
230
+ : null,
231
+ isSplittable: opts.entryAsset
232
+ ? opts.entryAsset.isBundleSplittable
233
+ : opts.isSplittable,
234
+ isPlaceholder,
235
+ target,
236
+ name: null,
237
+ displayName: null,
238
+ publicId,
239
+ manualSharedBundle: opts.manualSharedBundle,
240
+ },
241
+ };
242
+
243
+ let bundleNodeId = this.#graph._graph.addNodeByContentKey(
244
+ bundleId,
245
+ bundleNode,
246
+ );
247
+
248
+ if (opts.entryAsset) {
249
+ this.#graph._graph.addEdge(
250
+ bundleNodeId,
251
+ this.#graph._graph.getNodeIdByContentKey(opts.entryAsset.id),
252
+ );
253
+ }
254
+ return Bundle.get(bundleNode.value, this.#graph, this.#options);
255
+ }
256
+
257
+ addBundleToBundleGroup(bundle: IBundle, bundleGroup: IBundleGroup) {
258
+ this.#graph.addBundleToBundleGroup(
259
+ bundleToInternalBundle(bundle),
260
+ bundleGroupToInternalBundleGroup(bundleGroup),
261
+ );
262
+ }
263
+
264
+ createAssetReference(
265
+ dependency: IDependency,
266
+ asset: IAsset,
267
+ bundle: IBundle,
268
+ ): void {
269
+ return this.#graph.createAssetReference(
270
+ dependencyToInternalDependency(dependency),
271
+ assetToAssetValue(asset),
272
+ bundleToInternalBundle(bundle),
273
+ );
274
+ }
275
+
276
+ createBundleReference(from: IBundle, to: IBundle): void {
277
+ return this.#graph.createBundleReference(
278
+ bundleToInternalBundle(from),
279
+ bundleToInternalBundle(to),
280
+ );
281
+ }
282
+
283
+ getDependencyAssets(dependency: IDependency): Array<IAsset> {
284
+ return this.#graph
285
+ .getDependencyAssets(dependencyToInternalDependency(dependency))
286
+ .map(asset => assetFromValue(asset, this.#options));
287
+ }
288
+
289
+ getBundleGroupsContainingBundle(bundle: IBundle): Array<IBundleGroup> {
290
+ return this.#graph
291
+ .getBundleGroupsContainingBundle(bundleToInternalBundle(bundle))
292
+ .map(bundleGroup => new BundleGroup(bundleGroup, this.#options));
293
+ }
294
+
295
+ getParentBundlesOfBundleGroup(bundleGroup: IBundleGroup): Array<IBundle> {
296
+ return this.#graph
297
+ .getParentBundlesOfBundleGroup(
298
+ bundleGroupToInternalBundleGroup(bundleGroup),
299
+ )
300
+ .map(bundle => Bundle.get(bundle, this.#graph, this.#options));
301
+ }
302
+
303
+ getTotalSize(asset: IAsset): number {
304
+ return this.#graph.getTotalSize(assetToAssetValue(asset));
305
+ }
306
+
307
+ isAssetReachableFromBundle(asset: IAsset, bundle: IBundle): boolean {
308
+ return this.#graph.isAssetReachableFromBundle(
309
+ assetToAssetValue(asset),
310
+ bundleToInternalBundle(bundle),
311
+ );
312
+ }
313
+
314
+ removeAssetGraphFromBundle(asset: IAsset, bundle: IBundle) {
315
+ this.#graph.removeAssetGraphFromBundle(
316
+ assetToAssetValue(asset),
317
+ bundleToInternalBundle(bundle),
318
+ );
319
+ }
320
+ }