@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,531 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ const assert_1 = __importDefault(require("assert"));
40
+ const build_cache_1 = require("@atlaspack/build-cache");
41
+ const utils_1 = require("@atlaspack/utils");
42
+ const logger_1 = require("@atlaspack/logger");
43
+ const diagnostic_1 = __importStar(require("@atlaspack/diagnostic"));
44
+ const stream_1 = require("stream");
45
+ const nullthrows_1 = __importDefault(require("nullthrows"));
46
+ const path_1 = __importDefault(require("path"));
47
+ const url_1 = __importDefault(require("url"));
48
+ const rust_1 = require("@atlaspack/rust");
49
+ const Bundle_1 = require("./public/Bundle");
50
+ const BundleGraph_1 = __importStar(require("./public/BundleGraph"));
51
+ const PluginOptions_1 = __importDefault(require("./public/PluginOptions"));
52
+ const Config_1 = __importDefault(require("./public/Config"));
53
+ const constants_1 = require("./constants");
54
+ const projectPath_1 = require("./projectPath");
55
+ const InternalConfig_1 = require("./InternalConfig");
56
+ const ConfigRequest_1 = require("./requests/ConfigRequest");
57
+ const DevDepRequest_1 = require("./requests/DevDepRequest");
58
+ const assetUtils_1 = require("./assetUtils");
59
+ const utils_2 = require("./utils");
60
+ const DevDepRequest_2 = require("./requests/DevDepRequest");
61
+ const profiler_1 = require("@atlaspack/profiler");
62
+ const EnvironmentManager_1 = require("./EnvironmentManager");
63
+ const feature_flags_1 = require("@atlaspack/feature-flags");
64
+ const BOUNDARY_LENGTH = constants_1.HASH_REF_PREFIX.length + 32 - 1;
65
+ // Packager/optimizer configs are not bundle-specific, so we only need to
66
+ // load them once per build.
67
+ const pluginConfigs = (0, build_cache_1.createBuildCache)();
68
+ class PackagerRunner {
69
+ constructor({ config, options, report, previousDevDeps, previousInvalidations, }) {
70
+ this.config = config;
71
+ this.options = options;
72
+ this.report = report;
73
+ this.previousDevDeps = previousDevDeps;
74
+ this.devDepRequests = new Map();
75
+ this.previousInvalidations = previousInvalidations;
76
+ this.invalidations = new Map();
77
+ this.pluginOptions = new PluginOptions_1.default((0, utils_2.optionsProxy)(this.options, (option) => {
78
+ let invalidation = {
79
+ type: 'option',
80
+ key: option,
81
+ };
82
+ this.invalidations.set((0, assetUtils_1.getInvalidationId)(invalidation), invalidation);
83
+ }));
84
+ }
85
+ async run(bundleGraph, bundle, invalidDevDeps) {
86
+ (0, DevDepRequest_2.invalidateDevDeps)(invalidDevDeps, this.options, this.config);
87
+ let { configs, bundleConfigs } = await this.loadConfigs(bundleGraph, bundle);
88
+ let bundleInfo = (await this.getBundleInfoFromCache(bundleGraph, bundle, configs, bundleConfigs)) ??
89
+ (await this.getBundleInfo(bundle, bundleGraph, configs, bundleConfigs));
90
+ let configRequests = (0, ConfigRequest_1.getConfigRequests)([
91
+ ...configs.values(),
92
+ ...bundleConfigs.values(),
93
+ ]);
94
+ let devDepRequests = (0, DevDepRequest_1.getWorkerDevDepRequests)([
95
+ ...this.devDepRequests.values(),
96
+ ]);
97
+ return {
98
+ bundleInfo,
99
+ configRequests,
100
+ devDepRequests,
101
+ invalidations: [...this.invalidations.values()],
102
+ };
103
+ }
104
+ async loadConfigs(bundleGraph, bundle) {
105
+ let configs = new Map();
106
+ let bundleConfigs = new Map();
107
+ await this.loadConfig(bundleGraph, bundle, configs, bundleConfigs);
108
+ for (let inlineBundle of bundleGraph.getInlineBundles(bundle)) {
109
+ await this.loadConfig(bundleGraph, inlineBundle, configs, bundleConfigs);
110
+ }
111
+ return { configs, bundleConfigs };
112
+ }
113
+ async loadConfig(bundleGraph, bundle, configs, bundleConfigs) {
114
+ let name = (0, nullthrows_1.default)(bundle.name);
115
+ let plugin = await this.config.getPackager(name);
116
+ await this.loadPluginConfig(bundleGraph, bundle, plugin, configs, bundleConfigs);
117
+ let optimizers = await this.config.getOptimizers(name, bundle.pipeline);
118
+ for (let optimizer of optimizers) {
119
+ await this.loadPluginConfig(bundleGraph, bundle, optimizer, configs, bundleConfigs);
120
+ }
121
+ }
122
+ async loadPluginConfig(bundleGraph, bundle, plugin, configs, bundleConfigs) {
123
+ if (!configs.has(plugin.name)) {
124
+ // Only load config for a plugin once per build.
125
+ let existing = pluginConfigs.get(plugin.name);
126
+ if (existing != null) {
127
+ // @ts-expect-error TS2345
128
+ configs.set(plugin.name, existing);
129
+ }
130
+ else {
131
+ if (plugin.plugin.loadConfig != null) {
132
+ let config = (0, InternalConfig_1.createConfig)({
133
+ plugin: plugin.name,
134
+ searchPath: (0, projectPath_1.toProjectPathUnsafe)('index'),
135
+ });
136
+ await (0, ConfigRequest_1.loadPluginConfig)(plugin, config, this.options);
137
+ for (let devDep of config.devDeps) {
138
+ let devDepRequest = await (0, DevDepRequest_1.createDevDependency)(devDep, this.previousDevDeps, this.options);
139
+ let key = `${devDep.specifier}:${(0, projectPath_1.fromProjectPath)(this.options.projectRoot, devDep.resolveFrom)}`;
140
+ this.devDepRequests.set(key, devDepRequest);
141
+ }
142
+ pluginConfigs.set(plugin.name, config);
143
+ configs.set(plugin.name, config);
144
+ }
145
+ }
146
+ }
147
+ let loadBundleConfig = plugin.plugin.loadBundleConfig;
148
+ if (!bundleConfigs.has(plugin.name) && loadBundleConfig != null) {
149
+ let config = (0, InternalConfig_1.createConfig)({
150
+ plugin: plugin.name,
151
+ searchPath: (0, projectPath_1.joinProjectPath)(bundle.target.distDir, bundle.name ?? bundle.id),
152
+ });
153
+ config.result = await loadBundleConfig({
154
+ bundle: Bundle_1.NamedBundle.get(bundle, bundleGraph, this.options),
155
+ bundleGraph: new BundleGraph_1.default(bundleGraph, Bundle_1.NamedBundle.get.bind(Bundle_1.NamedBundle), this.options),
156
+ config: new Config_1.default(config, this.options),
157
+ options: new PluginOptions_1.default(this.options),
158
+ logger: new logger_1.PluginLogger({ origin: plugin.name }),
159
+ tracer: new profiler_1.PluginTracer({ origin: plugin.name, category: 'loadConfig' }),
160
+ });
161
+ bundleConfigs.set(plugin.name, config);
162
+ }
163
+ }
164
+ async getBundleInfoFromCache(bundleGraph, bundle, configs, bundleConfigs) {
165
+ if (this.options.shouldDisableCache) {
166
+ return;
167
+ }
168
+ let cacheKey = await this.getCacheKey(bundle, bundleGraph, configs, bundleConfigs, this.previousInvalidations);
169
+ let infoKey = PackagerRunner.getInfoKey(cacheKey);
170
+ return this.options.cache.get(infoKey);
171
+ }
172
+ async getBundleInfo(bundle, bundleGraph, configs, bundleConfigs) {
173
+ let { type, contents, map } = await this.getBundleResult(bundle, bundleGraph, configs, bundleConfigs);
174
+ // Recompute cache keys as they may have changed due to dev dependencies.
175
+ let cacheKey = await this.getCacheKey(bundle, bundleGraph, configs, bundleConfigs, [...this.invalidations.values()]);
176
+ let cacheKeys = {
177
+ content: PackagerRunner.getContentKey(cacheKey),
178
+ map: PackagerRunner.getMapKey(cacheKey),
179
+ info: PackagerRunner.getInfoKey(cacheKey),
180
+ };
181
+ return this.writeToCache(cacheKeys, type, contents, map);
182
+ }
183
+ async getBundleResult(bundle, bundleGraph, configs, bundleConfigs) {
184
+ let packaged = await this.package(bundle, bundleGraph, configs, bundleConfigs);
185
+ let type = packaged.type ?? bundle.type;
186
+ let res = await this.optimize(bundle, bundleGraph, type, packaged.contents, packaged.map, configs, bundleConfigs);
187
+ let map = res.map != null ? await this.generateSourceMap(bundle, res.map) : null;
188
+ return {
189
+ type: res.type ?? type,
190
+ contents: res.contents,
191
+ map,
192
+ };
193
+ }
194
+ getSourceMapReference(bundle, map) {
195
+ if (map &&
196
+ bundle.env.sourceMap &&
197
+ bundle.bundleBehavior !== 'inline' &&
198
+ bundle.bundleBehavior !== 'inlineIsolated') {
199
+ if (bundle.env.sourceMap && bundle.env.sourceMap.inline) {
200
+ return this.generateSourceMap((0, Bundle_1.bundleToInternalBundle)(bundle), map);
201
+ }
202
+ else {
203
+ return path_1.default.basename(bundle.name) + '.map';
204
+ }
205
+ }
206
+ else {
207
+ return null;
208
+ }
209
+ }
210
+ async package(internalBundle, bundleGraph, configs, bundleConfigs) {
211
+ let bundle = Bundle_1.NamedBundle.get(internalBundle, bundleGraph, this.options);
212
+ this.report({
213
+ type: 'buildProgress',
214
+ phase: 'packaging',
215
+ bundle,
216
+ });
217
+ let packager = await this.config.getPackager(bundle.name);
218
+ let { name, resolveFrom, plugin } = packager;
219
+ let measurement;
220
+ try {
221
+ measurement = profiler_1.tracer.createMeasurement(name, 'packaging', bundle.name, {
222
+ type: bundle.type,
223
+ });
224
+ return await plugin.package({
225
+ config: configs.get(name)?.result,
226
+ bundleConfig: bundleConfigs.get(name)?.result,
227
+ bundle,
228
+ bundleGraph: new BundleGraph_1.default(bundleGraph, Bundle_1.NamedBundle.get.bind(Bundle_1.NamedBundle), this.options),
229
+ getSourceMapReference: (map) => {
230
+ return this.getSourceMapReference(bundle, map);
231
+ },
232
+ options: this.pluginOptions,
233
+ logger: new logger_1.PluginLogger({ origin: name }),
234
+ tracer: new profiler_1.PluginTracer({ origin: name, category: 'package' }),
235
+ getInlineBundleContents: async (bundle, bundleGraph) => {
236
+ if (bundle.bundleBehavior !== 'inline' &&
237
+ bundle.bundleBehavior !== 'inlineIsolated') {
238
+ throw new Error('Bundle is not inline and unable to retrieve contents');
239
+ }
240
+ let res = await this.getBundleResult((0, Bundle_1.bundleToInternalBundle)(bundle), (0, BundleGraph_1.bundleGraphToInternalBundleGraph)(bundleGraph), configs, bundleConfigs);
241
+ return { contents: res.contents };
242
+ },
243
+ });
244
+ }
245
+ catch (e) {
246
+ throw new diagnostic_1.default({
247
+ diagnostic: (0, diagnostic_1.errorToDiagnostic)(e, {
248
+ origin: name,
249
+ filePath: path_1.default.join(bundle.target.distDir, bundle.name),
250
+ }),
251
+ });
252
+ }
253
+ finally {
254
+ measurement && measurement.end();
255
+ // Add dev dependency for the packager. This must be done AFTER running it due to
256
+ // the potential for lazy require() that aren't executed until the request runs.
257
+ let devDepRequest = await (0, DevDepRequest_1.createDevDependency)({
258
+ specifier: name,
259
+ resolveFrom,
260
+ }, this.previousDevDeps, this.options);
261
+ this.devDepRequests.set(`${name}:${(0, projectPath_1.fromProjectPathRelative)(resolveFrom)}`, devDepRequest);
262
+ }
263
+ }
264
+ async optimize(internalBundle, internalBundleGraph, type, contents, map, configs, bundleConfigs) {
265
+ let bundle = Bundle_1.NamedBundle.get(internalBundle, internalBundleGraph, this.options);
266
+ let bundleGraph = new BundleGraph_1.default(internalBundleGraph, Bundle_1.NamedBundle.get.bind(Bundle_1.NamedBundle), this.options);
267
+ let optimizers = await this.config.getOptimizers(bundle.name, internalBundle.pipeline);
268
+ if (!optimizers.length) {
269
+ return { type: bundle.type, contents, map };
270
+ }
271
+ this.report({
272
+ type: 'buildProgress',
273
+ phase: 'optimizing',
274
+ bundle,
275
+ });
276
+ let optimized = {
277
+ type,
278
+ contents,
279
+ map,
280
+ };
281
+ for (let optimizer of optimizers) {
282
+ let measurement;
283
+ try {
284
+ measurement = profiler_1.tracer.createMeasurement(optimizer.name, 'optimize', bundle.name);
285
+ let next = await optimizer.plugin.optimize({
286
+ config: configs.get(optimizer.name)?.result,
287
+ bundleConfig: bundleConfigs.get(optimizer.name)?.result,
288
+ bundle,
289
+ bundleGraph,
290
+ contents: optimized.contents,
291
+ map: optimized.map,
292
+ getSourceMapReference: (map) => {
293
+ return this.getSourceMapReference(bundle, map);
294
+ },
295
+ options: this.pluginOptions,
296
+ logger: new logger_1.PluginLogger({ origin: optimizer.name }),
297
+ tracer: new profiler_1.PluginTracer({
298
+ origin: optimizer.name,
299
+ category: 'optimize',
300
+ }),
301
+ });
302
+ optimized.type = next.type ?? optimized.type;
303
+ optimized.contents = next.contents;
304
+ optimized.map = next.map;
305
+ }
306
+ catch (e) {
307
+ throw new diagnostic_1.default({
308
+ diagnostic: (0, diagnostic_1.errorToDiagnostic)(e, {
309
+ origin: optimizer.name,
310
+ filePath: path_1.default.join(bundle.target.distDir, bundle.name),
311
+ }),
312
+ });
313
+ }
314
+ finally {
315
+ measurement && measurement.end();
316
+ // Add dev dependency for the optimizer. This must be done AFTER running it due to
317
+ // the potential for lazy require() that aren't executed until the request runs.
318
+ let devDepRequest = await (0, DevDepRequest_1.createDevDependency)({
319
+ specifier: optimizer.name,
320
+ resolveFrom: optimizer.resolveFrom,
321
+ }, this.previousDevDeps, this.options);
322
+ this.devDepRequests.set(`${optimizer.name}:${(0, projectPath_1.fromProjectPathRelative)(optimizer.resolveFrom)}`, devDepRequest);
323
+ }
324
+ }
325
+ return optimized;
326
+ }
327
+ async generateSourceMap(bundle, map) {
328
+ // sourceRoot should be a relative path between outDir and rootDir for node.js targets
329
+ let filePath = (0, projectPath_1.joinProjectPath)(bundle.target.distDir, (0, nullthrows_1.default)(bundle.name));
330
+ let fullPath = (0, projectPath_1.fromProjectPath)(this.options.projectRoot, filePath);
331
+ let sourceRoot = path_1.default.relative(path_1.default.dirname(fullPath), this.options.projectRoot);
332
+ let inlineSources = false;
333
+ const bundleEnv = (0, EnvironmentManager_1.fromEnvironmentId)(bundle.env);
334
+ if (bundle.target) {
335
+ const bundleTargetEnv = (0, EnvironmentManager_1.fromEnvironmentId)(bundle.target.env);
336
+ if (bundleEnv.sourceMap && bundleEnv.sourceMap.sourceRoot !== undefined) {
337
+ sourceRoot = bundleEnv.sourceMap.sourceRoot;
338
+ }
339
+ else if (this.options.serveOptions &&
340
+ bundleTargetEnv.context === 'browser') {
341
+ sourceRoot = '/__parcel_source_root';
342
+ }
343
+ if (bundleEnv.sourceMap &&
344
+ bundleEnv.sourceMap.inlineSources !== undefined) {
345
+ inlineSources = bundleEnv.sourceMap.inlineSources;
346
+ }
347
+ else if (bundleTargetEnv.context !== 'node') {
348
+ // inlining should only happen in production for browser targets by default
349
+ inlineSources = this.options.mode === 'production';
350
+ }
351
+ }
352
+ let mapFilename = fullPath + '.map';
353
+ let isInlineMap = bundleEnv.sourceMap && bundleEnv.sourceMap.inline;
354
+ let stringified = await map.stringify({
355
+ file: path_1.default.basename(mapFilename),
356
+ fs: this.options.inputFS,
357
+ rootDir: this.options.projectRoot,
358
+ sourceRoot: !inlineSources
359
+ ? url_1.default.format(url_1.default.parse(sourceRoot + '/'))
360
+ : undefined,
361
+ inlineSources,
362
+ format: isInlineMap ? 'inline' : 'string',
363
+ });
364
+ (0, assert_1.default)(typeof stringified === 'string');
365
+ return stringified;
366
+ }
367
+ async getCacheKey(bundle, bundleGraph, configs, bundleConfigs, invalidations) {
368
+ let configResults = {};
369
+ for (let [pluginName, config] of configs) {
370
+ if (config) {
371
+ configResults[pluginName] = await (0, ConfigRequest_1.getConfigHash)(config, pluginName, this.options);
372
+ }
373
+ }
374
+ let globalInfoResults = {};
375
+ for (let [pluginName, config] of bundleConfigs) {
376
+ if (config) {
377
+ globalInfoResults[pluginName] = await (0, ConfigRequest_1.getConfigHash)(config, pluginName, this.options);
378
+ }
379
+ }
380
+ let devDepHashes = await this.getDevDepHashes(bundle);
381
+ for (let inlineBundle of bundleGraph.getInlineBundles(bundle)) {
382
+ devDepHashes += await this.getDevDepHashes(inlineBundle);
383
+ }
384
+ let invalidationHash = await (0, assetUtils_1.getInvalidationHash)(invalidations, this.options);
385
+ if ((0, feature_flags_1.getFeatureFlag)('cachePerformanceImprovements')) {
386
+ const hash = (0, rust_1.hashString)(constants_1.ATLASPACK_VERSION +
387
+ devDepHashes +
388
+ invalidationHash +
389
+ bundle.target.publicUrl +
390
+ bundleGraph.getHash(bundle) +
391
+ JSON.stringify(configResults) +
392
+ JSON.stringify(globalInfoResults) +
393
+ this.options.mode +
394
+ (this.options.shouldBuildLazily ? 'lazy' : 'eager'));
395
+ return path_1.default.join(bundle.displayName ?? bundle.name ?? bundle.id, hash);
396
+ }
397
+ return (0, rust_1.hashString)(constants_1.ATLASPACK_VERSION +
398
+ devDepHashes +
399
+ invalidationHash +
400
+ bundle.target.publicUrl +
401
+ bundleGraph.getHash(bundle) +
402
+ JSON.stringify(configResults) +
403
+ JSON.stringify(globalInfoResults) +
404
+ this.options.mode +
405
+ (this.options.shouldBuildLazily ? 'lazy' : 'eager'));
406
+ }
407
+ async getDevDepHashes(bundle) {
408
+ let name = (0, nullthrows_1.default)(bundle.name);
409
+ let packager = await this.config.getPackager(name);
410
+ let optimizers = await this.config.getOptimizers(name);
411
+ let key = `${packager.name}:${(0, projectPath_1.fromProjectPathRelative)(packager.resolveFrom)}`;
412
+ let devDepHashes = this.devDepRequests.get(key)?.hash ?? this.previousDevDeps.get(key) ?? '';
413
+ for (let { name, resolveFrom } of optimizers) {
414
+ let key = `${name}:${(0, projectPath_1.fromProjectPathRelative)(resolveFrom)}`;
415
+ devDepHashes +=
416
+ this.devDepRequests.get(key)?.hash ??
417
+ this.previousDevDeps.get(key) ??
418
+ '';
419
+ }
420
+ return devDepHashes;
421
+ }
422
+ async readFromCache(cacheKey) {
423
+ let contentKey = PackagerRunner.getContentKey(cacheKey);
424
+ let mapKey = PackagerRunner.getMapKey(cacheKey);
425
+ let isLargeBlob = await this.options.cache.hasLargeBlob(contentKey);
426
+ let contentExists = (0, feature_flags_1.getFeatureFlag)('cachePerformanceImprovements')
427
+ ? isLargeBlob
428
+ : isLargeBlob || (await this.options.cache.has(contentKey));
429
+ if (!contentExists) {
430
+ return null;
431
+ }
432
+ let mapExists = (0, feature_flags_1.getFeatureFlag)('cachePerformanceImprovements')
433
+ ? await this.options.cache.hasLargeBlob(mapKey)
434
+ : await this.options.cache.has(mapKey);
435
+ return {
436
+ contents: isLargeBlob
437
+ ? this.options.cache.getStream(contentKey)
438
+ : (0, utils_1.blobToStream)(await this.options.cache.getBlob(contentKey)),
439
+ map: mapExists
440
+ ? (0, utils_1.blobToStream)((0, feature_flags_1.getFeatureFlag)('cachePerformanceImprovements')
441
+ ? await this.options.cache.getLargeBlob(mapKey)
442
+ : await this.options.cache.getBlob(mapKey))
443
+ : null,
444
+ };
445
+ }
446
+ async writeToCache(cacheKeys, type, contents, map) {
447
+ let size = 0;
448
+ let hash;
449
+ // @ts-expect-error TS2702
450
+ let hashReferences = [];
451
+ let isLargeBlob = (0, feature_flags_1.getFeatureFlag)('cachePerformanceImprovements');
452
+ // TODO: don't replace hash references in binary files??
453
+ if (contents instanceof stream_1.Readable) {
454
+ if (!(0, feature_flags_1.getFeatureFlag)('cachePerformanceImprovements')) {
455
+ isLargeBlob = true;
456
+ }
457
+ let boundaryStr = '';
458
+ let h = new rust_1.Hash();
459
+ await this.options.cache.setStream(cacheKeys.content, (0, utils_1.blobToStream)(contents).pipe(
460
+ // @ts-expect-error TS2554
461
+ new utils_1.TapStream((buf) => {
462
+ let str = boundaryStr + buf.toString();
463
+ hashReferences = hashReferences.concat(str.match(constants_1.HASH_REF_REGEX) ?? []);
464
+ size += buf.length;
465
+ h.writeBuffer(buf);
466
+ boundaryStr = str.slice(str.length - BOUNDARY_LENGTH);
467
+ })));
468
+ hash = h.finish();
469
+ }
470
+ else if (typeof contents === 'string') {
471
+ let buffer = Buffer.from(contents);
472
+ size = buffer.byteLength;
473
+ hash = (0, rust_1.hashBuffer)(buffer);
474
+ hashReferences = contents.match(constants_1.HASH_REF_REGEX) ?? [];
475
+ if ((0, feature_flags_1.getFeatureFlag)('cachePerformanceImprovements')) {
476
+ await this.options.cache.setLargeBlob(cacheKeys.content, buffer);
477
+ }
478
+ else {
479
+ await this.options.cache.setBlob(cacheKeys.content, buffer);
480
+ }
481
+ }
482
+ else {
483
+ size = contents.length;
484
+ hash = (0, rust_1.hashBuffer)(contents);
485
+ hashReferences = contents.toString().match(constants_1.HASH_REF_REGEX) ?? [];
486
+ if ((0, feature_flags_1.getFeatureFlag)('cachePerformanceImprovements')) {
487
+ await this.options.cache.setLargeBlob(cacheKeys.content, contents);
488
+ }
489
+ else {
490
+ await this.options.cache.setBlob(cacheKeys.content, contents);
491
+ }
492
+ }
493
+ if (map != null) {
494
+ if ((0, feature_flags_1.getFeatureFlag)('cachePerformanceImprovements')) {
495
+ await this.options.cache.setLargeBlob(cacheKeys.map, map);
496
+ }
497
+ else {
498
+ await this.options.cache.setBlob(cacheKeys.map, map);
499
+ }
500
+ }
501
+ let info = {
502
+ type,
503
+ size,
504
+ hash,
505
+ hashReferences,
506
+ cacheKeys,
507
+ isLargeBlob,
508
+ };
509
+ await this.options.cache.set(cacheKeys.info, info);
510
+ return info;
511
+ }
512
+ static getContentKey(cacheKey) {
513
+ if ((0, feature_flags_1.getFeatureFlag)('cachePerformanceImprovements')) {
514
+ return `PackagerRunner/${constants_1.ATLASPACK_VERSION}/${cacheKey}/content`;
515
+ }
516
+ return (0, rust_1.hashString)(`${cacheKey}:content`);
517
+ }
518
+ static getMapKey(cacheKey) {
519
+ if ((0, feature_flags_1.getFeatureFlag)('cachePerformanceImprovements')) {
520
+ return `PackagerRunner/${constants_1.ATLASPACK_VERSION}/${cacheKey}/map`;
521
+ }
522
+ return (0, rust_1.hashString)(`${cacheKey}:map`);
523
+ }
524
+ static getInfoKey(cacheKey) {
525
+ if ((0, feature_flags_1.getFeatureFlag)('cachePerformanceImprovements')) {
526
+ return `PackagerRunner/${constants_1.ATLASPACK_VERSION}/${cacheKey}/info`;
527
+ }
528
+ return (0, rust_1.hashString)(`${cacheKey}:info`);
529
+ }
530
+ }
531
+ exports.default = PackagerRunner;
@@ -0,0 +1,151 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.reportWorker = reportWorker;
40
+ exports.report = report;
41
+ const assert_1 = __importDefault(require("assert"));
42
+ const Bundle_1 = require("./public/Bundle");
43
+ const workers_1 = require("@atlaspack/workers");
44
+ const logger_1 = __importStar(require("@atlaspack/logger"));
45
+ const PluginOptions_1 = __importDefault(require("./public/PluginOptions"));
46
+ const BundleGraph_1 = __importDefault(require("./BundleGraph"));
47
+ const profiler_1 = require("@atlaspack/profiler");
48
+ const diagnostic_1 = require("@atlaspack/diagnostic");
49
+ const instances = new Set();
50
+ class ReporterRunner {
51
+ constructor(opts) {
52
+ this.eventHandler = (event) => {
53
+ if (event.type === 'buildProgress' &&
54
+ (event.phase === 'optimizing' || event.phase === 'packaging') &&
55
+ !(event.bundle instanceof Bundle_1.NamedBundle)) {
56
+ // @ts-expect-error TS2339
57
+ let bundleGraphRef = event.bundleGraphRef;
58
+ // @ts-expect-error TS2739
59
+ let bundle = event.bundle;
60
+ // Convert any internal bundles back to their public equivalents as reporting
61
+ // is public api
62
+ let bundleGraph = this.workerFarm.workerApi.getSharedReference(bundleGraphRef);
63
+ (0, assert_1.default)(bundleGraph instanceof BundleGraph_1.default);
64
+ this.report({
65
+ ...event,
66
+ bundle: Bundle_1.NamedBundle.get(bundle, bundleGraph, this.options),
67
+ });
68
+ return;
69
+ }
70
+ this.report(event);
71
+ };
72
+ this.errors = [];
73
+ this.options = opts.options;
74
+ this.reporters = opts.reporters;
75
+ this.workerFarm = opts.workerFarm;
76
+ this.pluginOptions = new PluginOptions_1.default(this.options);
77
+ logger_1.default.onLog((event) => this.report(event));
78
+ profiler_1.tracer.onTrace((event) => this.report(event));
79
+ workers_1.bus.on('reporterEvent', this.eventHandler);
80
+ instances.add(this);
81
+ if (this.options.shouldPatchConsole) {
82
+ (0, logger_1.patchConsole)();
83
+ }
84
+ else {
85
+ (0, logger_1.unpatchConsole)();
86
+ }
87
+ }
88
+ async report(unsanitisedEvent) {
89
+ let event = unsanitisedEvent;
90
+ // @ts-expect-error TS2339
91
+ if (event.diagnostics) {
92
+ // Sanitise input before passing to reporters
93
+ event = {
94
+ ...event,
95
+ // @ts-expect-error TS2322
96
+ diagnostics: (0, diagnostic_1.anyToDiagnostic)(event.diagnostics),
97
+ };
98
+ }
99
+ for (let reporter of this.reporters) {
100
+ let measurement;
101
+ try {
102
+ // To avoid an infinite loop we don't measure trace events, as they'll
103
+ // result in another trace!
104
+ if (event.type !== 'trace') {
105
+ measurement = profiler_1.tracer.createMeasurement(reporter.name, 'reporter');
106
+ }
107
+ await reporter.plugin.report({
108
+ event,
109
+ options: this.pluginOptions,
110
+ logger: new logger_1.PluginLogger({ origin: reporter.name }),
111
+ tracer: new profiler_1.PluginTracer({
112
+ origin: reporter.name,
113
+ category: 'reporter',
114
+ }),
115
+ });
116
+ }
117
+ catch (reportError) {
118
+ if (event.type !== 'buildSuccess') {
119
+ // This will be captured by consumers
120
+ logger_1.INTERNAL_ORIGINAL_CONSOLE.error(reportError);
121
+ }
122
+ this.errors.push(reportError);
123
+ }
124
+ finally {
125
+ measurement && measurement.end();
126
+ }
127
+ }
128
+ }
129
+ dispose() {
130
+ workers_1.bus.off('reporterEvent', this.eventHandler);
131
+ instances.delete(this);
132
+ }
133
+ }
134
+ exports.default = ReporterRunner;
135
+ function reportWorker(workerApi, event) {
136
+ if (event.type === 'buildProgress' &&
137
+ (event.phase === 'optimizing' || event.phase === 'packaging')) {
138
+ // Convert any public api bundles to their internal equivalents for
139
+ // easy serialization
140
+ workers_1.bus.emit('reporterEvent', {
141
+ ...event,
142
+ bundle: (0, Bundle_1.bundleToInternalBundle)(event.bundle),
143
+ bundleGraphRef: workerApi.resolveSharedReference((0, Bundle_1.bundleToInternalBundleGraph)(event.bundle)),
144
+ });
145
+ return;
146
+ }
147
+ workers_1.bus.emit('reporterEvent', event);
148
+ }
149
+ async function report(event) {
150
+ await Promise.all([...instances].map((instance) => instance.report(event)));
151
+ }