@atlaspack/core 2.24.0 → 2.24.2-dev-ts-project-refs-d30e9754f.0

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 (92) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/LICENSE +201 -0
  3. package/dist/AssetGraph.js +591 -0
  4. package/dist/Atlaspack.js +656 -0
  5. package/dist/AtlaspackConfig.js +324 -0
  6. package/dist/AtlaspackConfig.schema.js +108 -0
  7. package/dist/BundleGraph.js +1628 -0
  8. package/dist/CommittedAsset.js +142 -0
  9. package/dist/Dependency.js +125 -0
  10. package/dist/Environment.js +132 -0
  11. package/dist/EnvironmentManager.js +108 -0
  12. package/dist/IdentifierRegistry.js +38 -0
  13. package/dist/InternalConfig.js +37 -0
  14. package/dist/PackagerRunner.js +531 -0
  15. package/dist/ReporterRunner.js +151 -0
  16. package/dist/RequestTracker.js +1368 -0
  17. package/dist/SymbolPropagation.js +620 -0
  18. package/dist/TargetDescriptor.schema.js +143 -0
  19. package/dist/Transformation.js +487 -0
  20. package/dist/UncommittedAsset.js +315 -0
  21. package/dist/Validation.js +196 -0
  22. package/dist/applyRuntimes.js +305 -0
  23. package/dist/assetUtils.js +168 -0
  24. package/dist/atlaspack-v3/AtlaspackV3.js +70 -0
  25. package/dist/atlaspack-v3/NapiWorkerPool.js +57 -0
  26. package/dist/atlaspack-v3/fs.js +52 -0
  27. package/dist/atlaspack-v3/index.js +25 -0
  28. package/dist/atlaspack-v3/jsCallable.js +16 -0
  29. package/dist/atlaspack-v3/worker/compat/asset-symbols.js +190 -0
  30. package/dist/atlaspack-v3/worker/compat/bitflags.js +94 -0
  31. package/dist/atlaspack-v3/worker/compat/dependency.js +43 -0
  32. package/dist/atlaspack-v3/worker/compat/environment.js +57 -0
  33. package/dist/atlaspack-v3/worker/compat/index.js +25 -0
  34. package/dist/atlaspack-v3/worker/compat/mutable-asset.js +152 -0
  35. package/dist/atlaspack-v3/worker/compat/plugin-config.js +76 -0
  36. package/dist/atlaspack-v3/worker/compat/plugin-logger.js +26 -0
  37. package/dist/atlaspack-v3/worker/compat/plugin-options.js +122 -0
  38. package/dist/atlaspack-v3/worker/compat/plugin-tracer.js +10 -0
  39. package/dist/atlaspack-v3/worker/compat/target.js +14 -0
  40. package/dist/atlaspack-v3/worker/worker.js +292 -0
  41. package/dist/constants.js +17 -0
  42. package/dist/dumpGraphToGraphViz.js +281 -0
  43. package/dist/index.js +62 -0
  44. package/dist/loadAtlaspackPlugin.js +128 -0
  45. package/dist/loadDotEnv.js +41 -0
  46. package/dist/projectPath.js +83 -0
  47. package/dist/public/Asset.js +279 -0
  48. package/dist/public/Bundle.js +224 -0
  49. package/dist/public/BundleGraph.js +359 -0
  50. package/dist/public/BundleGroup.js +53 -0
  51. package/dist/public/Config.js +286 -0
  52. package/dist/public/Dependency.js +138 -0
  53. package/dist/public/Environment.js +278 -0
  54. package/dist/public/MutableBundleGraph.js +277 -0
  55. package/dist/public/PluginOptions.js +80 -0
  56. package/dist/public/Symbols.js +248 -0
  57. package/dist/public/Target.js +69 -0
  58. package/dist/registerCoreWithSerializer.js +38 -0
  59. package/dist/requests/AssetGraphRequest.js +429 -0
  60. package/dist/requests/AssetGraphRequestRust.js +246 -0
  61. package/dist/requests/AssetRequest.js +130 -0
  62. package/dist/requests/AtlaspackBuildRequest.js +60 -0
  63. package/dist/requests/AtlaspackConfigRequest.js +490 -0
  64. package/dist/requests/BundleGraphRequest.js +441 -0
  65. package/dist/requests/ConfigRequest.js +222 -0
  66. package/dist/requests/DevDepRequest.js +204 -0
  67. package/dist/requests/EntryRequest.js +314 -0
  68. package/dist/requests/PackageRequest.js +65 -0
  69. package/dist/requests/PathRequest.js +349 -0
  70. package/dist/requests/TargetRequest.js +1310 -0
  71. package/dist/requests/ValidationRequest.js +49 -0
  72. package/dist/requests/WriteBundleRequest.js +254 -0
  73. package/dist/requests/WriteBundlesRequest.js +165 -0
  74. package/dist/requests/asset-graph-diff.js +126 -0
  75. package/dist/requests/asset-graph-dot.js +131 -0
  76. package/dist/resolveOptions.js +268 -0
  77. package/dist/rustWorkerThreadDylibHack.js +19 -0
  78. package/dist/serializerCore.browser.js +43 -0
  79. package/dist/summarizeRequest.js +39 -0
  80. package/dist/types.js +31 -0
  81. package/dist/utils.js +172 -0
  82. package/dist/worker.js +130 -0
  83. package/lib/AssetGraph.js +1 -0
  84. package/lib/atlaspack-v3/AtlaspackV3.js +7 -3
  85. package/lib/requests/AssetGraphRequestRust.js +1 -1
  86. package/lib/types/atlaspack-v3/AtlaspackV3.d.ts +1 -1
  87. package/package.json +22 -22
  88. package/src/AssetGraph.ts +1 -0
  89. package/src/atlaspack-v3/AtlaspackV3.ts +12 -3
  90. package/src/requests/AssetGraphRequestRust.ts +1 -1
  91. package/tsconfig.json +55 -2
  92. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,656 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
36
+ if (kind === "m") throw new TypeError("Private method is not writable");
37
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
38
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
39
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
40
+ };
41
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
42
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
43
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
44
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
45
+ };
46
+ var __importDefault = (this && this.__importDefault) || function (mod) {
47
+ return (mod && mod.__esModule) ? mod : { "default": mod };
48
+ };
49
+ var _Atlaspack_requestTracker, _Atlaspack_config, _Atlaspack_farm, _Atlaspack_initialized, _Atlaspack_disposable, _Atlaspack_initialOptions, _Atlaspack_reporterRunner, _Atlaspack_resolvedOptions, _Atlaspack_optionsRef, _Atlaspack_watchAbortController, _Atlaspack_watchQueue, _Atlaspack_watchEvents, _Atlaspack_watcherSubscription, _Atlaspack_watcherCount, _Atlaspack_requestedAssetIds;
50
+ Object.defineProperty(exports, "__esModule", { value: true });
51
+ exports.BuildError = exports.WORKER_PATH = exports.INTERNAL_RESOLVE = exports.INTERNAL_TRANSFORM = void 0;
52
+ exports.createWorkerFarm = createWorkerFarm;
53
+ const path_1 = __importDefault(require("path"));
54
+ const assert_1 = __importDefault(require("assert"));
55
+ const diagnostic_1 = __importStar(require("@atlaspack/diagnostic"));
56
+ const Asset_1 = require("./public/Asset");
57
+ const Bundle_1 = require("./public/Bundle");
58
+ const BundleGraph_1 = __importDefault(require("./public/BundleGraph"));
59
+ const workers_1 = __importDefault(require("@atlaspack/workers"));
60
+ const nullthrows_1 = __importDefault(require("nullthrows"));
61
+ const utils_1 = require("./utils");
62
+ const AtlaspackConfigRequest_1 = require("./requests/AtlaspackConfigRequest");
63
+ const ReporterRunner_1 = __importDefault(require("./ReporterRunner"));
64
+ const dumpGraphToGraphViz_1 = __importDefault(require("./dumpGraphToGraphViz"));
65
+ const resolveOptions_1 = __importDefault(require("./resolveOptions"));
66
+ const events_1 = require("@atlaspack/events");
67
+ const registerCoreWithSerializer_1 = require("./registerCoreWithSerializer");
68
+ const utils_2 = require("@atlaspack/utils");
69
+ const AtlaspackConfig_1 = require("./AtlaspackConfig");
70
+ const logger_1 = __importDefault(require("@atlaspack/logger"));
71
+ const RequestTracker_1 = __importStar(require("./RequestTracker"));
72
+ const ValidationRequest_1 = __importDefault(require("./requests/ValidationRequest"));
73
+ const AtlaspackBuildRequest_1 = __importDefault(require("./requests/AtlaspackBuildRequest"));
74
+ const AssetRequest_1 = __importDefault(require("./requests/AssetRequest"));
75
+ const PathRequest_1 = __importDefault(require("./requests/PathRequest"));
76
+ const Environment_1 = require("./Environment");
77
+ const Dependency_1 = require("./Dependency");
78
+ const events_2 = require("@atlaspack/events");
79
+ const source_map_1 = require("@parcel/source-map");
80
+ const cache_1 = require("@atlaspack/cache");
81
+ const rust_1 = require("@atlaspack/rust");
82
+ const projectPath_1 = require("./projectPath");
83
+ const profiler_1 = require("@atlaspack/profiler");
84
+ const feature_flags_1 = require("@atlaspack/feature-flags");
85
+ const atlaspack_v3_1 = require("./atlaspack-v3");
86
+ const AssetGraphRequest_1 = __importDefault(require("./requests/AssetGraphRequest"));
87
+ const AssetGraphRequestRust_1 = require("./requests/AssetGraphRequestRust");
88
+ const rustWorkerThreadDylibHack_1 = require("./rustWorkerThreadDylibHack");
89
+ (0, registerCoreWithSerializer_1.registerCoreWithSerializer)();
90
+ exports.INTERNAL_TRANSFORM = Symbol('internal_transform');
91
+ exports.INTERNAL_RESOLVE = Symbol('internal_resolve');
92
+ exports.WORKER_PATH = path_1.default.join(__dirname, 'worker.js');
93
+ if (__filename.endsWith('.ts')) {
94
+ exports.WORKER_PATH = path_1.default.join(__dirname, 'worker.ts');
95
+ }
96
+ class Atlaspack {
97
+ constructor(options) {
98
+ // @ts-expect-error TS2564
99
+ _Atlaspack_requestTracker.set(this, void 0);
100
+ // @ts-expect-error TS2564
101
+ _Atlaspack_config.set(this, void 0);
102
+ // @ts-expect-error TS2564
103
+ _Atlaspack_farm.set(this, void 0);
104
+ _Atlaspack_initialized.set(this, false);
105
+ // @ts-expect-error TS2564
106
+ _Atlaspack_disposable.set(this, void 0);
107
+ _Atlaspack_initialOptions.set(this, void 0);
108
+ // @ts-expect-error TS2564
109
+ _Atlaspack_reporterRunner.set(this, void 0);
110
+ _Atlaspack_resolvedOptions.set(this, null);
111
+ // @ts-expect-error TS2564
112
+ _Atlaspack_optionsRef.set(this, void 0);
113
+ // @ts-expect-error TS2564
114
+ _Atlaspack_watchAbortController.set(this, void 0);
115
+ _Atlaspack_watchQueue.set(this, new utils_2.PromiseQueue({
116
+ maxConcurrent: 1,
117
+ }));
118
+ // @ts-expect-error TS2564
119
+ _Atlaspack_watchEvents.set(this, void 0);
120
+ _Atlaspack_watcherSubscription.set(this, void 0);
121
+ _Atlaspack_watcherCount.set(this, 0);
122
+ _Atlaspack_requestedAssetIds.set(this, new Set());
123
+ __classPrivateFieldSet(this, _Atlaspack_initialOptions, options, "f");
124
+ }
125
+ async _init() {
126
+ if (__classPrivateFieldGet(this, _Atlaspack_initialized, "f")) {
127
+ return;
128
+ }
129
+ const featureFlags = {
130
+ ...feature_flags_1.DEFAULT_FEATURE_FLAGS,
131
+ ...__classPrivateFieldGet(this, _Atlaspack_initialOptions, "f").featureFlags,
132
+ };
133
+ (0, feature_flags_1.setFeatureFlags)(featureFlags);
134
+ (0, rustWorkerThreadDylibHack_1.loadRustWorkerThreadDylibHack)();
135
+ await source_map_1.init;
136
+ await (0, rust_1.init)?.();
137
+ __classPrivateFieldSet(this, _Atlaspack_disposable, new events_2.Disposable(), "f");
138
+ try {
139
+ (0, rust_1.initializeMonitoring)?.();
140
+ const onExit = () => {
141
+ (0, rust_1.closeMonitoring)?.();
142
+ };
143
+ process.on('exit', onExit);
144
+ __classPrivateFieldGet(this, _Atlaspack_disposable, "f").add(() => {
145
+ process.off('exit', onExit);
146
+ onExit();
147
+ });
148
+ }
149
+ catch (e) {
150
+ // Fallthrough
151
+ // eslint-disable-next-line no-console
152
+ console.warn(e);
153
+ }
154
+ let resolvedOptions = await (0, resolveOptions_1.default)({
155
+ ...__classPrivateFieldGet(this, _Atlaspack_initialOptions, "f"),
156
+ featureFlags,
157
+ });
158
+ __classPrivateFieldSet(this, _Atlaspack_resolvedOptions, resolvedOptions, "f");
159
+ let rustAtlaspack;
160
+ if (resolvedOptions.featureFlags.atlaspackV3) {
161
+ // eslint-disable-next-line no-unused-vars
162
+ let { entries, inputFS, outputFS, ...options } = __classPrivateFieldGet(this, _Atlaspack_initialOptions, "f");
163
+ if (!(resolvedOptions.cache instanceof cache_1.LMDBLiteCache)) {
164
+ throw new Error('Atlaspack v3 must be run with lmdb lite cache');
165
+ }
166
+ const lmdb = resolvedOptions.cache.getNativeRef();
167
+ const version = require('../package.json').version;
168
+ await lmdb.put('current_session_version', Buffer.from(version));
169
+ let threads = undefined;
170
+ if (process.env.ATLASPACK_NATIVE_THREADS !== undefined) {
171
+ threads = parseInt(process.env.ATLASPACK_NATIVE_THREADS, 10);
172
+ }
173
+ else if (process.env.NODE_ENV === 'test') {
174
+ threads = 2;
175
+ }
176
+ rustAtlaspack = await atlaspack_v3_1.AtlaspackV3.create({
177
+ ...options,
178
+ // @ts-expect-error TS2353
179
+ corePath: path_1.default.join(__dirname, '..'),
180
+ threads,
181
+ entries: Array.isArray(entries)
182
+ ? entries
183
+ : entries == null
184
+ ? undefined
185
+ : [entries],
186
+ env: resolvedOptions.env,
187
+ fs: inputFS && new atlaspack_v3_1.FileSystemV3(inputFS),
188
+ defaultTargetOptions: resolvedOptions.defaultTargetOptions,
189
+ lmdb,
190
+ featureFlags: resolvedOptions.featureFlags,
191
+ });
192
+ __classPrivateFieldGet(this, _Atlaspack_disposable, "f").add(() => {
193
+ rustAtlaspack.end();
194
+ });
195
+ }
196
+ // @ts-expect-error TS2454
197
+ this.rustAtlaspack = rustAtlaspack;
198
+ let { config } = await (0, AtlaspackConfigRequest_1.loadAtlaspackConfig)(resolvedOptions);
199
+ __classPrivateFieldSet(this, _Atlaspack_config, new AtlaspackConfig_1.AtlaspackConfig(config, resolvedOptions), "f");
200
+ if (__classPrivateFieldGet(this, _Atlaspack_initialOptions, "f").workerFarm) {
201
+ if (__classPrivateFieldGet(this, _Atlaspack_initialOptions, "f").workerFarm.ending) {
202
+ throw new Error('Supplied WorkerFarm is ending');
203
+ }
204
+ __classPrivateFieldSet(this, _Atlaspack_farm, __classPrivateFieldGet(this, _Atlaspack_initialOptions, "f").workerFarm, "f");
205
+ }
206
+ else {
207
+ __classPrivateFieldSet(this, _Atlaspack_farm, createWorkerFarm({
208
+ shouldPatchConsole: resolvedOptions.shouldPatchConsole,
209
+ shouldTrace: resolvedOptions.shouldTrace,
210
+ }), "f");
211
+ }
212
+ await resolvedOptions.cache.ensure();
213
+ let { dispose: disposeOptions, ref: optionsRef } = await __classPrivateFieldGet(this, _Atlaspack_farm, "f").createSharedReference(resolvedOptions, false);
214
+ __classPrivateFieldSet(this, _Atlaspack_optionsRef, optionsRef, "f");
215
+ if (__classPrivateFieldGet(this, _Atlaspack_initialOptions, "f").workerFarm) {
216
+ // If we don't own the farm, dispose of only these references when
217
+ // Atlaspack ends.
218
+ __classPrivateFieldGet(this, _Atlaspack_disposable, "f").add(disposeOptions);
219
+ }
220
+ else {
221
+ // Otherwise, when shutting down, end the entire farm we created.
222
+ __classPrivateFieldGet(this, _Atlaspack_disposable, "f").add(() => __classPrivateFieldGet(this, _Atlaspack_farm, "f").end());
223
+ }
224
+ __classPrivateFieldSet(this, _Atlaspack_watchEvents, new events_1.ValueEmitter(), "f");
225
+ __classPrivateFieldGet(this, _Atlaspack_disposable, "f").add(() => __classPrivateFieldGet(this, _Atlaspack_watchEvents, "f").dispose());
226
+ __classPrivateFieldSet(this, _Atlaspack_reporterRunner, new ReporterRunner_1.default({
227
+ options: resolvedOptions,
228
+ reporters: await __classPrivateFieldGet(this, _Atlaspack_config, "f").getReporters(),
229
+ workerFarm: __classPrivateFieldGet(this, _Atlaspack_farm, "f"),
230
+ }), "f");
231
+ __classPrivateFieldGet(this, _Atlaspack_disposable, "f").add(__classPrivateFieldGet(this, _Atlaspack_reporterRunner, "f"));
232
+ logger_1.default.verbose({
233
+ origin: '@atlaspack/core',
234
+ message: 'Intializing request tracker...',
235
+ });
236
+ __classPrivateFieldSet(this, _Atlaspack_requestTracker, await RequestTracker_1.default.init({
237
+ farm: __classPrivateFieldGet(this, _Atlaspack_farm, "f"),
238
+ options: resolvedOptions,
239
+ // @ts-expect-error TS2454
240
+ rustAtlaspack,
241
+ }), "f");
242
+ __classPrivateFieldSet(this, _Atlaspack_initialized, true, "f");
243
+ }
244
+ async run() {
245
+ let startTime = Date.now();
246
+ if (!__classPrivateFieldGet(this, _Atlaspack_initialized, "f")) {
247
+ await this._init();
248
+ }
249
+ let result = await this._build({ startTime });
250
+ await __classPrivateFieldGet(this, _Atlaspack_requestTracker, "f").writeToCache();
251
+ await this._end();
252
+ if (result.type === 'buildFailure') {
253
+ throw new BuildError(result.diagnostics);
254
+ }
255
+ return result;
256
+ }
257
+ async _end() {
258
+ __classPrivateFieldSet(this, _Atlaspack_initialized, false, "f");
259
+ await __classPrivateFieldGet(this, _Atlaspack_disposable, "f").dispose();
260
+ }
261
+ async writeRequestTrackerToCache() {
262
+ if (__classPrivateFieldGet(this, _Atlaspack_watchQueue, "f").getNumWaiting() === 0) {
263
+ // If there's no queued events, we are safe to write the request graph to disk
264
+ const abortController = new AbortController();
265
+ const unsubscribe = __classPrivateFieldGet(this, _Atlaspack_watchQueue, "f").subscribeToAdd(() => {
266
+ abortController.abort();
267
+ });
268
+ try {
269
+ await __classPrivateFieldGet(this, _Atlaspack_requestTracker, "f").writeToCache(abortController.signal);
270
+ }
271
+ catch (err) {
272
+ if (!abortController.signal.aborted) {
273
+ // We expect abort errors if we interrupt the cache write
274
+ throw err;
275
+ }
276
+ }
277
+ unsubscribe();
278
+ }
279
+ }
280
+ async _startNextBuild() {
281
+ __classPrivateFieldSet(this, _Atlaspack_watchAbortController, new AbortController(), "f");
282
+ await this.clearBuildCaches();
283
+ try {
284
+ let buildEvent = await this._build({
285
+ signal: __classPrivateFieldGet(this, _Atlaspack_watchAbortController, "f").signal,
286
+ });
287
+ __classPrivateFieldGet(this, _Atlaspack_watchEvents, "f").emit({
288
+ buildEvent,
289
+ });
290
+ return buildEvent;
291
+ }
292
+ catch (err) {
293
+ // Ignore BuildAbortErrors and only emit critical errors.
294
+ if (!(err instanceof utils_1.BuildAbortError)) {
295
+ throw err;
296
+ }
297
+ }
298
+ finally {
299
+ // If the build passes or fails, we want to cache the request graph
300
+ await this.writeRequestTrackerToCache();
301
+ }
302
+ }
303
+ async watch(cb) {
304
+ var _a;
305
+ if (!__classPrivateFieldGet(this, _Atlaspack_initialized, "f")) {
306
+ await this._init();
307
+ }
308
+ // @ts-expect-error TS7034
309
+ let watchEventsDisposable;
310
+ if (cb) {
311
+ watchEventsDisposable = __classPrivateFieldGet(this, _Atlaspack_watchEvents, "f").addListener(({ error, buildEvent }) => cb(error, buildEvent));
312
+ }
313
+ if (__classPrivateFieldGet(this, _Atlaspack_watcherCount, "f") === 0) {
314
+ __classPrivateFieldSet(this, _Atlaspack_watcherSubscription, await this._getWatcherSubscription(), "f");
315
+ await __classPrivateFieldGet(this, _Atlaspack_reporterRunner, "f").report({ type: 'watchStart' });
316
+ // Kick off a first build, but don't await its results. Its results will
317
+ // be provided to the callback.
318
+ __classPrivateFieldGet(this, _Atlaspack_watchQueue, "f").add(() => this._startNextBuild());
319
+ __classPrivateFieldGet(this, _Atlaspack_watchQueue, "f").run();
320
+ }
321
+ __classPrivateFieldSet(this, _Atlaspack_watcherCount, (_a = __classPrivateFieldGet(this, _Atlaspack_watcherCount, "f"), _a++, _a), "f");
322
+ // @ts-expect-error TS7034
323
+ let unsubscribePromise;
324
+ const unsubscribe = async () => {
325
+ var _a;
326
+ // @ts-expect-error TS7005
327
+ if (watchEventsDisposable) {
328
+ watchEventsDisposable.dispose();
329
+ }
330
+ __classPrivateFieldSet(this, _Atlaspack_watcherCount, (_a = __classPrivateFieldGet(this, _Atlaspack_watcherCount, "f"), _a--, _a), "f");
331
+ if (__classPrivateFieldGet(this, _Atlaspack_watcherCount, "f") === 0) {
332
+ await (0, nullthrows_1.default)(__classPrivateFieldGet(this, _Atlaspack_watcherSubscription, "f")).unsubscribe();
333
+ __classPrivateFieldSet(this, _Atlaspack_watcherSubscription, null, "f");
334
+ await __classPrivateFieldGet(this, _Atlaspack_reporterRunner, "f").report({ type: 'watchEnd' });
335
+ __classPrivateFieldGet(this, _Atlaspack_watchAbortController, "f").abort();
336
+ await __classPrivateFieldGet(this, _Atlaspack_watchQueue, "f").run();
337
+ await this._end();
338
+ }
339
+ };
340
+ return {
341
+ unsubscribe() {
342
+ // @ts-expect-error TS7005
343
+ if (unsubscribePromise == null) {
344
+ unsubscribePromise = unsubscribe();
345
+ }
346
+ // @ts-expect-error TS7005
347
+ return unsubscribePromise;
348
+ },
349
+ };
350
+ }
351
+ async _build({ signal, startTime = Date.now(), } = {
352
+ /*::...null*/
353
+ }) {
354
+ let options = (0, nullthrows_1.default)(__classPrivateFieldGet(this, _Atlaspack_resolvedOptions, "f"));
355
+ try {
356
+ if (options.shouldProfile) {
357
+ await this.startProfiling();
358
+ }
359
+ if (options.shouldTrace) {
360
+ profiler_1.tracer.enable();
361
+ // We need to ensure the tracer is disabled when Atlaspack is disposed as it is a module level object.
362
+ // While rare (except for tests), if another instance is created later it should not have tracing enabled.
363
+ __classPrivateFieldGet(this, _Atlaspack_disposable, "f").add(() => {
364
+ profiler_1.tracer.disable();
365
+ });
366
+ }
367
+ await __classPrivateFieldGet(this, _Atlaspack_reporterRunner, "f").report({
368
+ type: 'buildStart',
369
+ });
370
+ __classPrivateFieldGet(this, _Atlaspack_requestTracker, "f").graph.invalidateOnBuildNodes();
371
+ let request = (0, AtlaspackBuildRequest_1.default)({
372
+ optionsRef: __classPrivateFieldGet(this, _Atlaspack_optionsRef, "f"),
373
+ requestedAssetIds: __classPrivateFieldGet(this, _Atlaspack_requestedAssetIds, "f"),
374
+ signal,
375
+ });
376
+ let { bundleGraph, bundleInfo, changedAssets, assetRequests } = await __classPrivateFieldGet(this, _Atlaspack_requestTracker, "f").runRequest(request, { force: true });
377
+ __classPrivateFieldGet(this, _Atlaspack_requestedAssetIds, "f").clear();
378
+ await (0, dumpGraphToGraphViz_1.default)(
379
+ // @ts-expect-error TS2345
380
+ __classPrivateFieldGet(this, _Atlaspack_requestTracker, "f").graph, 'RequestGraph', RequestTracker_1.requestGraphEdgeTypes);
381
+ let event = {
382
+ type: 'buildSuccess',
383
+ changedAssets: new Map(Array.from(changedAssets).map(([id, asset]) => [
384
+ id,
385
+ (0, Asset_1.assetFromValue)(asset, options),
386
+ ])),
387
+ bundleGraph: new BundleGraph_1.default(bundleGraph, (
388
+ // @ts-expect-error TS2304
389
+ bundle,
390
+ // @ts-expect-error TS2314
391
+ bundleGraph, options) => Bundle_1.PackagedBundle.getWithInfo(bundle, bundleGraph, options, bundleInfo.get(bundle.id)), options),
392
+ buildTime: Date.now() - startTime,
393
+ // @ts-expect-error TS7006
394
+ requestBundle: async (bundle) => {
395
+ let bundleNode = bundleGraph._graph.getNodeByContentKey(bundle.id);
396
+ (0, assert_1.default)(bundleNode?.type === 'bundle', 'Bundle does not exist');
397
+ if (!bundleNode.value.isPlaceholder) {
398
+ // Nothing to do.
399
+ return {
400
+ type: 'buildSuccess',
401
+ changedAssets: new Map(),
402
+ bundleGraph: event.bundleGraph,
403
+ buildTime: 0,
404
+ requestBundle: event.requestBundle,
405
+ unstable_requestStats: {},
406
+ };
407
+ }
408
+ for (let assetId of bundleNode.value.entryAssetIds) {
409
+ __classPrivateFieldGet(this, _Atlaspack_requestedAssetIds, "f").add(assetId);
410
+ }
411
+ if (__classPrivateFieldGet(this, _Atlaspack_watchQueue, "f").getNumWaiting() === 0) {
412
+ if (__classPrivateFieldGet(this, _Atlaspack_watchAbortController, "f")) {
413
+ __classPrivateFieldGet(this, _Atlaspack_watchAbortController, "f").abort();
414
+ }
415
+ __classPrivateFieldGet(this, _Atlaspack_watchQueue, "f").add(() => this._startNextBuild());
416
+ }
417
+ let results = await __classPrivateFieldGet(this, _Atlaspack_watchQueue, "f").run();
418
+ let result = results.filter(Boolean).pop();
419
+ // @ts-expect-error TS18049
420
+ if (result.type === 'buildFailure') {
421
+ // @ts-expect-error TS18049
422
+ throw new BuildError(result.diagnostics);
423
+ }
424
+ return result;
425
+ },
426
+ unstable_requestStats: __classPrivateFieldGet(this, _Atlaspack_requestTracker, "f").flushStats(),
427
+ };
428
+ // @ts-expect-error TS2345
429
+ await __classPrivateFieldGet(this, _Atlaspack_reporterRunner, "f").report(event);
430
+ await __classPrivateFieldGet(this, _Atlaspack_requestTracker, "f").runRequest(
431
+ // @ts-expect-error TS2345
432
+ (0, ValidationRequest_1.default)({ optionsRef: __classPrivateFieldGet(this, _Atlaspack_optionsRef, "f"), assetRequests }), { force: assetRequests.length > 0 });
433
+ if (__classPrivateFieldGet(this, _Atlaspack_reporterRunner, "f").errors.length) {
434
+ throw __classPrivateFieldGet(this, _Atlaspack_reporterRunner, "f").errors;
435
+ }
436
+ // @ts-expect-error TS2322
437
+ return event;
438
+ }
439
+ catch (e) {
440
+ if (e instanceof utils_1.BuildAbortError) {
441
+ throw e;
442
+ }
443
+ let diagnostic = (0, diagnostic_1.anyToDiagnostic)(e);
444
+ let event = {
445
+ type: 'buildFailure',
446
+ diagnostics: Array.isArray(diagnostic) ? diagnostic : [diagnostic],
447
+ unstable_requestStats: __classPrivateFieldGet(this, _Atlaspack_requestTracker, "f").flushStats(),
448
+ };
449
+ // @ts-expect-error TS2345
450
+ await __classPrivateFieldGet(this, _Atlaspack_reporterRunner, "f").report(event);
451
+ // @ts-expect-error TS2322
452
+ return event;
453
+ }
454
+ finally {
455
+ if (this.isProfiling) {
456
+ await this.stopProfiling();
457
+ }
458
+ await this.clearBuildCaches();
459
+ }
460
+ }
461
+ async _getWatcherSubscription() {
462
+ (0, assert_1.default)(__classPrivateFieldGet(this, _Atlaspack_watcherSubscription, "f") == null);
463
+ let resolvedOptions = (0, nullthrows_1.default)(__classPrivateFieldGet(this, _Atlaspack_resolvedOptions, "f"));
464
+ let opts = (0, RequestTracker_1.getWatcherOptions)(resolvedOptions);
465
+ let sub = await resolvedOptions.inputFS.watch(resolvedOptions.watchDir, async (err, events) => {
466
+ if (err) {
467
+ logger_1.default.warn({
468
+ message: `File watch event error occured`,
469
+ meta: {
470
+ err,
471
+ trackableEvent: 'watcher_error',
472
+ },
473
+ });
474
+ __classPrivateFieldGet(this, _Atlaspack_watchEvents, "f").emit({ error: err });
475
+ return;
476
+ }
477
+ logger_1.default.verbose({
478
+ message: `File watch event emitted with ${events.length} events. Sample event: [${events[0]?.type}] ${events[0]?.path}`,
479
+ });
480
+ let nativeInvalid = false;
481
+ if (this.rustAtlaspack) {
482
+ nativeInvalid = await this.rustAtlaspack.respondToFsEvents(events);
483
+ }
484
+ let { didInvalidate: isInvalid } = await __classPrivateFieldGet(this, _Atlaspack_requestTracker, "f").respondToFSEvents(events, Number.POSITIVE_INFINITY);
485
+ if ((nativeInvalid || isInvalid) &&
486
+ __classPrivateFieldGet(this, _Atlaspack_watchQueue, "f").getNumWaiting() === 0) {
487
+ if (__classPrivateFieldGet(this, _Atlaspack_watchAbortController, "f")) {
488
+ __classPrivateFieldGet(this, _Atlaspack_watchAbortController, "f").abort();
489
+ }
490
+ __classPrivateFieldGet(this, _Atlaspack_watchQueue, "f").add(() => this._startNextBuild());
491
+ __classPrivateFieldGet(this, _Atlaspack_watchQueue, "f").run();
492
+ }
493
+ }, opts);
494
+ return { unsubscribe: () => sub.unsubscribe() };
495
+ }
496
+ // This is mainly for integration tests and it not public api!
497
+ _getResolvedAtlaspackOptions() {
498
+ return (0, nullthrows_1.default)(__classPrivateFieldGet(this, _Atlaspack_resolvedOptions, "f"), 'Resolved options is null, please let atlaspack initialize before accessing this.');
499
+ }
500
+ async startProfiling() {
501
+ if (this.isProfiling) {
502
+ throw new Error('Atlaspack is already profiling');
503
+ }
504
+ logger_1.default.info({ origin: '@atlaspack/core', message: 'Starting profiling...' });
505
+ this.isProfiling = true;
506
+ await __classPrivateFieldGet(this, _Atlaspack_farm, "f").startProfile();
507
+ }
508
+ stopProfiling() {
509
+ if (!this.isProfiling) {
510
+ throw new Error('Atlaspack is not profiling');
511
+ }
512
+ logger_1.default.info({ origin: '@atlaspack/core', message: 'Stopping profiling...' });
513
+ this.isProfiling = false;
514
+ return __classPrivateFieldGet(this, _Atlaspack_farm, "f").endProfile();
515
+ }
516
+ takeHeapSnapshot() {
517
+ logger_1.default.info({
518
+ origin: '@atlaspack/core',
519
+ message: 'Taking heap snapshot...',
520
+ });
521
+ return __classPrivateFieldGet(this, _Atlaspack_farm, "f").takeHeapSnapshot();
522
+ }
523
+ /**
524
+ * Must be called between builds otherwise there is global state that will
525
+ * break things unexpectedly.
526
+ */
527
+ async clearBuildCaches() {
528
+ await __classPrivateFieldGet(this, _Atlaspack_farm, "f")?.callAllWorkers('clearWorkerBuildCaches', []);
529
+ }
530
+ async unstable_invalidate() {
531
+ await this._init();
532
+ }
533
+ /**
534
+ * Build the asset graph
535
+ */
536
+ async unstable_buildAssetGraph(writeToCache = true) {
537
+ await this._init();
538
+ const origin = '@atlaspack/core';
539
+ const input = {
540
+ optionsRef: __classPrivateFieldGet(this, _Atlaspack_optionsRef, "f"),
541
+ name: 'Main',
542
+ entries: __classPrivateFieldGet(this, _Atlaspack_config, "f").options.entries,
543
+ shouldBuildLazily: false,
544
+ lazyIncludes: [],
545
+ lazyExcludes: [],
546
+ requestedAssetIds: __classPrivateFieldGet(this, _Atlaspack_requestedAssetIds, "f"),
547
+ };
548
+ const start = Date.now();
549
+ const result = await __classPrivateFieldGet(this, _Atlaspack_requestTracker, "f").runRequest(this.rustAtlaspack != null
550
+ ? // @ts-expect-error TS2345
551
+ (0, AssetGraphRequestRust_1.createAssetGraphRequestRust)(this.rustAtlaspack)(input)
552
+ : // @ts-expect-error TS2345
553
+ (0, AssetGraphRequest_1.default)(input), { force: true });
554
+ const duration = Date.now() - start;
555
+ logger_1.default.info({
556
+ message: `Done building asset graph in ${duration / 1000}s!`,
557
+ origin,
558
+ });
559
+ if (writeToCache) {
560
+ logger_1.default.info({ message: 'Write request tracker to cache', origin });
561
+ await this.writeRequestTrackerToCache();
562
+ logger_1.default.info({ message: 'Done writing request tracker to cache', origin });
563
+ }
564
+ return result;
565
+ }
566
+ /**
567
+ * Copy the cache to a new directory and compact it.
568
+ */
569
+ async unstable_compactCache() {
570
+ await this._init();
571
+ const cache = (0, nullthrows_1.default)(__classPrivateFieldGet(this, _Atlaspack_resolvedOptions, "f")).cache;
572
+ if (cache instanceof cache_1.LMDBLiteCache) {
573
+ await cache.compact('parcel-cache-compacted');
574
+ }
575
+ else {
576
+ throw new Error('Cache is not an LMDBLiteCache');
577
+ }
578
+ }
579
+ async unstable_transform(options) {
580
+ if (!__classPrivateFieldGet(this, _Atlaspack_initialized, "f")) {
581
+ await this._init();
582
+ }
583
+ let projectRoot = (0, nullthrows_1.default)(__classPrivateFieldGet(this, _Atlaspack_resolvedOptions, "f")).projectRoot;
584
+ let request = (0, AssetRequest_1.default)({
585
+ ...options,
586
+ filePath: (0, projectPath_1.toProjectPath)(projectRoot, options.filePath),
587
+ optionsRef: __classPrivateFieldGet(this, _Atlaspack_optionsRef, "f"),
588
+ env: (0, Environment_1.createEnvironment)({
589
+ ...options.env,
590
+ loc: options.env?.loc != null
591
+ ? {
592
+ ...options.env.loc,
593
+ filePath: (0, projectPath_1.toProjectPath)(projectRoot, options.env.loc.filePath),
594
+ }
595
+ : undefined,
596
+ }),
597
+ });
598
+ let res = await __classPrivateFieldGet(this, _Atlaspack_requestTracker, "f").runRequest(request, {
599
+ force: true,
600
+ });
601
+ return res.map((asset) => (0, Asset_1.assetFromValue)(asset, (0, nullthrows_1.default)(__classPrivateFieldGet(this, _Atlaspack_resolvedOptions, "f"))));
602
+ }
603
+ async unstable_resolve(request) {
604
+ if (!__classPrivateFieldGet(this, _Atlaspack_initialized, "f")) {
605
+ await this._init();
606
+ }
607
+ let projectRoot = (0, nullthrows_1.default)(__classPrivateFieldGet(this, _Atlaspack_resolvedOptions, "f")).projectRoot;
608
+ if (request.resolveFrom == null && path_1.default.isAbsolute(request.specifier)) {
609
+ request.specifier = (0, projectPath_1.fromProjectPathRelative)((0, projectPath_1.toProjectPath)(projectRoot, request.specifier));
610
+ }
611
+ let dependency = (0, Dependency_1.createDependency)(projectRoot, {
612
+ ...request,
613
+ env: (0, Environment_1.createEnvironment)({
614
+ ...request.env,
615
+ loc: request.env?.loc != null
616
+ ? {
617
+ ...request.env.loc,
618
+ filePath: (0, projectPath_1.toProjectPath)(projectRoot, request.env.loc.filePath),
619
+ }
620
+ : undefined,
621
+ }),
622
+ });
623
+ let req = (0, PathRequest_1.default)({
624
+ dependency,
625
+ name: request.specifier,
626
+ });
627
+ let res = await __classPrivateFieldGet(this, _Atlaspack_requestTracker, "f").runRequest(req, {
628
+ force: true,
629
+ });
630
+ if (!res) {
631
+ return null;
632
+ }
633
+ return {
634
+ // @ts-expect-error TS2322
635
+ filePath: (0, projectPath_1.fromProjectPath)(projectRoot, res.filePath),
636
+ code: res.code,
637
+ query: res.query,
638
+ sideEffects: res.sideEffects,
639
+ };
640
+ }
641
+ }
642
+ _Atlaspack_requestTracker = new WeakMap(), _Atlaspack_config = new WeakMap(), _Atlaspack_farm = new WeakMap(), _Atlaspack_initialized = new WeakMap(), _Atlaspack_disposable = new WeakMap(), _Atlaspack_initialOptions = new WeakMap(), _Atlaspack_reporterRunner = new WeakMap(), _Atlaspack_resolvedOptions = new WeakMap(), _Atlaspack_optionsRef = new WeakMap(), _Atlaspack_watchAbortController = new WeakMap(), _Atlaspack_watchQueue = new WeakMap(), _Atlaspack_watchEvents = new WeakMap(), _Atlaspack_watcherSubscription = new WeakMap(), _Atlaspack_watcherCount = new WeakMap(), _Atlaspack_requestedAssetIds = new WeakMap();
643
+ exports.default = Atlaspack;
644
+ class BuildError extends diagnostic_1.default {
645
+ constructor(diagnostic) {
646
+ super({ diagnostic });
647
+ this.name = 'BuildError';
648
+ }
649
+ }
650
+ exports.BuildError = BuildError;
651
+ function createWorkerFarm(options = {}) {
652
+ return new workers_1.default({
653
+ ...options,
654
+ workerPath: exports.WORKER_PATH,
655
+ });
656
+ }