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