@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,427 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.AssetGraphBuilder = void 0;
7
+ exports.default = createAssetGraphRequest;
8
+ function _logger() {
9
+ const data = _interopRequireDefault(require("@atlaspack/logger"));
10
+ _logger = function () {
11
+ return data;
12
+ };
13
+ return data;
14
+ }
15
+ function _assert() {
16
+ const data = _interopRequireDefault(require("assert"));
17
+ _assert = function () {
18
+ return data;
19
+ };
20
+ return data;
21
+ }
22
+ function _nullthrows() {
23
+ const data = _interopRequireDefault(require("nullthrows"));
24
+ _nullthrows = function () {
25
+ return data;
26
+ };
27
+ return data;
28
+ }
29
+ function _utils() {
30
+ const data = require("@atlaspack/utils");
31
+ _utils = function () {
32
+ return data;
33
+ };
34
+ return data;
35
+ }
36
+ function _rust() {
37
+ const data = require("@atlaspack/rust");
38
+ _rust = function () {
39
+ return data;
40
+ };
41
+ return data;
42
+ }
43
+ function _diagnostic() {
44
+ const data = _interopRequireDefault(require("@atlaspack/diagnostic"));
45
+ _diagnostic = function () {
46
+ return data;
47
+ };
48
+ return data;
49
+ }
50
+ var _types = require("../types");
51
+ var _AssetGraph = _interopRequireDefault(require("../AssetGraph"));
52
+ var _constants = require("../constants");
53
+ var _EntryRequest = _interopRequireDefault(require("./EntryRequest"));
54
+ var _TargetRequest = _interopRequireDefault(require("./TargetRequest"));
55
+ var _AssetRequest = _interopRequireDefault(require("./AssetRequest"));
56
+ var _PathRequest = _interopRequireDefault(require("./PathRequest"));
57
+ var _projectPath = require("../projectPath");
58
+ var _dumpGraphToGraphViz = _interopRequireDefault(require("../dumpGraphToGraphViz"));
59
+ var _SymbolPropagation = require("../SymbolPropagation");
60
+ var _RequestTracker = require("../RequestTracker");
61
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
62
+ function createAssetGraphRequest(requestInput) {
63
+ return {
64
+ type: _RequestTracker.requestTypes.asset_graph_request,
65
+ id: requestInput.name,
66
+ run: async input => {
67
+ let prevResult = await input.api.getPreviousResult();
68
+ let builder = new AssetGraphBuilder(input, prevResult);
69
+ let assetGraphRequest = await await builder.build();
70
+
71
+ // early break for incremental bundling if production or flag is off;
72
+ if (!input.options.shouldBundleIncrementally || input.options.mode === 'production') {
73
+ assetGraphRequest.assetGraph.safeToIncrementallyBundle = false;
74
+ }
75
+ return assetGraphRequest;
76
+ },
77
+ input: requestInput
78
+ };
79
+ }
80
+ const typesWithRequests = new Set(['entry_specifier', 'entry_file', 'dependency', 'asset_group']);
81
+ class AssetGraphBuilder {
82
+ assetRequests = [];
83
+ constructor({
84
+ input,
85
+ api,
86
+ options
87
+ }, prevResult) {
88
+ let {
89
+ entries,
90
+ assetGroups,
91
+ optionsRef,
92
+ name,
93
+ requestedAssetIds,
94
+ shouldBuildLazily,
95
+ lazyIncludes,
96
+ lazyExcludes
97
+ } = input;
98
+ let assetGraph = (prevResult === null || prevResult === void 0 ? void 0 : prevResult.assetGraph) ?? new _AssetGraph.default();
99
+ assetGraph.safeToIncrementallyBundle = true;
100
+ assetGraph.setRootConnections({
101
+ entries,
102
+ assetGroups
103
+ });
104
+ assetGraph.undeferredDependencies.clear();
105
+ this.assetGroupsWithRemovedParents = (prevResult === null || prevResult === void 0 ? void 0 : prevResult.assetGroupsWithRemovedParents) ?? new Set();
106
+ this.previousSymbolPropagationErrors = (prevResult === null || prevResult === void 0 ? void 0 : prevResult.previousSymbolPropagationErrors) ?? new Map();
107
+ this.changedAssets = (prevResult === null || prevResult === void 0 ? void 0 : prevResult.changedAssets) ?? new Map();
108
+ this.changedAssetsPropagation = new Set();
109
+ this.prevChangedAssetsPropagation = prevResult === null || prevResult === void 0 ? void 0 : prevResult.changedAssetsPropagation;
110
+ this.assetGraph = assetGraph;
111
+ this.optionsRef = optionsRef;
112
+ this.options = options;
113
+ this.api = api;
114
+ this.name = name;
115
+ this.requestedAssetIds = requestedAssetIds ?? new Set();
116
+ this.shouldBuildLazily = shouldBuildLazily ?? false;
117
+ this.lazyIncludes = lazyIncludes ?? [];
118
+ this.lazyExcludes = lazyExcludes ?? [];
119
+ this.cacheKey = (0, _rust().hashString)(`${_constants.ATLASPACK_VERSION}${name}${JSON.stringify(entries) ?? ''}${options.mode}${options.shouldBuildLazily ? 'lazy' : 'eager'}`) + '-AssetGraph';
120
+ this.isSingleChangeRebuild = api.getInvalidSubRequests().filter(req => req.requestType === 'asset_request').length === 1;
121
+ this.queue = new (_utils().PromiseQueue)();
122
+ assetGraph.onNodeRemoved = nodeId => {
123
+ this.assetGroupsWithRemovedParents.delete(nodeId);
124
+
125
+ // This needs to mark all connected nodes that doesn't become orphaned
126
+ // due to replaceNodesConnectedTo to make sure that the symbols of
127
+ // nodes from which at least one parent was removed are updated.
128
+ let node = (0, _nullthrows().default)(assetGraph.getNode(nodeId));
129
+ if (assetGraph.isOrphanedNode(nodeId) && node.type === 'dependency') {
130
+ let children = assetGraph.getNodeIdsConnectedFrom(nodeId);
131
+ for (let child of children) {
132
+ let childNode = (0, _nullthrows().default)(assetGraph.getNode(child));
133
+ (0, _assert().default)(childNode.type === 'asset_group' || childNode.type === 'asset');
134
+ childNode.usedSymbolsDownDirty = true;
135
+ this.assetGroupsWithRemovedParents.add(child);
136
+ }
137
+ }
138
+ };
139
+ }
140
+ async build() {
141
+ let errors = [];
142
+ let rootNodeId = (0, _nullthrows().default)(this.assetGraph.rootNodeId, 'A root node is required to traverse');
143
+ let visitedAssetGroups = new Set();
144
+ let visited = new Set([rootNodeId]);
145
+ const visit = nodeId => {
146
+ if (errors.length > 0) {
147
+ return;
148
+ }
149
+ if (this.shouldSkipRequest(nodeId)) {
150
+ visitChildren(nodeId);
151
+ } else {
152
+ // ? do we need to visit children inside of the promise that is queued?
153
+ this.queueCorrespondingRequest(nodeId, errors).then(() => visitChildren(nodeId));
154
+ }
155
+ };
156
+ const visitChildren = nodeId => {
157
+ for (let childNodeId of this.assetGraph.getNodeIdsConnectedFrom(nodeId)) {
158
+ let child = (0, _nullthrows().default)(this.assetGraph.getNode(childNodeId));
159
+ if ((!visited.has(childNodeId) || child.hasDeferred) && this.shouldVisitChild(nodeId, childNodeId)) {
160
+ if (child.type === 'asset_group') {
161
+ visitedAssetGroups.add(childNodeId);
162
+ }
163
+ visited.add(childNodeId);
164
+ visit(childNodeId);
165
+ }
166
+ }
167
+ };
168
+ visit(rootNodeId);
169
+ await this.queue.run();
170
+ _logger().default.verbose({
171
+ origin: '@atlaspack/core',
172
+ message: 'Asset graph walked',
173
+ meta: {
174
+ visitedAssetGroupsCount: visitedAssetGroups.size
175
+ }
176
+ });
177
+ if (this.prevChangedAssetsPropagation) {
178
+ // Add any previously seen Assets that have not been propagated yet to
179
+ // 'this.changedAssetsPropagation', but only if they still remain in the graph
180
+ // as they could have been removed since the last build
181
+ for (let assetId of this.prevChangedAssetsPropagation) {
182
+ if (this.assetGraph.hasContentKey(assetId)) {
183
+ this.changedAssetsPropagation.add(assetId);
184
+ }
185
+ }
186
+ }
187
+ if (errors.length) {
188
+ this.api.storeResult({
189
+ assetGraph: this.assetGraph,
190
+ changedAssets: this.changedAssets,
191
+ changedAssetsPropagation: this.changedAssetsPropagation,
192
+ assetGroupsWithRemovedParents: this.assetGroupsWithRemovedParents,
193
+ previousSymbolPropagationErrors: undefined,
194
+ assetRequests: []
195
+ }, this.cacheKey);
196
+
197
+ // TODO: eventually support multiple errors since requests could reject in parallel
198
+ throw errors[0];
199
+ }
200
+ if (this.assetGraph.nodes.length > 1) {
201
+ await (0, _dumpGraphToGraphViz.default)(this.assetGraph, 'AssetGraph_' + this.name + '_before_prop');
202
+ try {
203
+ let errors = (0, _SymbolPropagation.propagateSymbols)({
204
+ options: this.options,
205
+ assetGraph: this.assetGraph,
206
+ changedAssetsPropagation: this.changedAssetsPropagation,
207
+ assetGroupsWithRemovedParents: this.assetGroupsWithRemovedParents,
208
+ previousErrors: this.previousSymbolPropagationErrors
209
+ });
210
+ this.changedAssetsPropagation.clear();
211
+ if (errors.size > 0) {
212
+ this.api.storeResult({
213
+ assetGraph: this.assetGraph,
214
+ changedAssets: this.changedAssets,
215
+ changedAssetsPropagation: this.changedAssetsPropagation,
216
+ assetGroupsWithRemovedParents: this.assetGroupsWithRemovedParents,
217
+ previousSymbolPropagationErrors: errors,
218
+ assetRequests: []
219
+ }, this.cacheKey);
220
+
221
+ // Just throw the first error. Since errors can bubble (e.g. reexporting a reexported symbol also fails),
222
+ // determining which failing export is the root cause is nontrivial (because of circular dependencies).
223
+ throw new (_diagnostic().default)({
224
+ diagnostic: [...errors.values()][0]
225
+ });
226
+ }
227
+ } catch (e) {
228
+ await (0, _dumpGraphToGraphViz.default)(this.assetGraph, 'AssetGraph_' + this.name + '_failed');
229
+ throw e;
230
+ }
231
+ }
232
+ await (0, _dumpGraphToGraphViz.default)(this.assetGraph, 'AssetGraph_' + this.name);
233
+ this.api.storeResult({
234
+ assetGraph: this.assetGraph,
235
+ changedAssets: new Map(),
236
+ changedAssetsPropagation: this.changedAssetsPropagation,
237
+ assetGroupsWithRemovedParents: undefined,
238
+ previousSymbolPropagationErrors: undefined,
239
+ assetRequests: []
240
+ }, this.cacheKey);
241
+ return {
242
+ assetGraph: this.assetGraph,
243
+ changedAssets: this.changedAssets,
244
+ changedAssetsPropagation: this.changedAssetsPropagation,
245
+ assetGroupsWithRemovedParents: undefined,
246
+ previousSymbolPropagationErrors: undefined,
247
+ assetRequests: this.assetRequests
248
+ };
249
+ }
250
+ shouldVisitChild(nodeId, childNodeId) {
251
+ if (this.shouldBuildLazily) {
252
+ let node = (0, _nullthrows().default)(this.assetGraph.getNode(nodeId));
253
+ let childNode = (0, _nullthrows().default)(this.assetGraph.getNode(childNodeId));
254
+ if (node.type === 'asset' && childNode.type === 'dependency') {
255
+ // This logic will set `node.requested` to `true` if the node is in the list of requested asset ids
256
+ // (i.e. this is an entry of a (probably) placeholder bundle that wasn't previously requested)
257
+ //
258
+ // Otherwise, if this node either is explicitly not requested, or has had it's requested attribute deleted,
259
+ // it will determine whether this node is an "async child" - that is, is it a (probably)
260
+ // dynamic import(). If so, it will explicitly have it's `node.requested` set to `false`
261
+ //
262
+ // If it's not requested, but it's not an async child then it's `node.requested` is deleted (undefined)
263
+
264
+ // by default with lazy compilation all nodes are lazy
265
+ let isNodeLazy = true;
266
+
267
+ // For conditional lazy building - if this node matches the `lazyInclude` globs that means we want
268
+ // only those nodes to be treated as lazy - that means if this node does _NOT_ match that glob, then we
269
+ // also consider it not lazy (so it gets marked as requested).
270
+ const relativePath = (0, _projectPath.fromProjectPathRelative)(node.value.filePath);
271
+ if (this.lazyIncludes.length > 0) {
272
+ isNodeLazy = this.lazyIncludes.some(lazyIncludeRegex => relativePath.match(lazyIncludeRegex));
273
+ }
274
+ // Excludes override includes, so a node is _not_ lazy if it is included in the exclude list.
275
+ if (this.lazyExcludes.length > 0 && isNodeLazy) {
276
+ isNodeLazy = !this.lazyExcludes.some(lazyExcludeRegex => relativePath.match(lazyExcludeRegex));
277
+ }
278
+ if (this.requestedAssetIds.has(node.value.id) || !isNodeLazy) {
279
+ node.requested = true;
280
+ } else if (!node.requested) {
281
+ let isAsyncChild = this.assetGraph.getIncomingDependencies(node.value).every(dep => dep.isEntry || dep.priority !== _types.Priority.sync);
282
+ if (isAsyncChild) {
283
+ node.requested = !isNodeLazy;
284
+ } else {
285
+ delete node.requested;
286
+ }
287
+ }
288
+ let previouslyDeferred = childNode.deferred;
289
+ childNode.deferred = node.requested === false;
290
+
291
+ // The child dependency node we're now evaluating should not be deferred if it's parent
292
+ // is explicitly not requested (requested = false, but not requested = undefined)
293
+ //
294
+ // if we weren't previously deferred but we are now, then this dependency node's parents should also
295
+ // be marked as deferred
296
+ //
297
+ // if we were previously deferred but we not longer are, then then all parents should no longer be
298
+ // deferred either
299
+ if (!previouslyDeferred && childNode.deferred) {
300
+ this.assetGraph.markParentsWithHasDeferred(childNodeId);
301
+ } else if (previouslyDeferred && !childNode.deferred) {
302
+ // Mark Asset and Dependency as dirty for symbol propagation as it was
303
+ // previously deferred and it's used symbols may have changed
304
+ this.changedAssetsPropagation.add(node.id);
305
+ node.usedSymbolsDownDirty = true;
306
+ this.changedAssetsPropagation.add(childNode.id);
307
+ childNode.usedSymbolsDownDirty = true;
308
+ this.assetGraph.unmarkParentsWithHasDeferred(childNodeId);
309
+ }
310
+
311
+ // We `shouldVisitChild` if the childNode is not deferred
312
+ return !childNode.deferred;
313
+ }
314
+ }
315
+ return this.assetGraph.shouldVisitChild(nodeId, childNodeId);
316
+ }
317
+ shouldSkipRequest(nodeId) {
318
+ let node = (0, _nullthrows().default)(this.assetGraph.getNode(nodeId));
319
+ return node.complete === true || !typesWithRequests.has(node.type) || node.correspondingRequest != null && this.api.canSkipSubrequest(node.correspondingRequest);
320
+ }
321
+ queueCorrespondingRequest(nodeId, errors) {
322
+ let promise;
323
+ let node = (0, _nullthrows().default)(this.assetGraph.getNode(nodeId));
324
+ switch (node.type) {
325
+ case 'entry_specifier':
326
+ promise = this.runEntryRequest(node.value);
327
+ break;
328
+ case 'entry_file':
329
+ promise = this.runTargetRequest(node.value);
330
+ break;
331
+ case 'dependency':
332
+ promise = this.runPathRequest(node.value);
333
+ break;
334
+ case 'asset_group':
335
+ promise = this.runAssetRequest(node.value);
336
+ break;
337
+ default:
338
+ throw new Error(`Can not queue corresponding request of node with type ${node.type}`);
339
+ }
340
+ return this.queue.add(() => promise.then(null, error => errors.push(error)));
341
+ }
342
+ async runEntryRequest(input) {
343
+ let prevEntries = this.assetGraph.safeToIncrementallyBundle ? this.assetGraph.getEntryAssets().map(asset => asset.id).sort() : [];
344
+ let request = (0, _EntryRequest.default)(input);
345
+ let result = await this.api.runRequest(request, {
346
+ force: true
347
+ });
348
+ this.assetGraph.resolveEntry(request.input, result.entries, request.id);
349
+ if (this.assetGraph.safeToIncrementallyBundle) {
350
+ let currentEntries = this.assetGraph.getEntryAssets().map(asset => asset.id).sort();
351
+ let didEntriesChange = prevEntries.length !== currentEntries.length || prevEntries.every((entryId, index) => entryId === currentEntries[index]);
352
+ if (didEntriesChange) {
353
+ this.assetGraph.safeToIncrementallyBundle = false;
354
+ }
355
+ }
356
+ }
357
+ async runTargetRequest(input) {
358
+ let request = (0, _TargetRequest.default)(input);
359
+ let targets = await this.api.runRequest(request, {
360
+ force: true
361
+ });
362
+ this.assetGraph.resolveTargets(request.input, targets, request.id);
363
+ }
364
+ async runPathRequest(input) {
365
+ let request = (0, _PathRequest.default)({
366
+ dependency: input,
367
+ name: this.name
368
+ });
369
+ let result = await this.api.runRequest(request, {
370
+ force: true
371
+ });
372
+ this.assetGraph.resolveDependency(input, result, request.id);
373
+ }
374
+ async runAssetRequest(input) {
375
+ this.assetRequests.push(input);
376
+ let request = (0, _AssetRequest.default)({
377
+ ...input,
378
+ name: this.name,
379
+ optionsRef: this.optionsRef,
380
+ isSingleChangeRebuild: this.isSingleChangeRebuild
381
+ });
382
+ let assets = await this.api.runRequest(request, {
383
+ force: true
384
+ });
385
+ if (assets != null) {
386
+ for (let asset of assets) {
387
+ if (this.assetGraph.safeToIncrementallyBundle) {
388
+ let otherAsset = this.assetGraph.getNodeByContentKey(asset.id);
389
+ if (otherAsset != null) {
390
+ (0, _assert().default)(otherAsset.type === 'asset');
391
+ if (!this._areDependenciesEqualForAssets(asset, otherAsset.value)) {
392
+ this.assetGraph.safeToIncrementallyBundle = false;
393
+ }
394
+ } else {
395
+ // adding a new entry or dependency
396
+ this.assetGraph.safeToIncrementallyBundle = false;
397
+ }
398
+ }
399
+ this.changedAssets.set(asset.id, asset);
400
+ this.changedAssetsPropagation.add(asset.id);
401
+ }
402
+ this.assetGraph.resolveAssetGroup(input, assets, request.id);
403
+ } else {
404
+ this.assetGraph.safeToIncrementallyBundle = false;
405
+ }
406
+ this.isSingleChangeRebuild = false;
407
+ }
408
+
409
+ /**
410
+ * Used for incremental bundling of modified assets
411
+ */
412
+ _areDependenciesEqualForAssets(asset, otherAsset) {
413
+ let assetDependencies = Array.from(asset === null || asset === void 0 ? void 0 : asset.dependencies.keys()).sort();
414
+ let otherAssetDependencies = Array.from(otherAsset === null || otherAsset === void 0 ? void 0 : otherAsset.dependencies.keys()).sort();
415
+ if (assetDependencies.length !== otherAssetDependencies.length) {
416
+ return false;
417
+ }
418
+ return assetDependencies.every((key, index) => {
419
+ var _asset$dependencies$g, _otherAsset$dependenc;
420
+ if (key !== otherAssetDependencies[index]) {
421
+ return false;
422
+ }
423
+ return (0, _utils().setEqual)(new Set(asset === null || asset === void 0 || (_asset$dependencies$g = asset.dependencies.get(key)) === null || _asset$dependencies$g === void 0 || (_asset$dependencies$g = _asset$dependencies$g.symbols) === null || _asset$dependencies$g === void 0 ? void 0 : _asset$dependencies$g.keys()), new Set(otherAsset === null || otherAsset === void 0 || (_otherAsset$dependenc = otherAsset.dependencies.get(key)) === null || _otherAsset$dependenc === void 0 || (_otherAsset$dependenc = _otherAsset$dependenc.symbols) === null || _otherAsset$dependenc === void 0 ? void 0 : _otherAsset$dependenc.keys()));
424
+ });
425
+ }
426
+ }
427
+ exports.AssetGraphBuilder = AssetGraphBuilder;
@@ -0,0 +1,187 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createAssetGraphRequestRust = createAssetGraphRequestRust;
7
+ function _assert() {
8
+ const data = _interopRequireDefault(require("assert"));
9
+ _assert = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _diagnostic() {
15
+ const data = _interopRequireDefault(require("@atlaspack/diagnostic"));
16
+ _diagnostic = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ var _AssetGraph = _interopRequireWildcard(require("../AssetGraph"));
22
+ var _projectPath = require("../projectPath");
23
+ var _RequestTracker = require("../RequestTracker");
24
+ var _SymbolPropagation = require("../SymbolPropagation");
25
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
26
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
27
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
28
+ function createAssetGraphRequestRust(rustAtlaspack) {
29
+ return input => ({
30
+ type: _RequestTracker.requestTypes.asset_graph_request,
31
+ id: input.name,
32
+ run: async input => {
33
+ let options = input.options;
34
+ let serializedAssetGraph;
35
+ try {
36
+ serializedAssetGraph = await rustAtlaspack.buildAssetGraph();
37
+ } catch (err) {
38
+ throw new (_diagnostic().default)({
39
+ diagnostic: err
40
+ });
41
+ }
42
+ let {
43
+ assetGraph,
44
+ cachedAssets,
45
+ changedAssets
46
+ } = getAssetGraph(serializedAssetGraph, options);
47
+
48
+ // TODO: Make it a bulk transaction
49
+ await Promise.all(Array.from(cachedAssets.entries(), ([id, code]) => options.cache.setBlob(id, Buffer.from(code))));
50
+ let changedAssetsPropagation = new Set(changedAssets.keys());
51
+ let errors = (0, _SymbolPropagation.propagateSymbols)({
52
+ options,
53
+ assetGraph,
54
+ changedAssetsPropagation,
55
+ assetGroupsWithRemovedParents: new Set(),
56
+ previousErrors: new Map() //this.previousSymbolPropagationErrors,
57
+ });
58
+
59
+ if (errors.size > 0) {
60
+ // Just throw the first error. Since errors can bubble (e.g. reexporting a reexported symbol also fails),
61
+ // determining which failing export is the root cause is nontrivial (because of circular dependencies).
62
+ throw new (_diagnostic().default)({
63
+ diagnostic: [...errors.values()][0]
64
+ });
65
+ }
66
+ return {
67
+ assetGraph,
68
+ assetRequests: [],
69
+ assetGroupsWithRemovedParents: new Set(),
70
+ changedAssets,
71
+ changedAssetsPropagation,
72
+ previousSymbolPropagationErrors: undefined
73
+ };
74
+ },
75
+ input
76
+ });
77
+ }
78
+ function getAssetGraph(serializedGraph, options) {
79
+ let graph = new _AssetGraph.default({
80
+ _contentKeyToNodeId: new Map(),
81
+ _nodeIdToContentKey: new Map()
82
+ });
83
+ graph.safeToIncrementallyBundle = false;
84
+ let cachedAssets = new Map();
85
+ let changedAssets = new Map();
86
+ let entry = 0;
87
+ for (let node of serializedGraph.nodes) {
88
+ if (node.type === 'root') {
89
+ let index = graph.addNodeByContentKey('@@root', {
90
+ id: '@@root',
91
+ type: 'root',
92
+ value: null
93
+ });
94
+ graph.setRootNodeId(index);
95
+ } else if (node.type === 'entry') {
96
+ let id = 'entry:' + ++entry;
97
+ graph.addNodeByContentKey(id, {
98
+ id: id,
99
+ type: 'root',
100
+ value: null
101
+ });
102
+ } else if (node.type === 'asset') {
103
+ let id = node.value.id;
104
+ let asset = node.value.asset;
105
+ asset = {
106
+ ...asset,
107
+ id,
108
+ committed: true,
109
+ contentKey: id,
110
+ filePath: (0, _projectPath.toProjectPath)(options.projectRoot, asset.filePath),
111
+ symbols: asset.hasSymbols ? new Map(asset.symbols.map(({
112
+ exported,
113
+ ...symbol
114
+ }) => [exported, symbol])) : null
115
+ };
116
+ cachedAssets.set(id, asset.code);
117
+ changedAssets.set(id, asset);
118
+ graph.addNodeByContentKey(id, {
119
+ id,
120
+ type: 'asset',
121
+ usedSymbols: new Set(),
122
+ usedSymbolsDownDirty: true,
123
+ usedSymbolsUpDirty: true,
124
+ value: asset
125
+ });
126
+ } else if (node.type === 'dependency') {
127
+ let id = node.value.id;
128
+ let dependency = node.value.dependency;
129
+ dependency = {
130
+ ...dependency,
131
+ id,
132
+ contentKey: id,
133
+ sourcePath: dependency.sourcePath ? (0, _projectPath.toProjectPath)(options.projectRoot, dependency.sourcePath) : null,
134
+ symbols: dependency.hasSymbols ? new Map(dependency.symbols.map(({
135
+ exported,
136
+ ...symbol
137
+ }) => [exported, symbol])) : null
138
+ };
139
+ let usedSymbolsDown = new Set();
140
+ let usedSymbolsUp = new Map();
141
+ if (dependency.isEntry && dependency.isLibrary) {
142
+ usedSymbolsDown.add('*');
143
+ usedSymbolsUp.set('*', undefined);
144
+ }
145
+ graph.addNodeByContentKey(id, {
146
+ id,
147
+ type: 'dependency',
148
+ deferred: false,
149
+ excluded: false,
150
+ hasDeferred: node.has_deferred,
151
+ usedSymbolsDown,
152
+ usedSymbolsDownDirty: true,
153
+ usedSymbolsUp,
154
+ usedSymbolsUpDirtyDown: true,
155
+ usedSymbolsUpDirtyUp: true,
156
+ value: dependency
157
+ });
158
+ }
159
+ }
160
+ for (let i = 0; i < serializedGraph.edges.length; i += 2) {
161
+ let from = serializedGraph.edges[i];
162
+ let to = serializedGraph.edges[i + 1];
163
+ let fromNode = graph.getNode(from);
164
+ if ((fromNode === null || fromNode === void 0 ? void 0 : fromNode.type) === 'dependency') {
165
+ let toNode = graph.getNode(to);
166
+ (0, _assert().default)((toNode === null || toNode === void 0 ? void 0 : toNode.type) === 'asset');
167
+
168
+ // For backwards compatibility, create asset group node if needed.
169
+ let assetGroupNode = (0, _AssetGraph.nodeFromAssetGroup)({
170
+ filePath: toNode.value.filePath,
171
+ env: fromNode.value.env,
172
+ pipeline: toNode.value.pipeline,
173
+ sideEffects: Boolean(toNode.value.sideEffects)
174
+ });
175
+ let index = graph.addNodeByContentKeyIfNeeded(assetGroupNode.id, assetGroupNode);
176
+ graph.addEdge(from, index);
177
+ graph.addEdge(index, to);
178
+ } else {
179
+ graph.addEdge(from, to);
180
+ }
181
+ }
182
+ return {
183
+ assetGraph: graph,
184
+ cachedAssets,
185
+ changedAssets
186
+ };
187
+ }