@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,794 @@
1
+ // @flow
2
+
3
+ import type {ContentKey, NodeId} from '@atlaspack/graph';
4
+ import type {Meta, Symbol} from '@atlaspack/types';
5
+ import type {Diagnostic} from '@atlaspack/diagnostic';
6
+ import type {
7
+ AssetNode,
8
+ DependencyNode,
9
+ InternalSourceLocation,
10
+ AtlaspackOptions,
11
+ } from './types';
12
+ import {type default as AssetGraph} from './AssetGraph';
13
+
14
+ import invariant from 'assert';
15
+ import nullthrows from 'nullthrows';
16
+ import {setEqual} from '@atlaspack/utils';
17
+ import logger from '@atlaspack/logger';
18
+ import {md, convertSourceLocationToHighlight} from '@atlaspack/diagnostic';
19
+ import {BundleBehavior} from './types';
20
+ import {fromProjectPathRelative, fromProjectPath} from './projectPath';
21
+
22
+ export function propagateSymbols({
23
+ options,
24
+ assetGraph,
25
+ changedAssetsPropagation,
26
+ assetGroupsWithRemovedParents,
27
+ previousErrors,
28
+ }: {|
29
+ options: AtlaspackOptions,
30
+ assetGraph: AssetGraph,
31
+ changedAssetsPropagation: Set<string>,
32
+ assetGroupsWithRemovedParents: Set<NodeId>,
33
+ previousErrors?: ?Map<NodeId, Array<Diagnostic>>,
34
+ |}): Map<NodeId, Array<Diagnostic>> {
35
+ let changedAssets = new Set(
36
+ [...changedAssetsPropagation].map(id =>
37
+ assetGraph.getNodeIdByContentKey(id),
38
+ ),
39
+ );
40
+
41
+ // To reorder once at the end
42
+ let changedDeps = new Set<DependencyNode>();
43
+
44
+ // For the down traversal, the nodes with `usedSymbolsDownDirty = true` are exactly
45
+ // `changedAssetsPropagation` (= asset and therefore potentially dependencies changed) or the
46
+ // asset children of `assetGroupsWithRemovedParents` (= fewer incoming dependencies causing less
47
+ // used symbols).
48
+ //
49
+ // The up traversal has to consider all nodes that changed in the down traversal
50
+ // (`useSymbolsUpDirtyDown = true`) which are listed in `changedDepsUsedSymbolsUpDirtyDown`
51
+ // (more or less requested symbols) and in `changedAssetsPropagation` (changing an asset might
52
+ // change exports).
53
+
54
+ // The dependencies that changed in the down traversal causing an update in the up traversal.
55
+ let changedDepsUsedSymbolsUpDirtyDown = new Set<ContentKey>();
56
+
57
+ // Propagate the requested symbols down from the root to the leaves
58
+ propagateSymbolsDown(
59
+ assetGraph,
60
+ changedAssets,
61
+ assetGroupsWithRemovedParents,
62
+ (assetNode, incomingDeps, outgoingDeps) => {
63
+ // exportSymbol -> identifier
64
+ let assetSymbols: ?$ReadOnlyMap<
65
+ Symbol,
66
+ {|local: Symbol, loc: ?InternalSourceLocation, meta?: ?Meta|},
67
+ > = assetNode.value.symbols;
68
+ // identifier -> exportSymbol
69
+ let assetSymbolsInverse;
70
+ if (assetSymbols) {
71
+ assetSymbolsInverse = new Map<Symbol, Set<Symbol>>();
72
+ for (let [s, {local}] of assetSymbols) {
73
+ let set = assetSymbolsInverse.get(local);
74
+
75
+ if (!set) {
76
+ set = new Set();
77
+ assetSymbolsInverse.set(local, set);
78
+ }
79
+ set.add(s);
80
+ }
81
+ }
82
+ let hasNamespaceOutgoingDeps = outgoingDeps.some(
83
+ d => d.value.symbols?.get('*')?.local === '*',
84
+ );
85
+
86
+ // 1) Determine what the incomingDeps requests from the asset
87
+ // ----------------------------------------------------------
88
+
89
+ let isEntry = false;
90
+ let addAll = false;
91
+
92
+ // Used symbols that are exported or reexported (symbol will be removed again later) by asset.
93
+ assetNode.usedSymbols = new Set();
94
+
95
+ // Symbols that have to be namespace reexported by outgoingDeps.
96
+ let namespaceReexportedSymbols = new Set<Symbol>();
97
+
98
+ if (incomingDeps.length === 0) {
99
+ // Root in the runtimes Graph
100
+ assetNode.usedSymbols.add('*');
101
+ namespaceReexportedSymbols.add('*');
102
+ } else {
103
+ for (let incomingDep of incomingDeps) {
104
+ if (incomingDep.value.symbols == null) {
105
+ if (incomingDep.value.sourceAssetId == null) {
106
+ // The root dependency on non-library builds
107
+ isEntry = true;
108
+ } else {
109
+ // A regular dependency with cleared symbols
110
+ addAll = true;
111
+ }
112
+ continue;
113
+ }
114
+
115
+ for (let exportSymbol of incomingDep.usedSymbolsDown) {
116
+ if (exportSymbol === '*') {
117
+ assetNode.usedSymbols.add('*');
118
+ namespaceReexportedSymbols.add('*');
119
+ }
120
+ if (
121
+ !assetSymbols ||
122
+ assetSymbols.has(exportSymbol) ||
123
+ assetSymbols.has('*')
124
+ ) {
125
+ // An own symbol or a non-namespace reexport
126
+ assetNode.usedSymbols.add(exportSymbol);
127
+ }
128
+ // A namespace reexport
129
+ // (but only if we actually have namespace-exporting outgoing dependencies,
130
+ // This usually happens with a reexporting asset with many namespace exports which means that
131
+ // we cannot match up the correct asset with the used symbol at this level.)
132
+ else if (hasNamespaceOutgoingDeps && exportSymbol !== 'default') {
133
+ namespaceReexportedSymbols.add(exportSymbol);
134
+ }
135
+ }
136
+ }
137
+ }
138
+
139
+ // Incomding dependency with cleared symbols, add everything
140
+ if (addAll) {
141
+ assetSymbols?.forEach((_, exportSymbol) =>
142
+ assetNode.usedSymbols.add(exportSymbol),
143
+ );
144
+ }
145
+
146
+ // 2) Distribute the symbols to the outgoing dependencies
147
+ // ----------------------------------------------------------
148
+ for (let dep of outgoingDeps) {
149
+ let depUsedSymbolsDownOld = dep.usedSymbolsDown;
150
+ let depUsedSymbolsDown = new Set();
151
+ dep.usedSymbolsDown = depUsedSymbolsDown;
152
+ if (
153
+ assetNode.value.sideEffects ||
154
+ // Incoming dependency with cleared symbols
155
+ addAll ||
156
+ // For entries, we still need to add dep.value.symbols of the entry (which are "used" but not according to the symbols data)
157
+ isEntry ||
158
+ // If not a single symbol is used, we can say the entire subgraph is not used.
159
+ // This is e.g. needed when some symbol is imported and then used for a export which isn't used (= "semi-weak" reexport)
160
+ // index.js: `import {bar} from "./lib"; ...`
161
+ // lib/index.js: `export * from "./foo.js"; export * from "./bar.js";`
162
+ // lib/foo.js: `import { data } from "./bar.js"; export const foo = data + " esm2";`
163
+ assetNode.usedSymbols.size > 0 ||
164
+ namespaceReexportedSymbols.size > 0
165
+ ) {
166
+ let depSymbols = dep.value.symbols;
167
+ if (!depSymbols) continue;
168
+
169
+ if (depSymbols.get('*')?.local === '*') {
170
+ if (addAll) {
171
+ depUsedSymbolsDown.add('*');
172
+ } else {
173
+ for (let s of namespaceReexportedSymbols) {
174
+ // We need to propagate the namespaceReexportedSymbols to all namespace dependencies (= even wrong ones because we don't know yet)
175
+ depUsedSymbolsDown.add(s);
176
+ }
177
+ }
178
+ }
179
+
180
+ for (let [symbol, {local}] of depSymbols) {
181
+ // Was already handled above
182
+ if (local === '*') continue;
183
+
184
+ if (!assetSymbolsInverse || !depSymbols.get(symbol)?.isWeak) {
185
+ // Bailout or non-weak symbol (= used in the asset itself = not a reexport)
186
+ depUsedSymbolsDown.add(symbol);
187
+ } else {
188
+ let reexportedExportSymbols = assetSymbolsInverse.get(local);
189
+ if (reexportedExportSymbols == null) {
190
+ // not reexported = used in asset itself
191
+ depUsedSymbolsDown.add(symbol);
192
+ } else if (assetNode.usedSymbols.has('*')) {
193
+ // we need everything
194
+ depUsedSymbolsDown.add(symbol);
195
+
196
+ [...reexportedExportSymbols].forEach(s =>
197
+ assetNode.usedSymbols.delete(s),
198
+ );
199
+ } else {
200
+ let usedReexportedExportSymbols = [
201
+ ...reexportedExportSymbols,
202
+ ].filter(s => assetNode.usedSymbols.has(s));
203
+ if (usedReexportedExportSymbols.length > 0) {
204
+ // The symbol is indeed a reexport, so it's not used from the asset itself
205
+ depUsedSymbolsDown.add(symbol);
206
+
207
+ usedReexportedExportSymbols.forEach(s =>
208
+ assetNode.usedSymbols.delete(s),
209
+ );
210
+ }
211
+ }
212
+ }
213
+ }
214
+ } else {
215
+ depUsedSymbolsDown.clear();
216
+ }
217
+ if (!setEqual(depUsedSymbolsDownOld, depUsedSymbolsDown)) {
218
+ dep.usedSymbolsDownDirty = true;
219
+ dep.usedSymbolsUpDirtyDown = true;
220
+ changedDepsUsedSymbolsUpDirtyDown.add(dep.id);
221
+ }
222
+ if (dep.usedSymbolsUpDirtyDown) {
223
+ // Set on node creation
224
+ changedDepsUsedSymbolsUpDirtyDown.add(dep.id);
225
+ }
226
+ }
227
+ },
228
+ );
229
+
230
+ const logFallbackNamespaceInsertion = (
231
+ assetNode,
232
+ symbol: Symbol,
233
+ depNode1,
234
+ depNode2,
235
+ ) => {
236
+ if (options.logLevel === 'verbose') {
237
+ logger.warn({
238
+ message: `${fromProjectPathRelative(
239
+ assetNode.value.filePath,
240
+ )} reexports "${symbol}", which could be resolved either to the dependency "${
241
+ depNode1.value.specifier
242
+ }" or "${
243
+ depNode2.value.specifier
244
+ }" at runtime. Adding a namespace object to fall back on.`,
245
+ origin: '@atlaspack/core',
246
+ });
247
+ }
248
+ };
249
+
250
+ // Because namespace reexports introduce ambiguity, go up the graph from the leaves to the
251
+ // root and remove requested symbols that aren't actually exported
252
+ let errors = propagateSymbolsUp(
253
+ assetGraph,
254
+ changedAssets,
255
+ changedDepsUsedSymbolsUpDirtyDown,
256
+ previousErrors,
257
+ (assetNode, incomingDeps, outgoingDeps) => {
258
+ let assetSymbols: ?$ReadOnlyMap<
259
+ Symbol,
260
+ {|local: Symbol, loc: ?InternalSourceLocation, meta?: ?Meta|},
261
+ > = assetNode.value.symbols;
262
+
263
+ let assetSymbolsInverse = null;
264
+ if (assetSymbols) {
265
+ assetSymbolsInverse = new Map<Symbol, Set<Symbol>>();
266
+ for (let [s, {local}] of assetSymbols) {
267
+ let set = assetSymbolsInverse.get(local);
268
+ if (!set) {
269
+ set = new Set();
270
+ assetSymbolsInverse.set(local, set);
271
+ }
272
+ set.add(s);
273
+ }
274
+ }
275
+
276
+ // the symbols that are reexported (not used in `asset`) -> asset they resolved to
277
+ let reexportedSymbols = new Map<
278
+ Symbol,
279
+ ?{|asset: ContentKey, symbol: ?Symbol|},
280
+ >();
281
+ // the symbols that are reexported (not used in `asset`) -> the corresponding outgoingDep(s)
282
+ // To generate the diagnostic when there are multiple dependencies with non-statically
283
+ // analyzable exports
284
+ let reexportedSymbolsSource = new Map<Symbol, DependencyNode>();
285
+ for (let outgoingDep of outgoingDeps) {
286
+ let outgoingDepSymbols = outgoingDep.value.symbols;
287
+ if (!outgoingDepSymbols) continue;
288
+
289
+ let isExcluded =
290
+ assetGraph.getNodeIdsConnectedFrom(
291
+ assetGraph.getNodeIdByContentKey(outgoingDep.id),
292
+ ).length === 0;
293
+ // excluded, assume everything that is requested exists
294
+ if (isExcluded) {
295
+ outgoingDep.usedSymbolsDown.forEach((_, s) =>
296
+ outgoingDep.usedSymbolsUp.set(s, null),
297
+ );
298
+ }
299
+
300
+ if (outgoingDepSymbols.get('*')?.local === '*') {
301
+ outgoingDep.usedSymbolsUp.forEach((sResolved, s) => {
302
+ if (s === 'default') {
303
+ return;
304
+ }
305
+
306
+ // If the symbol could come from multiple assets at runtime, assetNode's
307
+ // namespace will be needed at runtime to perform the lookup on.
308
+ if (reexportedSymbols.has(s)) {
309
+ if (!assetNode.usedSymbols.has('*')) {
310
+ logFallbackNamespaceInsertion(
311
+ assetNode,
312
+ s,
313
+ nullthrows(reexportedSymbolsSource.get(s)),
314
+ outgoingDep,
315
+ );
316
+ }
317
+ assetNode.usedSymbols.add('*');
318
+ reexportedSymbols.set(s, {asset: assetNode.id, symbol: s});
319
+ } else {
320
+ reexportedSymbols.set(s, sResolved);
321
+ reexportedSymbolsSource.set(s, outgoingDep);
322
+ }
323
+ });
324
+ }
325
+
326
+ for (let [s, sResolved] of outgoingDep.usedSymbolsUp) {
327
+ if (!outgoingDep.usedSymbolsDown.has(s)) {
328
+ // usedSymbolsDown is a superset of usedSymbolsUp
329
+ continue;
330
+ }
331
+
332
+ let local = outgoingDepSymbols.get(s)?.local;
333
+
334
+ if (local == null) {
335
+ // Caused by '*' => '*', already handled
336
+ continue;
337
+ }
338
+
339
+ let reexported = assetSymbolsInverse?.get(local);
340
+ if (reexported != null) {
341
+ reexported.forEach(s => {
342
+ // see same code above
343
+ if (reexportedSymbols.has(s)) {
344
+ if (!assetNode.usedSymbols.has('*')) {
345
+ logFallbackNamespaceInsertion(
346
+ assetNode,
347
+ s,
348
+ nullthrows(reexportedSymbolsSource.get(s)),
349
+ outgoingDep,
350
+ );
351
+ }
352
+ assetNode.usedSymbols.add('*');
353
+ reexportedSymbols.set(s, {asset: assetNode.id, symbol: s});
354
+ } else {
355
+ reexportedSymbols.set(s, sResolved);
356
+ reexportedSymbolsSource.set(s, outgoingDep);
357
+ }
358
+ });
359
+ }
360
+ }
361
+ }
362
+
363
+ let errors: Array<Diagnostic> = [];
364
+
365
+ function usedSymbolsUpAmbiguous(old, current, s, value) {
366
+ if (old.has(s)) {
367
+ let valueOld = old.get(s);
368
+ if (
369
+ valueOld !== value &&
370
+ !(
371
+ valueOld?.asset === value.asset &&
372
+ valueOld?.symbol === value.symbol
373
+ )
374
+ ) {
375
+ // The dependency points to multiple assets (via an asset group).
376
+ current.set(s, undefined);
377
+ return;
378
+ }
379
+ }
380
+ current.set(s, value);
381
+ }
382
+
383
+ for (let incomingDep of incomingDeps) {
384
+ let incomingDepUsedSymbolsUpOld = incomingDep.usedSymbolsUp;
385
+ incomingDep.usedSymbolsUp = new Map();
386
+ let incomingDepSymbols = incomingDep.value.symbols;
387
+ if (!incomingDepSymbols) continue;
388
+
389
+ let hasNamespaceReexport = incomingDepSymbols.get('*')?.local === '*';
390
+ for (let s of incomingDep.usedSymbolsDown) {
391
+ if (
392
+ assetSymbols == null || // Assume everything could be provided if symbols are cleared
393
+ assetNode.value.bundleBehavior === BundleBehavior.isolated ||
394
+ assetNode.value.bundleBehavior === BundleBehavior.inline ||
395
+ s === '*' ||
396
+ assetNode.usedSymbols.has(s)
397
+ ) {
398
+ usedSymbolsUpAmbiguous(
399
+ incomingDepUsedSymbolsUpOld,
400
+ incomingDep.usedSymbolsUp,
401
+ s,
402
+ {
403
+ asset: assetNode.id,
404
+ symbol: s,
405
+ },
406
+ );
407
+ } else if (reexportedSymbols.has(s)) {
408
+ let reexport = reexportedSymbols.get(s);
409
+ let v =
410
+ // Forward a reexport only if the current asset is side-effect free and not external
411
+ !assetNode.value.sideEffects && reexport != null
412
+ ? reexport
413
+ : {
414
+ asset: assetNode.id,
415
+ symbol: s,
416
+ };
417
+ usedSymbolsUpAmbiguous(
418
+ incomingDepUsedSymbolsUpOld,
419
+ incomingDep.usedSymbolsUp,
420
+ s,
421
+ v,
422
+ );
423
+ } else if (!hasNamespaceReexport) {
424
+ let loc = incomingDep.value.symbols?.get(s)?.loc;
425
+ let [resolutionNodeId] = assetGraph.getNodeIdsConnectedFrom(
426
+ assetGraph.getNodeIdByContentKey(incomingDep.id),
427
+ );
428
+ let resolution = nullthrows(assetGraph.getNode(resolutionNodeId));
429
+ invariant(
430
+ resolution &&
431
+ (resolution.type === 'asset_group' ||
432
+ resolution.type === 'asset'),
433
+ );
434
+
435
+ errors.push({
436
+ message: md`${fromProjectPathRelative(
437
+ resolution.value.filePath,
438
+ )} does not export '${s}'`,
439
+ origin: '@atlaspack/core',
440
+ codeFrames: loc
441
+ ? [
442
+ {
443
+ filePath:
444
+ fromProjectPath(options.projectRoot, loc?.filePath) ??
445
+ undefined,
446
+ language: incomingDep.value.sourceAssetType ?? undefined,
447
+ codeHighlights: [convertSourceLocationToHighlight(loc)],
448
+ },
449
+ ]
450
+ : undefined,
451
+ });
452
+ }
453
+ }
454
+
455
+ if (!equalMap(incomingDepUsedSymbolsUpOld, incomingDep.usedSymbolsUp)) {
456
+ changedDeps.add(incomingDep);
457
+ incomingDep.usedSymbolsUpDirtyUp = true;
458
+ }
459
+
460
+ incomingDep.excluded = false;
461
+ if (
462
+ incomingDep.value.symbols != null &&
463
+ incomingDep.usedSymbolsUp.size === 0
464
+ ) {
465
+ let assetGroups = assetGraph.getNodeIdsConnectedFrom(
466
+ assetGraph.getNodeIdByContentKey(incomingDep.id),
467
+ );
468
+ if (assetGroups.length === 1) {
469
+ let [assetGroupId] = assetGroups;
470
+ let assetGroup = nullthrows(assetGraph.getNode(assetGroupId));
471
+ if (
472
+ assetGroup.type === 'asset_group' &&
473
+ assetGroup.value.sideEffects === false
474
+ ) {
475
+ incomingDep.excluded = true;
476
+ }
477
+ } else {
478
+ invariant(assetGroups.length === 0);
479
+ }
480
+ }
481
+ }
482
+ return errors;
483
+ },
484
+ );
485
+
486
+ // Sort usedSymbolsUp so they are a consistent order across builds.
487
+ // This ensures a consistent ordering of these symbols when packaging.
488
+ // See https://github.com/parcel-bundler/parcel/pull/8212
489
+ for (let dep of changedDeps) {
490
+ dep.usedSymbolsUp = new Map(
491
+ [...dep.usedSymbolsUp].sort(([a], [b]) => a.localeCompare(b)),
492
+ );
493
+ }
494
+
495
+ return errors;
496
+ }
497
+
498
+ function propagateSymbolsDown(
499
+ assetGraph: AssetGraph,
500
+ changedAssets: Set<NodeId>,
501
+ assetGroupsWithRemovedParents: Set<NodeId>,
502
+ visit: (
503
+ assetNode: AssetNode,
504
+ incoming: $ReadOnlyArray<DependencyNode>,
505
+ outgoing: $ReadOnlyArray<DependencyNode>,
506
+ ) => void,
507
+ ) {
508
+ if (changedAssets.size === 0 && assetGroupsWithRemovedParents.size === 0) {
509
+ return;
510
+ }
511
+
512
+ // We care about changed assets and their changed dependencies. So start with the first changed
513
+ // asset or dependency and continue while the symbols change. If the queue becomes empty,
514
+ // continue with the next unvisited changed asset.
515
+ //
516
+ // In the end, nodes, which are neither listed in changedAssets nor in
517
+ // assetGroupsWithRemovedParents nor reached via a dirty flag, don't have to be visited at all.
518
+ //
519
+ // In the worst case, some nodes have to be revisited because we don't want to sort the assets
520
+ // into topological order. For example in a diamond graph where the join point is visited twice
521
+ // via each parent (the numbers signifiying the order of re/visiting, `...` being unvisited).
522
+ // However, this only continues as long as there are changes in the used symbols that influence
523
+ // child nodes.
524
+ //
525
+ // |
526
+ // ...
527
+ // / \
528
+ // 1 4
529
+ // \ /
530
+ // 2+5
531
+ // |
532
+ // 3+6
533
+ // |
534
+ // ...
535
+ // |
536
+ //
537
+
538
+ let unreachedAssets = new Set([
539
+ ...changedAssets,
540
+ ...assetGroupsWithRemovedParents,
541
+ ]);
542
+ let queue = new Set([setPop(unreachedAssets)]);
543
+
544
+ while (queue.size > 0) {
545
+ let queuedNodeId = setPop(queue);
546
+ unreachedAssets.delete(queuedNodeId);
547
+
548
+ let outgoing = assetGraph.getNodeIdsConnectedFrom(queuedNodeId);
549
+ let node = nullthrows(assetGraph.getNode(queuedNodeId));
550
+
551
+ let wasNodeDirty = false;
552
+ if (node.type === 'dependency' || node.type === 'asset_group') {
553
+ wasNodeDirty = node.usedSymbolsDownDirty;
554
+ node.usedSymbolsDownDirty = false;
555
+ } else if (node.type === 'asset' && node.usedSymbolsDownDirty) {
556
+ visit(
557
+ node,
558
+ assetGraph.getIncomingDependencies(node.value).map(d => {
559
+ let dep = assetGraph.getNodeByContentKey(d.id);
560
+ invariant(dep && dep.type === 'dependency');
561
+ return dep;
562
+ }),
563
+ outgoing.map(dep => {
564
+ let depNode = nullthrows(assetGraph.getNode(dep));
565
+ invariant(depNode.type === 'dependency');
566
+ return depNode;
567
+ }),
568
+ );
569
+ node.usedSymbolsDownDirty = false;
570
+ }
571
+
572
+ for (let child of outgoing) {
573
+ let childNode = nullthrows(assetGraph.getNode(child));
574
+ let childDirty = false;
575
+ if (
576
+ (childNode.type === 'asset' || childNode.type === 'asset_group') &&
577
+ wasNodeDirty
578
+ ) {
579
+ childNode.usedSymbolsDownDirty = true;
580
+ childDirty = true;
581
+ } else if (childNode.type === 'dependency') {
582
+ childDirty = childNode.usedSymbolsDownDirty;
583
+ }
584
+ if (childDirty) {
585
+ queue.add(child);
586
+ }
587
+ }
588
+
589
+ if (queue.size === 0 && unreachedAssets.size > 0) {
590
+ queue.add(setPop(unreachedAssets));
591
+ }
592
+ }
593
+ }
594
+
595
+ function propagateSymbolsUp(
596
+ assetGraph: AssetGraph,
597
+ changedAssets: Set<NodeId>,
598
+ changedDepsUsedSymbolsUpDirtyDown: Set<ContentKey>,
599
+ previousErrors: ?Map<NodeId, Array<Diagnostic>>,
600
+ visit: (
601
+ assetNode: AssetNode,
602
+ incoming: $ReadOnlyArray<DependencyNode>,
603
+ outgoing: $ReadOnlyArray<DependencyNode>,
604
+ ) => Array<Diagnostic>,
605
+ ): Map<NodeId, Array<Diagnostic>> {
606
+ // For graphs in general (so with cyclic dependencies), some nodes will have to be revisited. So
607
+ // run a regular queue-based BFS for anything that's still dirty.
608
+ //
609
+ // (Previously, there was first a recursive post-order DFS, with the idea that all children of a
610
+ // node should be processed first. With a tree, this would result in a minimal amount of work by
611
+ // processing every asset exactly once and then the remaining cycles would have been handled
612
+ // with the loop. This was slightly faster for initial builds but had O(project) instead of
613
+ // O(changes).)
614
+
615
+ let errors: Map<NodeId, Array<Diagnostic>> = previousErrors
616
+ ? // Some nodes might have been removed since the last build
617
+ new Map([...previousErrors].filter(([n]) => assetGraph.hasNode(n)))
618
+ : new Map();
619
+
620
+ let changedDepsUsedSymbolsUpDirtyDownAssets = new Set([
621
+ ...[...changedDepsUsedSymbolsUpDirtyDown]
622
+ .reverse()
623
+ .flatMap(id => getDependencyResolution(assetGraph, id)),
624
+ ...changedAssets,
625
+ ]);
626
+
627
+ // Do a more efficient full traversal (less recomputations) if more than half of the assets
628
+ // changed.
629
+ let runFullPass =
630
+ // If there are n nodes in the graph, then the asset count is approximately
631
+ // n/6 (for every asset, there are ~4 dependencies and ~1 asset_group).
632
+ assetGraph.nodes.length * (1 / 6) * 0.5 <
633
+ changedDepsUsedSymbolsUpDirtyDownAssets.size;
634
+
635
+ let dirtyDeps;
636
+ if (runFullPass) {
637
+ dirtyDeps = new Set<NodeId>();
638
+ let rootNodeId = nullthrows(
639
+ assetGraph.rootNodeId,
640
+ 'A root node is required to traverse',
641
+ );
642
+
643
+ const nodeVisitor = nodeId => {
644
+ let node = nullthrows(assetGraph.getNode(nodeId));
645
+ let outgoing = assetGraph.getNodeIdsConnectedFrom(nodeId);
646
+
647
+ for (let childId of outgoing) {
648
+ let child = nullthrows(assetGraph.getNode(childId));
649
+ if (node.type === 'asset') {
650
+ invariant(child.type === 'dependency');
651
+ if (child.usedSymbolsUpDirtyUp) {
652
+ node.usedSymbolsUpDirty = true;
653
+ child.usedSymbolsUpDirtyUp = false;
654
+ }
655
+ }
656
+ }
657
+
658
+ if (node.type === 'asset') {
659
+ let incoming = assetGraph.getIncomingDependencies(node.value).map(d => {
660
+ let n = assetGraph.getNodeByContentKey(d.id);
661
+ invariant(n && n.type === 'dependency');
662
+ return n;
663
+ });
664
+ for (let dep of incoming) {
665
+ if (dep.usedSymbolsUpDirtyDown) {
666
+ dep.usedSymbolsUpDirtyDown = false;
667
+ node.usedSymbolsUpDirty = true;
668
+ }
669
+ }
670
+ if (node.usedSymbolsUpDirty) {
671
+ let e = visit(
672
+ node,
673
+ incoming,
674
+ outgoing.map(depNodeId => {
675
+ let depNode = nullthrows(assetGraph.getNode(depNodeId));
676
+ invariant(depNode.type === 'dependency');
677
+ return depNode;
678
+ }),
679
+ );
680
+ if (e.length > 0) {
681
+ node.usedSymbolsUpDirty = true;
682
+ errors.set(nodeId, e);
683
+ } else {
684
+ node.usedSymbolsUpDirty = false;
685
+ errors.delete(nodeId);
686
+ }
687
+ }
688
+ } else {
689
+ if (node.type === 'dependency') {
690
+ if (node.usedSymbolsUpDirtyUp) {
691
+ dirtyDeps.add(nodeId);
692
+ } else {
693
+ dirtyDeps.delete(nodeId);
694
+ }
695
+ }
696
+ }
697
+ };
698
+ assetGraph.postOrderDfsFast(nodeVisitor, rootNodeId);
699
+ }
700
+
701
+ let queue = dirtyDeps ?? changedDepsUsedSymbolsUpDirtyDownAssets;
702
+ while (queue.size > 0) {
703
+ let queuedNodeId = setPop(queue);
704
+ let node = nullthrows(assetGraph.getNode(queuedNodeId));
705
+ if (node.type === 'asset') {
706
+ let incoming = assetGraph.getIncomingDependencies(node.value).map(dep => {
707
+ let depNode = assetGraph.getNodeByContentKey(dep.id);
708
+ invariant(depNode && depNode.type === 'dependency');
709
+ return depNode;
710
+ });
711
+ for (let dep of incoming) {
712
+ if (dep.usedSymbolsUpDirtyDown) {
713
+ dep.usedSymbolsUpDirtyDown = false;
714
+ node.usedSymbolsUpDirty = true;
715
+ }
716
+ }
717
+ let outgoing = assetGraph
718
+ .getNodeIdsConnectedFrom(queuedNodeId)
719
+ .map(depNodeId => {
720
+ let depNode = nullthrows(assetGraph.getNode(depNodeId));
721
+ invariant(depNode.type === 'dependency');
722
+ return depNode;
723
+ });
724
+ for (let dep of outgoing) {
725
+ if (dep.usedSymbolsUpDirtyUp) {
726
+ node.usedSymbolsUpDirty = true;
727
+ dep.usedSymbolsUpDirtyUp = false;
728
+ }
729
+ }
730
+
731
+ if (node.usedSymbolsUpDirty) {
732
+ let e = visit(node, incoming, outgoing);
733
+ if (e.length > 0) {
734
+ node.usedSymbolsUpDirty = true;
735
+ errors.set(queuedNodeId, e);
736
+ } else {
737
+ node.usedSymbolsUpDirty = false;
738
+ errors.delete(queuedNodeId);
739
+ }
740
+ }
741
+
742
+ for (let i of incoming) {
743
+ if (i.usedSymbolsUpDirtyUp) {
744
+ queue.add(assetGraph.getNodeIdByContentKey(i.id));
745
+ }
746
+ }
747
+ } else {
748
+ let connectedNodes = assetGraph.getNodeIdsConnectedTo(queuedNodeId);
749
+ if (connectedNodes.length > 0) {
750
+ queue.add(...connectedNodes);
751
+ }
752
+ }
753
+ }
754
+
755
+ return errors;
756
+ }
757
+
758
+ function getDependencyResolution(
759
+ graph: AssetGraph,
760
+ depId: ContentKey,
761
+ ): Array<NodeId> {
762
+ let depNodeId = graph.getNodeIdByContentKey(depId);
763
+ let connected = graph.getNodeIdsConnectedFrom(depNodeId);
764
+ invariant(connected.length <= 1);
765
+ let child = connected[0];
766
+ if (child) {
767
+ let childNode = nullthrows(graph.getNode(child));
768
+ if (childNode.type === 'asset_group') {
769
+ return graph.getNodeIdsConnectedFrom(child);
770
+ } else {
771
+ return [child];
772
+ }
773
+ }
774
+ return [];
775
+ }
776
+
777
+ function equalMap<K>(
778
+ a: $ReadOnlyMap<K, ?{|asset: ContentKey, symbol: ?Symbol|}>,
779
+ b: $ReadOnlyMap<K, ?{|asset: ContentKey, symbol: ?Symbol|}>,
780
+ ) {
781
+ if (a.size !== b.size) return false;
782
+ for (let [k, v] of a) {
783
+ if (!b.has(k)) return false;
784
+ let vB = b.get(k);
785
+ if (vB?.asset !== v?.asset || vB?.symbol !== v?.symbol) return false;
786
+ }
787
+ return true;
788
+ }
789
+
790
+ function setPop<T>(set: Set<T>): T {
791
+ let v = nullthrows(set.values().next().value);
792
+ set.delete(v);
793
+ return v;
794
+ }