@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,587 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.INTERNAL_TRANSFORM = exports.INTERNAL_RESOLVE = exports.BuildError = void 0;
7
+ exports.createWorkerFarm = createWorkerFarm;
8
+ exports.default = void 0;
9
+ function _path() {
10
+ const data = _interopRequireDefault(require("path"));
11
+ _path = function () {
12
+ return data;
13
+ };
14
+ return data;
15
+ }
16
+ function _assert() {
17
+ const data = _interopRequireDefault(require("assert"));
18
+ _assert = function () {
19
+ return data;
20
+ };
21
+ return data;
22
+ }
23
+ function _diagnostic() {
24
+ const data = _interopRequireWildcard(require("@atlaspack/diagnostic"));
25
+ _diagnostic = function () {
26
+ return data;
27
+ };
28
+ return data;
29
+ }
30
+ var _Asset = require("./public/Asset");
31
+ var _Bundle = require("./public/Bundle");
32
+ var _BundleGraph = _interopRequireDefault(require("./public/BundleGraph"));
33
+ function _workers() {
34
+ const data = _interopRequireDefault(require("@atlaspack/workers"));
35
+ _workers = function () {
36
+ return data;
37
+ };
38
+ return data;
39
+ }
40
+ function _nullthrows() {
41
+ const data = _interopRequireDefault(require("nullthrows"));
42
+ _nullthrows = function () {
43
+ return data;
44
+ };
45
+ return data;
46
+ }
47
+ var _utils = require("./utils");
48
+ var _AtlaspackConfigRequest = require("./requests/AtlaspackConfigRequest");
49
+ var _ReporterRunner = _interopRequireDefault(require("./ReporterRunner"));
50
+ var _dumpGraphToGraphViz = _interopRequireDefault(require("./dumpGraphToGraphViz"));
51
+ var _resolveOptions = _interopRequireDefault(require("./resolveOptions"));
52
+ function _events() {
53
+ const data = require("@atlaspack/events");
54
+ _events = function () {
55
+ return data;
56
+ };
57
+ return data;
58
+ }
59
+ var _registerCoreWithSerializer = require("./registerCoreWithSerializer");
60
+ function _utils2() {
61
+ const data = require("@atlaspack/utils");
62
+ _utils2 = function () {
63
+ return data;
64
+ };
65
+ return data;
66
+ }
67
+ var _AtlaspackConfig = _interopRequireDefault(require("./AtlaspackConfig"));
68
+ function _logger() {
69
+ const data = _interopRequireDefault(require("@atlaspack/logger"));
70
+ _logger = function () {
71
+ return data;
72
+ };
73
+ return data;
74
+ }
75
+ var _RequestTracker = _interopRequireWildcard(require("./RequestTracker"));
76
+ var _ValidationRequest = _interopRequireDefault(require("./requests/ValidationRequest"));
77
+ var _AtlaspackBuildRequest = _interopRequireDefault(require("./requests/AtlaspackBuildRequest"));
78
+ var _AssetRequest = _interopRequireDefault(require("./requests/AssetRequest"));
79
+ var _PathRequest = _interopRequireDefault(require("./requests/PathRequest"));
80
+ var _Environment = require("./Environment");
81
+ var _Dependency = require("./Dependency");
82
+ function _sourceMap() {
83
+ const data = require("@parcel/source-map");
84
+ _sourceMap = function () {
85
+ return data;
86
+ };
87
+ return data;
88
+ }
89
+ function _rust() {
90
+ const data = require("@atlaspack/rust");
91
+ _rust = function () {
92
+ return data;
93
+ };
94
+ return data;
95
+ }
96
+ var _projectPath = require("./projectPath");
97
+ function _profiler() {
98
+ const data = require("@atlaspack/profiler");
99
+ _profiler = function () {
100
+ return data;
101
+ };
102
+ return data;
103
+ }
104
+ function _featureFlags() {
105
+ const data = require("@atlaspack/feature-flags");
106
+ _featureFlags = function () {
107
+ return data;
108
+ };
109
+ return data;
110
+ }
111
+ var _atlaspackV = require("./atlaspack-v3");
112
+ 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); }
113
+ 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; }
114
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
115
+ // eslint-disable-next-line no-unused-vars
116
+ (0, _registerCoreWithSerializer.registerCoreWithSerializer)();
117
+ const INTERNAL_TRANSFORM = exports.INTERNAL_TRANSFORM = Symbol('internal_transform');
118
+ const INTERNAL_RESOLVE = exports.INTERNAL_RESOLVE = Symbol('internal_resolve');
119
+ class Atlaspack {
120
+ #requestTracker /*: RequestTracker*/;
121
+ #config /*: AtlaspackConfig*/;
122
+ #farm /*: WorkerFarm*/;
123
+ #initialized /*: boolean*/ = false;
124
+ #disposable /*: Disposable */;
125
+ #initialOptions /*: InitialAtlaspackOptions */;
126
+ #atlaspackV3;
127
+ #reporterRunner /*: ReporterRunner*/;
128
+ #resolvedOptions /*: ?AtlaspackOptions*/ = null;
129
+ #optionsRef /*: SharedReference */;
130
+ #watchAbortController /*: AbortController*/;
131
+ #watchQueue /*: PromiseQueue<?BuildEvent>*/ = new (_utils2().PromiseQueue)({
132
+ maxConcurrent: 1
133
+ });
134
+ #watchEvents /*: ValueEmitter<
135
+ | {|
136
+ +error: Error,
137
+ +buildEvent?: void,
138
+ |}
139
+ | {|
140
+ +buildEvent: BuildEvent,
141
+ +error?: void,
142
+ |},
143
+ > */;
144
+ #watcherSubscription /*: ?AsyncSubscription*/;
145
+ #watcherCount /*: number*/ = 0;
146
+ #requestedAssetIds /*: Set<string>*/ = new Set();
147
+ constructor(options) {
148
+ this.#initialOptions = options;
149
+ }
150
+ async _init() {
151
+ if (this.#initialized) {
152
+ return;
153
+ }
154
+ await _sourceMap().init;
155
+ await (_rust().init === null || _rust().init === void 0 ? void 0 : (0, _rust().init)());
156
+ try {
157
+ _rust().initializeMonitoring === null || _rust().initializeMonitoring === void 0 || (0, _rust().initializeMonitoring)();
158
+ process.on('exit', () => {
159
+ _rust().closeMonitoring === null || _rust().closeMonitoring === void 0 || (0, _rust().closeMonitoring)();
160
+ });
161
+ } catch (e) {
162
+ // Fallthrough
163
+ _logger().default.warn(e);
164
+ }
165
+ let resolvedOptions = await (0, _resolveOptions.default)(this.#initialOptions);
166
+ this.#resolvedOptions = resolvedOptions;
167
+ let rustAtlaspack;
168
+ if (resolvedOptions.featureFlags.atlaspackV3) {
169
+ // eslint-disable-next-line no-unused-vars
170
+ let {
171
+ entries,
172
+ inputFS,
173
+ outputFS,
174
+ ...options
175
+ } = this.#initialOptions;
176
+ rustAtlaspack = new _atlaspackV.AtlaspackV3({
177
+ ...options,
178
+ corePath: _path().default.join(__dirname, '..'),
179
+ entries: Array.isArray(entries) ? entries : entries == null ? undefined : [entries],
180
+ fs: inputFS && (0, _atlaspackV.toFileSystemV3)(inputFS)
181
+ });
182
+ }
183
+ let {
184
+ config
185
+ } = await (0, _AtlaspackConfigRequest.loadAtlaspackConfig)(resolvedOptions);
186
+ this.#config = new _AtlaspackConfig.default(config, resolvedOptions);
187
+ (0, _featureFlags().setFeatureFlags)(resolvedOptions.featureFlags);
188
+ if (this.#initialOptions.workerFarm) {
189
+ if (this.#initialOptions.workerFarm.ending) {
190
+ throw new Error('Supplied WorkerFarm is ending');
191
+ }
192
+ this.#farm = this.#initialOptions.workerFarm;
193
+ } else {
194
+ this.#farm = createWorkerFarm({
195
+ shouldPatchConsole: resolvedOptions.shouldPatchConsole,
196
+ shouldTrace: resolvedOptions.shouldTrace
197
+ });
198
+ }
199
+ await resolvedOptions.cache.ensure();
200
+ let {
201
+ dispose: disposeOptions,
202
+ ref: optionsRef
203
+ } = await this.#farm.createSharedReference(resolvedOptions, false);
204
+ this.#optionsRef = optionsRef;
205
+ this.#disposable = new (_events().Disposable)();
206
+ if (this.#initialOptions.workerFarm) {
207
+ // If we don't own the farm, dispose of only these references when
208
+ // Atlaspack ends.
209
+ this.#disposable.add(disposeOptions);
210
+ } else {
211
+ // Otherwise, when shutting down, end the entire farm we created.
212
+ this.#disposable.add(() => this.#farm.end());
213
+ }
214
+ this.#watchEvents = new (_events().ValueEmitter)();
215
+ this.#disposable.add(() => this.#watchEvents.dispose());
216
+ this.#reporterRunner = new _ReporterRunner.default({
217
+ options: resolvedOptions,
218
+ reporters: await this.#config.getReporters(),
219
+ workerFarm: this.#farm
220
+ });
221
+ this.#disposable.add(this.#reporterRunner);
222
+ _logger().default.verbose({
223
+ origin: '@atlaspack/core',
224
+ message: 'Intializing request tracker...'
225
+ });
226
+ this.#requestTracker = await _RequestTracker.default.init({
227
+ farm: this.#farm,
228
+ options: resolvedOptions,
229
+ rustAtlaspack
230
+ });
231
+ this.#initialized = true;
232
+ }
233
+ async run() {
234
+ let startTime = Date.now();
235
+ if (!this.#initialized) {
236
+ await this._init();
237
+ }
238
+ let result = await this._build({
239
+ startTime
240
+ });
241
+ await this.#requestTracker.writeToCache();
242
+ await this._end();
243
+ if (result.type === 'buildFailure') {
244
+ throw new BuildError(result.diagnostics);
245
+ }
246
+ return result;
247
+ }
248
+ async _end() {
249
+ this.#initialized = false;
250
+ await this.#disposable.dispose();
251
+ }
252
+ async writeRequestTrackerToCache() {
253
+ if (this.#watchQueue.getNumWaiting() === 0) {
254
+ // If there's no queued events, we are safe to write the request graph to disk
255
+ const abortController = new AbortController();
256
+ const unsubscribe = this.#watchQueue.subscribeToAdd(() => {
257
+ abortController.abort();
258
+ });
259
+ try {
260
+ await this.#requestTracker.writeToCache(abortController.signal);
261
+ } catch (err) {
262
+ if (!abortController.signal.aborted) {
263
+ // We expect abort errors if we interrupt the cache write
264
+ throw err;
265
+ }
266
+ }
267
+ unsubscribe();
268
+ }
269
+ }
270
+ async _startNextBuild() {
271
+ this.#watchAbortController = new AbortController();
272
+ await this.#farm.callAllWorkers('clearConfigCache', []);
273
+ try {
274
+ let buildEvent = await this._build({
275
+ signal: this.#watchAbortController.signal
276
+ });
277
+ this.#watchEvents.emit({
278
+ buildEvent
279
+ });
280
+ return buildEvent;
281
+ } catch (err) {
282
+ // Ignore BuildAbortErrors and only emit critical errors.
283
+ if (!(err instanceof _utils.BuildAbortError)) {
284
+ throw err;
285
+ }
286
+ } finally {
287
+ // If the build passes or fails, we want to cache the request graph
288
+ await this.writeRequestTrackerToCache();
289
+ }
290
+ }
291
+ async watch(cb) {
292
+ if (!this.#initialized) {
293
+ await this._init();
294
+ }
295
+ let watchEventsDisposable;
296
+ if (cb) {
297
+ watchEventsDisposable = this.#watchEvents.addListener(({
298
+ error,
299
+ buildEvent
300
+ }) => cb(error, buildEvent));
301
+ }
302
+ if (this.#watcherCount === 0) {
303
+ this.#watcherSubscription = await this._getWatcherSubscription();
304
+ await this.#reporterRunner.report({
305
+ type: 'watchStart'
306
+ });
307
+
308
+ // Kick off a first build, but don't await its results. Its results will
309
+ // be provided to the callback.
310
+ this.#watchQueue.add(() => this._startNextBuild());
311
+ this.#watchQueue.run();
312
+ }
313
+ this.#watcherCount++;
314
+ let unsubscribePromise;
315
+ const unsubscribe = async () => {
316
+ if (watchEventsDisposable) {
317
+ watchEventsDisposable.dispose();
318
+ }
319
+ this.#watcherCount--;
320
+ if (this.#watcherCount === 0) {
321
+ await (0, _nullthrows().default)(this.#watcherSubscription).unsubscribe();
322
+ this.#watcherSubscription = null;
323
+ await this.#reporterRunner.report({
324
+ type: 'watchEnd'
325
+ });
326
+ this.#watchAbortController.abort();
327
+ await this.#watchQueue.run();
328
+ await this._end();
329
+ }
330
+ };
331
+ return {
332
+ unsubscribe() {
333
+ if (unsubscribePromise == null) {
334
+ unsubscribePromise = unsubscribe();
335
+ }
336
+ return unsubscribePromise;
337
+ }
338
+ };
339
+ }
340
+ async _build({
341
+ signal,
342
+ startTime = Date.now()
343
+ } = {
344
+ /*::...null*/
345
+ }) {
346
+ this.#requestTracker.setSignal(signal);
347
+ let options = (0, _nullthrows().default)(this.#resolvedOptions);
348
+ try {
349
+ if (options.shouldProfile) {
350
+ await this.startProfiling();
351
+ }
352
+ if (options.shouldTrace) {
353
+ _profiler().tracer.enable();
354
+ }
355
+ await this.#reporterRunner.report({
356
+ type: 'buildStart'
357
+ });
358
+ this.#requestTracker.graph.invalidateOnBuildNodes();
359
+ let request = (0, _AtlaspackBuildRequest.default)({
360
+ optionsRef: this.#optionsRef,
361
+ requestedAssetIds: this.#requestedAssetIds,
362
+ signal
363
+ });
364
+ let {
365
+ bundleGraph,
366
+ bundleInfo,
367
+ changedAssets,
368
+ assetRequests
369
+ } = await this.#requestTracker.runRequest(request, {
370
+ force: true
371
+ });
372
+ this.#requestedAssetIds.clear();
373
+ await (0, _dumpGraphToGraphViz.default)(
374
+ // $FlowFixMe
375
+ this.#requestTracker.graph, 'RequestGraph', _RequestTracker.requestGraphEdgeTypes);
376
+ let event = {
377
+ type: 'buildSuccess',
378
+ changedAssets: new Map(Array.from(changedAssets).map(([id, asset]) => [id, (0, _Asset.assetFromValue)(asset, options)])),
379
+ bundleGraph: new _BundleGraph.default(bundleGraph, (bundle, bundleGraph, options) => _Bundle.PackagedBundle.getWithInfo(bundle, bundleGraph, options, bundleInfo.get(bundle.id)), options),
380
+ buildTime: Date.now() - startTime,
381
+ requestBundle: async bundle => {
382
+ let bundleNode = bundleGraph._graph.getNodeByContentKey(bundle.id);
383
+ (0, _assert().default)((bundleNode === null || bundleNode === void 0 ? void 0 : bundleNode.type) === 'bundle', 'Bundle does not exist');
384
+ if (!bundleNode.value.isPlaceholder) {
385
+ // Nothing to do.
386
+ return {
387
+ type: 'buildSuccess',
388
+ changedAssets: new Map(),
389
+ bundleGraph: event.bundleGraph,
390
+ buildTime: 0,
391
+ requestBundle: event.requestBundle,
392
+ unstable_requestStats: {}
393
+ };
394
+ }
395
+ for (let assetId of bundleNode.value.entryAssetIds) {
396
+ this.#requestedAssetIds.add(assetId);
397
+ }
398
+ if (this.#watchQueue.getNumWaiting() === 0) {
399
+ if (this.#watchAbortController) {
400
+ this.#watchAbortController.abort();
401
+ }
402
+ this.#watchQueue.add(() => this._startNextBuild());
403
+ }
404
+ let results = await this.#watchQueue.run();
405
+ let result = results.filter(Boolean).pop();
406
+ if (result.type === 'buildFailure') {
407
+ throw new BuildError(result.diagnostics);
408
+ }
409
+ return result;
410
+ },
411
+ unstable_requestStats: this.#requestTracker.flushStats()
412
+ };
413
+ await this.#reporterRunner.report(event);
414
+ await this.#requestTracker.runRequest((0, _ValidationRequest.default)({
415
+ optionsRef: this.#optionsRef,
416
+ assetRequests
417
+ }), {
418
+ force: assetRequests.length > 0
419
+ });
420
+ if (this.#reporterRunner.errors.length) {
421
+ throw this.#reporterRunner.errors;
422
+ }
423
+ return event;
424
+ } catch (e) {
425
+ if (e instanceof _utils.BuildAbortError) {
426
+ throw e;
427
+ }
428
+ let diagnostic = (0, _diagnostic().anyToDiagnostic)(e);
429
+ let event = {
430
+ type: 'buildFailure',
431
+ diagnostics: Array.isArray(diagnostic) ? diagnostic : [diagnostic],
432
+ unstable_requestStats: this.#requestTracker.flushStats()
433
+ };
434
+ await this.#reporterRunner.report(event);
435
+ return event;
436
+ } finally {
437
+ if (this.isProfiling) {
438
+ await this.stopProfiling();
439
+ }
440
+ await this.#farm.callAllWorkers('clearConfigCache', []);
441
+ }
442
+ }
443
+ async _getWatcherSubscription() {
444
+ (0, _assert().default)(this.#watcherSubscription == null);
445
+ let resolvedOptions = (0, _nullthrows().default)(this.#resolvedOptions);
446
+ let opts = (0, _RequestTracker.getWatcherOptions)(resolvedOptions);
447
+ let sub = await resolvedOptions.inputFS.watch(resolvedOptions.watchDir, async (err, events) => {
448
+ var _events$, _events$2;
449
+ if (err) {
450
+ _logger().default.verbose({
451
+ message: `File watch event error occured`,
452
+ meta: {
453
+ err
454
+ }
455
+ });
456
+ this.#watchEvents.emit({
457
+ error: err
458
+ });
459
+ return;
460
+ }
461
+ _logger().default.verbose({
462
+ message: `File watch event emitted with ${events.length} events. Sample event: [${(_events$ = events[0]) === null || _events$ === void 0 ? void 0 : _events$.type}] ${(_events$2 = events[0]) === null || _events$2 === void 0 ? void 0 : _events$2.path}`
463
+ });
464
+ let isInvalid = await this.#requestTracker.respondToFSEvents(events, Number.POSITIVE_INFINITY);
465
+ if (isInvalid && this.#watchQueue.getNumWaiting() === 0) {
466
+ if (this.#watchAbortController) {
467
+ this.#watchAbortController.abort();
468
+ }
469
+ this.#watchQueue.add(() => this._startNextBuild());
470
+ this.#watchQueue.run();
471
+ }
472
+ }, opts);
473
+ return {
474
+ unsubscribe: () => sub.unsubscribe()
475
+ };
476
+ }
477
+
478
+ // This is mainly for integration tests and it not public api!
479
+ _getResolvedAtlaspackOptions() {
480
+ return (0, _nullthrows().default)(this.#resolvedOptions, 'Resolved options is null, please let atlaspack initialize before accessing this.');
481
+ }
482
+ async startProfiling() {
483
+ if (this.isProfiling) {
484
+ throw new Error('Atlaspack is already profiling');
485
+ }
486
+ _logger().default.info({
487
+ origin: '@atlaspack/core',
488
+ message: 'Starting profiling...'
489
+ });
490
+ this.isProfiling = true;
491
+ await this.#farm.startProfile();
492
+ }
493
+ stopProfiling() {
494
+ if (!this.isProfiling) {
495
+ throw new Error('Atlaspack is not profiling');
496
+ }
497
+ _logger().default.info({
498
+ origin: '@atlaspack/core',
499
+ message: 'Stopping profiling...'
500
+ });
501
+ this.isProfiling = false;
502
+ return this.#farm.endProfile();
503
+ }
504
+ takeHeapSnapshot() {
505
+ _logger().default.info({
506
+ origin: '@atlaspack/core',
507
+ message: 'Taking heap snapshot...'
508
+ });
509
+ return this.#farm.takeHeapSnapshot();
510
+ }
511
+ async unstable_transform(options) {
512
+ var _options$env;
513
+ if (!this.#initialized) {
514
+ await this._init();
515
+ }
516
+ let projectRoot = (0, _nullthrows().default)(this.#resolvedOptions).projectRoot;
517
+ let request = (0, _AssetRequest.default)({
518
+ ...options,
519
+ filePath: (0, _projectPath.toProjectPath)(projectRoot, options.filePath),
520
+ optionsRef: this.#optionsRef,
521
+ env: (0, _Environment.createEnvironment)({
522
+ ...options.env,
523
+ loc: ((_options$env = options.env) === null || _options$env === void 0 ? void 0 : _options$env.loc) != null ? {
524
+ ...options.env.loc,
525
+ filePath: (0, _projectPath.toProjectPath)(projectRoot, options.env.loc.filePath)
526
+ } : undefined
527
+ })
528
+ });
529
+ let res = await this.#requestTracker.runRequest(request, {
530
+ force: true
531
+ });
532
+ return res.map(asset => (0, _Asset.assetFromValue)(asset, (0, _nullthrows().default)(this.#resolvedOptions)));
533
+ }
534
+ async unstable_resolve(request) {
535
+ var _request$env;
536
+ if (!this.#initialized) {
537
+ await this._init();
538
+ }
539
+ let projectRoot = (0, _nullthrows().default)(this.#resolvedOptions).projectRoot;
540
+ if (request.resolveFrom == null && _path().default.isAbsolute(request.specifier)) {
541
+ request.specifier = (0, _projectPath.fromProjectPathRelative)((0, _projectPath.toProjectPath)(projectRoot, request.specifier));
542
+ }
543
+ let dependency = (0, _Dependency.createDependency)(projectRoot, {
544
+ ...request,
545
+ env: (0, _Environment.createEnvironment)({
546
+ ...request.env,
547
+ loc: ((_request$env = request.env) === null || _request$env === void 0 ? void 0 : _request$env.loc) != null ? {
548
+ ...request.env.loc,
549
+ filePath: (0, _projectPath.toProjectPath)(projectRoot, request.env.loc.filePath)
550
+ } : undefined
551
+ })
552
+ });
553
+ let req = (0, _PathRequest.default)({
554
+ dependency,
555
+ name: request.specifier
556
+ });
557
+ let res = await this.#requestTracker.runRequest(req, {
558
+ force: true
559
+ });
560
+ if (!res) {
561
+ return null;
562
+ }
563
+ return {
564
+ filePath: (0, _projectPath.fromProjectPath)(projectRoot, res.filePath),
565
+ code: res.code,
566
+ query: res.query,
567
+ sideEffects: res.sideEffects
568
+ };
569
+ }
570
+ }
571
+ exports.default = Atlaspack;
572
+ class BuildError extends _diagnostic().default {
573
+ constructor(diagnostic) {
574
+ super({
575
+ diagnostic
576
+ });
577
+ this.name = 'BuildError';
578
+ }
579
+ }
580
+ exports.BuildError = BuildError;
581
+ function createWorkerFarm(options = {}) {
582
+ return new (_workers().default)({
583
+ ...options,
584
+ // $FlowFixMe
585
+ workerPath: process.browser ? '@atlaspack/core/src/worker.js' : require.resolve('./worker')
586
+ });
587
+ }