@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,305 @@
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.default = applyRuntimes;
40
+ const path_1 = __importDefault(require("path"));
41
+ const assert_1 = __importDefault(require("assert"));
42
+ const assert_2 = __importDefault(require("assert"));
43
+ const nullthrows_1 = __importDefault(require("nullthrows"));
44
+ const AssetGraph_1 = require("./AssetGraph");
45
+ const BundleGraph_1 = __importDefault(require("./public/BundleGraph"));
46
+ const BundleGraph_2 = __importStar(require("./BundleGraph"));
47
+ const Bundle_1 = require("./public/Bundle");
48
+ const logger_1 = require("@atlaspack/logger");
49
+ const rust_1 = require("@atlaspack/rust");
50
+ const diagnostic_1 = __importStar(require("@atlaspack/diagnostic"));
51
+ const Dependency_1 = require("./public/Dependency");
52
+ const Environment_1 = require("./Environment");
53
+ const AssetGraphRequest_1 = __importDefault(require("./requests/AssetGraphRequest"));
54
+ const DevDepRequest_1 = require("./requests/DevDepRequest");
55
+ const projectPath_1 = require("./projectPath");
56
+ const profiler_1 = require("@atlaspack/profiler");
57
+ const utils_1 = require("@atlaspack/utils");
58
+ const EnvironmentManager_1 = require("./EnvironmentManager");
59
+ function nameRuntimeBundle(bundle, siblingBundle) {
60
+ // We don't run custom namers on runtime bundles as the runtime assumes that they are
61
+ // located at the same nesting level as their owning bundle. Custom naming could
62
+ // be added in future as long as the custom name is validated.
63
+ let { hashReference } = bundle;
64
+ let name = (0, nullthrows_1.default)(siblingBundle.name)
65
+ // Remove the existing hash from standard file patterns
66
+ // e.g. 'main.[hash].js' -> 'main.js' or 'main~[hash].js' -> 'main.js'
67
+ .replace(new RegExp(`[\\.~\\-_]?${siblingBundle.hashReference}`), '')
68
+ // Ensure the file ends with 'runtime.[hash].js'
69
+ .replace(`.${bundle.type}`, `.runtime.${hashReference}.${bundle.type}`);
70
+ bundle.name = name;
71
+ bundle.displayName = name.replace(hashReference, '[hash]');
72
+ }
73
+ /**
74
+ * The applyRuntimes function is responsible for generating all the runtimes
75
+ * (assets created during the build that don't actually exist on disk) and then
76
+ * linking them into the bundle graph.
77
+ *
78
+ * Introduction of manifest bundles: https://github.com/parcel-bundler/parcel/pull/8837
79
+ * Introduction of reverse topology: https://github.com/parcel-bundler/parcel/pull/8981
80
+ */
81
+ async function applyRuntimes({ bundleGraph, config, options, pluginOptions, api, optionsRef, previousDevDeps, devDepRequests, configs, }) {
82
+ let runtimes = await config.getRuntimes();
83
+ /**
84
+ * Usually, the assets returned from a runtime will go in the same bundle. It is
85
+ * possible though, for a runtime to return an asset with a `parallel` priority,
86
+ * which allows it to be moved to a separate bundle. In practice, this is
87
+ * usually used to generate application manifest files.
88
+ *
89
+ * When adding a manifest bundle (a whole new separate bundle) during a runtime,
90
+ * it needs to be added to a bundle group which will be potentially referenced
91
+ * by another bundle group. To avoid trying to reference a manifest entry which
92
+ * hasn't been created yet, we process the bundles from the bottom up (topological
93
+ * order), so that children will always be available when parents try to reference
94
+ * them.
95
+ */
96
+ // @ts-expect-error TS2304
97
+ let bundles = [];
98
+ bundleGraph.traverseBundles({
99
+ // @ts-expect-error TS2304
100
+ exit(bundle) {
101
+ bundles.push(bundle);
102
+ },
103
+ });
104
+ let connectionMap = new utils_1.DefaultMap(() => []);
105
+ for (let bundle of bundles) {
106
+ for (let runtime of runtimes) {
107
+ let measurement;
108
+ try {
109
+ const namedBundle = Bundle_1.NamedBundle.get(bundle, bundleGraph, options);
110
+ measurement = profiler_1.tracer.createMeasurement(runtime.name, 'applyRuntime', namedBundle.displayName);
111
+ let applied = await runtime.plugin.apply({
112
+ bundle: namedBundle,
113
+ bundleGraph: new BundleGraph_1.default(bundleGraph, Bundle_1.NamedBundle.get.bind(Bundle_1.NamedBundle), options),
114
+ config: configs.get(runtime.name)?.result,
115
+ options: pluginOptions,
116
+ logger: new logger_1.PluginLogger({ origin: runtime.name }),
117
+ tracer: new profiler_1.PluginTracer({
118
+ origin: runtime.name,
119
+ category: 'applyRuntime',
120
+ }),
121
+ });
122
+ if (applied) {
123
+ let runtimeAssets = Array.isArray(applied) ? applied : [applied];
124
+ for (let { code, dependency, filePath, isEntry, env, runtimeAssetRequiringExecutionOnLoad, priority, } of runtimeAssets) {
125
+ let sourceName = path_1.default.join(path_1.default.dirname(filePath), `runtime-${(0, rust_1.hashString)(code)}.${bundle.type}`);
126
+ let assetGroup = {
127
+ code,
128
+ runtimeAssetRequiringExecutionOnLoad,
129
+ filePath: (0, projectPath_1.toProjectPath)(options.projectRoot, sourceName),
130
+ env: (0, Environment_1.mergeEnvironments)(options.projectRoot, (0, EnvironmentManager_1.fromEnvironmentId)(bundle.env), env),
131
+ // Runtime assets should be considered source, as they should be
132
+ // e.g. compiled to run in the target environment
133
+ isSource: true,
134
+ };
135
+ let connectionBundle = bundle;
136
+ /**
137
+ * If a runtime asset is marked with a priority of `parallel` this
138
+ * means we need to create a new bundle for the asset and add it to
139
+ * all the same bundle groups.
140
+ */
141
+ if (priority === 'parallel' && !bundle.needsStableName) {
142
+ let bundleGroups = bundleGraph.getBundleGroupsContainingBundle(bundle);
143
+ connectionBundle = (0, nullthrows_1.default)(bundleGraph.createBundle({
144
+ type: bundle.type,
145
+ needsStableName: false,
146
+ env: bundle.env,
147
+ target: bundle.target,
148
+ uniqueKey: 'runtime-manifest:' + bundle.id,
149
+ shouldContentHash: options.shouldContentHash,
150
+ }));
151
+ for (let bundleGroup of bundleGroups) {
152
+ bundleGraph.addBundleToBundleGroup(connectionBundle, bundleGroup);
153
+ }
154
+ bundleGraph.createBundleReference(bundle, connectionBundle);
155
+ // Ensure we name the bundle now as all other bundles have already
156
+ // been named as this point.
157
+ nameRuntimeBundle(connectionBundle, bundle);
158
+ }
159
+ // @ts-expect-error TS2345
160
+ connectionMap.get(connectionBundle).push({
161
+ bundle: connectionBundle,
162
+ assetGroup,
163
+ dependency,
164
+ isEntry,
165
+ });
166
+ }
167
+ }
168
+ }
169
+ catch (e) {
170
+ throw new diagnostic_1.default({
171
+ diagnostic: (0, diagnostic_1.errorToDiagnostic)(e, {
172
+ origin: runtime.name,
173
+ }),
174
+ });
175
+ }
176
+ finally {
177
+ measurement && measurement.end();
178
+ }
179
+ }
180
+ }
181
+ /**
182
+ * When merging the connections in to the bundle graph, the topological
183
+ * order can create module not found errors in some situations, often when HMR
184
+ * is enabled. To fix this, we put the connections into DFS order.
185
+ *
186
+ * Note: While DFS order seems to be the most reliable order to process the
187
+ * connections, this is likely due to it being close to the order that the bundles were
188
+ * inserted into the graph. There is a known issue where runtime assets marked
189
+ * as `isEntry` can create scenarios where there is no correct load order that
190
+ * won't error, as the entry runtime assets are added to many bundles in a
191
+ * single bundle group but their dependencies are not.
192
+ *
193
+ * This issue is almost exclusive to HMR scenarios as the two HMR runtime
194
+ * plugins (@atlaspack/runtime-browser-hmr and @atlaspack/runtime-react-refresh)
195
+ * are the only known cases where a runtime asset is marked as `isEntry`.
196
+ */
197
+ let connections = [];
198
+ bundleGraph.traverseBundles({
199
+ // @ts-expect-error TS2304
200
+ enter(bundle) {
201
+ connections.push(...connectionMap.get(bundle));
202
+ },
203
+ });
204
+ // Add dev deps for runtime plugins AFTER running them, to account for lazy require().
205
+ for (let runtime of runtimes) {
206
+ let devDepRequest = await (0, DevDepRequest_1.createDevDependency)({
207
+ specifier: runtime.name,
208
+ resolveFrom: runtime.resolveFrom,
209
+ }, previousDevDeps, options);
210
+ devDepRequests.set(`${devDepRequest.specifier}:${(0, projectPath_1.fromProjectPathRelative)(devDepRequest.resolveFrom)}`, devDepRequest);
211
+ await (0, DevDepRequest_1.runDevDepRequest)(api, devDepRequest);
212
+ }
213
+ // Create a new AssetGraph from the generated runtime assets which also runs
214
+ // transforms and resolves all dependencies.
215
+ let { assetGraph: runtimesAssetGraph, changedAssets } = await reconcileNewRuntimes(api, connections, optionsRef);
216
+ // Convert the runtime AssetGraph into a BundleGraph, this includes assigning
217
+ // the assets their public ids
218
+ let runtimesBundleGraph = BundleGraph_2.default.fromAssetGraph(runtimesAssetGraph, options.mode === 'production', bundleGraph._publicIdByAssetId, bundleGraph._assetPublicIds);
219
+ // Merge the runtimes bundle graph into the main bundle graph.
220
+ bundleGraph.merge(runtimesBundleGraph);
221
+ // Add the public id mappings from the runtumes bundlegraph to the main bundle graph
222
+ for (let [assetId, publicId] of runtimesBundleGraph._publicIdByAssetId) {
223
+ bundleGraph._publicIdByAssetId.set(assetId, publicId);
224
+ bundleGraph._assetPublicIds.add(publicId);
225
+ }
226
+ // Connect each of the generated runtime assets to bundles in the main bundle
227
+ // graph. This is like a mini-bundling algorithm for runtime assets.
228
+ for (let { bundle, assetGroup, dependency, isEntry } of connections) {
229
+ let assetGroupNode = (0, AssetGraph_1.nodeFromAssetGroup)(assetGroup);
230
+ let assetGroupAssetNodeIds = runtimesAssetGraph.getNodeIdsConnectedFrom(runtimesAssetGraph.getNodeIdByContentKey(assetGroupNode.id));
231
+ (0, assert_2.default)(assetGroupAssetNodeIds.length === 1);
232
+ let runtimeNodeId = assetGroupAssetNodeIds[0];
233
+ let runtimeNode = (0, nullthrows_1.default)(runtimesAssetGraph.getNode(runtimeNodeId));
234
+ (0, assert_2.default)(runtimeNode.type === 'asset');
235
+ // Find the asset that the runtime asset should be connected from by resolving
236
+ // it's dependency.
237
+ let resolution = dependency &&
238
+ bundleGraph.getResolvedAsset((0, Dependency_1.dependencyToInternalDependency)(dependency), bundle);
239
+ // Walk all the dependencies of the runtime assets and check if they are
240
+ // already reachable from the bundle that the runtime asset is assigned to.
241
+ // If so, we add them to `duplicatedContentKeys` to be skipped when assigning
242
+ // assets to bundles.
243
+ let runtimesBundleGraphRuntimeNodeId = runtimesBundleGraph._graph.getNodeIdByContentKey(runtimeNode.id);
244
+ let duplicatedContentKeys = new Set();
245
+ runtimesBundleGraph._graph.traverse((nodeId, _, actions) => {
246
+ let node = (0, nullthrows_1.default)(runtimesBundleGraph._graph.getNode(nodeId));
247
+ if (node.type !== 'dependency') {
248
+ return;
249
+ }
250
+ let assets = runtimesBundleGraph._graph
251
+ .getNodeIdsConnectedFrom(nodeId)
252
+ .map((assetNodeId) => {
253
+ let assetNode = (0, nullthrows_1.default)(runtimesBundleGraph._graph.getNode(assetNodeId));
254
+ (0, assert_2.default)(assetNode.type === 'asset');
255
+ return assetNode.value;
256
+ });
257
+ for (let asset of assets) {
258
+ if (bundleGraph.isAssetReachableFromBundle(asset, bundle) ||
259
+ resolution?.id === asset.id) {
260
+ duplicatedContentKeys.add(asset.id);
261
+ actions.skipChildren();
262
+ }
263
+ }
264
+ }, runtimesBundleGraphRuntimeNodeId);
265
+ let bundleNodeId = bundleGraph._graph.getNodeIdByContentKey(bundle.id);
266
+ let bundleGraphRuntimeNodeId = bundleGraph._graph.getNodeIdByContentKey(runtimeNode.id); // the node id is not constant between graphs
267
+ // Assign the runtime assets and all of it's depepdencies to the bundle unless
268
+ // we have detected it as already being reachable from this bundle via `duplicatedContentKeys`.
269
+ runtimesBundleGraph._graph.traverse((nodeId, _, actions) => {
270
+ let node = (0, nullthrows_1.default)(runtimesBundleGraph._graph.getNode(nodeId));
271
+ if (node.type === 'asset' || node.type === 'dependency') {
272
+ if (duplicatedContentKeys.has(node.id)) {
273
+ actions.skipChildren();
274
+ return;
275
+ }
276
+ const bundleGraphNodeId = bundleGraph._graph.getNodeIdByContentKey(node.id); // the node id is not constant between graphs
277
+ bundleGraph._graph.addEdge(bundleNodeId, bundleGraphNodeId, BundleGraph_2.bundleGraphEdgeTypes.contains);
278
+ }
279
+ }, runtimesBundleGraphRuntimeNodeId);
280
+ if (isEntry) {
281
+ bundleGraph._graph.addEdge(bundleNodeId, bundleGraphRuntimeNodeId);
282
+ bundle.entryAssetIds.unshift(runtimeNode.id);
283
+ }
284
+ if (dependency == null) {
285
+ // Verify this asset won't become an island
286
+ (0, assert_1.default)(bundleGraph._graph.getNodeIdsConnectedTo(bundleGraphRuntimeNodeId)
287
+ .length > 0, 'Runtime must have an inbound dependency or be an entry');
288
+ }
289
+ else {
290
+ let dependencyNodeId = bundleGraph._graph.getNodeIdByContentKey(dependency.id);
291
+ bundleGraph._graph.addEdge(dependencyNodeId, bundleGraphRuntimeNodeId);
292
+ }
293
+ }
294
+ return changedAssets;
295
+ }
296
+ function reconcileNewRuntimes(api, connections, optionsRef) {
297
+ let assetGroups = connections.map((t) => t.assetGroup);
298
+ let request = (0, AssetGraphRequest_1.default)({
299
+ name: 'Runtimes',
300
+ assetGroups,
301
+ optionsRef,
302
+ });
303
+ // rebuild the graph
304
+ return api.runRequest(request, { force: true });
305
+ }
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createAssetIdFromOptions = createAssetIdFromOptions;
7
+ exports.createAsset = createAsset;
8
+ exports.generateFromAST = generateFromAST;
9
+ exports.getInvalidationId = getInvalidationId;
10
+ exports.getInvalidationHash = getInvalidationHash;
11
+ const stream_1 = require("stream");
12
+ const build_cache_1 = require("@atlaspack/build-cache");
13
+ const logger_1 = require("@atlaspack/logger");
14
+ const nullthrows_1 = __importDefault(require("nullthrows"));
15
+ const loadAtlaspackPlugin_1 = __importDefault(require("./loadAtlaspackPlugin"));
16
+ const Asset_1 = require("./public/Asset");
17
+ const PluginOptions_1 = __importDefault(require("./public/PluginOptions"));
18
+ const utils_1 = require("@atlaspack/utils");
19
+ const utils_2 = require("./utils");
20
+ const projectPath_1 = require("./projectPath");
21
+ const rust_1 = require("@atlaspack/rust");
22
+ const types_1 = require("./types");
23
+ const profiler_1 = require("@atlaspack/profiler");
24
+ const IdentifierRegistry_1 = require("./IdentifierRegistry");
25
+ const EnvironmentManager_1 = require("./EnvironmentManager");
26
+ function createAssetIdFromOptions(options) {
27
+ const data = {
28
+ environmentId: (0, EnvironmentManager_1.toEnvironmentId)(options.env),
29
+ filePath: options.filePath,
30
+ code: options.code,
31
+ pipeline: options.pipeline,
32
+ query: options.query,
33
+ uniqueKey: options.uniqueKey,
34
+ fileType: options.type,
35
+ };
36
+ const id = (0, rust_1.createAssetId)(data);
37
+ IdentifierRegistry_1.identifierRegistry.addIdentifier('asset', id, data);
38
+ return id;
39
+ }
40
+ function createAsset(projectRoot, options) {
41
+ return {
42
+ id: options.id != null ? options.id : createAssetIdFromOptions(options),
43
+ committed: options.committed ?? false,
44
+ filePath: options.filePath,
45
+ query: options.query,
46
+ bundleBehavior: options.bundleBehavior
47
+ ? types_1.BundleBehavior[options.bundleBehavior]
48
+ : null,
49
+ isBundleSplittable: options.isBundleSplittable ?? true,
50
+ type: options.type,
51
+ contentKey: options.contentKey,
52
+ mapKey: options.mapKey,
53
+ astKey: options.astKey,
54
+ astGenerator: options.astGenerator,
55
+ dependencies: options.dependencies || new Map(),
56
+ isSource: options.isSource,
57
+ outputHash: options.outputHash,
58
+ pipeline: options.pipeline,
59
+ env: options.env,
60
+ meta: options.meta || {},
61
+ stats: options.stats,
62
+ symbols: options.symbols &&
63
+ new Map([...options.symbols].map(([k, v]) => [
64
+ k,
65
+ {
66
+ local: v.local,
67
+ meta: v.meta,
68
+ loc: (0, utils_2.toInternalSourceLocation)(projectRoot, v.loc),
69
+ },
70
+ ])),
71
+ sideEffects: options.sideEffects ?? true,
72
+ uniqueKey: options.uniqueKey,
73
+ plugin: options.plugin,
74
+ configPath: options.configPath,
75
+ configKeyPath: options.configKeyPath,
76
+ };
77
+ }
78
+ const generateResults = new WeakMap();
79
+ function generateFromAST(asset) {
80
+ let output = generateResults.get(asset.value);
81
+ if (output == null) {
82
+ output = _generateFromAST(asset);
83
+ generateResults.set(asset.value, output);
84
+ }
85
+ return output;
86
+ }
87
+ async function _generateFromAST(asset) {
88
+ let ast = await asset.getAST();
89
+ if (ast == null) {
90
+ throw new Error('Asset has no AST');
91
+ }
92
+ let pluginName = (0, nullthrows_1.default)(asset.value.plugin);
93
+ let { plugin } = await (0, loadAtlaspackPlugin_1.default)(pluginName, (0, projectPath_1.fromProjectPath)(asset.options.projectRoot, (0, nullthrows_1.default)(asset.value.configPath)), (0, nullthrows_1.default)(asset.value.configKeyPath), asset.options);
94
+ let generate = plugin.generate?.bind(plugin);
95
+ if (!generate) {
96
+ throw new Error(`${pluginName} does not have a generate method`);
97
+ }
98
+ let { content, map } = await generate({
99
+ asset: new Asset_1.Asset(asset),
100
+ ast,
101
+ options: new PluginOptions_1.default(asset.options),
102
+ logger: new logger_1.PluginLogger({ origin: pluginName }),
103
+ tracer: new profiler_1.PluginTracer({ origin: pluginName, category: 'asset-generate' }),
104
+ });
105
+ let mapBuffer = map?.toBuffer();
106
+ // Store the results in the cache so we can avoid generating again next time
107
+ await Promise.all([
108
+ asset.options.cache.setStream((0, nullthrows_1.default)(asset.value.contentKey), (0, utils_1.blobToStream)(content)),
109
+ mapBuffer != null &&
110
+ asset.options.cache.setBlob((0, nullthrows_1.default)(asset.value.mapKey), mapBuffer),
111
+ ]);
112
+ return {
113
+ content: content instanceof stream_1.Readable
114
+ ? asset.options.cache.getStream((0, nullthrows_1.default)(asset.value.contentKey))
115
+ : content,
116
+ map,
117
+ };
118
+ }
119
+ function getInvalidationId(invalidation) {
120
+ switch (invalidation.type) {
121
+ case 'file':
122
+ return 'file:' + (0, projectPath_1.fromProjectPathRelative)(invalidation.filePath);
123
+ case 'env':
124
+ return 'env:' + invalidation.key;
125
+ case 'option':
126
+ return 'option:' + invalidation.key;
127
+ default:
128
+ // @ts-expect-error TS2339
129
+ throw new Error('Unknown invalidation type: ' + invalidation.type);
130
+ }
131
+ }
132
+ const hashCache = (0, build_cache_1.createBuildCache)();
133
+ async function getInvalidationHash(invalidations, options) {
134
+ if (invalidations.length === 0) {
135
+ return '';
136
+ }
137
+ let sortedInvalidations = invalidations
138
+ .slice()
139
+ .sort((a, b) => (getInvalidationId(a) < getInvalidationId(b) ? -1 : 1));
140
+ let hashes = '';
141
+ for (let invalidation of sortedInvalidations) {
142
+ switch (invalidation.type) {
143
+ case 'file': {
144
+ // Only recompute the hash of this file if we haven't seen it already during this build.
145
+ let fileHash = hashCache.get(invalidation.filePath);
146
+ if (fileHash == null) {
147
+ fileHash = (0, utils_1.hashFile)(options.inputFS, (0, projectPath_1.fromProjectPath)(options.projectRoot, invalidation.filePath));
148
+ hashCache.set(invalidation.filePath, fileHash);
149
+ }
150
+ hashes += await fileHash;
151
+ break;
152
+ }
153
+ case 'env':
154
+ hashes +=
155
+ invalidation.key + ':' + (options.env[invalidation.key] || '');
156
+ break;
157
+ case 'option':
158
+ hashes +=
159
+ // @ts-expect-error TS7053
160
+ invalidation.key + ':' + (0, utils_2.hashFromOption)(options[invalidation.key]);
161
+ break;
162
+ default:
163
+ // @ts-expect-error TS2339
164
+ throw new Error('Unknown invalidation type: ' + invalidation.type);
165
+ }
166
+ }
167
+ return (0, rust_1.hashString)(hashes);
168
+ }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.AtlaspackV3 = void 0;
7
+ const rust_1 = require("@atlaspack/rust");
8
+ const NapiWorkerPool_1 = require("./NapiWorkerPool");
9
+ const diagnostic_1 = __importDefault(require("@atlaspack/diagnostic"));
10
+ class AtlaspackV3 {
11
+ constructor(atlaspack_napi, napiWorkerPool, isDefaultNapiWorkerPool) {
12
+ this._atlaspack_napi = atlaspack_napi;
13
+ this._napiWorkerPool = napiWorkerPool;
14
+ this._isDefaultNapiWorkerPool = isDefaultNapiWorkerPool;
15
+ }
16
+ static async create({ fs, packageManager, threads, lmdb, napiWorkerPool, ...options }) {
17
+ // @ts-expect-error TS2339
18
+ options.logLevel = options.logLevel || 'error';
19
+ // @ts-expect-error TS2339
20
+ options.defaultTargetOptions = options.defaultTargetOptions || {};
21
+ // @ts-expect-error TS2339
22
+ options.defaultTargetOptions.engines =
23
+ // @ts-expect-error TS2339
24
+ options.defaultTargetOptions.engines || {};
25
+ let isDefaultNapiWorkerPool = false;
26
+ if (!napiWorkerPool) {
27
+ napiWorkerPool = new NapiWorkerPool_1.NapiWorkerPool();
28
+ isDefaultNapiWorkerPool = true;
29
+ }
30
+ // @ts-expect-error TS2488
31
+ const [internal, error] = await (0, rust_1.atlaspackNapiCreate)({
32
+ fs,
33
+ packageManager,
34
+ threads,
35
+ options,
36
+ napiWorkerPool,
37
+ }, lmdb);
38
+ if (error !== null) {
39
+ throw new diagnostic_1.default({
40
+ diagnostic: error,
41
+ });
42
+ }
43
+ return new AtlaspackV3(internal, napiWorkerPool, isDefaultNapiWorkerPool);
44
+ }
45
+ end() {
46
+ // If the worker pool was provided to us, don't shut it down, it's up to the provider.
47
+ if (this._isDefaultNapiWorkerPool) {
48
+ this._napiWorkerPool.shutdown();
49
+ }
50
+ }
51
+ async buildAssetGraph() {
52
+ // @ts-expect-error TS2488
53
+ let [graph, error] = await (0, rust_1.atlaspackNapiBuildAssetGraph)(this._atlaspack_napi);
54
+ if (error !== null) {
55
+ throw new diagnostic_1.default({
56
+ diagnostic: error,
57
+ });
58
+ }
59
+ return graph;
60
+ }
61
+ async respondToFsEvents(events) {
62
+ // @ts-expect-error TS2488
63
+ let [needsRebuild, error] = await (0, rust_1.atlaspackNapiRespondToFsEvents)(this._atlaspack_napi, events);
64
+ if (error) {
65
+ throw new Error(error);
66
+ }
67
+ return needsRebuild;
68
+ }
69
+ }
70
+ exports.AtlaspackV3 = AtlaspackV3;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ 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");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ 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");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ var _NapiWorkerPool_workers, _NapiWorkerPool_napiWorkers, _NapiWorkerPool_workerCount;
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.NapiWorkerPool = void 0;
19
+ const worker_threads_1 = require("worker_threads");
20
+ const path_1 = __importDefault(require("path"));
21
+ const process_1 = __importDefault(require("process"));
22
+ const rust_1 = require("@atlaspack/rust");
23
+ const WORKER_PATH = path_1.default.join(__dirname, 'worker', 'index.js');
24
+ const ATLASPACK_NAPI_WORKERS = process_1.default.env.ATLASPACK_NAPI_WORKERS &&
25
+ parseInt(process_1.default.env.ATLASPACK_NAPI_WORKERS, 10);
26
+ class NapiWorkerPool {
27
+ constructor({ workerCount } = { workerCount: undefined }) {
28
+ _NapiWorkerPool_workers.set(this, void 0);
29
+ _NapiWorkerPool_napiWorkers.set(this, void 0);
30
+ _NapiWorkerPool_workerCount.set(this, void 0);
31
+ // @ts-expect-error TS2322
32
+ __classPrivateFieldSet(this, _NapiWorkerPool_workerCount, workerCount ?? ATLASPACK_NAPI_WORKERS ?? (0, rust_1.getAvailableThreads)(), "f");
33
+ if (!__classPrivateFieldGet(this, _NapiWorkerPool_workerCount, "f")) {
34
+ // TODO use main thread if workerCount is 0
35
+ }
36
+ __classPrivateFieldSet(this, _NapiWorkerPool_workers, [], "f");
37
+ __classPrivateFieldSet(this, _NapiWorkerPool_napiWorkers, [], "f");
38
+ for (let i = 0; i < __classPrivateFieldGet(this, _NapiWorkerPool_workerCount, "f"); i++) {
39
+ let worker = new worker_threads_1.Worker(WORKER_PATH);
40
+ __classPrivateFieldGet(this, _NapiWorkerPool_workers, "f").push(worker);
41
+ __classPrivateFieldGet(this, _NapiWorkerPool_napiWorkers, "f").push(new Promise((res) => worker.once('message', res)));
42
+ }
43
+ }
44
+ workerCount() {
45
+ return __classPrivateFieldGet(this, _NapiWorkerPool_workerCount, "f");
46
+ }
47
+ getWorkers() {
48
+ return Promise.all(__classPrivateFieldGet(this, _NapiWorkerPool_napiWorkers, "f"));
49
+ }
50
+ shutdown() {
51
+ for (const worker of __classPrivateFieldGet(this, _NapiWorkerPool_workers, "f")) {
52
+ worker.terminate();
53
+ }
54
+ }
55
+ }
56
+ exports.NapiWorkerPool = NapiWorkerPool;
57
+ _NapiWorkerPool_workers = new WeakMap(), _NapiWorkerPool_napiWorkers = new WeakMap(), _NapiWorkerPool_workerCount = new WeakMap();
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ 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");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ 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");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _FileSystemV3_fs;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.FileSystemV3 = void 0;
16
+ const jsCallable_1 = require("./jsCallable");
17
+ // @ts-expect-error TS2420
18
+ class FileSystemV3 {
19
+ constructor(fs) {
20
+ _FileSystemV3_fs.set(this, void 0);
21
+ this.canonicalize = (0, jsCallable_1.jsCallable)((path) => __classPrivateFieldGet(this, _FileSystemV3_fs, "f").realpathSync(path));
22
+ this.createDirectory = (0, jsCallable_1.jsCallable)((path) => __classPrivateFieldGet(this, _FileSystemV3_fs, "f").mkdirp(path));
23
+ this.cwd = (0, jsCallable_1.jsCallable)(() => __classPrivateFieldGet(this, _FileSystemV3_fs, "f").cwd());
24
+ this.isFile = (path) => {
25
+ try {
26
+ return __classPrivateFieldGet(this, _FileSystemV3_fs, "f").statSync(path).isFile();
27
+ }
28
+ catch {
29
+ return false;
30
+ }
31
+ };
32
+ this.isDir = (path) => {
33
+ try {
34
+ return __classPrivateFieldGet(this, _FileSystemV3_fs, "f").statSync(path).isDirectory();
35
+ }
36
+ catch {
37
+ return false;
38
+ }
39
+ };
40
+ this.readFile = (0, jsCallable_1.jsCallable)((path, encoding) => {
41
+ if (!encoding) {
42
+ return [...__classPrivateFieldGet(this, _FileSystemV3_fs, "f").readFileSync(path)];
43
+ }
44
+ else {
45
+ return __classPrivateFieldGet(this, _FileSystemV3_fs, "f").readFileSync(path, encoding);
46
+ }
47
+ });
48
+ __classPrivateFieldSet(this, _FileSystemV3_fs, fs, "f");
49
+ }
50
+ }
51
+ exports.FileSystemV3 = FileSystemV3;
52
+ _FileSystemV3_fs = new WeakMap();