@atlaspack/core 2.12.1-canary.3354

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (215) hide show
  1. package/LICENSE +201 -0
  2. package/index.d.ts +22 -0
  3. package/lib/AssetGraph.js +518 -0
  4. package/lib/Atlaspack.js +587 -0
  5. package/lib/AtlaspackConfig.js +298 -0
  6. package/lib/AtlaspackConfig.schema.js +125 -0
  7. package/lib/BundleGraph.js +1445 -0
  8. package/lib/CommittedAsset.js +133 -0
  9. package/lib/Dependency.js +88 -0
  10. package/lib/Environment.js +128 -0
  11. package/lib/InternalConfig.js +48 -0
  12. package/lib/PackagerRunner.js +519 -0
  13. package/lib/ReporterRunner.js +151 -0
  14. package/lib/RequestTracker.js +1089 -0
  15. package/lib/SymbolPropagation.js +625 -0
  16. package/lib/TargetDescriptor.schema.js +115 -0
  17. package/lib/Transformation.js +536 -0
  18. package/lib/UncommittedAsset.js +342 -0
  19. package/lib/Validation.js +215 -0
  20. package/lib/applyRuntimes.js +279 -0
  21. package/lib/assetUtils.js +189 -0
  22. package/lib/atlaspack-v3/AtlaspackV3.js +74 -0
  23. package/lib/atlaspack-v3/fs.js +30 -0
  24. package/lib/atlaspack-v3/index.js +19 -0
  25. package/lib/atlaspack-v3/jsCallable.js +15 -0
  26. package/lib/atlaspack-v3/plugins/Resolver.js +12 -0
  27. package/lib/atlaspack-v3/plugins/index.js +16 -0
  28. package/lib/atlaspack-v3/worker/index.js +3 -0
  29. package/lib/atlaspack-v3/worker/worker.js +30 -0
  30. package/lib/buildCache.js +18 -0
  31. package/lib/constants.js +21 -0
  32. package/lib/dumpGraphToGraphViz.js +206 -0
  33. package/lib/index.js +106 -0
  34. package/lib/loadAtlaspackPlugin.js +148 -0
  35. package/lib/loadDotEnv.js +54 -0
  36. package/lib/projectPath.js +86 -0
  37. package/lib/public/Asset.js +259 -0
  38. package/lib/public/Bundle.js +231 -0
  39. package/lib/public/BundleGraph.js +193 -0
  40. package/lib/public/BundleGroup.js +44 -0
  41. package/lib/public/Config.js +202 -0
  42. package/lib/public/Dependency.js +131 -0
  43. package/lib/public/Environment.js +244 -0
  44. package/lib/public/MutableBundleGraph.js +184 -0
  45. package/lib/public/PluginOptions.js +71 -0
  46. package/lib/public/Symbols.js +247 -0
  47. package/lib/public/Target.js +64 -0
  48. package/lib/registerCoreWithSerializer.js +45 -0
  49. package/lib/requests/AssetGraphRequest.js +427 -0
  50. package/lib/requests/AssetGraphRequestRust.js +187 -0
  51. package/lib/requests/AssetRequest.js +137 -0
  52. package/lib/requests/AtlaspackBuildRequest.js +78 -0
  53. package/lib/requests/AtlaspackConfigRequest.js +473 -0
  54. package/lib/requests/BundleGraphRequest.js +419 -0
  55. package/lib/requests/ConfigRequest.js +198 -0
  56. package/lib/requests/DevDepRequest.js +166 -0
  57. package/lib/requests/EntryRequest.js +295 -0
  58. package/lib/requests/PackageRequest.js +88 -0
  59. package/lib/requests/PathRequest.js +349 -0
  60. package/lib/requests/TargetRequest.js +1177 -0
  61. package/lib/requests/ValidationRequest.js +66 -0
  62. package/lib/requests/WriteBundleRequest.js +252 -0
  63. package/lib/requests/WriteBundlesRequest.js +153 -0
  64. package/lib/resolveOptions.js +234 -0
  65. package/lib/serializer.js +216 -0
  66. package/lib/serializerCore.browser.js +29 -0
  67. package/lib/serializerCore.js +16 -0
  68. package/lib/summarizeRequest.js +55 -0
  69. package/lib/types.js +35 -0
  70. package/lib/utils.js +160 -0
  71. package/lib/worker.js +170 -0
  72. package/package.json +59 -0
  73. package/src/AssetGraph.js +646 -0
  74. package/src/Atlaspack.js +632 -0
  75. package/src/AtlaspackConfig.js +490 -0
  76. package/src/AtlaspackConfig.schema.js +141 -0
  77. package/src/BundleGraph.js +2193 -0
  78. package/src/CommittedAsset.js +143 -0
  79. package/src/Dependency.js +142 -0
  80. package/src/Environment.js +151 -0
  81. package/src/InternalConfig.js +72 -0
  82. package/src/PackagerRunner.js +790 -0
  83. package/src/ReporterRunner.js +158 -0
  84. package/src/RequestTracker.js +1697 -0
  85. package/src/SymbolPropagation.js +794 -0
  86. package/src/TargetDescriptor.schema.js +136 -0
  87. package/src/Transformation.js +752 -0
  88. package/src/UncommittedAsset.js +426 -0
  89. package/src/Validation.js +223 -0
  90. package/src/applyRuntimes.js +341 -0
  91. package/src/assetUtils.js +254 -0
  92. package/src/atlaspack-v3/AtlaspackV3.js +73 -0
  93. package/src/atlaspack-v3/fs.js +36 -0
  94. package/src/atlaspack-v3/index.js +5 -0
  95. package/src/atlaspack-v3/jsCallable.js +13 -0
  96. package/src/atlaspack-v3/plugins/Resolver.js +9 -0
  97. package/src/atlaspack-v3/plugins/index.js +3 -0
  98. package/src/atlaspack-v3/worker/index.js +8 -0
  99. package/src/atlaspack-v3/worker/worker.js +14 -0
  100. package/src/buildCache.js +15 -0
  101. package/src/constants.js +22 -0
  102. package/src/dumpGraphToGraphViz.js +244 -0
  103. package/src/index.js +22 -0
  104. package/src/loadAtlaspackPlugin.js +239 -0
  105. package/src/loadDotEnv.js +56 -0
  106. package/src/projectPath.js +87 -0
  107. package/src/public/Asset.js +359 -0
  108. package/src/public/Bundle.js +337 -0
  109. package/src/public/BundleGraph.js +335 -0
  110. package/src/public/BundleGroup.js +55 -0
  111. package/src/public/Config.js +261 -0
  112. package/src/public/Dependency.js +176 -0
  113. package/src/public/Environment.js +316 -0
  114. package/src/public/MutableBundleGraph.js +320 -0
  115. package/src/public/PluginOptions.js +99 -0
  116. package/src/public/Symbols.js +315 -0
  117. package/src/public/Target.js +71 -0
  118. package/src/registerCoreWithSerializer.js +34 -0
  119. package/src/requests/AssetGraphRequest.js +577 -0
  120. package/src/requests/AssetGraphRequestRust.js +222 -0
  121. package/src/requests/AssetRequest.js +179 -0
  122. package/src/requests/AtlaspackBuildRequest.js +109 -0
  123. package/src/requests/AtlaspackConfigRequest.js +709 -0
  124. package/src/requests/BundleGraphRequest.js +547 -0
  125. package/src/requests/ConfigRequest.js +287 -0
  126. package/src/requests/DevDepRequest.js +246 -0
  127. package/src/requests/EntryRequest.js +385 -0
  128. package/src/requests/PackageRequest.js +105 -0
  129. package/src/requests/PathRequest.js +454 -0
  130. package/src/requests/TargetRequest.js +1632 -0
  131. package/src/requests/ValidationRequest.js +79 -0
  132. package/src/requests/WriteBundleRequest.js +362 -0
  133. package/src/requests/WriteBundlesRequest.js +209 -0
  134. package/src/resolveOptions.js +278 -0
  135. package/src/serializer.js +255 -0
  136. package/src/serializerCore.browser.js +8 -0
  137. package/src/serializerCore.js +5 -0
  138. package/src/summarizeRequest.js +47 -0
  139. package/src/types.js +581 -0
  140. package/src/utils.js +211 -0
  141. package/src/worker.js +189 -0
  142. package/test/AssetGraph.test.js +679 -0
  143. package/test/Atlaspack.test.js +142 -0
  144. package/test/AtlaspackConfig.test.js +405 -0
  145. package/test/AtlaspackConfigRequest.test.js +993 -0
  146. package/test/BundleGraph.test.js +121 -0
  147. package/test/EntryRequest.test.js +215 -0
  148. package/test/Environment.test.js +99 -0
  149. package/test/InternalAsset.test.js +76 -0
  150. package/test/PackagerRunner.test.js +27 -0
  151. package/test/PublicAsset.test.js +40 -0
  152. package/test/PublicBundle.test.js +68 -0
  153. package/test/PublicDependency.test.js +22 -0
  154. package/test/PublicEnvironment.test.js +27 -0
  155. package/test/PublicMutableBundleGraph.test.js +192 -0
  156. package/test/RequestTracker.test.js +448 -0
  157. package/test/SymbolPropagation.test.js +716 -0
  158. package/test/TargetRequest.test.js +1715 -0
  159. package/test/fixtures/application-targets/package.json +3 -0
  160. package/test/fixtures/atlaspack/index.js +1 -0
  161. package/test/fixtures/atlaspack/other.js +3 -0
  162. package/test/fixtures/atlaspack/package.json +1 -0
  163. package/test/fixtures/atlaspack/yarn.lock +0 -0
  164. package/test/fixtures/bundle.js +10 -0
  165. package/test/fixtures/common-targets/package.json +24 -0
  166. package/test/fixtures/common-targets-ignore/package.json +13 -0
  167. package/test/fixtures/config/.atlaspackrc +6 -0
  168. package/test/fixtures/config/subfolder/.atlaspackrc +6 -0
  169. package/test/fixtures/config-extends-not-found/.atlaspackrc +3 -0
  170. package/test/fixtures/config-extends-not-found/.atlaspackrc-json5 +3 -0
  171. package/test/fixtures/config-extends-not-found/.atlaspackrc-multiple +3 -0
  172. package/test/fixtures/config-extends-not-found/.atlaspackrc-node-modules +3 -0
  173. package/test/fixtures/config-malformed/.atlaspackrc +3 -0
  174. package/test/fixtures/config-node-pipeline/.atlaspackrc +6 -0
  175. package/test/fixtures/config-plugin-not-found/.atlaspackrc +6 -0
  176. package/test/fixtures/context/package.json +8 -0
  177. package/test/fixtures/custom-format-infer-ext/package.json +6 -0
  178. package/test/fixtures/custom-format-infer-type/package.json +7 -0
  179. package/test/fixtures/custom-format-mismatch/package.json +8 -0
  180. package/test/fixtures/custom-targets/package.json +20 -0
  181. package/test/fixtures/custom-targets-distdir/package.json +11 -0
  182. package/test/fixtures/duplicate-targets/package.json +4 -0
  183. package/test/fixtures/glob-like/[entry].js +0 -0
  184. package/test/fixtures/invalid-distpath/package.json +11 -0
  185. package/test/fixtures/invalid-engines/package.json +12 -0
  186. package/test/fixtures/invalid-source-missing/package.json +5 -0
  187. package/test/fixtures/invalid-source-not-file/package.json +5 -0
  188. package/test/fixtures/invalid-source-not-file/src/index.js +1 -0
  189. package/test/fixtures/invalid-target-source-missing/package.json +9 -0
  190. package/test/fixtures/invalid-target-source-not-file/package.json +9 -0
  191. package/test/fixtures/invalid-target-source-not-file/src/index.js +1 -0
  192. package/test/fixtures/invalid-targets/package.json +19 -0
  193. package/test/fixtures/library-custom-scopehoist/package.json +9 -0
  194. package/test/fixtures/library-scopehoist/package.json +8 -0
  195. package/test/fixtures/local-plugin-config-pkg/.atlaspackrc +3 -0
  196. package/test/fixtures/local-plugin-config-pkg/node_modules/atlaspack-config-local/index.json +8 -0
  197. package/test/fixtures/local-plugin-config-pkg/node_modules/atlaspack-config-local/local-plugin.js +7 -0
  198. package/test/fixtures/local-plugin-config-pkg/node_modules/atlaspack-config-local/package.json +7 -0
  199. package/test/fixtures/main-format-mismatch/package.json +8 -0
  200. package/test/fixtures/main-global/package.json +8 -0
  201. package/test/fixtures/main-mjs/package.json +8 -0
  202. package/test/fixtures/module-a.js +1 -0
  203. package/test/fixtures/module-b.js +1 -0
  204. package/test/fixtures/plugins/local-plugin.js +7 -0
  205. package/test/fixtures/plugins/node_modules/atlaspack-transformer-bad-engines/index.js +7 -0
  206. package/test/fixtures/plugins/node_modules/atlaspack-transformer-bad-engines/package.json +7 -0
  207. package/test/fixtures/plugins/node_modules/atlaspack-transformer-no-engines/index.js +7 -0
  208. package/test/fixtures/plugins/node_modules/atlaspack-transformer-no-engines/package.json +4 -0
  209. package/test/fixtures/targets-default-distdir-none/package.json +5 -0
  210. package/test/fixtures/targets-default-distdir-one/package.json +8 -0
  211. package/test/fixtures/targets-default-distdir-two/package.json +18 -0
  212. package/test/requests/ConfigRequest.test.js +254 -0
  213. package/test/serializer.test.js +302 -0
  214. package/test/test-utils.js +80 -0
  215. package/test/utils.test.js +43 -0
@@ -0,0 +1,13 @@
1
+ // @flow strict-local
2
+
3
+ export function jsCallable<Args: $ReadOnlyArray<mixed>, Return>(
4
+ fn: (...Args) => Return,
5
+ ): (...Args) => Return {
6
+ return (...args: Args) => {
7
+ try {
8
+ return fn(...args);
9
+ } catch (err) {
10
+ return err;
11
+ }
12
+ };
13
+ }
@@ -0,0 +1,9 @@
1
+ // @flow
2
+
3
+ export class ResolverNapi {
4
+ constructor() {}
5
+
6
+ loadConfig() {}
7
+
8
+ resolve() {}
9
+ }
@@ -0,0 +1,3 @@
1
+ // @flow
2
+
3
+ export * from './Resolver';
@@ -0,0 +1,8 @@
1
+ if (
2
+ process.env.ATLASPACK_BUILD_ENV !== 'production' ||
3
+ process.env.ATLASPACK_SELF_BUILD
4
+ ) {
5
+ require('@atlaspack/babel-register');
6
+ }
7
+
8
+ require('./worker');
@@ -0,0 +1,14 @@
1
+ // @flow
2
+ import * as napi from '@atlaspack/rust';
3
+ import {workerData} from 'worker_threads';
4
+ import type {ResolverNapi} from '../plugins/Resolver';
5
+
6
+ export class AtlaspackWorker {
7
+ #resolvers: Map<string, ResolverNapi>;
8
+
9
+ ping() {
10
+ // console.log('Hi');
11
+ }
12
+ }
13
+
14
+ napi.registerWorker(workerData.tx_worker, new AtlaspackWorker());
@@ -0,0 +1,15 @@
1
+ // @flow
2
+
3
+ const buildCaches: Array<Map<any, any>> = [];
4
+
5
+ export function createBuildCache<K, V>(): Map<K, V> {
6
+ let cache = new Map<K, V>();
7
+ buildCaches.push(cache);
8
+ return cache;
9
+ }
10
+
11
+ export function clearBuildCaches() {
12
+ for (let cache of buildCaches) {
13
+ cache.clear();
14
+ }
15
+ }
@@ -0,0 +1,22 @@
1
+ // @flow strict-local
2
+
3
+ // $FlowFixMe
4
+ import {version} from '../package.json';
5
+
6
+ export const ATLASPACK_VERSION = version;
7
+ export const HASH_REF_PREFIX = 'HASH_REF_';
8
+ export const HASH_REF_HASH_LEN = 16;
9
+ export const HASH_REF_REGEX: RegExp = new RegExp(
10
+ `${HASH_REF_PREFIX}\\w{${HASH_REF_HASH_LEN}}`,
11
+ 'g',
12
+ );
13
+
14
+ export const VALID = 0;
15
+ export const INITIAL_BUILD = 1 << 0;
16
+ export const FILE_CREATE = 1 << 1;
17
+ export const FILE_UPDATE = 1 << 2;
18
+ export const FILE_DELETE = 1 << 3;
19
+ export const ENV_CHANGE = 1 << 4;
20
+ export const OPTION_CHANGE = 1 << 5;
21
+ export const STARTUP = 1 << 6;
22
+ export const ERROR = 1 << 7;
@@ -0,0 +1,244 @@
1
+ // @flow
2
+
3
+ import type {Asset, BundleBehavior} from '@atlaspack/types';
4
+ import type {Graph} from '@atlaspack/graph';
5
+ import type {AssetGraphNode, BundleGraphNode, Environment} from './types';
6
+ import {bundleGraphEdgeTypes} from './BundleGraph';
7
+ import {requestGraphEdgeTypes} from './RequestTracker';
8
+
9
+ import path from 'path';
10
+ import {fromNodeId} from '@atlaspack/graph';
11
+ import {fromProjectPathRelative} from './projectPath';
12
+ import {SpecifierType, Priority} from './types';
13
+
14
+ const COLORS = {
15
+ root: 'gray',
16
+ asset: 'green',
17
+ dependency: 'orange',
18
+ transformer_request: 'cyan',
19
+ file: 'gray',
20
+ default: 'white',
21
+ };
22
+
23
+ const TYPE_COLORS = {
24
+ // bundle graph
25
+ bundle: 'blue',
26
+ contains: 'grey',
27
+ internal_async: 'orange',
28
+ references: 'red',
29
+ sibling: 'green',
30
+ // asset graph
31
+ // request graph
32
+ invalidated_by_create: 'green',
33
+ invalidated_by_create_above: 'orange',
34
+ invalidate_by_update: 'cyan',
35
+ invalidated_by_delete: 'red',
36
+ };
37
+
38
+ export default async function dumpGraphToGraphViz(
39
+ graph:
40
+ | Graph<AssetGraphNode>
41
+ | Graph<{|
42
+ assets: Set<Asset>,
43
+ sourceBundles: Set<number>,
44
+ bundleBehavior?: ?BundleBehavior,
45
+ |}>
46
+ | Graph<BundleGraphNode>,
47
+ name: string,
48
+ edgeTypes?: typeof bundleGraphEdgeTypes | typeof requestGraphEdgeTypes,
49
+ ): Promise<void> {
50
+ if (
51
+ process.env.ATLASPACK_BUILD_ENV === 'production' &&
52
+ !process.env.ATLASPACK_BUILD_REPL
53
+ ) {
54
+ return;
55
+ }
56
+
57
+ let mode: ?string = process.env.ATLASPACK_BUILD_REPL
58
+ ? // $FlowFixMe
59
+ globalThis.ATLASPACK_DUMP_GRAPHVIZ?.mode
60
+ : process.env.ATLASPACK_DUMP_GRAPHVIZ;
61
+
62
+ // $FlowFixMe[invalid-compare]
63
+ if (mode == null || mode == false) {
64
+ return;
65
+ }
66
+
67
+ let detailedSymbols = mode === 'symbols';
68
+
69
+ let GraphVizGraph = require('graphviz/lib/deps/graph').Graph;
70
+ let g = new GraphVizGraph(null, 'G');
71
+ g.type = 'digraph';
72
+ // $FlowFixMe
73
+ for (let [id, node] of graph.nodes.entries()) {
74
+ if (node == null) continue;
75
+ let n = g.addNode(nodeId(id));
76
+ // $FlowFixMe default is fine. Not every type needs to be in the map.
77
+ n.set('color', COLORS[node.type || 'default']);
78
+ n.set('shape', 'box');
79
+ n.set('style', 'filled');
80
+ let label;
81
+ if (typeof node === 'string') {
82
+ label = node;
83
+ } else if (node.assets) {
84
+ label = `(${nodeId(id)}), (assetIds: ${[...node.assets]
85
+ .map(a => {
86
+ let arr = a.filePath.split('/');
87
+ return arr[arr.length - 1];
88
+ })
89
+ .join(', ')}) (sourceBundles: ${[...node.sourceBundles].join(
90
+ ', ',
91
+ )}) (bb ${node.bundleBehavior ?? 'none'})`;
92
+ } else if (node.type) {
93
+ label = `[${fromNodeId(id)}] ${node.type || 'No Type'}: [${node.id}]: `;
94
+ if (node.type === 'dependency') {
95
+ label += node.value.specifier;
96
+ let parts = [];
97
+ if (node.value.priority !== Priority.sync) {
98
+ parts.push(
99
+ Object.entries(Priority).find(
100
+ ([, v]) => v === node.value.priority,
101
+ )?.[0],
102
+ );
103
+ }
104
+ if (node.value.isOptional) parts.push('optional');
105
+ if (node.value.specifierType === SpecifierType.url) parts.push('url');
106
+ if (node.hasDeferred) parts.push('deferred');
107
+ if (node.deferred) parts.push('deferred');
108
+ if (node.excluded) parts.push('excluded');
109
+ if (parts.length) label += ' (' + parts.join(', ') + ')';
110
+ if (node.value.env) label += ` (${getEnvDescription(node.value.env)})`;
111
+ let depSymbols = node.value.symbols;
112
+ if (detailedSymbols) {
113
+ if (depSymbols) {
114
+ if (depSymbols.size) {
115
+ label +=
116
+ '\\nsymbols: ' +
117
+ [...depSymbols].map(([e, {local}]) => [e, local]).join(';');
118
+ }
119
+ let weakSymbols = [...depSymbols]
120
+ .filter(([, {isWeak}]) => isWeak)
121
+ .map(([s]) => s);
122
+ if (weakSymbols.length) {
123
+ label += '\\nweakSymbols: ' + weakSymbols.join(',');
124
+ }
125
+ if (node.usedSymbolsUp.size > 0) {
126
+ label +=
127
+ '\\nusedSymbolsUp: ' +
128
+ [...node.usedSymbolsUp]
129
+ .map(([s, sAsset]) =>
130
+ sAsset
131
+ ? `${s}(${sAsset.asset}.${sAsset.symbol ?? ''})`
132
+ : sAsset === null
133
+ ? `${s}(external)`
134
+ : `${s}(ambiguous)`,
135
+ )
136
+ .join(',');
137
+ }
138
+ if (node.usedSymbolsDown.size > 0) {
139
+ label +=
140
+ '\\nusedSymbolsDown: ' + [...node.usedSymbolsDown].join(',');
141
+ }
142
+ // if (node.usedSymbolsDownDirty) label += '\\nusedSymbolsDownDirty';
143
+ // if (node.usedSymbolsUpDirtyDown)
144
+ // label += '\\nusedSymbolsUpDirtyDown';
145
+ // if (node.usedSymbolsUpDirtyUp) label += '\\nusedSymbolsUpDirtyUp';
146
+ } else {
147
+ label += '\\nsymbols: cleared';
148
+ }
149
+ }
150
+ } else if (node.type === 'asset') {
151
+ label +=
152
+ path.basename(fromProjectPathRelative(node.value.filePath)) +
153
+ '#' +
154
+ node.value.type;
155
+ if (detailedSymbols) {
156
+ if (!node.value.symbols) {
157
+ label += '\\nsymbols: cleared';
158
+ } else if (node.value.symbols.size) {
159
+ label +=
160
+ '\\nsymbols: ' +
161
+ [...node.value.symbols]
162
+ .map(([e, {local}]) => [e, local])
163
+ .join(';');
164
+ }
165
+ if (node.usedSymbols.size) {
166
+ label += '\\nusedSymbols: ' + [...node.usedSymbols].join(',');
167
+ }
168
+ // if (node.usedSymbolsDownDirty) label += '\\nusedSymbolsDownDirty';
169
+ // if (node.usedSymbolsUpDirty) label += '\\nusedSymbolsUpDirty';
170
+ } else {
171
+ label += '\\nsymbols: cleared';
172
+ }
173
+ } else if (node.type === 'asset_group') {
174
+ if (node.deferred) label += '(deferred)';
175
+ } else if (node.type === 'file') {
176
+ label += path.basename(node.id);
177
+ } else if (node.type === 'transformer_request') {
178
+ label +=
179
+ path.basename(node.value.filePath) +
180
+ ` (${getEnvDescription(node.value.env)})`;
181
+ } else if (node.type === 'bundle') {
182
+ let parts = [];
183
+ if (node.value.needsStableName) parts.push('stable name');
184
+ parts.push(node.value.name);
185
+ parts.push('bb:' + (node.value.bundleBehavior ?? 'null'));
186
+ if (node.value.isPlaceholder) parts.push('placeholder');
187
+ if (parts.length) label += ' (' + parts.join(', ') + ')';
188
+ if (node.value.env) label += ` (${getEnvDescription(node.value.env)})`;
189
+ } else if (node.type === 'request') {
190
+ label = node.requestType + ':' + node.id;
191
+ }
192
+ }
193
+ n.set('label', label);
194
+ }
195
+
196
+ let edgeNames;
197
+ if (edgeTypes) {
198
+ edgeNames = Object.fromEntries(
199
+ Object.entries(edgeTypes).map(([k, v]) => [v, k]),
200
+ );
201
+ }
202
+
203
+ for (let edge of graph.getAllEdges()) {
204
+ let gEdge = g.addEdge(nodeId(edge.from), nodeId(edge.to));
205
+ let color = null;
206
+ if (edge.type != 1 && edgeNames) {
207
+ color = TYPE_COLORS[edgeNames[edge.type]];
208
+ }
209
+ if (color != null) {
210
+ gEdge.set('color', color);
211
+ }
212
+ }
213
+
214
+ if (process.env.ATLASPACK_BUILD_REPL) {
215
+ // $FlowFixMe
216
+ globalThis.ATLASPACK_DUMP_GRAPHVIZ?.(name, g.to_dot());
217
+ } else {
218
+ const tempy = require('tempy');
219
+ let tmp = tempy.file({name: `atlaspack-${name}.png`});
220
+ await g.output('png', tmp);
221
+ // eslint-disable-next-line no-console
222
+ console.log('Dumped', tmp);
223
+ }
224
+ }
225
+
226
+ function nodeId(id) {
227
+ // $FlowFixMe
228
+ return `node${id}`;
229
+ }
230
+
231
+ function getEnvDescription(env: Environment) {
232
+ let description;
233
+ if (typeof env.engines.browsers === 'string') {
234
+ description = `${env.context}: ${env.engines.browsers}`;
235
+ } else if (Array.isArray(env.engines.browsers)) {
236
+ description = `${env.context}: ${env.engines.browsers.join(', ')}`;
237
+ } else if (env.engines.node) {
238
+ description = `node: ${env.engines.node}`;
239
+ } else if (env.engines.electron) {
240
+ description = `electron: ${env.engines.electron}`;
241
+ }
242
+
243
+ return description ?? '';
244
+ }
package/src/index.js ADDED
@@ -0,0 +1,22 @@
1
+ // @flow
2
+
3
+ // Needs to be exported first because of circular imports
4
+ export {
5
+ registerSerializableClass,
6
+ unregisterSerializableClass,
7
+ prepareForSerialization,
8
+ restoreDeserializedObject,
9
+ serialize,
10
+ deserialize,
11
+ } from './serializer';
12
+
13
+ export {
14
+ default,
15
+ default as Atlaspack,
16
+ BuildError,
17
+ createWorkerFarm,
18
+ INTERNAL_RESOLVE,
19
+ INTERNAL_TRANSFORM,
20
+ } from './Atlaspack';
21
+
22
+ export * from './atlaspack-v3';
@@ -0,0 +1,239 @@
1
+ // @flow
2
+ import type {
3
+ FilePath,
4
+ PackageName,
5
+ Semver,
6
+ SemverRange,
7
+ } from '@atlaspack/types';
8
+ import type {AtlaspackOptions} from './types';
9
+
10
+ import path from 'path';
11
+ import semver from 'semver';
12
+ import logger from '@atlaspack/logger';
13
+ import nullthrows from 'nullthrows';
14
+ import ThrowableDiagnostic, {
15
+ generateJSONCodeHighlights,
16
+ md,
17
+ } from '@atlaspack/diagnostic';
18
+ import {
19
+ findAlternativeNodeModules,
20
+ loadConfig,
21
+ resolveConfig,
22
+ } from '@atlaspack/utils';
23
+ import {type ProjectPath, toProjectPath} from './projectPath';
24
+ import {version as ATLASPACK_VERSION} from '../package.json';
25
+
26
+ const NODE_MODULES = `${path.sep}node_modules${path.sep}`;
27
+ const CONFIG = Symbol.for('atlaspack-plugin-config');
28
+
29
+ export default async function loadPlugin<T>(
30
+ pluginName: PackageName,
31
+ configPath: FilePath,
32
+ keyPath?: string,
33
+ options: AtlaspackOptions,
34
+ ): Promise<{|
35
+ plugin: T,
36
+ version: Semver,
37
+ resolveFrom: ProjectPath,
38
+ range: ?SemverRange,
39
+ |}> {
40
+ let resolveFrom = configPath;
41
+ let range;
42
+ if (resolveFrom.includes(NODE_MODULES)) {
43
+ // Config packages can reference plugins, but cannot contain other plugins within them.
44
+ // This forces every published plugin to be published separately so they can be mixed and matched if needed.
45
+ if (pluginName.startsWith('.')) {
46
+ let configContents = await options.inputFS.readFile(configPath, 'utf8');
47
+ throw new ThrowableDiagnostic({
48
+ diagnostic: {
49
+ message: md`Local plugins are not supported in Atlaspack config packages. Please publish "${pluginName}" as a separate npm package.`,
50
+ origin: '@atlaspack/core',
51
+ codeFrames: keyPath
52
+ ? [
53
+ {
54
+ filePath: configPath,
55
+ language: 'json5',
56
+ code: configContents,
57
+ codeHighlights: generateJSONCodeHighlights(configContents, [
58
+ {
59
+ key: keyPath,
60
+ type: 'value',
61
+ },
62
+ ]),
63
+ },
64
+ ]
65
+ : undefined,
66
+ },
67
+ });
68
+ }
69
+
70
+ let configPkg = await loadConfig(
71
+ options.inputFS,
72
+ resolveFrom,
73
+ ['package.json'],
74
+ options.projectRoot,
75
+ );
76
+ if (
77
+ configPkg != null &&
78
+ configPkg.config.dependencies?.[pluginName] == null
79
+ ) {
80
+ // If not in the config's dependencies, the plugin will be auto installed with
81
+ // the version declared in "atlaspackDependencies".
82
+ range = configPkg.config.atlaspackDependencies?.[pluginName];
83
+
84
+ if (range == null) {
85
+ let contents = await options.inputFS.readFile(
86
+ configPkg.files[0].filePath,
87
+ 'utf8',
88
+ );
89
+ throw new ThrowableDiagnostic({
90
+ diagnostic: {
91
+ message: md`Could not determine version of ${pluginName} in ${path.relative(
92
+ process.cwd(),
93
+ resolveFrom,
94
+ )}. Either include it in "dependencies" or "atlaspackDependencies".`,
95
+ origin: '@atlaspack/core',
96
+ codeFrames:
97
+ configPkg.config.dependencies ||
98
+ configPkg.config.atlaspackDependencies
99
+ ? [
100
+ {
101
+ filePath: configPkg.files[0].filePath,
102
+ language: 'json5',
103
+ code: contents,
104
+ codeHighlights: generateJSONCodeHighlights(contents, [
105
+ {
106
+ key: configPkg.config.atlaspackDependencies
107
+ ? '/atlaspackDependencies'
108
+ : '/dependencies',
109
+ type: 'key',
110
+ },
111
+ ]),
112
+ },
113
+ ]
114
+ : undefined,
115
+ },
116
+ });
117
+ }
118
+
119
+ // Resolve from project root if not in the config's dependencies.
120
+ resolveFrom = path.join(options.projectRoot, 'index');
121
+ }
122
+ }
123
+
124
+ let resolved, pkg;
125
+ try {
126
+ ({resolved, pkg} = await options.packageManager.resolve(
127
+ pluginName,
128
+ resolveFrom,
129
+ {
130
+ shouldAutoInstall: options.shouldAutoInstall,
131
+ range,
132
+ },
133
+ ));
134
+ } catch (err) {
135
+ if (err.code !== 'MODULE_NOT_FOUND') {
136
+ throw err;
137
+ }
138
+
139
+ let configContents = await options.inputFS.readFile(configPath, 'utf8');
140
+ let alternatives = await findAlternativeNodeModules(
141
+ options.inputFS,
142
+ pluginName,
143
+ path.dirname(resolveFrom),
144
+ );
145
+ throw new ThrowableDiagnostic({
146
+ diagnostic: {
147
+ message: md`Cannot find Atlaspack plugin "${pluginName}"`,
148
+ origin: '@atlaspack/core',
149
+ codeFrames: keyPath
150
+ ? [
151
+ {
152
+ filePath: configPath,
153
+ language: 'json5',
154
+ code: configContents,
155
+ codeHighlights: generateJSONCodeHighlights(configContents, [
156
+ {
157
+ key: keyPath,
158
+ type: 'value',
159
+ message: md`Cannot find module "${pluginName}"${
160
+ alternatives[0]
161
+ ? `, did you mean "${alternatives[0]}"?`
162
+ : ''
163
+ }`,
164
+ },
165
+ ]),
166
+ },
167
+ ]
168
+ : undefined,
169
+ },
170
+ });
171
+ }
172
+
173
+ // Remove plugin version compatiblility validation in canary builds as they don't use semver
174
+ if (!process.env.SKIP_PLUGIN_COMPATIBILITY_CHECK) {
175
+ if (!pluginName.startsWith('.')) {
176
+ // Validate the engines.atlaspack field in the plugin's package.json
177
+ let atlaspackVersionRange = pkg && pkg.engines && pkg.engines.atlaspack;
178
+ if (!atlaspackVersionRange) {
179
+ logger.warn({
180
+ origin: '@atlaspack/core',
181
+ message: `The plugin "${pluginName}" needs to specify a \`package.json#engines.atlaspack\` field with the supported Atlaspack version range.`,
182
+ });
183
+ }
184
+
185
+ if (
186
+ atlaspackVersionRange &&
187
+ !semver.satisfies(ATLASPACK_VERSION, atlaspackVersionRange)
188
+ ) {
189
+ let pkgFile = nullthrows(
190
+ await resolveConfig(
191
+ options.inputFS,
192
+ resolved,
193
+ ['package.json'],
194
+ options.projectRoot,
195
+ ),
196
+ );
197
+ let pkgContents = await options.inputFS.readFile(pkgFile, 'utf8');
198
+ throw new ThrowableDiagnostic({
199
+ diagnostic: {
200
+ message: md`The plugin "${pluginName}" is not compatible with the current version of Atlaspack. Requires "${atlaspackVersionRange}" but the current version is "${ATLASPACK_VERSION}".`,
201
+ origin: '@atlaspack/core',
202
+ codeFrames: [
203
+ {
204
+ filePath: pkgFile,
205
+ language: 'json5',
206
+ code: pkgContents,
207
+ codeHighlights: generateJSONCodeHighlights(pkgContents, [
208
+ {
209
+ key: '/engines/atlaspack',
210
+ },
211
+ ]),
212
+ },
213
+ ],
214
+ },
215
+ });
216
+ }
217
+ }
218
+ }
219
+
220
+ let plugin = await options.packageManager.require(pluginName, resolveFrom, {
221
+ shouldAutoInstall: options.shouldAutoInstall,
222
+ });
223
+ plugin = plugin.default ? plugin.default : plugin;
224
+ if (!plugin) {
225
+ throw new Error(`Plugin ${pluginName} has no exports.`);
226
+ }
227
+ plugin = plugin[CONFIG];
228
+ if (!plugin) {
229
+ throw new Error(
230
+ `Plugin ${pluginName} is not a valid Atlaspack plugin, should export an instance of a Atlaspack plugin ex. "export default new Reporter({ ... })".`,
231
+ );
232
+ }
233
+ return {
234
+ plugin,
235
+ version: nullthrows(pkg).version,
236
+ resolveFrom: toProjectPath(options.projectRoot, resolveFrom),
237
+ range,
238
+ };
239
+ }
@@ -0,0 +1,56 @@
1
+ // @flow strict-local
2
+
3
+ import type {FileSystem} from '@atlaspack/fs';
4
+ import type {EnvMap, FilePath} from '@atlaspack/types';
5
+
6
+ import {resolveConfig} from '@atlaspack/utils';
7
+ import dotenv from 'dotenv';
8
+ import variableExpansion from 'dotenv-expand';
9
+
10
+ export default async function loadEnv(
11
+ env: EnvMap,
12
+ fs: FileSystem,
13
+ filePath: FilePath,
14
+ projectRoot: FilePath,
15
+ ): Promise<EnvMap> {
16
+ const NODE_ENV = env.NODE_ENV ?? 'development';
17
+
18
+ const dotenvFiles = [
19
+ '.env',
20
+ // Don't include `.env.local` for `test` environment
21
+ // since normally you expect tests to produce the same
22
+ // results for everyone
23
+ NODE_ENV === 'test' ? null : '.env.local',
24
+ `.env.${NODE_ENV}`,
25
+ `.env.${NODE_ENV}.local`,
26
+ ].filter(Boolean);
27
+
28
+ let envs = await Promise.all(
29
+ dotenvFiles.map(async dotenvFile => {
30
+ const envPath = await resolveConfig(
31
+ fs,
32
+ filePath,
33
+ [dotenvFile],
34
+ projectRoot,
35
+ );
36
+ if (envPath == null) {
37
+ return;
38
+ }
39
+
40
+ // `ignoreProcessEnv` prevents dotenv-expand from writing values into `process.env`:
41
+ // https://github.com/motdotla/dotenv-expand/blob/ddb73d02322fe8522b4e05b73e1c1ad24ea7c14a/lib/main.js#L5
42
+ let output = variableExpansion({
43
+ parsed: dotenv.parse(await fs.readFile(envPath)),
44
+ ignoreProcessEnv: true,
45
+ });
46
+
47
+ if (output.error != null) {
48
+ throw output.error;
49
+ }
50
+
51
+ return output.parsed;
52
+ }),
53
+ );
54
+
55
+ return Object.assign({}, ...envs);
56
+ }