@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,547 @@
1
+ // @flow strict-local
2
+
3
+ import type {Async, Bundle as IBundle, Namer} from '@atlaspack/types';
4
+ import type {SharedReference} from '@atlaspack/workers';
5
+ import type AtlaspackConfig, {LoadedPlugin} from '../AtlaspackConfig';
6
+ import type {StaticRunOpts, RunAPI} from '../RequestTracker';
7
+ import type {
8
+ Asset,
9
+ AssetGroup,
10
+ Bundle as InternalBundle,
11
+ Config,
12
+ DevDepRequest,
13
+ AtlaspackOptions,
14
+ } from '../types';
15
+ import type {ConfigAndCachePath} from './AtlaspackConfigRequest';
16
+
17
+ import invariant from 'assert';
18
+ import assert from 'assert';
19
+ import nullthrows from 'nullthrows';
20
+ import {PluginLogger} from '@atlaspack/logger';
21
+ import ThrowableDiagnostic, {errorToDiagnostic} from '@atlaspack/diagnostic';
22
+ import AssetGraph from '../AssetGraph';
23
+ import BundleGraph from '../public/BundleGraph';
24
+ import InternalBundleGraph, {bundleGraphEdgeTypes} from '../BundleGraph';
25
+ import MutableBundleGraph from '../public/MutableBundleGraph';
26
+ import {Bundle, NamedBundle} from '../public/Bundle';
27
+ import {report} from '../ReporterRunner';
28
+ import dumpGraphToGraphViz from '../dumpGraphToGraphViz';
29
+ import {unique, setDifference} from '@atlaspack/utils';
30
+ import {hashString} from '@atlaspack/rust';
31
+ import PluginOptions from '../public/PluginOptions';
32
+ import applyRuntimes from '../applyRuntimes';
33
+ import {ATLASPACK_VERSION, OPTION_CHANGE} from '../constants';
34
+ import {assertSignalNotAborted, optionsProxy} from '../utils';
35
+ import createAtlaspackConfigRequest, {
36
+ getCachedAtlaspackConfig,
37
+ } from './AtlaspackConfigRequest';
38
+ import {
39
+ createDevDependency,
40
+ getDevDepRequests,
41
+ invalidateDevDeps,
42
+ runDevDepRequest,
43
+ } from './DevDepRequest';
44
+ import {createConfig} from '../InternalConfig';
45
+ import {
46
+ loadPluginConfig,
47
+ runConfigRequest,
48
+ type PluginWithLoadConfig,
49
+ } from './ConfigRequest';
50
+ import {
51
+ joinProjectPath,
52
+ fromProjectPathRelative,
53
+ toProjectPathUnsafe,
54
+ } from '../projectPath';
55
+ import createAssetGraphRequestJS from './AssetGraphRequest';
56
+ import {createAssetGraphRequestRust} from './AssetGraphRequestRust';
57
+ import {tracer, PluginTracer} from '@atlaspack/profiler';
58
+ import {requestTypes} from '../RequestTracker';
59
+
60
+ type BundleGraphRequestInput = {|
61
+ requestedAssetIds: Set<string>,
62
+ signal?: AbortSignal,
63
+ optionsRef: SharedReference,
64
+ |};
65
+
66
+ type BundleGraphRequestResult = {|
67
+ bundleGraph: InternalBundleGraph,
68
+ |};
69
+
70
+ type RunInput = {|
71
+ input: BundleGraphRequestInput,
72
+ ...StaticRunOpts<BundleGraphResult>,
73
+ |};
74
+
75
+ // TODO: Rename to BundleGraphRequestResult
76
+ export type BundleGraphResult = {|
77
+ bundleGraph: InternalBundleGraph,
78
+ changedAssets: Map<string, Asset>,
79
+ assetRequests: Array<AssetGroup>,
80
+ |};
81
+
82
+ type BundleGraphRequest = {|
83
+ id: string,
84
+ +type: typeof requestTypes.bundle_graph_request,
85
+ run: RunInput => Async<BundleGraphResult>,
86
+ input: BundleGraphRequestInput,
87
+ |};
88
+
89
+ export default function createBundleGraphRequest(
90
+ input: BundleGraphRequestInput,
91
+ ): BundleGraphRequest {
92
+ return {
93
+ type: requestTypes.bundle_graph_request,
94
+ id: 'BundleGraph',
95
+ run: async input => {
96
+ let {options, api, invalidateReason} = input;
97
+ let {optionsRef, requestedAssetIds, signal} = input.input;
98
+ let measurement = tracer.createMeasurement('building');
99
+
100
+ let createAssetGraphRequest = input.rustAtlaspack
101
+ ? createAssetGraphRequestRust(input.rustAtlaspack)
102
+ : createAssetGraphRequestJS;
103
+
104
+ let request = createAssetGraphRequest({
105
+ name: 'Main',
106
+ entries: options.entries,
107
+ optionsRef,
108
+ shouldBuildLazily: options.shouldBuildLazily,
109
+ lazyIncludes: options.lazyIncludes,
110
+ lazyExcludes: options.lazyExcludes,
111
+ requestedAssetIds,
112
+ });
113
+
114
+ let {assetGraph, changedAssets, assetRequests} = await api.runRequest(
115
+ request,
116
+ {
117
+ force: options.shouldBuildLazily && requestedAssetIds.size > 0,
118
+ },
119
+ );
120
+
121
+ measurement && measurement.end();
122
+ assertSignalNotAborted(signal);
123
+
124
+ // If any subrequests are invalid (e.g. dev dep requests or config requests),
125
+ // bail on incremental bundling. We also need to invalidate for option changes,
126
+ // which are hoisted to direct invalidations on the bundle graph request.
127
+ let subRequestsInvalid =
128
+ Boolean(invalidateReason & OPTION_CHANGE) ||
129
+ input.api
130
+ .getSubRequests()
131
+ .some(req => !input.api.canSkipSubrequest(req.id));
132
+
133
+ if (subRequestsInvalid) {
134
+ assetGraph.safeToIncrementallyBundle = false;
135
+ }
136
+
137
+ let configResult = nullthrows(
138
+ await input.api.runRequest<null, ConfigAndCachePath>(
139
+ createAtlaspackConfigRequest(),
140
+ ),
141
+ );
142
+
143
+ assertSignalNotAborted(signal);
144
+
145
+ let atlaspackConfig = getCachedAtlaspackConfig(
146
+ configResult,
147
+ input.options,
148
+ );
149
+ let {devDeps, invalidDevDeps} = await getDevDepRequests(input.api);
150
+ invalidateDevDeps(invalidDevDeps, input.options, atlaspackConfig);
151
+
152
+ let bundlingMeasurement = tracer.createMeasurement('bundling');
153
+ let builder = new BundlerRunner(input, atlaspackConfig, devDeps);
154
+ let res: BundleGraphResult = await builder.bundle({
155
+ graph: assetGraph,
156
+ changedAssets: changedAssets,
157
+ assetRequests,
158
+ });
159
+ bundlingMeasurement && bundlingMeasurement.end();
160
+ for (let [id, asset] of changedAssets) {
161
+ res.changedAssets.set(id, asset);
162
+ }
163
+
164
+ await dumpGraphToGraphViz(
165
+ // $FlowFixMe Added in Flow 0.121.0 upgrade in #4381 (Windows only)
166
+ res.bundleGraph._graph,
167
+ 'BundleGraph',
168
+ bundleGraphEdgeTypes,
169
+ );
170
+
171
+ return res;
172
+ },
173
+ input,
174
+ };
175
+ }
176
+
177
+ class BundlerRunner {
178
+ options: AtlaspackOptions;
179
+ optionsRef: SharedReference;
180
+ config: AtlaspackConfig;
181
+ pluginOptions: PluginOptions;
182
+ api: RunAPI<BundleGraphResult>;
183
+ previousDevDeps: Map<string, string>;
184
+ devDepRequests: Map<string, DevDepRequest>;
185
+ configs: Map<string, Config>;
186
+ cacheKey: string;
187
+
188
+ constructor(
189
+ {input, api, options}: RunInput,
190
+ config: AtlaspackConfig,
191
+ previousDevDeps: Map<string, string>,
192
+ ) {
193
+ this.options = options;
194
+ this.api = api;
195
+ this.optionsRef = input.optionsRef;
196
+ this.config = config;
197
+ this.previousDevDeps = previousDevDeps;
198
+ this.devDepRequests = new Map();
199
+ this.configs = new Map();
200
+ this.pluginOptions = new PluginOptions(
201
+ optionsProxy(this.options, api.invalidateOnOptionChange),
202
+ );
203
+ this.cacheKey =
204
+ hashString(
205
+ `${ATLASPACK_VERSION}:BundleGraph:${
206
+ JSON.stringify(options.entries) ?? ''
207
+ }${options.mode}${options.shouldBuildLazily ? 'lazy' : 'eager'}`,
208
+ ) + '-BundleGraph';
209
+ }
210
+
211
+ async loadConfigs() {
212
+ // Load all configs up front so we can use them in the cache key
213
+ let bundler = await this.config.getBundler();
214
+ await this.loadConfig(bundler);
215
+
216
+ let namers = await this.config.getNamers();
217
+ for (let namer of namers) {
218
+ await this.loadConfig(namer);
219
+ }
220
+
221
+ let runtimes = await this.config.getRuntimes();
222
+ for (let runtime of runtimes) {
223
+ await this.loadConfig(runtime);
224
+ }
225
+ }
226
+
227
+ async loadConfig<T: PluginWithLoadConfig>(plugin: LoadedPlugin<T>) {
228
+ let config = createConfig({
229
+ plugin: plugin.name,
230
+ searchPath: toProjectPathUnsafe('index'),
231
+ });
232
+
233
+ await loadPluginConfig(plugin, config, this.options);
234
+ await runConfigRequest(this.api, config);
235
+ for (let devDep of config.devDeps) {
236
+ let devDepRequest = await createDevDependency(
237
+ devDep,
238
+ this.previousDevDeps,
239
+ this.options,
240
+ );
241
+ await this.runDevDepRequest(devDepRequest);
242
+ }
243
+
244
+ this.configs.set(plugin.name, config);
245
+ }
246
+
247
+ async runDevDepRequest(devDepRequest: DevDepRequest) {
248
+ let {specifier, resolveFrom} = devDepRequest;
249
+ let key = `${specifier}:${fromProjectPathRelative(resolveFrom)}`;
250
+ this.devDepRequests.set(key, devDepRequest);
251
+ await runDevDepRequest(this.api, devDepRequest);
252
+ }
253
+
254
+ async bundle({
255
+ graph,
256
+ changedAssets,
257
+ assetRequests,
258
+ }: {|
259
+ graph: AssetGraph,
260
+ changedAssets: Map<string, Asset>,
261
+ assetRequests: Array<AssetGroup>,
262
+ |}): Promise<BundleGraphResult> {
263
+ report({
264
+ type: 'buildProgress',
265
+ phase: 'bundling',
266
+ });
267
+
268
+ await this.loadConfigs();
269
+
270
+ let plugin = await this.config.getBundler();
271
+ let {plugin: bundler, name, resolveFrom} = plugin;
272
+
273
+ // if a previous asset graph hash is passed in, check if the bundle graph is also available
274
+ let previousBundleGraphResult: ?BundleGraphRequestResult;
275
+ if (graph.safeToIncrementallyBundle) {
276
+ try {
277
+ previousBundleGraphResult = await this.api.getPreviousResult();
278
+ } catch {
279
+ // if the bundle graph had an error or was removed, don't fail the build
280
+ }
281
+ }
282
+ if (previousBundleGraphResult == null) {
283
+ graph.safeToIncrementallyBundle = false;
284
+ }
285
+
286
+ let internalBundleGraph;
287
+
288
+ let logger = new PluginLogger({origin: name});
289
+ let tracer = new PluginTracer({
290
+ origin: name,
291
+ category: 'bundle',
292
+ });
293
+ try {
294
+ if (previousBundleGraphResult) {
295
+ internalBundleGraph = previousBundleGraphResult.bundleGraph;
296
+ for (let changedAssetId of changedAssets.keys()) {
297
+ // Copy over the whole node to also have correct symbol data
298
+ let changedAssetNode = nullthrows(
299
+ graph.getNodeByContentKey(changedAssetId),
300
+ );
301
+ invariant(changedAssetNode.type === 'asset');
302
+ internalBundleGraph.updateAsset(changedAssetNode);
303
+ }
304
+ } else {
305
+ internalBundleGraph = InternalBundleGraph.fromAssetGraph(
306
+ graph,
307
+ this.options.mode === 'production',
308
+ );
309
+ invariant(internalBundleGraph != null); // ensures the graph was created
310
+
311
+ await dumpGraphToGraphViz(
312
+ // $FlowFixMe
313
+ internalBundleGraph._graph,
314
+ 'before_bundle',
315
+ bundleGraphEdgeTypes,
316
+ );
317
+ let mutableBundleGraph = new MutableBundleGraph(
318
+ internalBundleGraph,
319
+ this.options,
320
+ );
321
+
322
+ let measurement;
323
+ let measurementFilename;
324
+ if (tracer.enabled) {
325
+ measurementFilename = graph
326
+ .getEntryAssets()
327
+ .map(asset => fromProjectPathRelative(asset.filePath))
328
+ .join(', ');
329
+ measurement = tracer.createMeasurement(
330
+ plugin.name,
331
+ 'bundling:bundle',
332
+ measurementFilename,
333
+ );
334
+ }
335
+
336
+ // this the normal bundle workflow (bundle, optimizing, run-times, naming)
337
+ await bundler.bundle({
338
+ bundleGraph: mutableBundleGraph,
339
+ config: this.configs.get(plugin.name)?.result,
340
+ options: this.pluginOptions,
341
+ logger,
342
+ tracer,
343
+ });
344
+
345
+ measurement && measurement.end();
346
+
347
+ if (this.pluginOptions.mode === 'production') {
348
+ let optimizeMeasurement;
349
+ try {
350
+ if (tracer.enabled) {
351
+ optimizeMeasurement = tracer.createMeasurement(
352
+ plugin.name,
353
+ 'bundling:optimize',
354
+ nullthrows(measurementFilename),
355
+ );
356
+ }
357
+ await bundler.optimize({
358
+ bundleGraph: mutableBundleGraph,
359
+ config: this.configs.get(plugin.name)?.result,
360
+ options: this.pluginOptions,
361
+ logger,
362
+ });
363
+ } catch (e) {
364
+ throw new ThrowableDiagnostic({
365
+ diagnostic: errorToDiagnostic(e, {
366
+ origin: plugin.name,
367
+ }),
368
+ });
369
+ } finally {
370
+ optimizeMeasurement && optimizeMeasurement.end();
371
+ await dumpGraphToGraphViz(
372
+ // $FlowFixMe[incompatible-call]
373
+ internalBundleGraph._graph,
374
+ 'after_optimize',
375
+ );
376
+ }
377
+ }
378
+
379
+ // Add dev dependency for the bundler. This must be done AFTER running it due to
380
+ // the potential for lazy require() that aren't executed until the request runs.
381
+ let devDepRequest = await createDevDependency(
382
+ {
383
+ specifier: name,
384
+ resolveFrom,
385
+ },
386
+ this.previousDevDeps,
387
+ this.options,
388
+ );
389
+ await this.runDevDepRequest(devDepRequest);
390
+ }
391
+ } catch (e) {
392
+ if (internalBundleGraph != null) {
393
+ this.api.storeResult(
394
+ {
395
+ bundleGraph: internalBundleGraph,
396
+ changedAssets: new Map(),
397
+ assetRequests: [],
398
+ },
399
+ this.cacheKey,
400
+ );
401
+ }
402
+
403
+ throw new ThrowableDiagnostic({
404
+ diagnostic: errorToDiagnostic(e, {
405
+ origin: name,
406
+ }),
407
+ });
408
+ } finally {
409
+ invariant(internalBundleGraph != null); // ensures the graph was created
410
+ await dumpGraphToGraphViz(
411
+ // $FlowFixMe[incompatible-call]
412
+ internalBundleGraph._graph,
413
+ 'after_bundle',
414
+ bundleGraphEdgeTypes,
415
+ );
416
+ }
417
+
418
+ let changedRuntimes = new Map();
419
+ if (!previousBundleGraphResult) {
420
+ let namers = await this.config.getNamers();
421
+ // inline bundles must still be named so the PackagerRunner
422
+ // can match them to the correct packager/optimizer plugins.
423
+ let bundles = internalBundleGraph.getBundles({includeInline: true});
424
+ await Promise.all(
425
+ bundles.map(bundle =>
426
+ this.nameBundle(namers, bundle, internalBundleGraph),
427
+ ),
428
+ );
429
+
430
+ changedRuntimes = await applyRuntimes({
431
+ bundleGraph: internalBundleGraph,
432
+ api: this.api,
433
+ config: this.config,
434
+ options: this.options,
435
+ optionsRef: this.optionsRef,
436
+ pluginOptions: this.pluginOptions,
437
+ previousDevDeps: this.previousDevDeps,
438
+ devDepRequests: this.devDepRequests,
439
+ configs: this.configs,
440
+ });
441
+
442
+ // Add dev deps for namers, AFTER running them to account for lazy require().
443
+ for (let namer of namers) {
444
+ let devDepRequest = await createDevDependency(
445
+ {
446
+ specifier: namer.name,
447
+ resolveFrom: namer.resolveFrom,
448
+ },
449
+ this.previousDevDeps,
450
+ this.options,
451
+ );
452
+ await this.runDevDepRequest(devDepRequest);
453
+ }
454
+
455
+ this.validateBundles(internalBundleGraph);
456
+
457
+ // Pre-compute the hashes for each bundle so they are only computed once and shared between workers.
458
+ internalBundleGraph.getBundleGraphHash();
459
+ }
460
+
461
+ await dumpGraphToGraphViz(
462
+ // $FlowFixMe
463
+ internalBundleGraph._graph,
464
+ 'after_runtimes',
465
+ bundleGraphEdgeTypes,
466
+ );
467
+
468
+ this.api.storeResult(
469
+ {
470
+ bundleGraph: internalBundleGraph,
471
+ changedAssets: new Map(),
472
+ assetRequests: [],
473
+ },
474
+ this.cacheKey,
475
+ );
476
+
477
+ return {
478
+ bundleGraph: internalBundleGraph,
479
+ changedAssets: changedRuntimes,
480
+ assetRequests,
481
+ };
482
+ }
483
+
484
+ validateBundles(bundleGraph: InternalBundleGraph): void {
485
+ let bundles = bundleGraph.getBundles();
486
+
487
+ let bundleNames = bundles.map(b =>
488
+ joinProjectPath(b.target.distDir, nullthrows(b.name)),
489
+ );
490
+ assert.deepEqual(
491
+ bundleNames,
492
+ unique(bundleNames),
493
+ 'Bundles must have unique name. Conflicting names: ' +
494
+ [
495
+ ...setDifference(new Set(bundleNames), new Set(unique(bundleNames))),
496
+ ].join(),
497
+ );
498
+ }
499
+
500
+ async nameBundle(
501
+ namers: Array<LoadedPlugin<Namer<mixed>>>,
502
+ internalBundle: InternalBundle,
503
+ internalBundleGraph: InternalBundleGraph,
504
+ ): Promise<void> {
505
+ let bundle = Bundle.get(internalBundle, internalBundleGraph, this.options);
506
+ let bundleGraph = new BundleGraph<IBundle>(
507
+ internalBundleGraph,
508
+ NamedBundle.get.bind(NamedBundle),
509
+ this.options,
510
+ );
511
+
512
+ for (let namer of namers) {
513
+ let measurement;
514
+ try {
515
+ measurement = tracer.createMeasurement(namer.name, 'namer', bundle.id);
516
+ let name = await namer.plugin.name({
517
+ bundle,
518
+ bundleGraph,
519
+ config: this.configs.get(namer.name)?.result,
520
+ options: this.pluginOptions,
521
+ logger: new PluginLogger({origin: namer.name}),
522
+ tracer: new PluginTracer({origin: namer.name, category: 'namer'}),
523
+ });
524
+
525
+ if (name != null) {
526
+ internalBundle.name = name;
527
+ let {hashReference} = internalBundle;
528
+ internalBundle.displayName = name.includes(hashReference)
529
+ ? name.replace(hashReference, '[hash]')
530
+ : name;
531
+
532
+ return;
533
+ }
534
+ } catch (e) {
535
+ throw new ThrowableDiagnostic({
536
+ diagnostic: errorToDiagnostic(e, {
537
+ origin: namer.name,
538
+ }),
539
+ });
540
+ } finally {
541
+ measurement && measurement.end();
542
+ }
543
+ }
544
+
545
+ throw new Error('Unable to name bundle');
546
+ }
547
+ }