@exodus/stasis 1.0.0-beta.13 → 1.0.0-beta.15

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 (46) hide show
  1. package/README.md +5 -1
  2. package/bin/stasis.js +191 -149
  3. package/package.json +31 -7
  4. package/src/audit.js +224 -90
  5. package/src/babel.js +44 -37
  6. package/src/bundle-reader.js +24 -4
  7. package/src/cmd/build.js +218 -8
  8. package/src/cmd/bundle-options.js +31 -25
  9. package/src/cmd/bundle.js +1147 -311
  10. package/src/cmd/github-bundle.js +17 -9
  11. package/src/cmd/output.js +10 -3
  12. package/src/cmd/tokens.js +73 -0
  13. package/src/define.js +5 -0
  14. package/src/diff.js +2 -2
  15. package/src/flow.js +42 -0
  16. package/src/installed-commits.js +4 -2
  17. package/src/loaders/bash.js +2 -27
  18. package/src/loaders/cargo.js +6 -6
  19. package/src/loaders/foundry.js +4 -6
  20. package/src/loaders/paths.js +4 -15
  21. package/src/loaders/php.js +3 -5
  22. package/src/loaders/rust.js +19 -39
  23. package/src/loaders/solidity-ownership.js +4 -15
  24. package/src/loaders/solidity.js +4 -67
  25. package/src/metro-assets.js +65 -0
  26. package/src/metro-resolver.js +76 -123
  27. package/src/parse.js +7 -3
  28. package/src/resolve-fields.js +133 -366
  29. package/src/resolve-node.js +9 -7
  30. package/src/resolve-typescript.js +102 -30
  31. package/src/scan.js +154 -54
  32. package/src/sixel.js +333 -0
  33. package/src/table.js +116 -0
  34. package/src/validate-notes.d.ts +18 -0
  35. package/src/validate-notes.js +66 -0
  36. package/src/validate.d.ts +132 -0
  37. package/src/validate.js +1349 -0
  38. package/src/vfs-bundle/entries.js +5 -2
  39. package/src/vfs-bundle/github.js +68 -19
  40. package/src/vfs-bundle/glob.js +420 -0
  41. package/src/vfs-bundle/lazy.js +128 -0
  42. package/src/vfs-bundle/prisma.js +102 -29
  43. package/src/vfs-bundle/tree.js +238 -75
  44. package/src/vfs-bundle/yarnclean.js +100 -0
  45. package/src/vfs-bundle.js +16 -7
  46. package/src/why.js +9 -7
package/src/cmd/bundle.js CHANGED
@@ -1,26 +1,28 @@
1
1
  import { isUtf8 } from 'node:buffer'
2
2
  import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs'
3
- import { basename, dirname, extname, join, posix, relative, resolve } from 'node:path'
4
- import { fileURLToPath } from 'node:url'
5
- import { brotliDecompressSync } from 'node:zlib'
3
+ import { basename, dirname, extname, isAbsolute, join, posix, relative, resolve } from 'node:path'
4
+ import { fileURLToPath, pathToFileURL } from 'node:url'
6
5
 
7
6
  import { Bundle, isValidRepoField } from '@exodus/stasis-core/bundle'
8
7
  import { Lockfile } from '@exodus/stasis-core/lockfile'
9
8
  import { extraArtifacts } from '@exodus/stasis-core/add'
10
- import { Scan, importsTypescriptByOutputName, scan } from '../scan.js'
11
- import { parseDefine } from '../define.js'
12
- import { createFieldResolver, resolveConditions } from '../resolve-fields.js'
13
- import { discoverTsconfig, isDir, loadTsconfigPaths, packageTsconfigPaths } from '../resolve-typescript.js'
9
+ import { getParser, importsTypescriptByOutputName, isResolvableExt, literalSpec, scan, scanCounts } from '../scan.js'
10
+ import { METRO_RELEASE_DEFINE, parseDefine } from '../define.js'
11
+ import { METRO_ASSET_EXTS, METRO_ASSET_RESOLUTIONS, METRO_SOURCE_EXTS, createFieldResolver, resolveConditions } from '../resolve-fields.js'
12
+ import { discoverTsconfig, isDir, loadTsconfigPaths, packageTsconfigPaths, tsconfigOutputsOf } from '../resolve-typescript.js'
14
13
  import { createMetroResolver } from '../metro-resolver.js'
15
- import { createAliases, createBabelAliases } from '../babel.js'
16
- import { linkedNativePackages, looksNative, nativeInputs } from '@exodus/stasis-plugins/metro-native'
14
+ import { ASSET_REGISTRY } from '../metro-assets.js'
15
+ import { createAliases, createBabelAliases, createBabelTypeImportsKept } from '../babel.js'
16
+ import { AUTO_INCLUDES, RN_CORE_INCLUDE_FILES, linkedNativePackages, linkedPackages, listing, looksNative, metroAssetFiles, nativeInputs } from '@exodus/stasis-plugins/metro-native'
17
17
  import { pinInstalledCommits } from '../installed-commits.js'
18
18
  import { State } from '@exodus/stasis-core/state'
19
- import { readFileSyncMaybe, sha512integrity } from '@exodus/stasis-core/state-util'
20
- import { checkoutCommit, detectRepo, findPackageMetadata, normalizeEntries, packageJSONStat, packageType, parseGithubRepository, readJson, readModuleManifest, readPackageJson, readRegularFileOrNull, scopeManifestOf } from '@exodus/stasis-core/bundle-util'
21
- import { EMPTY_MODULE_PATH, RN_CORE_INCLUDE_FILES, assertRealPathWithinBase, hasNodeModulesSegment, isDotEnvFile, isExecutableFile, isPathWithin, isPlainObject, moduleFileKey, moduleInfo, parseExtraOption, parseResourcesOption, posixPathEscapes, relativeEscapes, splitNodeModulesPath, toPosix, withMetadataOf } from '@exodus/stasis-core/util'
19
+ import { deferredLockfile, readFileSyncMaybe } from '@exodus/stasis-core/state-util'
20
+ import { checkoutCommit, detectRepo, normalizeEntries, packageLookup, packageJSONStat, packageType, parseGithubRepository, readJson, readModuleManifest, readPackageJson, readRegularFileOrNull, scopeManifestOf } from '@exodus/stasis-core/bundle-util'
21
+ import { EMPTY_MODULE_PATH, assertRealPathWithinBase, classifyFormat, fileInBucket, hasNodeModulesSegment, isDotEnvFile, isPathWithin, isPlainObject, moduleFileKey, moduleInfo, nestedMap, nodeExtFormat, observeExecutable, parseExtraOption, parseGeneratePrisma, parseInstallDevDependencies, parseLazyDependencies, parseResourcesOption, posixPathEscapes, relativeEscapes, splitNodeModulesPath, toPosix, withMetadataOf } from '@exodus/stasis-core/util'
22
22
  import { diskHost } from '@exodus/stasis-core/host'
23
+ import { parseBundleBytes } from '@exodus/stasis-core/brotli'
23
24
  import { parseSoldeerLockfile } from '@preventive/lockfile/soldeer.js'
25
+ import { isAssetFile } from '@preventive/rn/metro-assets.js'
24
26
  import {
25
27
  SOLIDITY_PACKAGE_MANIFESTS,
26
28
  SOLIDITY_ROOT_MANIFESTS,
@@ -41,7 +43,8 @@ import {
41
43
  loadComposerPackages,
42
44
  loadLaravelProviderFiles,
43
45
  } from '../loaders/php.js'
44
- import { DEFAULT_BUNDLE_FILE, bundledSummary, packagesLabel, writeBundle, writeFile } from './output.js'
46
+ import { DEFAULT_BUNDLE_FILE, bundledSummary, packagesLabel, unpackedSummary, writeBundle, writeFile } from './output.js'
47
+ import { withNpmToken } from './tokens.js'
45
48
 
46
49
  const JS_EXTS = new Set(['.js', '.cjs', '.mjs', '.ts', '.cts', '.mts', '.jsx', '.tsx'])
47
50
  const BASH_EXTS = new Set(['.sh', '.bash'])
@@ -216,9 +219,6 @@ function makeRustClassifier(cargo) {
216
219
  }
217
220
  }
218
221
 
219
- // A file's key inside its bucket: the inverse of moduleFileKey.
220
- const fileInBucket = (bucketDir, path) => (bucketDir === '.' ? path : path.slice(bucketDir.length + 1))
221
-
222
222
  // A file of the bundle root (`realBase` its real path), its bytes, by project-relative path, or
223
223
  // null when it isn't a regular file there (a manifest the context saw may have gone since; a FIFO
224
224
  // would hang a read) or, with `within` a directory, doesn't really lie in it (a vendored crate's
@@ -241,7 +241,8 @@ function readFileWithinBase(baseDir, realBase, rel, within) {
241
241
  function executableSources(baseDir, sources, host) {
242
242
  const executable = new Set()
243
243
  for (const path of sources.keys()) {
244
- if (isExecutableFile(join(baseDir, path), host)) executable.add(path)
244
+ // A fresh set: a file that can't be looked at is just not marked.
245
+ if (observeExecutable(join(baseDir, path), host) === true) executable.add(path)
245
246
  }
246
247
  return executable
247
248
  }
@@ -268,11 +269,11 @@ function assertSelfContained(lang, noun, entries, sources, missing) {
268
269
  // (packageLookup by default, which walks past a malformed one). `classifyDep(path)` optionally
269
270
  // places a file directly (non-node_modules ecosystems like Soldeer/github); null defers. `format` tags
270
271
  // every file; `formats` (Map<path,format>) overrides it per file. `resolutions` values are
271
- // a flat target string or a Map<platform,target>; both round-trip untouched. `consumer` is
272
- // codeBundle's.
272
+ // a flat target string or a Map<platform,target>; both round-trip untouched. `keyedResolutions`,
273
+ // `consumer` and `consumerFiles` are codeBundle's.
273
274
  function assembleCodeBundle({
274
- baseDir, entries, sources, resolutions, workspaceName, workspaceVersion, format, formats, conditionKey, classifyDep, host,
275
- packageOf = packageLookup(baseDir, { host }), consumer,
275
+ baseDir, entries, sources, resolutions, keyedResolutions, workspaceName, workspaceVersion, format, formats, conditionKey, classifyDep, host,
276
+ packageOf = packageLookup(baseDir, { host }), consumer, consumerFiles,
276
277
  }) {
277
278
  const modules = new Map()
278
279
  // `identity`'s name, version, ecosystem, repo and vendored (moduleInfo takes those alone); a later
@@ -290,35 +291,30 @@ function assembleCodeBundle({
290
291
  continue
291
292
  }
292
293
  const meta = packageOf(path)
293
- const inNodeModules = splitNodeModulesPath(path) !== null
294
- if (meta) {
295
- if (inNodeModules && !hasNodeModulesSegment(meta.pkgDir)) {
296
- throw new Error(`No package.json with name+version found for ${path}`)
297
- }
298
- ensureBucket(meta.pkgDir, meta).files[fileInBucket(meta.pkgDir, path)] = content
299
- } else {
300
- if (inNodeModules) throw new Error(`No package.json with name+version found for ${path}`)
301
- ensureBucket('.', { name: workspaceName, version: workspaceVersion }).files[path] = content
294
+ // A node_modules file whose nearest package.json is the workspace root (or none at all) is a misconfigured dep -- refuse.
295
+ if (splitNodeModulesPath(path) !== null && !(meta && hasNodeModulesSegment(meta.pkgDir))) {
296
+ throw new Error(`No package.json with name+version found for ${path}`)
302
297
  }
298
+ const [dir, identity] = meta ? [meta.pkgDir, meta] : ['.', { name: workspaceName, version: workspaceVersion }]
299
+ ensureBucket(dir, identity).files[fileInBucket(dir, path)] = content
303
300
  }
304
301
 
305
- return codeBundle({ baseDir, entries, sources, modules, resolutions, format, formats, conditionKey, host, consumer })
302
+ return codeBundle({ baseDir, entries, sources, modules, resolutions, keyedResolutions, format, formats, conditionKey, host, consumer, consumerFiles })
306
303
  }
307
304
 
308
305
  // A full-scope code Bundle of `sources`, bucketed as `modules`: `format` tags every file, `formats`
309
306
  // (Map<path,format>) overrides it per file (a file neither tags has none), and the `resolutions` edges
310
- // are keyed under `conditionKey`. Every file is attributed to `consumer` in the `reason` map: `bundle`,
311
- // or `bundle:metro` for --metro.
312
- function codeBundle({ baseDir, entries, sources, modules, resolutions, format, formats, conditionKey, host, consumer = 'bundle' }) {
307
+ // are keyed under `conditionKey`, beside the `keyedResolutions` (conditions key -> parent -> specifier
308
+ // -> target) under keys of their own. The `consumerFiles` (every file, by default) are attributed to
309
+ // `consumer` in the `reason` map: `bundle`, or `bundle:metro` for --metro.
310
+ function codeBundle({ baseDir, entries, sources, modules, resolutions, keyedResolutions = new Map(), format, formats, conditionKey, host, consumer = 'bundle', consumerFiles }) {
313
311
  const formatsMap = new Map()
314
312
  for (const path of sources.keys()) {
315
313
  const fileFormat = formats?.get(path) ?? format
316
314
  if (fileFormat !== undefined) formatsMap.set(path, fileFormat)
317
315
  }
318
316
 
319
- const importsForKey = new Map()
320
- for (const [parent, specMap] of resolutions) importsForKey.set(parent, specMap)
321
- const imports = new Map([[conditionKey, importsForKey]])
317
+ const imports = new Map([[conditionKey, new Map(resolutions)], ...keyedResolutions])
322
318
 
323
319
  // Executable bits, straight off disk (a synthetic source with no file there is simply not
324
320
  // executable). Shell bundles lean on this most: `stasis extract` puts the +x back on the scripts.
@@ -332,23 +328,13 @@ function codeBundle({ baseDir, entries, sources, modules, resolutions, format, f
332
328
  formats: formatsMap,
333
329
  imports,
334
330
  executable,
335
- }).withReason(consumer)
331
+ }).withReason(consumer, consumerFiles)
336
332
  }
337
333
 
338
334
  // Files never carried, whatever reads them: `.env` files, and Hardhat's config, which is code
339
335
  // (both however they're cased).
340
336
  const neverCarried = (rel) => isDotEnvFile(rel) || posix.basename(rel).toLowerCase().startsWith('hardhat.config.')
341
337
 
342
- // findPackageMetadata (with `options`) once per directory, the only thing its answer depends on.
343
- function packageLookup(baseDir, options) {
344
- const byDir = new Map()
345
- return (path) => {
346
- const dir = posix.dirname(path)
347
- if (!byDir.has(dir)) byDir.set(dir, findPackageMetadata(baseDir, path, options))
348
- return byDir.get(dir)
349
- }
350
- }
351
-
352
338
  // The build-description files of a Solidity bundle (--manifests), as Map<path, text>, sorted:
353
339
  // `configFiles` (what discoverSolidityConfig read, whatever they're called), each of which must be
354
340
  // carried -- one outside the root, neverCarried, refused or gone is an error, as the bundle
@@ -398,7 +384,7 @@ function solidityManifests(baseDir, sources, configFiles, { classifyDep, package
398
384
 
399
385
  // An entry `stasis bundle` takes for a directory: one that is, or an extensionless path that
400
386
  // doesn't exist (a project without `script/` still bundles with `src test script`).
401
- export const isDirEntry = (abs, host = diskHost) => isDir(abs, host) || (extname(abs) === '' && host.stat(abs) === null)
387
+ const isDirEntry = (abs, host = diskHost) => isDir(abs, host) || (extname(abs) === '' && host.stat(abs) === null)
402
388
 
403
389
  // Whether `entry` (resolved against `cwd`) is a Solidity bundle's: a .sol file or a directory entry.
404
390
  export const isSolidityEntry = (entry, cwd = process.cwd(), host = diskHost) => entry.endsWith('.sol') || isDirEntry(resolve(cwd, entry), host)
@@ -752,9 +738,9 @@ function refuseSplit(split, baseDir) {
752
738
  }
753
739
  }
754
740
 
755
- // Classify scanner unresolved edges + parse errors into fatal (broken/divergent at load)
756
- // vs tolerated (a catchable runtime miss). Shared so the plain and --mainFields JS paths
757
- // gate identically. `baseDir` relativizes the paths in the emitted messages (see displayPath).
741
+ // Classify scanner unresolved edges into fatal (broken/divergent at load) vs tolerated (a
742
+ // catchable runtime miss); every parse error is fatal. Shared so the plain and --mainFields JS
743
+ // paths gate identically. `baseDir` relativizes the paths in the emitted messages (see displayPath).
758
744
  function analyzeScanner(scanner, { baseDir }) {
759
745
  const show = (url) => displayPath(url, baseDir)
760
746
  const staticReachable = eagerlyLinked(scanner)
@@ -762,11 +748,9 @@ function analyzeScanner(scanner, { baseDir }) {
762
748
  const fatal = scanner.unresolved
763
749
  .filter((u) => fatalUnresolved(u))
764
750
  .map((u) => `unresolved ${describeUnresolved(u, show)}`)
765
- // Fatal parse: an eagerly-linked module-family file, or any file the parser couldn't process.
766
- const fatalParse = (p) => staticReachable.has(p.url) && (p.format?.startsWith('module') === true || !p.recovered)
767
- for (const p of scanner.parseErrors) {
768
- if (fatalParse(p)) fatal.push(`parse error in ${show(p.url)}: ${p.message}`)
769
- }
751
+ // A file the parser couldn't read cleanly, however it is reached: its edges are unknown (a module
752
+ // file's are discarded) or partial (those of a script's recovered AST), never a graph to record.
753
+ for (const p of scanner.parseErrors) fatal.push(`parse error in ${show(p.url)}: ${p.message}`)
770
754
  // Edge resolving to a file a source bundle can't carry (.node/.wasm/extensionless): scan
771
755
  // records it but never queues the child, so load would die.
772
756
  for (const [, byParent] of scanner.imports) {
@@ -779,13 +763,12 @@ function analyzeScanner(scanner, { baseDir }) {
779
763
  }
780
764
  }
781
765
  const tolerated = scanner.unresolved.filter((u) => !fatalUnresolved(u))
782
- const toleratedParse = scanner.parseErrors.filter((p) => !fatalParse(p))
783
- return { fatal, tolerated, toleratedParse }
766
+ return { fatal, tolerated }
784
767
  }
785
768
 
786
769
  // Throw on fatal scan issues; warn on tolerated ones. `label` tags the scan pass; `baseDir`
787
770
  // relativizes the paths in the warnings (see displayPath), matching analyzeScanner's messages.
788
- function reportScanIssues({ fatal, tolerated, toleratedParse, leftOut = [] }, { label = '', baseDir, root = '' }) {
771
+ function reportScanIssues({ fatal, tolerated, leftOut = [] }, { label = '', baseDir, root = '' }) {
789
772
  const where = label ? ` (${label})` : ''
790
773
  const show = (url) => displayPath(url, baseDir)
791
774
  // The first 10 of `items` as indented lines (`line` renders one), then how many more there are.
@@ -800,10 +783,6 @@ function reportScanIssues({ fatal, tolerated, toleratedParse, leftOut = [] }, {
800
783
  const summary = listed(tolerated, (u) => describeUnresolved(u, show))
801
784
  console.warn(`[stasis] Bundle has ${tolerated.length} unresolved import(s)${where}; they will fall through at load time:\n${summary}`)
802
785
  }
803
- if (toleratedParse.length > 0) {
804
- const summary = listed(toleratedParse, (p) => `${show(p.url)}: ${p.message}`)
805
- console.warn(`[stasis] Bundle has ${toleratedParse.length} file(s) with parse errors${where}; their recorded imports may be incomplete:\n${summary}`)
806
- }
807
786
  if (leftOut.length > 0) {
808
787
  // The file outside the root by its path from the cwd, as the refusal of one linked eagerly names it.
809
788
  const summary = listed(leftOut, (u) => `${u.kind} ${u.spec} from ${show(u.parentURL)} (at ${relative(baseDir, fileURLToPath(u.outsideURL))})`)
@@ -817,11 +796,14 @@ const cleanConditions = (conditions) => conditions.map((c) => (typeof c === 'str
817
796
 
818
797
  // --typescript honours tsconfig `paths` aliases: an explicit --tsconfig's for every file (it must
819
798
  // exist), else those of the tsconfig.json beside each importing file's package.json
820
- // (packageTsconfigPaths, the project read as the State reads it, to `env`'s PROJECT_CWD; null
821
- // matcher = no aliases). Without --typescript they only hint a miss (Scan#typescriptCandidate), so a
822
- // config that doesn't load is none.
799
+ // (packageTsconfigPaths, the project read as the State reads it, to `env`'s PROJECT_CWD). Either way
800
+ // it carries the packages' layouts (`outputs`). Without --typescript they only hint a miss
801
+ // (Scan#typescriptCandidate), so a config that doesn't load is none.
823
802
  function typescriptPathsFor(typescript, baseDir, tsconfig, host, env) {
824
- if (typescript && tsconfig !== undefined) return loadTsconfigPaths(discoverTsconfig(baseDir, tsconfig, host), host)
803
+ if (typescript && tsconfig !== undefined) {
804
+ const matcher = loadTsconfigPaths(discoverTsconfig(baseDir, tsconfig, host), host)
805
+ return { matchPaths: (spec) => matcher?.matchPaths(spec) ?? [], outputs: tsconfigOutputsOf({ host }) }
806
+ }
825
807
  return packageTsconfigPaths(baseDir, { host, lenient: !typescript, projectCwd: env.PROJECT_CWD })
826
808
  }
827
809
 
@@ -840,9 +822,11 @@ function typescriptPathsFor(typescript, baseDir, tsconfig, host, env) {
840
822
  // root above it, where that holds no stasis file of its own. With `extra`, the files its
841
823
  // stasis.config.json's `extra` names are added as `stasis run` adds them (State#includeExtra), never
842
824
  // one of `outputsAt(root)` (the absolute paths the build writes, given the root its paths are
843
- // relative to), resources by `resources` where given, else by the config's; from disk alone, as
844
- // Node's glob reads it.
845
- export async function buildJsBundle({ cwd = process.cwd(), env = process.env, entries, scope, conditions = [], jsx = false, flow = false, typescript = false, tsconfig, resources, define, packageJSON = false, host = diskHost, innermostRoot = false, extra = false, outputsAt = () => [] } = {}) {
825
+ // relative to), resources by `resources` where given, else by the config's; read through `host`, as
826
+ // Node's glob reads the disk. `memo` is what the build's scans keep across the rounds a lazy tree is
827
+ // read in, by each scan's label (vfs-bundle/lazy.js): the scan keeps what it reads there, and a round
828
+ // that read a package not unpacked yet (`host.touches()`) stops after it.
829
+ export async function buildJsBundle({ cwd = process.cwd(), env = process.env, entries, scope, conditions = [], jsx = false, flow = false, typescript = false, tsconfig, resources, define, packageJSON = false, host = diskHost, innermostRoot = false, extra = false, outputsAt = () => [], memo } = {}) {
846
830
  if (!Array.isArray(entries) || entries.length === 0) {
847
831
  throw new Error('buildJsBundle: at least one entry .js/.cjs/.mjs/.ts/.cts/.mts/.jsx/.tsx file is required')
848
832
  }
@@ -860,7 +844,9 @@ export async function buildJsBundle({ cwd = process.cwd(), env = process.env, en
860
844
  typescript ||= importsTypescriptByOutputName(absEntries, host)
861
845
  const typescriptPaths = typescriptPathsFor(typescript, baseDir, tsconfig, host, env)
862
846
 
863
- const scanned = scan(absEntries, { conditions: scanConditions, jsx, flow, typescript, typescriptPaths, resources: resourceSet, define, host })
847
+ const scanned = scan(absEntries, { conditions: scanConditions, jsx, flow, typescript, typescriptPaths, resources: resourceSet, define, host, memo: memo ? nestedMap(memo, 'js') : null })
848
+ // A round of a lazy tree that asked for a package already is void: the rest is moot.
849
+ if (memo && host.touches() > 0) throw new Error('buildJsBundle: this round reads a package not unpacked yet')
864
850
 
865
851
  // Materialise via a non-preload State: addFile bucketizes + records sources/formats,
866
852
  // addImport replays the edge map; serialize emits the runtime loader's v1 layout.
@@ -878,7 +864,7 @@ export async function buildJsBundle({ cwd = process.cwd(), env = process.env, en
878
864
  // Only what lies within the root is carried (see withinRoot). One the entries link eagerly is
879
865
  // refused, named here rather than as the State's bare assertion: a stasis file in a directory below
880
866
  // the project's root (a stray stasis.code.br there, say) roots the State at it, out of the files above.
881
- const { scanner, outside, leftOut } = withinRoot(scanned, (url) => !relativeEscapes(relative(state.root, fileURLToPath(url))))
867
+ const { scanner, outside, leftOut } = withinRoot(scanned, (url) => isPathWithin(state.root, fileURLToPath(url)))
882
868
  const markers = STASIS_ROOT_FILES.filter((name) => host.stat(join(state.root, name)) !== null)
883
869
  const rootedBy = markers.length === 0 ? '' : ` (rooted there by its ${markers.join(' and ')}, which can be removed if stale)`
884
870
  const root = ` at ${relative(baseDir, state.root) || '.'}${rootedBy}`
@@ -909,12 +895,7 @@ export async function buildJsBundle({ cwd = process.cwd(), env = process.env, en
909
895
  const byParent = new Map()
910
896
  for (const [key, parents] of scanner.imports) {
911
897
  for (const [parentURL, specs] of parents) {
912
- if (!byParent.has(parentURL)) byParent.set(parentURL, new Map())
913
- const bySpec = byParent.get(parentURL)
914
- for (const [spec, childURL] of specs) {
915
- if (!bySpec.has(spec)) bySpec.set(spec, new Map())
916
- bySpec.get(spec).set(key, childURL)
917
- }
898
+ for (const [spec, childURL] of specs) nestedMap(nestedMap(byParent, parentURL), spec).set(key, childURL)
918
899
  }
919
900
  }
920
901
  for (const [parentURL, bySpec] of byParent) {
@@ -955,16 +936,52 @@ function innermostRootOf(baseDir, files, root, host) {
955
936
  // Extensions probed when a resolved target names none, in Metro's default sourceExts order.
956
937
  // Limited to what a source bundle can carry (scan's RESOLVABLE_EXTS) so the resolver never
957
938
  // resolves a file the bundle would reject; an extensionless `import './Foo'` can land on
958
- // Foo.jsx/Foo.tsx, as it does under Metro. Like Metro's default, this doesn't track a project's
959
- // own sourceExts.
960
- const SOURCE_EXTS = ['js', 'jsx', 'json', 'ts', 'tsx']
939
+ // Foo.jsx/Foo.tsx, as it does under Metro. A project's own sourceExts are taken from its
940
+ // rn.config.json alone (metroLists), never off its Metro config, which is code.
941
+ const SOURCE_EXTS = METRO_SOURCE_EXTS
961
942
  // React Native preset mainFields for `--metro` (which also sets the RN conditions + platform suffixes).
962
943
  const METRO_MAIN_FIELDS = ['react-native', 'browser', 'main']
963
944
 
964
945
  // The config files a React Native app's build loads from its project root, though nothing the app
965
946
  // imports reaches them: Metro's and the React Native CLI's, and with --babel(-aliases) Babel's.
966
- const METRO_CONFIG_FILES = ['metro.config.js', 'react-native.config.js']
947
+ const METRO_CONFIG_FILE = 'metro.config.js'
948
+ const METRO_CONFIG_FILES = [METRO_CONFIG_FILE, 'react-native.config.js']
949
+ // @preventive/rn's config of a React Native app's build (rn-bundle's and rn-serve's), in its root.
950
+ const RN_CONFIG_FILE = 'rn.config.json'
967
951
  const BABEL_CONFIG_FILE = 'babel.config.js'
952
+ // What else a React Native app's build runs in Node, which neither the app nor the configs import:
953
+ // the CLI that runs Metro and Metro itself, and what they load by a name or path given as a string.
954
+ // Each { name, file, via }: the package `name` as the packages `via` find it, each the next in turn
955
+ // (as each requires it), else as fewer of them do, the last ones, down to the project alone; `file`
956
+ // in it, else its entry, as a require() of it from the last of `via` resolves.
957
+ // Metro, as React Native's CLI plugin runs it (`react-native bundle`, `start`).
958
+ const METRO_VIA = ['react-native', '@react-native/community-cli-plugin', 'metro']
959
+ const METRO_TOOLCHAIN = [
960
+ // React Native's CLI, which an app runs Metro through.
961
+ { name: 'react-native', file: 'cli.js', via: [] },
962
+ { name: 'metro', via: METRO_VIA.slice(0, -1) },
963
+ // Metro's worker, which its workers run (require.resolve'd, to fork), its transform worker
964
+ // (metro-config's default `transformerPath`), the child process jest-worker forks them in, and
965
+ // metro-file-map's Haste worker: each loaded by a path.
966
+ { name: 'metro', file: 'src/DeltaBundler/Worker.js', via: METRO_VIA.slice(0, -1) },
967
+ { name: 'metro-transform-worker', via: METRO_VIA },
968
+ { name: 'jest-worker', file: 'build/workers/processChild.js', via: METRO_VIA },
969
+ { name: 'metro-file-map', file: 'src/plugins/haste/worker.js', via: METRO_VIA },
970
+ // And its minifier, metro-config's default `minifierPath` (DEFAULT_METRO_MINIFIER_PATH), which the
971
+ // worker requires from its own directory, by that name, as it minifies: what a pnpm override puts there
972
+ // in its stead too (`module.exports = {}`).
973
+ { name: 'metro-minify-terser', via: [...METRO_VIA, 'metro-transform-worker'] },
974
+ ]
975
+ // With --babel(-aliases), the Babel transformer React Native's Metro config names
976
+ // (`babelTransformerPath`, require.resolve'd from @react-native/metro-config), which runs @babel/core
977
+ // and React Native's Babel preset, with its plugins.
978
+ const BABEL_TOOLCHAIN = [{ name: '@react-native/metro-babel-transformer', via: ['@react-native/metro-config'] }]
979
+ // The transformers stasis's Metro plugin (src/metro.js) points Metro at by path, beside it, which
980
+ // Metro's workers run when a config wraps its own in withStasis().
981
+ const STASIS_METRO_TRANSFORMERS = ['metro-transformer.js', 'metro-babel-transformer.js']
982
+ // The conditions a require() resolves with in Node, and the key its edges take (the scan's).
983
+ const NODE_REQUIRE_CONDITIONS = new Set(['require', 'node', 'node-addons', 'module-sync'])
984
+ const REQUIRE_KEY = [...NODE_REQUIRE_CONDITIONS].join(', ')
968
985
  // What esbuild and webpack 5 both assert on every platform besides `import`/`require` and
969
986
  // `default`, and so `--mainFields` too: `module`, the bundler-only condition a package's ESM build
970
987
  // sits under, from an import and a require alike. (esbuild drops it when given conditions of its
@@ -976,39 +993,54 @@ const BUNDLER_CONDITIONS = ['module']
976
993
  // it adds to the bundler base (`import`/`require` + `default`, never Node's `node`; see
977
994
  // resolveConditions): --metro asserts the RN conditions (+ browser on web), Metro's own set;
978
995
  // --mainFields asserts esbuild's and webpack's `module` (BUNDLER_CONDITIONS) and the user's --conditions.
979
- export function fieldResolverFor(platform, { mainFields, metro = false, conditions = [], typescript = false, typescriptPaths = null, host = diskHost }) {
996
+ // Under --metro, `assetExts`, `sourceExts` and `assetResolutions` are Metro's resolver lists (its
997
+ // defaults where not given; metroLists).
998
+ export function fieldResolverFor(platform, { mainFields, metro = false, assetExts, sourceExts = SOURCE_EXTS, assetResolutions, conditions = [], typescript = false, typescriptPaths = null, host = diskHost, kept }) {
980
999
  const extras = metro ? ['react-native', ...(platform === 'web' ? ['browser'] : [])] : [...new Set([...BUNDLER_CONDITIONS, ...conditions])]
981
1000
  const fields = metro ? METRO_MAIN_FIELDS : mainFields
982
1001
  const resolver = createFieldResolver({
983
1002
  mainFields: fields,
984
1003
  platform,
985
1004
  preferNative: platform !== null && platform !== 'web',
986
- sourceExts: SOURCE_EXTS,
1005
+ sourceExts,
987
1006
  conditions: resolveConditions('commonjs', extras),
988
1007
  // Opt into Metro's package-entry browser-field quirks only on the --metro path.
989
1008
  metro,
1009
+ assetExts,
1010
+ assetResolutions,
990
1011
  // --typescript: tsc's mapping, inside the field resolver (the scanner's own fallback
991
1012
  // only backs the built-in resolver). Unreachable under --metro-resolver
992
1013
  // (classifyEntries rejects the combination -- metro-resolver can't substitute).
993
1014
  typescript,
994
1015
  typescriptPaths,
995
1016
  host,
1017
+ kept,
996
1018
  })
997
1019
  return { extras, mainFields: fields, resolver }
998
1020
  }
999
1021
 
1000
1022
  const CONFIG_FILE = 'stasis.config.json'
1001
1023
 
1002
- // The stasis.config.json in `cwd`, parsed; null where there is none.
1003
- function readConfigFile(name, cwd, host = diskHost) {
1024
+ // The stasis.config.json in `cwd`, parsed; null where there is none. `name`, if any, prefixes errors.
1025
+ export function readConfigFile(name, cwd, host = diskHost) {
1004
1026
  const text = readFileSyncMaybe(resolve(cwd), CONFIG_FILE, 'utf8', host)
1005
1027
  try {
1006
1028
  return text === null ? null : JSON.parse(text)
1007
1029
  } catch (cause) {
1008
- throw new Error(`${name}: ${CONFIG_FILE} is not valid JSON`, { cause })
1030
+ throw new Error(`${name === undefined ? '' : `${name}: `}${CONFIG_FILE} is not valid JSON`, { cause })
1009
1031
  }
1010
1032
  }
1011
1033
 
1034
+ // Whether the tree laid out from the lockfile for the project `host` holds at `cwd` installs
1035
+ // devDependencies: the `installDevDependencies` of the stasis.config.json there, true without one.
1036
+ export const installsDevDependencies = (name, cwd, host) => parseInstallDevDependencies(CONFIG_FILE, readConfigFile(name, cwd, host)?.installDevDependencies)
1037
+
1038
+ // Whether the tree laid out from the lockfile for the project `host` holds at `cwd` may be lazy: the
1039
+ // `lazyDependencies` of the stasis.config.json there, true without one.
1040
+ const lazesDependencies = (name, cwd, host) => parseLazyDependencies(CONFIG_FILE, readConfigFile(name, cwd, host)?.lazyDependencies)
1041
+ // `generatePrisma` of the stasis.config.json there, true without one.
1042
+ const generatesPrisma = (name, cwd, host) => parseGeneratePrisma(CONFIG_FILE, readConfigFile(name, cwd, host)?.generatePrisma)
1043
+
1012
1044
  // --babel-aliases: the alias map of the JSON file `file` (a path from `baseDir`, in the project, read through
1013
1045
  // `host`), babel-plugin-module-resolver's `alias` option as data: each alias to the string it maps to.
1014
1046
  function readAliasesFile(baseDir, file, host = diskHost) {
@@ -1079,11 +1111,415 @@ function metroEmptyModule(baseDir, host) {
1079
1111
  return isUtf8(buf) && EMPTY_MODULE_SOURCE.test(buf.toString('utf8').trim()) ? real : null
1080
1112
  }
1081
1113
 
1114
+ // The directory of package `name`, by its real path, in the nearest node_modules up from `dir` that
1115
+ // holds it, as Node looks for one; through each package of `via` first, one that depends on it, for
1116
+ // an install (pnpm) that links it only beside that one. -> undefined where none does
1117
+ function packageDirFrom(dir, name, host, via = []) {
1118
+ let from = dir
1119
+ for (const step of [...via, name]) {
1120
+ let found
1121
+ for (let d = from; found === undefined; d = dirname(d)) {
1122
+ const candidate = join(d, 'node_modules', step)
1123
+ if (host.stat(join(candidate, 'package.json'))?.isFile()) found = host.realpath(candidate)
1124
+ else if (dirname(d) === d) return undefined
1125
+ }
1126
+ from = found
1127
+ }
1128
+ return from
1129
+ }
1130
+
1131
+ // What a require() of `spec` from the directory `dir` resolves to, as Node resolves it. -> absolute
1132
+ // path, or undefined where none does
1133
+ function requireFrom(dir, spec, host) {
1134
+ try {
1135
+ return host.resolve(join(dir, 'package.json'), spec, NODE_REQUIRE_CONDITIONS)
1136
+ } catch {
1137
+ return undefined
1138
+ }
1139
+ }
1140
+
1141
+ // The AST of the module `file`, read through `host` (`source` where read already).
1142
+ const parseModule = (file, host, source = host.readFile(file).toString('utf8')) => getParser().parseSync(file, source, { sourceType: 'unambiguous' }).program
1143
+
1144
+ // Calls `fn` on each node of the AST `node`, depth first.
1145
+ function walkAst(node, fn) {
1146
+ if (Array.isArray(node)) {
1147
+ for (const child of node) walkAst(child, fn)
1148
+ return
1149
+ }
1150
+ if (!node || typeof node !== 'object' || typeof node.type !== 'string') return
1151
+ fn(node)
1152
+ for (const key of Object.keys(node)) if (key !== 'type') walkAst(node[key], fn)
1153
+ }
1154
+
1155
+ // The method `node` calls on an object (`x.concat(...)` -> 'concat'), on the one named `object` where
1156
+ // given (`require.resolve(...)`), or undefined.
1157
+ function memberCall(node, object) {
1158
+ if (node?.type !== 'CallExpression' || node.callee.type !== 'MemberExpression' || node.callee.computed) return undefined
1159
+ if (object !== undefined && (node.callee.object.type !== 'Identifier' || node.callee.object.name !== object)) return undefined
1160
+ return node.callee.property.name
1161
+ }
1162
+
1163
+ // What the module `file` names by `require.resolve('x')`, read off its source, never run, each where
1164
+ // Node resolves it (babelPathCall: from the module, or the `paths` it gives): the polyfills
1165
+ // @react-native/js-polyfills' index.js lists for React Native's Metro config to prepend
1166
+ // (`getPolyfills`), the Babel plugins a plugin runs Babel with itself. A specifier resolved more than
1167
+ // once (in other `paths` too) is each of its resolutions.
1168
+ // -> [specifier, absolute path][] of those that resolve, in source order
1169
+ function requireResolutions(file, host) {
1170
+ const resolved = []
1171
+ const source = host.readFile(file).toString('utf8')
1172
+ // Parsed only where both words occur, however the call spaces them (`require\n .resolve(`).
1173
+ if (!source.includes('require') || !source.includes('resolve')) return resolved
1174
+ walkAst(parseModule(file, host, source), (node) => {
1175
+ const spec = memberCall(node, 'require') === 'resolve' ? literalSpec(node.arguments[0]) : null
1176
+ const path = spec === null ? null : babelPathCall(node, file, host)
1177
+ if (path !== null) resolved.push([spec, path])
1178
+ })
1179
+ return resolved
1180
+ }
1181
+
1182
+ // The name Babel requires a plugin or preset its config names by (@babel/core's standardizeName):
1183
+ // a `module:` name as written, else a short one prefixed (`module-resolver` ->
1184
+ // babel-plugin-module-resolver, `@babel/env` -> @babel/preset-env, `@scope/x` -> @scope/babel-plugin-x,
1185
+ // `@scope` -> @scope/babel-plugin); a path as is.
1186
+ const BABEL_PREFIX = { plugin: /^(?!@|module:|[^/]+\/|babel-plugin-)/u, preset: /^(?!@|module:|[^/]+\/|babel-preset-)/u }
1187
+ const BABEL_ORG = { plugin: /^(@babel\/)(?!plugin-|[^/]+\/)/u, preset: /^(@babel\/)(?!preset-|[^/]+\/)/u }
1188
+ const OTHER_ORG = { plugin: /^(@(?!babel\/)[^/]+\/)(?![^/]*babel-plugin(?:-|\/|$)|[^/]+\/)/u, preset: /^(@(?!babel\/)[^/]+\/)(?![^/]*babel-preset(?:-|\/|$)|[^/]+\/)/u }
1189
+ const OTHER_ORG_DEFAULT = /^(@(?!babel$)[^/]+)$/u
1190
+ const standardizeBabelName = (type, name) => (isAbsolute(name) ? name : name
1191
+ .replace(BABEL_PREFIX[type], `babel-${type}-`)
1192
+ .replace(BABEL_ORG[type], `$1${type}-`)
1193
+ .replace(OTHER_ORG[type], `$1babel-${type}-`)
1194
+ .replace(OTHER_ORG_DEFAULT, `$1/babel-${type}`)
1195
+ .replace(/^module:/u, ''))
1196
+
1197
+ // A string that names a Babel plugin or preset by its form, a package or a path: `module:x`,
1198
+ // `@babel/plugin-x`, `babel-plugin-x`, `@react-native/babel-preset`,
1199
+ // `node_modules/@org/x-babel/babel/plugin-inline.js`.
1200
+ const BABEL_MODULE_STRING = /^module:|babel[-/](?:plugin|preset)/u
1201
+ // A module of a Babel config by its name: `babel` and `config` in its file's name (babel.config.js,
1202
+ // create-babel-config.js), or in its package's (a shared config, @org/babel-config).
1203
+ const BABEL_CONFIG_NAME = /babel.*config|config.*babel/iu
1204
+ const isBabelConfigModule = (rel) => BABEL_CONFIG_NAME.test(posix.basename(rel)) || BABEL_CONFIG_NAME.test(splitNodeModulesPath(rel)?.name ?? '')
1205
+
1206
+ // The lists a Babel config names its plugins and presets in, by the name of the key, variable or object
1207
+ // holding one, and what Babel takes each of them as.
1208
+ const BABEL_LISTS = new Map([['plugins', 'plugin'], ['presets', 'preset']])
1209
+ // And the list of extensions a Metro config gives Metro to take as assets (`resolver.assetExts`).
1210
+ const METRO_ASSET_LISTS = new Map([['assetExts', 'asset']])
1211
+ // The array methods making a list of the one they're called on: `concat` (with its arguments'),
1212
+ // `filter`, `flat`, `slice`.
1213
+ const LIST_METHODS = new Set(['concat', 'filter', 'flat', 'slice'])
1214
+ // A path the module `file` computes from its own directory and strings: `__dirname`, a string, or a
1215
+ // `path.resolve(...)` or `path.join(...)` of them. -> the path (a relative one from Babel's cwd), or null
1216
+ function staticPath(node, file) {
1217
+ if (node?.type === 'Identifier' && node.name === '__dirname') return dirname(file)
1218
+ const string = literalSpec(node)
1219
+ if (string !== null || node?.type !== 'CallExpression' || memberCall(node, 'require') !== undefined) return string
1220
+ const name = node.callee.type === 'Identifier' ? node.callee.name : memberCall(node)
1221
+ if (name !== 'resolve' && name !== 'join') return null
1222
+ const parts = node.arguments.map((arg) => staticPath(arg, file))
1223
+ if (parts.length === 0 || parts.includes(null)) return null
1224
+ return name === 'resolve' && parts.some((part) => isAbsolute(part)) ? resolve(...parts) : join(...parts)
1225
+ }
1226
+
1227
+ // The path a call computes, as Babel takes a plugin or preset named by path:
1228
+ // `path.resolve(__dirname, 'node_modules/@org/x/plugin')`, `path.join(__dirname, 'x')` (staticPath), or
1229
+ // what `require.resolve('x')` resolves to as Node resolves it: from the module, else from each of the
1230
+ // `paths` it gives (`{ paths: [path.join(__dirname, 'shared')] }`) in turn. -> it, or null
1231
+ function babelPathCall(node, file, host) {
1232
+ if (memberCall(node, 'require') !== 'resolve') {
1233
+ const path = node?.type === 'CallExpression' ? staticPath(node, file) : null
1234
+ return path === null || isAbsolute(path) || path.startsWith('.') ? path : `./${path}`
1235
+ }
1236
+ const spec = literalSpec(node.arguments[0])
1237
+ const option = node.arguments[1]
1238
+ const paths = option?.type === 'ObjectExpression'
1239
+ ? option.properties.find((property) => property.type === 'Property' && !property.computed && (property.key.name ?? property.key.value) === 'paths')
1240
+ : undefined
1241
+ const dirs = paths === undefined ? [dirname(file)] : paths.value.type === 'ArrayExpression' ? paths.value.elements.map((element) => staticPath(element, file)) : [null]
1242
+ // One only the run knows, or one relative to its cwd: no telling where Node looks.
1243
+ if (spec === null || dirs.some((dir) => dir === null || !isAbsolute(dir))) return null
1244
+ for (const dir of dirs) {
1245
+ try {
1246
+ return host.resolve(join(dir, 'index.js'), spec, NODE_REQUIRE_CONDITIONS)
1247
+ } catch { /* not from here: the next */ }
1248
+ }
1249
+ return null
1250
+ }
1251
+
1252
+ // What the lists of `lists` (the name of the key, variable or object holding one -> what each entry is
1253
+ // taken as) in the AST `program` hold, read off its source, never run: each list (an array under such a
1254
+ // key at any depth, a variable or property so named or assigned, one pushed onto either), through
1255
+ // spreads, variables, branches and the array methods making a list of another (LIST_METHODS); each
1256
+ // entry (or the first of a `[name, options]` pair) passed to `entry(type, node)`.
1257
+ function readLists(program, lists, entry) {
1258
+ // Each variable's declarators with an initializer, by its name.
1259
+ const declarators = []
1260
+ walkAst(program, (node) => {
1261
+ if (node.type === 'VariableDeclarator' && node.id.type === 'Identifier' && node.init) declarators.push(node)
1262
+ })
1263
+ const inits = Map.groupBy(declarators, (node) => node.id.name)
1264
+ const seen = new Map()
1265
+ // Read `node` for the `type`s it lists: `depth` levels of arrays in it lists (1 a list, 2 one `.flat()`
1266
+ // flattens), and an array below them an entry's `[name, options]` pair. A spread joins its elements to
1267
+ // the array spreading it; a variable is its initializers; a branch (`a ? b : c`, `a && b`) either side.
1268
+ const read = (node, type, depth) => {
1269
+ if (!node || depth > MAX_LIST_DEPTH) return
1270
+ const key = `${type}${depth}`
1271
+ if (!seen.has(node)) seen.set(node, new Set())
1272
+ if (seen.get(node).has(key)) return
1273
+ seen.get(node).add(key)
1274
+ if (node.type === 'ArrayExpression') {
1275
+ if (depth === 0) return read(node.elements[0], type, 0)
1276
+ for (const element of node.elements) {
1277
+ if (element?.type === 'SpreadElement') read(element.argument, type, depth)
1278
+ else read(element, type, depth - 1)
1279
+ }
1280
+ } else if (node.type === 'Identifier') {
1281
+ for (const { init } of inits.get(node.name) ?? []) read(init, type, depth)
1282
+ } else if (node.type === 'ConditionalExpression' || node.type === 'LogicalExpression') {
1283
+ for (const side of node.type === 'LogicalExpression' ? [node.left, node.right] : [node.consequent, node.alternate]) read(side, type, depth)
1284
+ } else if (depth > 0 && LIST_METHODS.has(memberCall(node))) {
1285
+ // `base.concat(more)` of both, `[a, b].filter(Boolean)` of its own, `[[a], b].flat()` of its own
1286
+ // flattened, as many levels as it says (`flat(2)`, `flat(Infinity)`).
1287
+ const method = memberCall(node)
1288
+ const levels = method === 'flat' ? flatDepth(node.arguments[0]) : 0
1289
+ read(node.callee.object, type, Math.min(depth + levels, MAX_LIST_DEPTH))
1290
+ if (method === 'concat') for (const arg of node.arguments) read(arg.argument ?? arg, type, arg.type === 'SpreadElement' ? depth + 1 : depth)
1291
+ } else {
1292
+ entry(type, node)
1293
+ }
1294
+ }
1295
+ const listName = (node) => (node.type === 'Identifier' ? node.name : node.type === 'MemberExpression' && !node.computed ? node.property.name : undefined)
1296
+ walkAst(program, (node) => {
1297
+ // A list: under a key, held in a variable or assigned to a property, so named.
1298
+ const [name, value] = node.type === 'Property' && !node.computed ? [node.key.name ?? node.key.value, node.value]
1299
+ : node.type === 'VariableDeclarator' ? [listName(node.id), node.init]
1300
+ : node.type === 'AssignmentExpression' ? [listName(node.left), node.right]
1301
+ : []
1302
+ if (lists.has(name)) read(value, lists.get(name), 1)
1303
+ // Or added to one: `push` and `unshift` each argument an entry, `concat` as it concatenates.
1304
+ const method = memberCall(node)
1305
+ const type = method && lists.get(listName(node.callee.object))
1306
+ if (!type) return
1307
+ if (method === 'concat') read(node, type, 1)
1308
+ else if (method === 'push' || method === 'unshift') for (const arg of node.arguments) read(arg.argument ?? arg, type, arg.type === 'SpreadElement' ? 1 : 0)
1309
+ })
1310
+ }
1311
+
1312
+ // The plugins and presets the module `file` names in the lists of them (readLists): by a string,
1313
+ // standardized as Babel requires it, or by a path the module computes (babelPathCall), as is. With
1314
+ // `anyString`, also any string naming one by its form (BABEL_MODULE_STRING) wherever it is, as
1315
+ // written. -> the names
1316
+ function babelNamedModules(file, host, { anyString = false } = {}) {
1317
+ const program = parseModule(file, host)
1318
+ const named = new Set()
1319
+ readLists(program, BABEL_LISTS, (type, node) => {
1320
+ const name = literalSpec(node)
1321
+ const path = name ? null : babelPathCall(node, file, host)
1322
+ if (name) named.add(standardizeBabelName(type, name))
1323
+ else if (path) named.add(path)
1324
+ })
1325
+ if (anyString) {
1326
+ walkAst(program, (node) => {
1327
+ const string = literalSpec(node)
1328
+ if (string && BABEL_MODULE_STRING.test(string)) named.add(string.replace(/^module:/u, ''))
1329
+ })
1330
+ }
1331
+ return named
1332
+ }
1333
+
1334
+ // The extensions Metro takes as assets (`resolver.assetExts`), as the project's Metro config gives them:
1335
+ // Metro's defaults, and each string its Metro config (`file`, from `baseDir`; metro.config.js where not
1336
+ // given) lists among them, read off its source, never run (readLists: `assetExts:
1337
+ // [...defaultConfig.resolver.assetExts, 'riv']`, `config.resolver.assetExts.push('riv')`). One it filters
1338
+ // out is still taken as one. Metro's defaults alone where there's no such file.
1339
+ function metroAssetExts(baseDir, host, file = METRO_CONFIG_FILE) {
1340
+ const exts = new Set(METRO_ASSET_EXTS)
1341
+ const config = resolve(baseDir, file)
1342
+ if (!host.stat(config)?.isFile()) return exts
1343
+ readLists(parseModule(config, host), METRO_ASSET_LISTS, (_, node) => {
1344
+ const ext = literalSpec(node)
1345
+ if (ext) exts.add(ext)
1346
+ })
1347
+ return exts
1348
+ }
1349
+
1350
+ // An extension as Metro lists one (`riv`: no dot before it, of no directory), and a scale as its
1351
+ // assetResolutions has one (`"0.75"`, or 0.75), as rn.config.json takes them.
1352
+ const isMetroExt = (value) => typeof value === 'string' && /^[^./\\][^/\\]*$/u.test(value)
1353
+ const isMetroScale = (value) => (typeof value === 'number' && Number.isFinite(value) && value > 0) || (typeof value === 'string' && /^\d+(?:\.\d+)?$/u.test(value) && Number(value) > 0)
1354
+ // rn.config.json's options that bear on Metro's resolver lists, as rn-bundle takes them: whether the
1355
+ // project's Metro config is loaded (or which file), and what it adds to each list.
1356
+ const RN_CONFIG_OPTIONS = {
1357
+ metroConfig: [(value) => typeof value === 'boolean' || (typeof value === 'string' && value !== ''), 'true, false or the path of a Metro config'],
1358
+ assetExts: [(value) => Array.isArray(value) && value.every(isMetroExt), 'a list of extensions, each without its dot ("riv")'],
1359
+ sourceExts: [(value) => Array.isArray(value) && value.every(isMetroExt), 'a list of extensions, each without its dot ("mjs")'],
1360
+ assetResolutions: [(value) => Array.isArray(value) && value.every(isMetroScale), 'a list of scales ("0.75", or 0.75)'],
1361
+ }
1362
+
1363
+ // The options of the project's rn.config.json that bear on Metro's resolver lists (RN_CONFIG_OPTIONS),
1364
+ // those it has; its others are rn-bundle's own. -> them, or null where there's no rn.config.json;
1365
+ // throws where it isn't one rn-bundle takes for them: not JSON, not an object, or one of them of
1366
+ // another kind.
1367
+ function readRnConfig(baseDir, host) {
1368
+ const text = readFileSyncMaybe(baseDir, RN_CONFIG_FILE, 'utf8', host)
1369
+ if (text === null) return null
1370
+ let config
1371
+ try {
1372
+ config = JSON.parse(text)
1373
+ } catch (cause) {
1374
+ throw new Error(`${RN_CONFIG_FILE} is not valid JSON`, { cause })
1375
+ }
1376
+ if (!isPlainObject(config)) throw new Error(`${RN_CONFIG_FILE} is not an object of options`)
1377
+ const options = {}
1378
+ for (const [key, [valid, what]] of Object.entries(RN_CONFIG_OPTIONS)) {
1379
+ if (config[key] === undefined) continue
1380
+ if (!valid(config[key])) throw new Error(`${RN_CONFIG_FILE}: ${key} is ${what}, not ${JSON.stringify(config[key])}`)
1381
+ options[key] = config[key]
1382
+ }
1383
+ return options
1384
+ }
1385
+
1386
+ // `base`, and what of `more` it has none of, after it, each once, as strings.
1387
+ const addedTo = (base, more = []) => [...base, ...more.map(String).filter((each, i, all) => !base.includes(each) && all.indexOf(each) === i)]
1388
+
1389
+ // --metro: Metro's resolver lists, as the project gives them, as rn-bundle takes them from its
1390
+ // rn.config.json (readRnConfig) and its Metro config, read off its source, never run:
1391
+ // - assetExts: Metro's defaults and those rn.config.json adds, where it has assetExts; Metro's defaults
1392
+ // alone where it has metroConfig false (no Metro config is loaded); else those the Metro config
1393
+ // (rn.config.json's metroConfig, where it names one; else metro.config.js) adds (metroAssetExts);
1394
+ // - sourceExts and assetResolutions: Metro's defaults and what rn.config.json adds, after them. Of the
1395
+ // sourceExts, those a bundle can carry a file of (scan's isResolvableExt).
1396
+ // An rn.config.json that isn't one rn-bundle takes for them, or adds an extension to one of assetExts
1397
+ // and sourceExts that the other has (Metro takes a file of it for an asset; rn.config.json removes none),
1398
+ // is warned of, and read as none. `configs` are the files the lists come of besides metro.config.js
1399
+ // (carried as it is): rn.config.json, where there is one, and the Metro config its metroConfig names.
1400
+ // -> { assetExts (a Set), sourceExts, assetResolutions, configs (absolute paths) }
1401
+ function metroLists(baseDir, host) {
1402
+ const rnFile = join(baseDir, RN_CONFIG_FILE)
1403
+ const configs = host.stat(rnFile)?.isFile() ? [rnFile] : []
1404
+ let rn = null
1405
+ try {
1406
+ rn = readRnConfig(baseDir, host)
1407
+ } catch (error) {
1408
+ console.warn(`[stasis] ${error.message}; reading ${METRO_CONFIG_FILE} as without it`)
1409
+ }
1410
+ const withoutRn = () => ({ assetExts: metroAssetExts(baseDir, host), sourceExts: SOURCE_EXTS, assetResolutions: METRO_ASSET_RESOLUTIONS, configs })
1411
+ if (rn === null) return withoutRn()
1412
+ let assetExts
1413
+ if (rn.assetExts !== undefined) assetExts = new Set(addedTo([...METRO_ASSET_EXTS], rn.assetExts))
1414
+ else if (rn.metroConfig === false) assetExts = new Set(METRO_ASSET_EXTS)
1415
+ else assetExts = metroAssetExts(baseDir, host, typeof rn.metroConfig === 'string' ? rn.metroConfig : METRO_CONFIG_FILE)
1416
+ const sourceExts = addedTo([...SOURCE_EXTS], rn.sourceExts)
1417
+ const both = [...(rn.sourceExts ?? []).filter((ext) => assetExts.has(ext)), ...(rn.assetExts ?? []).filter((ext) => sourceExts.includes(ext))][0]
1418
+ if (both !== undefined) {
1419
+ console.warn(`[stasis] ${RN_CONFIG_FILE}: ${both} is in both assetExts and sourceExts, which Metro takes a file of as an asset; reading ${METRO_CONFIG_FILE} as without it`)
1420
+ return withoutRn()
1421
+ }
1422
+ const named = rn.assetExts === undefined && typeof rn.metroConfig === 'string' ? [resolve(baseDir, rn.metroConfig)] : []
1423
+ return { assetExts, sourceExts: sourceExts.filter((ext) => isResolvableExt(`.${ext}`)), assetResolutions: addedTo([...METRO_ASSET_RESOLUTIONS], rn.assetResolutions), configs: [...configs, ...named] }
1424
+ }
1425
+
1426
+ // How deep a Babel config's lists nest, as read: arrays of arrays `.flat()` flattens.
1427
+ const MAX_LIST_DEPTH = 8
1428
+ // The levels `.flat(arg)` flattens: 1 by default, else the number written (Infinity as deep as read).
1429
+ function flatDepth(arg) {
1430
+ if (arg === undefined) return 1
1431
+ if (arg.type === 'Identifier' && arg.name === 'Infinity') return MAX_LIST_DEPTH
1432
+ return arg.type === 'Literal' && typeof arg.value === 'number' ? arg.value : 1
1433
+ }
1434
+
1435
+ const JSX_FILE_EXTS = new Set(['.jsx', '.tsx'])
1436
+ // Metro's asyncRequire (metro-config's default `asyncRequireModulePath`), which Metro adds to a build
1437
+ // with an `import()`, as the Metro plugin's graph then holds it.
1438
+ const ASYNC_REQUIRE = 'metro-runtime/src/modules/asyncRequire.js'
1439
+
1440
+ // A get-or-read cache for the rounds a lazy tree's build runs in (vfs-bundle/lazy.js), in its `memo`
1441
+ // under `label`: what `read()` gives for a key, kept where reading it through `host` read no package
1442
+ // not unpacked yet (`host.touches()`), as a scan keeps what it reads of a file; without a `memo`,
1443
+ // read each time.
1444
+ function keptAcross(label, host, memo) {
1445
+ const kept = memo ? nestedMap(memo, label) : null
1446
+ return (key, read) => {
1447
+ if (kept?.has(key)) return kept.get(key)
1448
+ const before = kept === null ? 0 : host.touches()
1449
+ const value = read()
1450
+ if (kept !== null && host.touches() === before) kept.set(key, value)
1451
+ return value
1452
+ }
1453
+ }
1454
+
1455
+ // `fn()` on `host`, its reads probes of what files packages hold, which read none of their code,
1456
+ // where the host tells them apart (a lazy tree's, vfs-bundle/tree.js's treeHost).
1457
+ const probing = (host, fn) => (host.probing === undefined ? fn() : host.probing(fn))
1458
+
1459
+ // What Babel and Metro add to a React Native build beyond what its source imports (#99 records no
1460
+ // edge to one, as Metro bundles them unasserted), each resolved as `resolver` resolves it (Babel's
1461
+ // aliases applied), or `platformResolver` where Babel never runs, from a file of `files` (absolute
1462
+ // paths of code files) or `assets` (of the assets reached) it is added to: under
1463
+ // --babel or --babel-aliases, the Babel helpers (every @babel/runtime/helpers/*.js of each
1464
+ // @babel/runtime a file finds, which ones Babel adds hanging on the syntax the file uses), Metro's
1465
+ // module system (metro-runtime), and React Native's polyfills (the files @react-native/js-polyfills
1466
+ // lists); with an `import()` (`dynamicImports`), Metro's asyncRequire (ASYNC_REQUIRE); with JSX (`jsx`, else a
1467
+ // .jsx/.tsx file), the JSX runtime Babel imports for it; for an asset, the asset registry
1468
+ // (ASSET_REGISTRY) the module Metro builds of it, `require(registry).registerAsset({...})`, requires,
1469
+ // which Metro builds past Babel, so no alias of Babel's applies to it (platformResolver). -> absolute
1470
+ // paths
1471
+ function metroInsertedFiles(files, { assets, baseDir, babelled, dynamicImports, jsx, resolver, platformResolver, conditions: conds, host, kept = (key, read) => read() }) {
1472
+ const inserted = new Set()
1473
+ // One file of each directory of `list`'s: a specifier resolves alike from every file of one, and
1474
+ // is kept by it (`kept`, keptAcross's).
1475
+ const onePerDir = (list) => [...new Map(list.map((file) => [dirname(file), file])).values()]
1476
+ const resolveFrom = (from, spec, by = resolver) => kept(`${by === resolver ? '' : 'platform\0'}${dirname(from)}\0${spec}`, () => {
1477
+ const url = by(from, spec, conds)?.url
1478
+ return url === undefined ? undefined : fileURLToPath(url)
1479
+ })
1480
+ if (babelled) {
1481
+ const helpersDirs = new Set()
1482
+ for (const from of onePerDir(files)) {
1483
+ const probe = resolveFrom(from, '@babel/runtime/helpers/interopRequireDefault')
1484
+ if (probe !== undefined) helpersDirs.add(dirname(probe))
1485
+ }
1486
+ for (const dir of helpersDirs) {
1487
+ for (const ent of host.readdir(dir)) if (ent.isFile() && ent.name.endsWith('.js')) inserted.add(join(dir, ent.name))
1488
+ }
1489
+ const metroRuntime = packageDirFrom(baseDir, 'metro-runtime', host) ?? packageDirFrom(baseDir, 'metro-runtime', host, ['metro'])
1490
+ const moduleSystem = metroRuntime === undefined ? undefined : join(metroRuntime, 'src/polyfills/require.js')
1491
+ if (moduleSystem !== undefined && host.stat(moduleSystem)?.isFile()) inserted.add(moduleSystem)
1492
+ const polyfills = packageDirFrom(baseDir, '@react-native/js-polyfills', host) ?? packageDirFrom(baseDir, '@react-native/js-polyfills', host, ['react-native'])
1493
+ if (polyfills !== undefined && host.stat(join(polyfills, 'index.js'))?.isFile()) {
1494
+ for (const [, file] of requireResolutions(join(polyfills, 'index.js'), host)) inserted.add(file)
1495
+ }
1496
+ }
1497
+ // Metro adds asyncRequire as a dependency of a module with an `import()`: from the project, else
1498
+ // through Metro, which depends on metro-runtime (an install that nests it, or a pnpm one, links it
1499
+ // only beside Metro).
1500
+ if (dynamicImports) {
1501
+ const metroDir = packageDirFrom(baseDir, 'metro', host)
1502
+ const file = requireFrom(baseDir, ASYNC_REQUIRE, host) ?? (metroDir === undefined ? undefined : requireFrom(metroDir, ASYNC_REQUIRE, host))
1503
+ if (file !== undefined) inserted.add(file)
1504
+ }
1505
+ // Babel's JSX transform imports the runtime into a file with JSX: under --jsx any one, else a
1506
+ // .jsx/.tsx one.
1507
+ for (const from of onePerDir(jsx ? files : files.filter((file) => JSX_FILE_EXTS.has(extname(file))))) {
1508
+ const runtime = resolveFrom(from, 'react/jsx-runtime')
1509
+ if (runtime !== undefined) inserted.add(runtime)
1510
+ }
1511
+ for (const from of onePerDir(assets)) {
1512
+ const registry = resolveFrom(from, ASSET_REGISTRY, platformResolver)
1513
+ if (registry !== undefined) inserted.add(registry)
1514
+ }
1515
+ return inserted
1516
+ }
1517
+
1082
1518
  // Build a JS/TS Bundle + companion Lockfile via the legacy-field resolver (`--mainFields`/
1083
1519
  // `--metro`). Scanned once per platform; each edge is recorded flat when the platforms that
1084
1520
  // have it agree, or as a `{ platform: target }` map where they diverge. `scope` and `resources`
1085
- // are fieldBuildOptions'. Returns { bundle, lockfile }.
1086
- async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, entries, mainFields, platforms, conditions = [], metro = false, metroResolver = false, babel = false, babelAliases: aliasesFile, jsx = false, flow = false, typescript = false, tsconfig, scope, resources, define, packageJSON = false, host = diskHost }) {
1521
+ // are fieldBuildOptions'. Returns { bundle, lockfile: () => Lockfile }, the lockfile built when called.
1522
+ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, entries, mainFields, platforms, conditions = [], metro, metroResolver, babel, babelAliases: aliasesFile, jsx = false, flow = false, typescript = false, tsconfig, scope, resources, define, packageJSON = false, host = diskHost, memo }) {
1087
1523
  const baseDir = resolve(cwd)
1088
1524
  const absEntries = entries.map((e) => resolve(baseDir, e))
1089
1525
  const normalized = normalizeEntries(entries, cwd)
@@ -1093,13 +1529,25 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1093
1529
  // is the root; a file below neither is outside it. (Realpathed only when needed, so a missing cwd
1094
1530
  // is still the scan's missing entry.)
1095
1531
  let realRoot
1532
+ // Each file's path from the root, and the path of each file URL, which the scans and what follows
1533
+ // them take again for each file they reach, and each edge: worked out once a build, or for a lazy
1534
+ // tree once across its rounds, in its `memo` (each is the path's alone).
1535
+ const fromRoot = memo ? nestedMap(memo, 'paths from the root') : new Map()
1096
1536
  const relWithin = (abs) => {
1537
+ if (fromRoot.has(abs)) return fromRoot.get(abs)
1097
1538
  let rel = toPosix(relative(baseDir, abs))
1098
1539
  if (relativeEscapes(rel)) {
1099
1540
  realRoot ??= host.realpath(baseDir)
1100
1541
  rel = toPosix(relative(realRoot, abs))
1101
1542
  }
1102
- return relativeEscapes(rel) ? null : rel
1543
+ fromRoot.set(abs, relativeEscapes(rel) ? null : rel)
1544
+ return fromRoot.get(abs)
1545
+ }
1546
+ const pathsOf = memo ? nestedMap(memo, 'file paths') : new Map()
1547
+ const pathOf = (url) => {
1548
+ let path = pathsOf.get(url)
1549
+ if (path === undefined) pathsOf.set(url, (path = fileURLToPath(url)))
1550
+ return path
1103
1551
  }
1104
1552
  const toRel = (abs) => {
1105
1553
  const rel = relWithin(abs)
@@ -1132,72 +1580,291 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1132
1580
 
1133
1581
  // --babel-aliases: the same aliasing as --babel's, from a JSON file of the project, for every platform alike.
1134
1582
  const fileAliases = aliasesFile === undefined ? null : createAliases(readAliasesFile(baseDir, aliasesFile, host), baseDir)
1583
+ // --babel or --babel-aliases: a build Metro runs through Babel, which adds its helpers to each file.
1584
+ const babelled = babel || fileAliases !== null
1135
1585
  // The edges each platform's scan left out above the root (withinRoot's leftOut), across platforms.
1136
1586
  const leftOutEdges = []
1587
+ // --metro: Metro's resolver lists, as the project's rn.config.json gives them, else its Metro config
1588
+ // (metroLists): the extensions Metro takes as assets, which it resolves by its asset rule, scale
1589
+ // variants and all (`assetExts: [...defaultConfig.resolver.assetExts, 'riv']`), those it probes, and
1590
+ // the scales of an asset's files.
1591
+ const { assetExts, sourceExts, assetResolutions, configs: listConfigs } = metro ? metroLists(baseDir, host) : { assetExts: METRO_ASSET_EXTS, sourceExts: SOURCE_EXTS, assetResolutions: METRO_ASSET_RESOLUTIONS, configs: [] }
1592
+ // The resources the app imports that are no asset of Metro's are no module of Metro's, which can't
1593
+ // resolve one: one reaches the build only as a transform's input, read and inlined by a Babel plugin
1594
+ // (`require('./messages.po')`, @exodus/i18n's plugin-inline-po), as the Metro plugin records it
1595
+ // (`metro`, no edge): no edge to one, and no asset registry for one.
1596
+ const isTransformInput = (abs, info) => metro && info?.resource === true && !isAssetFile(abs, assetExts)
1597
+ // A round of a lazy tree that asked for a package already is void (vfs-bundle/lazy.js): it reads
1598
+ // on, for all else it asks for, but reports and refuses nothing, which the round that asks for
1599
+ // none does, and stops before it reads the files in.
1600
+ const voided = () => Boolean(memo) && host.touches() > 0
1601
+ // Scan from `roots` with `options`, keeping what lies within the root (one the entries link eagerly
1602
+ // is refused, see withinRoot), its issues reported under `label`, by which a lazy tree's `memo`
1603
+ // keeps what each of its scans read. -> { scanner, leftOut }
1604
+ const scanWithin = (roots, options, label) => {
1605
+ const { scanner, outside, leftOut } = withinRoot(scan(roots, { ...options, memo: memo ? nestedMap(memo, label) : null }), (url) => relWithin(pathOf(url)) !== null)
1606
+ for (const url of outside) toRel(pathOf(url))
1607
+ if (!voided()) reportScanIssues({ ...analyzeScanner(scanner, { baseDir }), leftOut }, { baseDir, label, root: ' at .' })
1608
+ return { scanner, leftOut }
1609
+ }
1137
1610
  for (const platform of platforms) {
1138
- const field = fieldResolverFor(platform, { mainFields, metro, conditions: scanConditions, typescript, typescriptPaths, host })
1611
+ // What each resolves, kept across a lazy tree's rounds where it read nothing not unpacked yet.
1612
+ const keptFor = (label) => (memo ? nestedMap(memo, `${label} ${platform ?? '*'}`) : undefined)
1613
+ const field = fieldResolverFor(platform, { mainFields, metro, assetExts, sourceExts, assetResolutions, conditions: scanConditions, typescript, typescriptPaths, host, kept: keptFor('resolutions') })
1139
1614
  const { extras } = field
1140
1615
  // --metro --metro-resolver delegates to the project's own metro-resolver for byte-for-byte Metro
1141
1616
  // fidelity; otherwise the built-in field/suffix resolver reproduces it. metro-resolver derives
1142
1617
  // default/require|import/platform conditions itself, so it takes only the extra `react-native`
1143
1618
  // condition (browser comes from its per-platform map, keyed on `web`).
1144
1619
  const resolver = metroResolver
1145
- ? createMetroResolver({ projectDir: baseDir, platform, sourceExts: SOURCE_EXTS, mainFields, conditionNames: ['react-native'], host })
1620
+ ? createMetroResolver({ projectDir: baseDir, platform, sourceExts, mainFields, conditionNames: ['react-native'], assetExts, assetResolutions, host })
1146
1621
  : field.resolver
1147
1622
  // Without --typescript, the field resolver --typescript would build names the file a miss would
1148
1623
  // land on: a hint in the unresolved report, never an edge (see Scan#typescriptCandidate).
1149
- const typescriptResolve = typescript || metroResolver ? null : fieldResolverFor(platform, { mainFields, metro, conditions: scanConditions, typescript: true, typescriptPaths, host }).resolver
1624
+ const typescriptResolve = typescript || metroResolver ? null : fieldResolverFor(platform, { mainFields, metro, assetExts, sourceExts, assetResolutions, conditions: scanConditions, typescript: true, typescriptPaths, host, kept: keptFor('typescript resolutions') }).resolver
1150
1625
  // --babel: a specifier the project's babel-plugin-module-resolver rewrites resolves as rewritten,
1151
1626
  // as Metro resolves it after Babel; its edge stays keyed by the specifier the source holds.
1152
1627
  const aliases = babel ? createBabelAliases({ projectDir: baseDir, platform }) : fileAliases
1628
+ // --babel: and whether it keeps a TypeScript import listing types alone (onlyRemoveTypeImports).
1629
+ const keepsTypeImports = babel ? createBabelTypeImportsKept({ projectDir: baseDir, platform }) : null
1153
1630
  const aliased = (resolveFn) => (aliases && resolveFn ? (file, spec, conds) => resolveFn(file, aliases(file, spec) ?? spec, conds) : resolveFn)
1154
- const scanned = scan(absEntries, { conditions: extras, resolve: aliased(resolver), typescriptResolve: aliased(typescriptResolve), jsx, flow, resources: resourceSet, define, host })
1155
- // Only what lies within the root is carried; one the entries link eagerly is refused (see withinRoot).
1156
- const { scanner, outside, leftOut } = withinRoot(scanned, (url) => relWithin(fileURLToPath(url)) !== null)
1157
- for (const url of outside) toRel(fileURLToPath(url))
1158
- reportScanIssues({ ...analyzeScanner(scanner, { baseDir }), leftOut }, { baseDir, label: platform ?? 'mainFields', root: ' at .' })
1159
- leftOutEdges.push(...leftOut)
1160
-
1631
+ // --metro: built through Babel, which erases an import listing types alone (Scan's `babel`).
1632
+ const scanOptions = { conditions: extras, resolve: aliased(resolver), typescriptResolve: aliased(typescriptResolve), jsx, flow, resources: resourceSet, define, babel: metro, keepsTypeImports, host }
1161
1633
  const platformKey = platform ?? '*' // '*' is a private placeholder for the single mainFields pass; it never unflattens
1162
- for (const [url, info] of scanner.files) {
1163
- const abs = fileURLToPath(url)
1164
- const rel = toRel(abs)
1165
- reached.add(abs)
1166
- if (info.resource) resourceRels.add(rel)
1167
- // Under --metro a code file has no format, as the Metro plugin records it: Metro transforms
1168
- // it from source, and Node's module/commonjs isn't what it runs.
1169
- formatsByRel.set(rel, metro && !info.resource ? undefined : info.format)
1170
- for (const e of info.edges) {
1171
- if (e.builtin || e.dynamic) continue
1172
- let target
1173
- if (e.empty) { usesEmpty = true; target = emptyTarget }
1174
- else if (e.child) target = toRel(fileURLToPath(e.child))
1175
- else continue // unresolved (already warned/thrown by reportScanIssues)
1176
- if (!edges.has(rel)) edges.set(rel, new Map())
1177
- const bySpec = edges.get(rel)
1178
- if (!bySpec.has(e.spec)) bySpec.set(e.spec, new Map())
1179
- bySpec.get(e.spec).set(platformKey, target)
1634
+ // Record what scanning from `roots` reaches: its files, their formats and edges. -> its scanner
1635
+ const absorb = (roots) => {
1636
+ const { scanner, leftOut } = scanWithin(roots, scanOptions, platform ?? 'mainFields')
1637
+ leftOutEdges.push(...leftOut)
1638
+ for (const [url, info] of scanner.files) {
1639
+ const abs = pathOf(url)
1640
+ const rel = toRel(abs)
1641
+ reached.add(abs)
1642
+ if (info.resource) resourceRels.add(rel)
1643
+ // Under --metro a code file has no format, as the Metro plugin records it: Metro transforms
1644
+ // it from source, and Node's module/commonjs isn't what it runs. But the one its name alone
1645
+ // gives it (.json, .mjs, .cjs: nodeExtFormat), as Node loads it too.
1646
+ formatsByRel.set(rel, metro && !info.resource ? nodeExtFormat(abs) : info.format)
1647
+ for (const e of info.edges) {
1648
+ if (e.builtin || e.dynamic) continue
1649
+ let target
1650
+ if (e.empty) { usesEmpty = true; target = emptyTarget }
1651
+ else if (e.child) target = toRel(pathOf(e.child))
1652
+ else continue // unresolved (already warned/thrown by reportScanIssues)
1653
+ // A transform's input is no dependency of Metro's: the import is gone before Metro sees it.
1654
+ if (e.child && isTransformInput(pathOf(e.child), scanner.files.get(e.child))) continue
1655
+ nestedMap(nestedMap(edges, rel), e.spec).set(platformKey, target)
1656
+ }
1180
1657
  }
1658
+ return scanner
1659
+ }
1660
+ const scanner = staged('scan', () => absorb(absEntries))
1661
+ // --metro: and what Babel and Metro add, which the source doesn't import, carried as Metro's code
1662
+ // and scanned for what each imports in turn; one above the root, passed over, as the Metro plugin
1663
+ // passes one over.
1664
+ if (metro) {
1665
+ const metroAt = performance.now()
1666
+ const code = [...scanner.files].filter(([, info]) => !info.resource).map(([url]) => pathOf(url))
1667
+ const assets = [...scanner.files].filter(([url, info]) => info.resource && !isTransformInput(pathOf(url), info)).map(([url]) => pathOf(url))
1668
+ // As a require(): Babel's CommonJS transform makes one of each it adds, as Metro writes its own.
1669
+ const required = new Set(['require', 'default', ...extras])
1670
+ // And each asset's files Metro reads to build its module (`scales`, `hash`) and copies out with
1671
+ // it, its other scales (`logo@2x.png`, `logo@3x.png`), by the name Metro reads it by (one linked
1672
+ // in, its target's bytes; one linked from outside the root passed over, as the Metro plugin
1673
+ // passes it over): no import names one, so no edge, carried as the asset is.
1674
+ const scales = assets.flatMap((file) => metroAssetFiles(file, platform, host)).filter((file) => relWithin(host.realpath(file)) !== null)
1675
+ const dynamicImports = [...scanner.files.values()].some((info) => !info.resource && info.edges.some((e) => e.kind === 'dynamic-import'))
1676
+ const kept = keptAcross(`Metro's additions ${platformKey}`, host, memo)
1677
+ const added = [...new Set([...metroInsertedFiles(code, { assets, baseDir, babelled, dynamicImports, jsx, resolver: aliased(resolver), platformResolver: resolver, conditions: required, host, kept }), ...scales])]
1678
+ .filter((file) => !scanner.files.has(pathToFileURL(file).toString()) && relWithin(file) !== null)
1679
+ stageTimes.metro += performance.now() - metroAt
1680
+ if (added.length > 0) staged('scan', () => absorb(added))
1181
1681
  }
1182
1682
  }
1183
1683
 
1184
1684
  // One platform leaving out an edge another resolves within the root is a split too: collapsed
1185
1685
  // below, the edge would serve that platform the other's file in its place.
1186
- refuseSplit(leftOutEdges.filter((e) => edges.get(toRel(fileURLToPath(e.parentURL)))?.has(e.spec)), baseDir)
1686
+ if (!voided()) refuseSplit(leftOutEdges.filter((e) => edges.get(toRel(pathOf(e.parentURL)))?.has(e.spec)), baseDir)
1687
+
1688
+ // --metro: and the project's configs a React Native build loads, though nothing the app imports
1689
+ // reaches them: Metro's and the React Native CLI's, and with --babel or --babel-aliases the Babel
1690
+ // config Metro's transformer runs; and the toolchain Metro loads by name (METRO_TOOLCHAIN, and
1691
+ // under either BABEL_TOOLCHAIN), as Node resolves a require() of it from the package naming it, else
1692
+ // from the project. Node loads each, not Metro, so each is traced as a Node entry is
1693
+ // (buildJsBundle): with all it imports, resolved as Node resolves it, each file in the format Node
1694
+ // loads it in, which one the app's build reaches too keeps (Metro needs none).
1695
+ // Their edges, each under the conditions Node resolves it with, as the Metro plugin's run records
1696
+ // them (Node's loader keys every edge so), apart from Metro's under '*' in a file both reach, though
1697
+ // they agree: under '*', they'd be none of the edges the run records.
1698
+ // conditions key -> parentRel -> specifier -> targetRel, beside the bundle's '*' edges.
1699
+ const keyedResolutions = new Map()
1700
+ // The edges from `parent` (rel) keyed `key`: specifier -> targetRel.
1701
+ const keyedFrom = (key, parent) => nestedMap(nestedMap(keyedResolutions, key), parent)
1702
+ // The configs' edges some context misses or leaves out: `${parentRel}\0${specifier}` -> the miss.
1703
+ const configMissed = new Map()
1704
+ // The files the configs reach (rel), and of them those the app's build doesn't: the bundler's,
1705
+ // attributed to `bundle`, not to `bundle:metro` with the app's source Metro bundles.
1706
+ const configReached = new Set()
1707
+ const configOnly = new Set()
1708
+ const configsAt = performance.now()
1709
+ if (metro) {
1710
+ // And the files Metro's resolver lists came of (metroLists): rn.config.json, and the Metro config
1711
+ // its metroConfig names, where in the project.
1712
+ const configs = [...new Set([...[...METRO_CONFIG_FILES, ...(babelled ? [BABEL_CONFIG_FILE] : [])].map((rel) => join(baseDir, rel)), ...listConfigs.filter((abs) => relWithin(abs) !== null)])].filter((abs) => host.stat(abs)?.isFile())
1713
+ // A METRO_TOOLCHAIN entry's file. -> absolute path, or undefined
1714
+ const toolchainFile = ({ name, file, via }) => {
1715
+ for (let i = 0; i <= via.length; i++) {
1716
+ const chain = via.slice(i)
1717
+ if (file !== undefined) {
1718
+ const dir = packageDirFrom(baseDir, name, host, chain)
1719
+ if (dir !== undefined && host.stat(join(dir, file))?.isFile()) return join(dir, file)
1720
+ continue
1721
+ }
1722
+ const from = chain.length === 0 ? baseDir : packageDirFrom(baseDir, chain.at(-1), host, chain.slice(0, -1))
1723
+ const found = from === undefined ? undefined : requireFrom(from, name, host)
1724
+ if (found !== undefined) return found
1725
+ }
1726
+ return undefined
1727
+ }
1728
+ for (const entry of [...METRO_TOOLCHAIN, ...(babelled ? BABEL_TOOLCHAIN : [])]) {
1729
+ const file = toolchainFile(entry)
1730
+ if (file !== undefined && relWithin(file) !== null) configs.push(file)
1731
+ }
1732
+ // And the React Native configs of the app's packages, which React Native's CLI requires as it
1733
+ // loads its own (`react-native config`'s packages, read as data).
1734
+ // Each by its real path, as Node loads it (a pnpm install's package linked into node_modules
1735
+ // requires what lies beside it in the store).
1736
+ for (const dir of probing(host, () => linkedPackages(baseDir, host))) {
1737
+ const file = join(dir, 'react-native.config.js')
1738
+ if (!probing(host, () => host.stat(file)?.isFile())) continue
1739
+ const real = host.realpath(file)
1740
+ if (relWithin(real) !== null) configs.push(real)
1741
+ }
1742
+ // Trace `roots` as Node entries, recording what they reach. -> the scan
1743
+ const traceNode = (roots) => {
1744
+ const { scanner, leftOut } = scanWithin(roots, { resources: resourceSet, host }, 'configs and toolchain')
1745
+ for (const [url, info] of scanner.files) {
1746
+ const abs = pathOf(url)
1747
+ const rel = toRel(abs)
1748
+ configReached.add(rel)
1749
+ if (!reached.has(abs)) configOnly.add(rel)
1750
+ reached.add(abs)
1751
+ if (info.resource) resourceRels.add(rel)
1752
+ formatsByRel.set(rel, info.format)
1753
+ }
1754
+ for (const u of [...scanner.unresolved, ...leftOut]) {
1755
+ if (u.spec !== undefined) configMissed.set(`${toRel(pathOf(u.parentURL))}\0${u.spec}`, u)
1756
+ }
1757
+ for (const [key, byParent] of scanner.imports) {
1758
+ for (const [parentURL, specs] of byParent) {
1759
+ const keyed = keyedFrom(key, toRel(pathOf(parentURL)))
1760
+ for (const [spec, childURL] of specs) keyed.set(spec, toRel(pathOf(childURL)))
1761
+ }
1762
+ }
1763
+ return scanner
1764
+ }
1765
+ const traced = configs.length > 0 ? traceNode(configs) : null
1766
+ // And the transformers of stasis's Metro plugin beside it, where a config requires it, in turn.
1767
+ const transformers = [...(traced?.files.keys() ?? [])].map((url) => pathOf(url))
1768
+ .filter((file) => basename(file) === 'metro.js' && basename(dirname(file)) === 'src' &&
1769
+ readJson(join(dirname(dirname(file)), 'package.json'), host)?.name === '@exodus/stasis-plugins')
1770
+ .flatMap((file) => STASIS_METRO_TRANSFORMERS.map((name) => join(dirname(file), name)))
1771
+ .filter((file) => host.stat(file)?.isFile() && relWithin(file) !== null && !traced.files.has(pathToFileURL(file).toString()))
1772
+ if (transformers.length > 0) traceNode(transformers)
1773
+ // And the plugins and presets the Babel config names, which @babel/core requires by name: as each
1774
+ // module the config reaches names them, in `plugins` and `presets` where it is the project's own or a
1775
+ // Babel config's by its name (isBabelConfigModule), and anywhere, by their form, where it is a Babel
1776
+ // config's. Each resolved as @babel/core resolves it, from the config's directory, else from the
1777
+ // module naming it, a path from either; and what each reaches, in turn. And what any module it
1778
+ // reaches (a plugin's own too) names by `require.resolve('x')`, which Babel loads: a plugin running
1779
+ // Babel with plugins of its own (react-native-reanimated's, `require.resolve('@babel/plugin-…')`),
1780
+ // with the edge Node's resolution makes, under the conditions it resolves with.
1781
+ const babelConfig = pathToFileURL(join(baseDir, BABEL_CONFIG_FILE)).toString()
1782
+ // What each file names, read once across a lazy tree's rounds (keptAcross).
1783
+ const resolutionsOf = keptAcross('require.resolve', host, memo)
1784
+ const namedOf = keptAcross('babel names', host, memo)
1785
+ if (traced?.files.has(babelConfig)) {
1786
+ const files = new Map(traced.files)
1787
+ const visited = new Set()
1788
+ const resolveNamed = (name, from) => {
1789
+ const pathLike = name.startsWith('.') || isAbsolute(name) || name.startsWith('node_modules/')
1790
+ for (const dir of [baseDir, dirname(from)]) {
1791
+ const file = requireFrom(dir, pathLike ? resolve(dir, name) : name, host)
1792
+ if (file !== undefined) return file
1793
+ }
1794
+ return undefined
1795
+ }
1796
+ for (let frontier = [babelConfig]; frontier.length > 0;) {
1797
+ const named = new Set()
1798
+ for (let i = 0; i < frontier.length; i++) {
1799
+ const url = frontier[i]
1800
+ const info = files.get(url)
1801
+ if (visited.has(url) || info === undefined) continue
1802
+ visited.add(url)
1803
+ for (const e of info.edges) if (e.child) frontier.push(e.child)
1804
+ const file = pathOf(url)
1805
+ const rel = toRel(file)
1806
+ if (info.resource || !JS_EXTS.has(extname(file))) continue
1807
+ for (const [spec, target] of resolutionsOf(file, () => requireResolutions(file, host))) {
1808
+ if (!JS_EXTS.has(extname(target)) || relWithin(target) === null) continue
1809
+ named.add(target)
1810
+ // A specifier keeps one edge, the first recorded, as a run records a resolution only where no
1811
+ // edge is: one the module also require()s keeps that edge, Node's loader's (traced, so
1812
+ // recorded, before the module is read), and one resolved again in other `paths` its first.
1813
+ // Every copy they find (`require.resolve('x', { paths })` another than `require('x')`) is
1814
+ // still traced.
1815
+ const keyed = keyedFrom(REQUIRE_KEY, rel)
1816
+ if (!keyed.has(spec)) keyed.set(spec, toRel(target))
1817
+ }
1818
+ const configModule = isBabelConfigModule(rel)
1819
+ if (!configModule && splitNodeModulesPath(rel)) continue
1820
+ for (const name of namedOf(`${configModule}\0${file}`, () => babelNamedModules(file, host, { anyString: configModule }))) {
1821
+ const found = resolveNamed(name, file)
1822
+ if (found !== undefined && JS_EXTS.has(extname(found)) && relWithin(found) !== null) named.add(found)
1823
+ }
1824
+ }
1825
+ const fresh = [...named].filter((file) => !files.has(pathToFileURL(file).toString()))
1826
+ if (fresh.length > 0) for (const [url, info] of traceNode(fresh).files) files.set(url, info)
1827
+ frontier = [...named].map((file) => pathToFileURL(file).toString())
1828
+ }
1829
+ }
1830
+ // And the files of the toolchain the Metro plugin carries for every build, which nothing here
1831
+ // reaches (AUTO_INCLUDES): setup_env.sh, which React Native's CLI runs as each command of it starts.
1832
+ // As the plugin resolves and tags each: from the project, by the shared classifier, a resource
1833
+ // where it names no format.
1834
+ for (const specifier of AUTO_INCLUDES) {
1835
+ const file = requireFrom(baseDir, specifier, host)
1836
+ if (file === undefined || reached.has(file) || relWithin(file) === null) continue
1837
+ const rel = toRel(file)
1838
+ const format = classifyFormat(file, { content: host.readFile(file) })
1839
+ reached.add(file)
1840
+ configReached.add(rel)
1841
+ configOnly.add(rel)
1842
+ if (format === undefined) resourceRels.add(rel)
1843
+ else formatsByRel.set(rel, format ?? undefined)
1844
+ }
1845
+ }
1187
1846
 
1188
- // Read each reached file once: bytes for the bundle, integrity for the lockfile. Source is
1189
- // stored as UTF-8 text, so non-UTF-8 bytes would diverge from the hashed bytes -- reject them.
1847
+ stageTimes.configs += performance.now() - configsAt
1848
+ if (voided()) throw new Error('buildResolvedJsBundle: this round reads a package not unpacked yet')
1849
+ const carryingAt = performance.now()
1850
+
1851
+ // Read each reached file once, into the bundle as the bytes read, encoded without loss: the
1852
+ // lockfile hashes them from there, once it is wanted (deferredLockfile). Source is
1853
+ // stored as UTF-8 text, which non-UTF-8 bytes would not survive -- reject them.
1190
1854
  const sources = new Map()
1191
- const integrities = new Map()
1192
- // Carry `rel`: `content` in the bundle as `format`, the integrity of its bytes `buf` in the lockfile.
1193
- const put = (rel, content, format, buf) => {
1855
+ // Carry `rel`: `content` in the bundle as `format`.
1856
+ const put = (rel, content, format) => {
1194
1857
  sources.set(rel, content)
1195
1858
  formatsByRel.set(rel, format)
1196
- integrities.set(rel, sha512integrity(buf))
1197
1859
  }
1198
1860
  // A resource carries bytes, not source: UTF-8 verbatim or base64 when binary, tagged with the
1199
1861
  // byte-derived format (here and in the --metro native capture below).
1200
- const putResource = (rel, buf) => (isUtf8(buf) ? put(rel, buf.toString('utf8'), 'resource', buf) : put(rel, buf.toString('base64'), 'resource:base64', buf))
1862
+ const putResource = (rel, buf) => (isUtf8(buf) ? put(rel, buf.toString('utf8'), 'resource') : put(rel, buf.toString('base64'), 'resource:base64'))
1863
+ // Code carries its source, UTF-8 (`what` it is, refused where it isn't).
1864
+ const putCode = (rel, buf, format, what = 'JS bundle source') => {
1865
+ if (!isUtf8(buf)) throw new Error(`${what} is not valid UTF-8: ${rel}`)
1866
+ put(rel, buf.toString('utf8'), format)
1867
+ }
1201
1868
  const realBase = host.realpath(baseDir)
1202
1869
  // Metro's empty module is carried as the file it is, a code file of Metro's (no format).
1203
1870
  if (usesEmpty && metroEmpty !== null) reached.add(metroEmpty)
@@ -1209,20 +1876,16 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1209
1876
  // fail closed.
1210
1877
  assertRealPathWithinBase(realBase, baseDir, rel, host)
1211
1878
  const buf = host.readFile(abs)
1212
- if (resourceRels.has(rel)) {
1213
- putResource(rel, buf)
1214
- } else {
1215
- if (!isUtf8(buf)) throw new Error(`JS bundle source is not valid UTF-8: ${rel}`)
1216
- // Its format is the scan's.
1217
- put(rel, buf.toString('utf8'), formatsByRel.get(rel), buf)
1218
- }
1879
+ // A code file's format is the scan's.
1880
+ if (resourceRels.has(rel)) putResource(rel, buf)
1881
+ else putCode(rel, buf, formatsByRel.get(rel))
1219
1882
  }
1220
1883
  if (usesEmpty && metroEmpty === null) {
1221
1884
  // Refuse to shadow a real reached file sitting at the reserved empty-module path.
1222
1885
  if (formatsByRel.has(EMPTY_MODULE_PATH)) {
1223
1886
  throw new Error(`Bundle needs the reserved empty-module path ${EMPTY_MODULE_PATH}, but the project has a real file there`)
1224
1887
  }
1225
- put(EMPTY_MODULE_PATH, '', metro ? undefined : 'commonjs', Buffer.alloc(0))
1888
+ put(EMPTY_MODULE_PATH, '', metro ? undefined : 'commonjs')
1226
1889
  }
1227
1890
 
1228
1891
  // The package a bundled file is in (findPackageMetadata: its pkgDir and identity, null where no
@@ -1236,11 +1899,12 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1236
1899
  if (metro) {
1237
1900
  const pkgDirs = new Map() // package dir -> its package's name
1238
1901
  // Follow the CODE/module graph only: a package reached solely for an asset (--resources) is
1239
- // not a linked native dependency, so it must not drag in its ios/android surface.
1902
+ // not a linked native dependency, so it must not drag in its ios/android surface. Nor is one
1903
+ // only the configs reach, which Node runs at build time, as the Metro plugin's graph holds none.
1240
1904
  const isCode = (rel) => !resourceRels.has(rel)
1241
1905
  for (const abs of reached) {
1242
1906
  const rel = toRel(abs)
1243
- const nm = isCode(rel) && splitNodeModulesPath(rel)
1907
+ const nm = isCode(rel) && !configOnly.has(rel) && splitNodeModulesPath(rel)
1244
1908
  if (nm) pkgDirs.set(nm.dir, nm.name)
1245
1909
  }
1246
1910
  // A package linked into node_modules from out of it (a workspace package) is reached by its real
@@ -1260,22 +1924,15 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1260
1924
  if (!splitNodeModulesPath(rel)) links.add(rel)
1261
1925
  } catch { /* a dangling link, or one out of the root, whose files the scan pass refused already */ }
1262
1926
  }
1263
- const listing = (abs) => {
1264
- try {
1265
- return host.readdir(abs)
1266
- } catch {
1267
- return []
1268
- }
1269
- }
1270
1927
  // As locatePackage: a directory named node_modules holds no node_modules of its own to look in.
1271
1928
  if (basename(dir) !== 'node_modules') {
1272
1929
  const nodeModules = join(dir, 'node_modules')
1273
- for (const ent of listing(nodeModules)) {
1930
+ for (const ent of listing(nodeModules, host)) {
1274
1931
  const abs = join(nodeModules, ent.name)
1275
1932
  if (ent.name.startsWith('@')) {
1276
1933
  // A scope holds packages, a directory or a link to one (a whole scope linked, where
1277
1934
  // every package in it is reached through the link).
1278
- for (const scoped of listing(abs)) {
1935
+ for (const scoped of listing(abs, host)) {
1279
1936
  if (ent.isSymbolicLink() || scoped.isSymbolicLink()) link(join(abs, scoped.name))
1280
1937
  }
1281
1938
  } else if (ent.isSymbolicLink()) {
@@ -1304,14 +1961,9 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1304
1961
  }
1305
1962
  // And the app's packages React Native links whether JS imports them or not (`react-native config`'s
1306
1963
  // list, read as data), by their real path, but one above the root, which the plugin passes over too.
1307
- for (const dir of linkedNativePackages(baseDir, host)) {
1308
- let rel
1309
- try {
1310
- rel = toRel(host.realpath(dir))
1311
- } catch {
1312
- continue
1313
- }
1314
- if (!pkgDirs.has(rel)) pkgDirs.set(rel, readPackageJson(baseDir, `${rel}/package.json`, { host })?.name)
1964
+ for (const dir of probing(host, () => linkedNativePackages(baseDir, host))) {
1965
+ const rel = relWithin(host.realpath(dir))
1966
+ if (rel !== null && !pkgDirs.has(rel)) pkgDirs.set(rel, readPackageJson(baseDir, `${rel}/package.json`, { host })?.name)
1315
1967
  }
1316
1968
  // Each one's native build inputs, walked whole (nativeInputs, shared with the plugin), read only
1317
1969
  // once its real path is held to the root.
@@ -1328,47 +1980,21 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1328
1980
  // react-native core isn't a Pod (config reports it via reactNativePath, not `dependencies`),
1329
1981
  // but its native source (React/, ReactCommon/, ReactAndroid/, ...) is walked all the same.
1330
1982
  const isRnCore = name === 'react-native'
1331
- if (!isRnCore && !looksNative(pkgAbs, nativeFs)) continue
1983
+ if (!isRnCore && !probing(host, () => looksNative(pkgAbs, nativeFs))) continue
1332
1984
  for (const { file, source, action, format } of nativeInputs(pkgAbs, nativeFs, { resources: resourceSet, skip: (abs) => sources.has(toRel(abs)) })) {
1333
- const rel = toRel(file)
1334
- if (action === 'code') {
1335
- if (!isUtf8(source)) throw new Error(`native source is not valid UTF-8: ${rel}`)
1336
- put(rel, source.toString('utf8'), format, source)
1337
- } else {
1338
- putResource(rel, source)
1339
- }
1985
+ if (action === 'code') putCode(toRel(file), source, format, 'native source')
1986
+ else putResource(toRel(file), source)
1340
1987
  }
1341
1988
  // RN core's `.js` build scripts the classify loop skips (Metro-owned by extension, but never in
1342
1989
  // the graph): force-include as code so the podspec/Ruby that invokes them at pod-install resolves.
1343
1990
  if (isRnCore) {
1344
1991
  for (const file of RN_CORE_INCLUDE_FILES) {
1345
1992
  const abs = join(pkgAbs, file)
1346
- if (host.stat(abs) === null) continue
1347
- const rel = toRel(abs)
1348
- if (sources.has(rel)) continue
1349
- assertRealPathWithinBase(realBase, baseDir, rel, host)
1350
- const buf = host.readFile(abs)
1351
- if (!isUtf8(buf)) throw new Error(`native source is not valid UTF-8: ${rel}`)
1352
- put(rel, buf.toString('utf8'), packageType(abs, host) === 'module' ? 'module' : 'commonjs', buf)
1993
+ if (host.stat(abs) === null || sources.has(toRel(abs))) continue
1994
+ putCode(toRel(abs), nativeFs.readFile(abs), packageType(abs, host) === 'module' ? 'module' : 'commonjs', 'native source')
1353
1995
  }
1354
1996
  }
1355
1997
  }
1356
-
1357
- // And the project's configs the Metro plugin's run loads, which nothing the app imports reaches:
1358
- // Metro's and the React Native CLI's, and with --babel or --babel-aliases the Babel config Metro's
1359
- // transformer runs. Each file itself, in the format Node loads it in (its package's `type`, else
1360
- // the one Node detects from its syntax, as Scan#parseFile has it), not what it imports.
1361
- const configs = [...METRO_CONFIG_FILES, ...(babel || aliasesFile !== undefined ? [BABEL_CONFIG_FILE] : [])]
1362
- const configScan = new Scan({ host })
1363
- for (const rel of configs) {
1364
- const abs = join(baseDir, rel)
1365
- if (sources.has(rel) || !host.stat(abs)?.isFile()) continue
1366
- assertRealPathWithinBase(realBase, baseDir, rel, host)
1367
- const buf = host.readFile(abs)
1368
- if (!isUtf8(buf)) throw new Error(`config is not valid UTF-8: ${rel}`)
1369
- const parsed = configScan.parseFile(abs)
1370
- put(rel, buf.toString('utf8'), parsed.format ?? parsed.declared ?? 'commonjs', buf)
1371
- }
1372
1998
  }
1373
1999
 
1374
2000
  // --package-json (or the config's packageJSON): fold each bundled module's package.json into
@@ -1381,20 +2007,23 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1381
2007
  if (withManifests) {
1382
2008
  const found = new Map()
1383
2009
  const exists = (rel) => found.get(rel) ?? found.set(rel, packageJSONStat(host, join(baseDir, rel))?.isFile() ?? false).get(rel)
1384
- const manifests = new Set()
2010
+ const manifests = new Map() // manifest -> the reached files it serves
1385
2011
  for (const abs of reached) {
1386
2012
  const rel = toRel(abs)
1387
2013
  const pkgDir = packageOf(rel)?.pkgDir ?? (splitNodeModulesPath(rel) ? undefined : '.')
1388
2014
  if (pkgDir === undefined) continue
1389
- manifests.add(moduleFileKey(pkgDir, 'package.json'))
1390
- const nearest = scopeManifestOf(rel, pkgDir, exists)
1391
- if (nearest !== undefined) manifests.add(nearest)
2015
+ for (const manifest of [moduleFileKey(pkgDir, 'package.json'), scopeManifestOf(rel, pkgDir, exists)]) {
2016
+ if (manifest !== undefined) (manifests.get(manifest) ?? manifests.set(manifest, []).get(manifest)).push(rel)
2017
+ }
1392
2018
  }
1393
- for (const rel of manifests) {
2019
+ for (const [rel, of] of manifests) {
1394
2020
  if (sources.has(rel)) continue
1395
2021
  const buf = readModuleManifest({ baseDir, realBase, rel, host })
1396
2022
  if (!buf) continue
1397
- put(rel, buf.toString('utf8'), 'json', buf)
2023
+ put(rel, buf.toString('utf8'), 'json')
2024
+ // Attributed as the files it serves are, as State#includePackageJson attributes one.
2025
+ if (of.some((file) => configReached.has(file))) configReached.add(rel)
2026
+ if (of.every((file) => configOnly.has(file))) configOnly.add(rel)
1398
2027
  }
1399
2028
  }
1400
2029
 
@@ -1404,21 +2033,28 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1404
2033
  const specMap = new Map()
1405
2034
  for (const [spec, byPlatform] of bySpec) {
1406
2035
  const distinct = new Set(byPlatform.values())
1407
- if (distinct.size === 1) {
1408
- specMap.set(spec, [...distinct][0])
1409
- } else {
1410
- specMap.set(spec, new Map([...byPlatform].toSorted((a, b) => (a[0] < b[0] ? -1 : 1))))
1411
- }
2036
+ specMap.set(spec, distinct.size === 1 ? [...distinct][0] : new Map([...byPlatform].toSorted((a, b) => (a[0] < b[0] ? -1 : 1))))
1412
2037
  }
1413
2038
  resolutions.set(parent, specMap)
1414
2039
  }
2040
+ // A config's import Node misses or leaves out in a context, from a file the app's build reaches
2041
+ // too, where Metro resolves it: no miss is recorded, so the app's edge under '*' would serve that
2042
+ // context Metro's file in its place -- refused, as refuseSplit refuses a split.
2043
+ for (const [key, { parentURL, spec }] of configMissed) {
2044
+ if (!resolutions.get(key.slice(0, key.indexOf('\0')))?.has(spec)) continue
2045
+ throw new Error(
2046
+ `${spec} from ${displayPath(parentURL, baseDir)} resolves under Metro, for the app, and not under Node, for the ` +
2047
+ "project's configs: a bundle can't keep Node's miss without serving Node Metro's file in its place"
2048
+ )
2049
+ }
1415
2050
 
1416
2051
  const rootPkg = readJson(join(baseDir, 'package.json'), host) ?? {}
1417
- const bundle = assembleCodeBundle({
2052
+ const assembled = assembleCodeBundle({
1418
2053
  baseDir,
1419
2054
  entries: normalized,
1420
2055
  sources,
1421
2056
  resolutions,
2057
+ keyedResolutions,
1422
2058
  formats: formatsByRel,
1423
2059
  workspaceName: rootPkg.name ?? 'workspace',
1424
2060
  workspaceVersion: rootPkg.version ?? '0.0.0',
@@ -1426,26 +2062,14 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1426
2062
  host,
1427
2063
  packageOf,
1428
2064
  consumer: metro ? 'bundle:metro' : 'bundle',
2065
+ consumerFiles: [...sources.keys()].filter((rel) => !configOnly.has(rel)),
1429
2066
  })
2067
+ // What the configs reach is the bundler's, not the app's: `bundle`, as a Node build's files are.
2068
+ const bundle = configReached.size === 0 ? assembled : assembled.withReason('bundle', [...configReached])
1430
2069
 
1431
- // The companion lockfile mirrors the bundle, swapping file content for its integrity.
1432
- const lockModules = new Map()
1433
- for (const [dir, m] of bundle.modules) {
1434
- const files = Object.create(null)
1435
- for (const rel of Object.keys(m.files)) files[rel] = integrities.get(moduleFileKey(dir, rel))
1436
- lockModules.set(dir, moduleInfo({ ...m, files }))
1437
- }
1438
- const lockfile = new Lockfile({
1439
- config: bundle.config,
1440
- entries: bundle.entries,
1441
- modules: lockModules,
1442
- imports: bundle.imports,
1443
- formats: bundle.formats,
1444
- // Same file set as the bundle, so the same executable list attests it.
1445
- executable: bundle.executable,
1446
- })
1447
-
1448
- return { bundle, lockfile }
2070
+ // The companion lockfile mirrors the bundle, swapping file content for its integrity, once it is wanted.
2071
+ stageTimes.carrying += performance.now() - carryingAt
2072
+ return { bundle, lockfile: deferredLockfile(bundle) }
1449
2073
  }
1450
2074
 
1451
2075
  // Classify entries into their single shared language and check option applicability; `name` prefixes
@@ -1580,12 +2204,15 @@ function classifyEntries(name, { cwd = process.cwd(), entries, mappingFile, mani
1580
2204
  // `metro`, whose per-platform edges and synthetic empty module a State can't hold, else through a
1581
2205
  // State, which detects the bundle's repo itself. `root` is the directory the bundle's paths are
1582
2206
  // relative to: cwd for the resolver's, the State's root for the State's. `extra` and `outputsAt` are
1583
- // buildJsBundle's, for the State's alone (see withExtra).
2207
+ // buildJsBundle's, for the State's alone (see withExtra). `metro` turns on `jsx` and `flow`, as
2208
+ // Metro's Babel parses JSX and Flow in every file, and defines what Metro's release build does
2209
+ // (METRO_RELEASE_DEFINE), each name `define` gives a value taking that one instead.
1584
2210
  // -> { bundle, lockfile: () => Lockfile, stateBuilt, root }
1585
2211
  async function buildJs({ mainFields, platforms, metro, metroResolver, babel, babelAliases, innermostRoot, extra, outputsAt, ...options }) {
2212
+ if (metro) options = { ...options, jsx: true, flow: true, define: { ...METRO_RELEASE_DEFINE, ...options.define } }
1586
2213
  if (metro || mainFields !== undefined) {
1587
2214
  const built = await buildResolvedJsBundle({ ...options, mainFields: metro ? METRO_MAIN_FIELDS : mainFields, platforms: metro ? platforms : [null], metro: Boolean(metro), metroResolver: Boolean(metroResolver), babel: Boolean(babel), babelAliases })
1588
- return { bundle: built.bundle, lockfile: () => built.lockfile, stateBuilt: false, root: resolve(options.cwd) }
2215
+ return { bundle: built.bundle, lockfile: built.lockfile, stateBuilt: false, root: resolve(options.cwd) }
1589
2216
  }
1590
2217
  const state = await buildJsBundle({ ...options, innermostRoot, extra, outputsAt })
1591
2218
  const bundle = state.sourceBundle
@@ -1598,25 +2225,30 @@ async function buildJs({ mainFields, platforms, metro, metroResolver, babel, bab
1598
2225
  // there names in `extra`, as `stasis add` adds them (extraArtifacts), into its bundle and its
1599
2226
  // lockfile; never one of `outputs` (the absolute paths the build writes), and resources by the
1600
2227
  // build's `resources` where given, else by the config's. A State-built bundle takes them in its
1601
- // State instead, as `stasis run` does (State#includeExtra).
1602
- function withExtra(built, cwd, outputs, resources) {
1603
- const config = readConfigFile('bundle', cwd)
2228
+ // State instead, as `stasis run` does (State#includeExtra). The project is read through `host`,
2229
+ // `name`'s.
2230
+ function withExtra(built, cwd, { outputs = [], resources, host = diskHost, name = 'bundle' }) {
2231
+ const config = readConfigFile(name, cwd, host)
1604
2232
  const patterns = parseExtraOption(CONFIG_FILE, config?.extra)
1605
2233
  if (patterns.length === 0) return built
1606
2234
  // The union of `builds` (see bundleCommand) may be State-built in node_modules scope, which the
1607
2235
  // full-scope files `extra` adds can't join.
1608
- if (built.bundle.config.scope !== 'full') throw new Error(`bundle: ${CONFIG_FILE}'s extra can't be added to a ${built.bundle.config.scope}-scope bundle of its entries`)
2236
+ if (built.bundle.config.scope !== 'full') throw new Error(`${name}: ${CONFIG_FILE}'s extra can't be added to a ${built.bundle.config.scope}-scope bundle of its entries`)
1609
2237
  const baseDir = resolve(cwd)
1610
2238
  const written = new Set(outputs.map((file) => toPosix(relative(baseDir, file))))
1611
2239
  const allowlist = resources === undefined ? parseResourcesOption(CONFIG_FILE, config.resources) : parseResourcesOption('--resources', resources)
1612
- const added = extraArtifacts({ baseDir, patterns, resources: allowlist, bundle: built.bundle, outputs: written })
2240
+ const added = extraArtifacts({ baseDir, patterns, resources: allowlist, bundle: built.bundle, outputs: written, host })
1613
2241
  if (added.bundle === null && added.carried.length === 0) return built
2242
+ // The files are the build's own, at its root, so they are where it was built: its `repo`, which a
2243
+ // merge with a side lacking one would drop.
2244
+ if (added.bundle !== null) added.bundle.repo = built.bundle.repo
1614
2245
  // Each file attributed: to the build's own consumer (`bundle`, or `bundle:metro`, as codeBundle
1615
2246
  // stamps it), `extra`, or both, for one `extra` names that the build carries itself.
1616
2247
  const merged = added.bundle === null ? built.bundle : built.bundle.merge(added.bundle)
1617
2248
  const bundle = added.carried.length === 0 ? merged : merged.withReason('extra', added.carried)
2249
+ // The lockfile of what was added is built when read, as the build's is: not here.
1618
2250
  const { lockfile } = built
1619
- return { ...built, bundle, ...(lockfile && added.lockfile && { lockfile: () => lockfile().merge(added.lockfile) }) }
2251
+ return { ...built, bundle, ...(lockfile && added.bundle !== null && { lockfile: () => lockfile().merge(added.lockfile) }) }
1620
2252
  }
1621
2253
 
1622
2254
  // The bundle of `kind` (classifyEntries') built from buildBundle's options, from disk, with the files
@@ -1627,7 +2259,7 @@ function withExtra(built, cwd, outputs, resources) {
1627
2259
  async function buildOfKind(kind, { cwd, env, entries, mappingFile, manifests, scope, conditions, mainFields, platforms, metro, metroResolver, babel, babelAliases, jsx, flow, typescript, tsconfig, resources, define, packageJSON, cargo, cargoFeatures, cargoNoDefaultFeatures, cargoAllFeatures, cargoTarget, cargoManifests, outputsAt = () => [], extra = true }) {
1628
2260
  const outputs = outputsAt(resolve(cwd))
1629
2261
  // Without `extra`, built as if the config named none (one of several builds of a bundle, see bundleCommand).
1630
- const withExtraOf = (built, buildResources) => (extra ? withExtra(built, cwd, outputs, buildResources) : built)
2262
+ const withExtraOf = (built, buildResources) => (extra ? withExtra(built, cwd, { outputs, resources: buildResources }) : built)
1631
2263
  if (kind === 'sol') return withExtraOf({ bundle: await buildSolidityBundle({ cwd, env, entries, mappingFile, manifests }) })
1632
2264
  if (kind === 'php') return withExtraOf({ bundle: await buildPhpBundle({ cwd, entries }) })
1633
2265
  if (kind === 'bash') return withExtraOf({ bundle: await buildBashBundle({ cwd, entries }) })
@@ -1638,6 +2270,16 @@ async function buildOfKind(kind, { cwd, env, entries, mappingFile, manifests, sc
1638
2270
  return built
1639
2271
  }
1640
2272
 
2273
+ // Several builds' bundles (and lockfiles) as one, the i-th of `builts` built of `builds[i]` and
2274
+ // rooted at `roots[i]`: their strict union, rooted where every one of them is.
2275
+ function unionOfBuilds(name, builds, builts, roots) {
2276
+ const other = roots.findIndex((root) => root !== roots[0])
2277
+ if (other !== -1) {
2278
+ throw new Error(`${name}: ${builds[0].entries[0]} and ${builds[other].entries[0]} can't be bundled together: their bundles' paths are relative to different roots (${roots[0]} and ${roots[other]})`)
2279
+ }
2280
+ return builts.reduce((a, b) => ({ bundle: a.bundle.merge(b.bundle), lockfile: a.lockfile && (() => a.lockfile().merge(b.lockfile())) }))
2281
+ }
2282
+
1641
2283
  // buildVfsBundle's checks of `options`, which hold before anything is fetched (`host` the
1642
2284
  // project's, or an empty tree's with `fetched: false`): no metro-resolver, which reads the disk;
1643
2285
  // `generate` only for JS.
@@ -1647,9 +2289,9 @@ export function checkVfsOptions(name, options) {
1647
2289
  if (options.metroResolver) throw new Error(`${name}: metroResolver is not supported`)
1648
2290
  // The project's Babel config is code, run from the disk.
1649
2291
  if (options.babel) throw new Error(`${name}: babel is not supported`)
1650
- const { generate = [] } = options
1651
- if (!Array.isArray(generate) || generate.some((step) => step !== 'prisma')) throw new TypeError(`${name}: generate must be an array of 'prisma'`)
1652
- if (generate.length > 0 && kind !== 'js') throw new Error(`${name}: --generate is only valid for JS bundles`)
2292
+ const { generate } = options
2293
+ if (generate !== undefined && (!Array.isArray(generate) || generate.some((step) => step !== 'prisma'))) throw new TypeError(`${name}: generate must be an array of 'prisma'`)
2294
+ if (generate?.length > 0 && kind !== 'js') throw new Error(`${name}: --generate is only valid for JS bundles`)
1653
2295
  return kind
1654
2296
  }
1655
2297
 
@@ -1661,39 +2303,84 @@ export function checkVfsOptions(name, options) {
1661
2303
  // EXODUS_STASIS_* setting is read. The entries the stasis.config.json in cwd names in its `entries`
1662
2304
  // (every one, where none are given) are built with their options there, as `stasis bundle` builds
1663
2305
  // them, but none that runs a program; an option given has to agree with theirs, one left out is
1664
- // theirs (configuredBuilds).
2306
+ // theirs (configuredBuilds). The files its `extra` names are added as `stasis bundle` adds them,
2307
+ // globbed in the Vfs as Node globs the disk (vfs-bundle/glob.js), which needs the optional minimatch.
2308
+ // Where its `installDevDependencies` is false, what devDependencies alone reach is left out of the
2309
+ // tree, as the package manager's production install leaves it out.
1665
2310
  // `repo`, the informational `{ github, directory, commit }`,
1666
2311
  // is the Bundle's, over what is detected in the Vfs as `stasis bundle` detects it on disk. `os`,
1667
2312
  // `cpu` and `libc` are loadNodeModules'. Without a `packageManager`, it is the one whose lockfile
1668
2313
  // installs cwd, where only one's does. `innermostRoot` is buildJsBundle's, for a JS bundle built
1669
2314
  // through a State. `generate: ['prisma']` writes each project's Prisma Client into the laid-out
1670
- // tree before the scan, leaving `vfs` as it is (vfs-bundle/prisma.js). `client`, a
2315
+ // tree before the scan, leaving `vfs` as it is (vfs-bundle/prisma.js); without `generate`, those of
2316
+ // the projects the entries reach, silently, unless the stasis.config.json in cwd sets
2317
+ // `generatePrisma` false; `generate: []`, none. `client`, a
1671
2318
  // @preventive/upstream/github.js client, is what a Soldeer git dependency is fetched through; without
1672
2319
  // one, a soldeer.lock holding one is refused, naming deptree's `github`, before anything is fetched.
1673
2320
  // `cache` is where npm packages' tarballs and version documents are kept (checkCache). Each
1674
2321
  // dependency is recorded at the commit the tree installs it from, where deptree names one
1675
2322
  // (pinCommits). `root` is the directory in the Vfs the bundle's paths are relative to: cwd, or for a
1676
- // JS bundle built through a State, the State's root, which is at or above it.
1677
- // -> { bundle: Bundle, lockfile: Lockfile (of a JS bundle), stats, packageManager, root }
1678
- export async function buildVfsBundle({ vfs, packageManager, cwd = '/', packageManagerVersion, os, cpu, libc, repo, innermostRoot = false, generate = [], client, cache, ...options } = {}) {
1679
- const { checkCache, checkKind, checkTarget, checkVfs, loadTree, packageManagerFor, packageManagerOf, pinCommits, treeHost, vfsHost } = await import('../vfs-bundle/tree.js')
2323
+ // JS bundle built through a State, the State's root, which is at or above it. A lazy tree, pnpm's
2324
+ // or npm's, is laid out with nothing fetched, and each package is fetched and unpacked once the
2325
+ // build reads under it, the build run again until it reads none it has not (vfs-bundle/lazy.js):
2326
+ // the bundle the full tree gives, its warnings said once, with `stats` counting what is unpacked. A
2327
+ // tarball the build never reaches is never fetched, nor held to the lockfile, nor refused. The tree
2328
+ // is lazy where it can be (lazyTaken) and the stasis.config.json in cwd leaves `lazyDependencies`
2329
+ // true; `lazy: false` installs every package, and `lazy: true` is refused where the tree cannot be
2330
+ // lazy, or `lazyDependencies` is false. `stats` are deptree's, and `stats.unpacked` the packages
2331
+ // unpacked: each one installed, or those a lazy tree unpacked.
2332
+ // -> { bundle: Bundle, lockfile: Lockfile (of a JS bundle, built when first read), stats, packageManager, root }
2333
+ export async function buildVfsBundle(options = {}) {
2334
+ const { checkVfs, vfsHost } = await import('../vfs-bundle/tree.js')
2335
+ checkVfs('buildVfsBundle', options.vfs)
2336
+ return buildFromLockfile('buildVfsBundle', vfsHost(options.vfs), options)
2337
+ }
2338
+
2339
+ // buildVfsBundle's build, `name`'s, of the project `project` (a host, @exodus/stasis-core/host)
2340
+ // reads: of a Vfs, which `vfs` is (for `generate` alone), or of the disk, for `stasis bundle
2341
+ // --deptree`. As buildVfsBundle builds it, with `options` as it takes them, but for what bundleCommand
2342
+ // gives besides (none of which buildVfsBundle takes): `plan`, `{ builds, bundleExtra }`, the builds
2343
+ // (each `{ entries, ...options }`) bundleCommand plans (bundle-options.js) in place of those
2344
+ // configuredBuilds makes, and whether their union's `extra` is the bundle's, as bundleCommand adds
2345
+ // it; `env`, what the builds read their settings from (none by default); `outputsAt(root)`, the
2346
+ // absolute paths written, which `extra` never takes; and `refuseInstalled`, layOutTree's.
2347
+ async function buildFromLockfile(name, project, { vfs, packageManager, cwd = '/', packageManagerVersion, os, cpu, libc, repo, innermostRoot = false, generate, client, cache, lazy, ...options } = {}, { env = {}, plan, outputsAt = () => [], refuseInstalled = false } = {}) {
2348
+ const { checkCache, checkKind, checkLazy, checkTarget, lazyTaken, loadTree, packageManagerFor, packageManagerOf, pinCommits, treeHost, vfsHost } = await import('../vfs-bundle/tree.js')
1680
2349
  const { configuredBuilds } = await import('./bundle-options.js')
1681
- checkVfs('buildVfsBundle', vfs)
1682
- checkTarget('buildVfsBundle', { os, cpu, libc })
1683
- checkCache('buildVfsBundle', cache)
2350
+ const { checkGlob } = await import('../vfs-bundle/glob.js')
2351
+ checkTarget(name, { os, cpu, libc })
2352
+ checkCache(name, cache)
2353
+ if (lazy !== undefined && typeof lazy !== 'boolean') throw new TypeError(`${name}: lazy must be a boolean`)
1684
2354
  // Checked as the Bundle checks it, before anything is fetched.
1685
2355
  if (repo !== undefined) repo = new Bundle({ repo }).repo
1686
- const project = vfsHost(vfs)
1687
2356
  // A real path, as every file the scan reaches is.
1688
2357
  cwd = project.realpath(posix.resolve('/', cwd))
1689
- packageManager = packageManagerFor('buildVfsBundle', project, cwd, { packageManager, packageManagerVersion, os })
1690
- const pm = packageManagerOf('buildVfsBundle', packageManager)
2358
+ packageManager = packageManagerFor(name, project, cwd, { packageManager, packageManagerVersion, os })
2359
+ const pm = packageManagerOf(name, packageManager)
2360
+ if (lazy) checkLazy(name, packageManager)
1691
2361
  // The entries stasis.config.json's `entries` names, every one where none are given, each built
1692
2362
  // with its options there, which the options given agree with (configuredBuilds); else built with
1693
- // the options given. Each build is checked with the options it is built with.
1694
- const builds = configuredBuilds('buildVfsBundle', { cwd, host: project, entries: options.entries, options })?.builds ?? [options]
1695
- if (builds.length === 0) throw new Error(`buildVfsBundle: no entries given, and ${CONFIG_FILE}'s entries name none`)
1696
- for (const build of builds) checkKind('buildVfsBundle', checkVfsOptions('buildVfsBundle', { ...build, lockfile: options.lockfile, generate, cwd, host: project }), [packageManager])
2363
+ // the options given. Each build is checked with the options it is built with. With the builds its
2364
+ // `entries` make, `extra` is the bundle's, added once to their union, as bundleCommand adds it;
2365
+ // else the build's own, as buildOfKind adds it.
2366
+ const configured = plan === undefined ? configuredBuilds(name, { cwd, host: project, entries: options.entries, options }) : null
2367
+ const { builds, bundleExtra } = plan ?? { builds: configured?.builds ?? [options], bundleExtra: configured !== null }
2368
+ if (builds.length === 0) throw new Error(`${name}: no entries given, and ${CONFIG_FILE}'s entries name none`)
2369
+ // That the Vfs can be globbed for `extra` is checked before anything is fetched (for a State
2370
+ // rooted above cwd, where it reads the config there).
2371
+ if (parseExtraOption(CONFIG_FILE, readConfigFile(name, cwd, project)?.extra).some((pattern) => !pattern.startsWith('!'))) checkGlob()
2372
+ const installDevDependencies = installsDevDependencies(name, cwd, project)
2373
+ // Lazy where the tree can be, unless stasis.config.json's `lazyDependencies` or `lazy` says not.
2374
+ const lazyDependencies = lazesDependencies(name, cwd, project)
2375
+ if (lazy && !lazyDependencies) throw new Error(`${name}: lazy is given, and ${CONFIG_FILE}'s lazyDependencies is false`)
2376
+ lazy ??= lazyDependencies && lazyTaken(project, packageManager, cwd)
2377
+ // Each project's Prisma Client: asked for (`generate: ['prisma']`), every project's, strictly; else, of a JS
2378
+ // build of a Vfs, detected where stasis.config.json's `generatePrisma` leaves it: the projects the entries
2379
+ // are in and their workspace dependencies, silently. `generate: []`, or `generatePrisma` false: none.
2380
+ const generatePrisma = generatesPrisma(name, cwd, project)
2381
+ if (generate?.length > 0 && !generatePrisma) throw new Error(`${name}: generate is given, and ${CONFIG_FILE}'s generatePrisma is false`)
2382
+ const prismaGeneration = generate?.includes('prisma') ? 'asked' : generate === undefined && generatePrisma && vfs !== undefined && pm.kind === 'js' ? 'detected' : null
2383
+ for (const build of builds) checkKind(name, checkVfsOptions(name, { ...build, lockfile: options.lockfile, generate, cwd, host: project }), [packageManager])
1697
2384
  // Checked before anything is fetched: an entry out of what the tree installs is in the project
1698
2385
  // already. (A Solidity entry that is no .sol file is a directory, skipped where it is missing.)
1699
2386
  for (const entry of builds.flatMap((build) => build.entries)) {
@@ -1701,32 +2388,143 @@ export async function buildVfsBundle({ vfs, packageManager, cwd = '/', packageMa
1701
2388
  if (pm.kind === 'sol' && !abs.endsWith('.sol')) continue
1702
2389
  if (!posix.relative(cwd, abs).split('/').includes(pm.installs) && project.stat(abs) === null) throw new Error(`entry not found: ${abs}`)
1703
2390
  }
1704
- const tree = await loadTree({ project, packageManager, cwd, packageManagerVersion, os, cpu, libc, client, cache })
1705
- let { host } = tree
1706
- if (generate.includes('prisma')) {
1707
- const { generatePrismaClients, withPrismaClients } = await import('../vfs-bundle/prisma.js')
1708
- const outputs = await generatePrismaClients({ host, root: tree.root, projects: tree.projects })
1709
- if (outputs.length > 0) host = treeHost(tree, vfsHost(withPrismaClients(vfs, outputs)))
1710
- }
1711
- const args = { cwd, host, env: {} }
1712
- const builts = []
1713
- for (const build of builds) {
1714
- // eslint-disable-next-line no-await-in-loop -- one build at a time, in the order its warnings read
1715
- const built = pm.kind === 'sol' ? { bundle: await buildSolidityBundle({ ...build, ...args }), root: cwd } : await buildJs({ ...build, ...args, innermostRoot })
1716
- // Rooted at cwd, where `stasis bundle` detects its repo.
1717
- if (repo === undefined && !built.stateBuilt) built.bundle.repo ??= detectRepo(cwd, host)
1718
- builts.push(built)
1719
- }
1720
- // Merged as bundleCommand merges its builds: rooted where every one of them is.
1721
- const { root } = builts[0]
1722
- const other = builts.findIndex((built) => built.root !== root)
1723
- if (other !== -1) {
1724
- throw new Error(`buildVfsBundle: ${builds[0].entries[0]} and ${builds[other].entries[0]} can't be bundled together: their bundles' paths are relative to different roots (${root} and ${builts[other].root})`)
2391
+ const tree = await debugTimed(lazy ? 'Laid out the tree' : 'Installed the tree', () => loadTree({ project, packageManager, cwd, packageManagerVersion, os, cpu, libc, client, cache, lazy, installDevDependencies, refuseInstalled }))
2392
+ const { inRounds } = await import('../vfs-bundle/lazy.js')
2393
+ // The project the builds read: with the Prisma Clients `generate` writes, generated once before
2394
+ // any build, for a lazy tree in rounds of their own (they read the tree's `prisma`), so that each
2395
+ // of the build's rounds reads the same project, as what its scans keep across them takes it to.
2396
+ let outside = project
2397
+ if (prismaGeneration !== null) {
2398
+ const prisma = await import('../vfs-bundle/prisma.js')
2399
+ // The projects with a schema (looked for before any `prisma` is read, which a lazy tree fetches once read):
2400
+ // all of them, asked for; else those the entries reach. Nothing done, nor said, where none has one.
2401
+ const withSchema = prisma.schemaProjects(project, tree.root, tree.projects)
2402
+ const strict = prismaGeneration === 'asked'
2403
+ // Reached by the entries alone (cwd's own project, the root's say, only where an entry is in it), each by its
2404
+ // real path, as the scan reaches it, where the project holds it: an installed one is the tree's, no project's.
2405
+ const entryPath = (entry) => {
2406
+ const path = posix.resolve(cwd, entry)
2407
+ try {
2408
+ return project.realpath(path)
2409
+ } catch {
2410
+ return path
2411
+ }
2412
+ }
2413
+ const reached = () => prisma.reachedProjects(project, tree.root, tree.projects, builds.flatMap((build) => build.entries.map(entryPath)))
2414
+ const projects = strict || withSchema.size === 0 ? withSchema : withSchema.intersection(reached())
2415
+ if (projects.size > 0) {
2416
+ const generating = (reads) => prisma.generatePrismaClients({ host: treeHost(tree, project, reads), root: tree.root, projects, strict })
2417
+ const outputs = await debugTimed('Generated the Prisma Clients', () => (tree.unpack === undefined ? generating() : inRounds(generating, tree.unpack)))
2418
+ if (outputs.length > 0) outside = vfsHost(prisma.withPrismaClients(vfs, outputs))
2419
+ }
1725
2420
  }
1726
- const { bundle, lockfile } = builts.reduce((a, b) => ({ bundle: a.bundle.merge(b.bundle), lockfile: a.lockfile && (() => a.lockfile().merge(b.lockfile())) }))
2421
+ // What follows reads the tree through a host of `outside`: once, or for a lazy tree, in rounds,
2422
+ // each reading into its `reads`, each build keeping what its scans read across them in a memo of
2423
+ // its own.
2424
+ const memos = tree.unpack === undefined ? undefined : builds.map(() => new Map())
2425
+ const attempt = async (reads) => {
2426
+ const host = treeHost(tree, outside, reads)
2427
+ const args = { cwd, host, env }
2428
+ const builts = []
2429
+ // A build of a round that asked for a package already is void, whatever it threw: the builds
2430
+ // after it are run all the same, for all else they ask for, and the round is set aside after.
2431
+ let voided
2432
+ for (const [i, build] of builds.entries()) {
2433
+ let built
2434
+ try {
2435
+ // eslint-disable-next-line no-await-in-loop -- one build at a time, in the order its warnings read
2436
+ built = pm.kind === 'sol' ? { bundle: await buildSolidityBundle({ ...build, ...args }), root: cwd } : await buildJs({ ...build, ...args, innermostRoot, extra: !bundleExtra, outputsAt, memo: memos?.[i] })
2437
+ } catch (error) {
2438
+ if (reads === undefined || reads.asked.size === 0) throw error
2439
+ voided ??= { error }
2440
+ continue
2441
+ }
2442
+ // Rooted at cwd, where `stasis bundle` detects its repo.
2443
+ if (repo === undefined && !built.stateBuilt) built.bundle.repo ??= detectRepo(cwd, host)
2444
+ builts.push(built)
2445
+ }
2446
+ if (voided !== undefined) throw voided.error
2447
+ // Merged as bundleCommand merges its builds.
2448
+ const { root } = builts[0]
2449
+ const union = unionOfBuilds(name, builds, builts, builts.map((built) => built.root))
2450
+ // Once: to the union, by the allowlist of the stasis.config.json at the root, as bundleCommand adds
2451
+ // it; else to the one build given, by its `resources`, as buildOfKind adds it, unless its State
2452
+ // took it. Never one of the files written (none, into a Vfs).
2453
+ return { root, ...bundleExtra || !builts[0].stateBuilt ? withExtra(union, root, { outputs: outputsAt(root), resources: bundleExtra ? undefined : builds[0].resources, host, name }) : union }
2454
+ }
2455
+ const since = counts()
2456
+ const built = tree.unpack === undefined
2457
+ ? await debugTimed('Built', () => attempt(), () => builtSince(since))
2458
+ : await inRounds(...debugging() ? reportedRounds(attempt, tree.unpack) : [attempt, tree.unpack])
2459
+ const { bundle, lockfile, root } = built
1727
2460
  if (repo !== undefined) bundle.repo = repo
1728
2461
  pinCommits(bundle, root, tree)
1729
- return { bundle, lockfile: lockfile?.(), stats: tree.stats, packageManager, root }
2462
+ const unpacked = tree.lazy?.unpacked.size ?? tree.stats.installed ?? tree.stats.dependencies
2463
+ // The lockfile is built when first read, the same one after: `stasis github-bundle` and `stasis
2464
+ // bundle --deptree` read it for --lockfile alone.
2465
+ let lock
2466
+ return { bundle, get lockfile() { return (lock ??= lockfile?.()) }, stats: { ...tree.stats, unpacked }, packageManager, root }
2467
+ }
2468
+
2469
+ // EXODUS_STASIS_DEBUG=1 or true.
2470
+ export const debugging = () => process.env.EXODUS_STASIS_DEBUG === '1' || process.env.EXODUS_STASIS_DEBUG === 'true'
2471
+
2472
+ // The time a --metro build's stages took, all told, in milliseconds: its scans of what the app
2473
+ // imports, what Metro and Babel add to it, the configs and toolchain, and carrying the files reached
2474
+ // into the bundle. What EXODUS_STASIS_DEBUG says of each build.
2475
+ const stageTimes = { scan: 0, metro: 0, configs: 0, carrying: 0 }
2476
+ const staged = (stage, fn) => {
2477
+ const at = performance.now()
2478
+ try {
2479
+ return fn()
2480
+ } finally {
2481
+ stageTimes[stage] += performance.now() - at
2482
+ }
2483
+ }
2484
+ const seconds = (ms) => `${(ms / 1000).toFixed(2)} s`
2485
+
2486
+ // `run()`, said on stderr once what it gives settles where EXODUS_STASIS_DEBUG asks for it: `what`, in
2487
+ // how long, and what `detail()` adds. Written, not warned: inRounds holds back what a round it sets
2488
+ // aside warns. -> what `run()` gives
2489
+ export async function debugTimed(what, run, detail = () => '') {
2490
+ if (!debugging()) return run()
2491
+ const at = performance.now()
2492
+ try {
2493
+ return await run()
2494
+ } finally {
2495
+ process.stderr.write(`[stasis] ${what} in ${seconds(performance.now() - at)}${detail()}\n`)
2496
+ }
2497
+ }
2498
+
2499
+ // scanCounts and stageTimes as they are, for builtSince.
2500
+ const counts = () => ({ files: { ...scanCounts }, stages: { ...stageTimes } })
2501
+ const STAGES = { scan: 'scan', metro: "Metro's additions", configs: 'configs and toolchain', carrying: 'carrying' }
2502
+
2503
+ // What builds did since `since` (counts()'): the files their scans parsed, resolved again from a
2504
+ // parse kept and took as kept, and the time of each stage they went through.
2505
+ function builtSince(since) {
2506
+ const files = Object.keys(scanCounts).map((key) => `${scanCounts[key] - since.files[key]} ${key}`).join(', ')
2507
+ const times = Object.keys(STAGES).filter((key) => stageTimes[key] > since.stages[key]).map((key) => `${STAGES[key]} ${seconds(stageTimes[key] - since.stages[key])}`)
2508
+ return ` (files ${files}${times.length > 0 ? `; ${times.join(', ')}` : ''})`
2509
+ }
2510
+
2511
+ // A package's name, as its directory in a tree says it: what follows its last node_modules.
2512
+ const packageName = (dir) => dir.slice(dir.lastIndexOf('/node_modules/') + '/node_modules/'.length)
2513
+
2514
+ // `attempt` and `unpack` as inRounds takes them, each round said on stderr as it ends (debugTimed):
2515
+ // what it built (builtSince), the packages it asked for, the first of them named, and how long
2516
+ // unpacking them took.
2517
+ function reportedRounds(attempt, unpack) {
2518
+ let round = 0
2519
+ return [
2520
+ (reads) => {
2521
+ round++
2522
+ const since = counts()
2523
+ const asked = () => (reads.asked.size === 0 ? ', the build' : `, asked for ${reads.asked.size} package${reads.asked.size === 1 ? '' : 's'}: ${[...reads.asked].slice(0, 5).map(packageName).join(', ')}${reads.asked.size > 5 ? ', …' : ''}`)
2524
+ return debugTimed(`Lazy round ${round}: built`, () => attempt(reads), () => `${builtSince(since)}${asked()}`)
2525
+ },
2526
+ (asked, read) => debugTimed(`Lazy round ${round}: unpacked`, () => unpack(asked, read)),
2527
+ ]
1730
2528
  }
1731
2529
 
1732
2530
  // Programmatic equivalent of `stasis bundle`: build and return an in-memory Bundle without
@@ -1750,60 +2548,29 @@ export async function buildBundle({ cwd = process.cwd(), env = process.env, entr
1750
2548
  // of `entries` and the options building them, is several builds' (each `{ entries, ...options }`,
1751
2549
  // as stasis.config.json's `bundle` gives an entry its own, see bundle-options.js), whose bundles
1752
2550
  // (and lockfiles) are one's: their strict union, rooted where every one of them is.
1753
- export async function bundleCommand({ cwd = process.cwd(), env = process.env, entries, mappingFile, manifests = false, output, scope, lockfile, conditions, mainFields, platforms, metro, metroResolver, babel = false, babelAliases, jsx = false, flow = false, typescript = false, tsconfig, resources, define, packageJSON = false, cargo = false, cargoFeatures = [], cargoNoDefaultFeatures = false, cargoAllFeatures = false, cargoTarget = null, cargoManifests = false, brotliQuality, add = false, builds } = {}) {
1754
- const options = { cwd, env, entries, mappingFile, manifests, scope, conditions, mainFields, platforms, metro, metroResolver, babel, babelAliases, jsx, flow, typescript, tsconfig, resources, define, packageJSON, cargo, cargoFeatures, cargoNoDefaultFeatures, cargoAllFeatures, cargoTarget, cargoManifests }
1755
- if (builds !== undefined) {
1756
- if (!Array.isArray(builds) || builds.length === 0) throw new TypeError('bundleCommand: builds must be a non-empty array')
1757
- const given = Object.entries(options).find(([key, value]) => !['cwd', 'env'].includes(key) && value !== undefined && value !== false && value !== null && !(Array.isArray(value) && value.length === 0))
1758
- if (given) throw new TypeError(`bundleCommand: ${given[0]} can't be combined with builds (each build sets its own)`)
1759
- }
1760
- // Each build's options, the defaults above where it leaves one out.
1761
- const defaults = { manifests, babel, jsx, flow, typescript, packageJSON, cargo, cargoFeatures, cargoNoDefaultFeatures, cargoAllFeatures, cargoTarget, cargoManifests }
1762
- const plans = (builds ?? [{}]).map((build) => ({ ...options, ...defaults, ...build, cwd, env }))
1763
- const kinds = plans.map((plan) => classifyEntries('bundleCommand', { ...plan, lockfile }))
1764
-
2551
+ // With `deptree`, the dependencies are not read from disk but laid out in memory from the lockfile
2552
+ // that installs cwd, as buildVfsBundle lays them out with `lazy: false` (for JS, pnpm, yarn 1 or
2553
+ // npm's; for Solidity, Soldeer's), and the rest of the project read from disk through them:
2554
+ // refused where no lockfile of those installs cwd, or more than one does, and where anything is
2555
+ // installed already (layOutTree's `refuseInstalled`), before anything is fetched. The tarballs are fetched (or read
2556
+ // from a cache that holds them, held to the lockfile all the same) and nothing is cached: nothing
2557
+ // is written but the bundle and the lockfile. A Soldeer git dependency is fetched from GitHub with
2558
+ // `env`'s GITHUB_TOKEN where it is set; npm is asked with withNpmToken's token (tokens.js).
2559
+ export async function bundleCommand({ cwd = process.cwd(), output, lockfile, brotliQuality, add = false, ...options } = {}) {
1765
2560
  // --add has nothing to merge into on stdout (write-only).
1766
2561
  if (add && output === '-') {
1767
2562
  throw new Error('bundleCommand: --add cannot be combined with --output=- (nothing to merge into on stdout)')
1768
2563
  }
1769
-
1770
- // The files this build writes, never taken into `extra`, given the root its paths are relative to:
1771
- // the bundle (by default at that root, see `target` below) and the lockfile.
1772
- const outputsAt = (root) => [output ?? join(root, DEFAULT_BUNDLE_FILE), lockfile].filter((file) => file !== undefined && file !== '-').map((file) => resolve(cwd, file))
1773
- // With `builds` (stasis.config.json's entries, however many builds they make), `extra` is the
1774
- // bundle's, not any build's: added to their union below.
1775
- const bundleExtra = builds !== undefined
1776
- const builts = []
1777
- for (const [i, plan] of plans.entries()) {
1778
- // eslint-disable-next-line no-await-in-loop -- one build at a time, in the order its warnings read
1779
- builts.push(await buildOfKind(kinds[i], { ...plan, outputsAt, extra: !bundleExtra }))
1780
- }
1781
- // The directory the bundle's paths are relative to: a State-built JS bundle's root, which is the
1782
- // project's at or above cwd (a workspace package's is the workspace root's), else cwd.
1783
- const roots = builts.map((built) => built.root ?? resolve(cwd))
1784
- const root = roots[0]
1785
- const other = roots.findIndex((r) => r !== root)
1786
- if (other !== -1) {
1787
- throw new Error(`bundleCommand: ${plans[0].entries[0]} and ${plans[other].entries[0]} can't be bundled together: their bundles' paths are relative to different roots (${root} and ${roots[other]})`)
1788
- }
2564
+ const built = await buildOfCommand({ ...options, cwd, output, lockfile })
2565
+ const { root } = built
1789
2566
  // By default the bundle goes there, where `stasis run --bundle=load` discovers it. In cwd below
1790
2567
  // the root it would load as rooted at cwd, and root the next `stasis bundle` there too, out of the
1791
2568
  // files above it.
1792
2569
  const target = output ?? relative(resolve(cwd), join(root, DEFAULT_BUNDLE_FILE))
1793
2570
 
1794
- // State-built bundles keep the State root's repo (no cwd fallback: their paths are relative to that root).
1795
- for (const built of builts) if (!built.stateBuilt) built.bundle.repo ??= detectRepo(cwd)
1796
- let union = builts.reduce((a, b) => ({ bundle: a.bundle.merge(b.bundle), lockfile: a.lockfile && (() => a.lockfile().merge(b.lockfile())) }))
1797
- if (bundleExtra) {
1798
- // Once, whatever the builds' order: to their union, whose own record of a file stands, by the
1799
- // allowlist of the stasis.config.json at the root, not any one entry's `resources`.
1800
- const withExtras = withExtra(union, root, outputsAt(root))
1801
- if (withExtras !== union) pinInstalledCommits(withExtras.bundle, root)
1802
- union = withExtras
1803
- }
1804
- let { bundle } = union
2571
+ let { bundle } = built
1805
2572
  // Only a JS bundle has a lockfile (classifyEntries refuses one for the others).
1806
- let lockData = lockfile ? union.lockfile().serialize() : undefined
2573
+ let lockData = lockfile ? built.lockfile().serialize() : undefined
1807
2574
 
1808
2575
  // --add: union the fresh build into the existing on-disk bundle; a conflicting file throws. Skipped when nothing is on disk.
1809
2576
  const outAbs = target === '-' ? undefined : resolve(cwd, target)
@@ -1813,7 +2580,7 @@ export async function bundleCommand({ cwd = process.cwd(), env = process.env, en
1813
2580
  if (add && existsSync(outAbs)) {
1814
2581
  let existing
1815
2582
  try {
1816
- existing = Bundle.parse(brotliDecompressSync(readFileSync(outAbs)).toString('utf8'))
2583
+ existing = parseBundleBytes(readFileSync(outAbs))
1817
2584
  } catch (cause) {
1818
2585
  throw new Error(`bundleCommand: --add failed to read the existing bundle at ${target}`, { cause })
1819
2586
  }
@@ -1852,3 +2619,72 @@ export async function bundleCommand({ cwd = process.cwd(), env = process.env, en
1852
2619
  console.warn(bundledSummary(files.length, modules, fromDir, dest))
1853
2620
  }
1854
2621
  }
2622
+
2623
+ // The bundle bundleCommand writes of its options (but `brotliQuality` and `add`, which say how it is
2624
+ // written), built as it builds it, warnings and all, and not written: as `stasis audit` would read it
2625
+ // back from the file, a Bundle parsed from what bundleCommand would compress (no lockfile is
2626
+ // written either). Its errors are bundleCommand's.
2627
+ export async function buildCommandBundle(options = {}) {
2628
+ const { bundle } = await buildOfCommand(options)
2629
+ return Bundle.parse(bundle.serialize())
2630
+ }
2631
+
2632
+ // bundleCommand's bundle, and its lockfile (`lockfile()`, a JS bundle's), built as it builds them from
2633
+ // its options (but `brotliQuality` and `add`, which say how they are written) and not written, with
2634
+ // the root their paths are relative to. `output` and `lockfile` are where it would write them, which
2635
+ // `extra` never takes in. -> { bundle, lockfile: () => Lockfile, root }
2636
+ async function buildOfCommand({ cwd = process.cwd(), env = process.env, home, entries, mappingFile, manifests = false, output, scope, lockfile, conditions, mainFields, platforms, metro, metroResolver, babel = false, babelAliases, jsx = false, flow = false, typescript = false, tsconfig, resources, define, packageJSON = false, cargo = false, cargoFeatures = [], cargoNoDefaultFeatures = false, cargoAllFeatures = false, cargoTarget = null, cargoManifests = false, builds, deptree = false } = {}) {
2637
+ const options = { cwd, env, entries, mappingFile, manifests, scope, conditions, mainFields, platforms, metro, metroResolver, babel, babelAliases, jsx, flow, typescript, tsconfig, resources, define, packageJSON, cargo, cargoFeatures, cargoNoDefaultFeatures, cargoAllFeatures, cargoTarget, cargoManifests }
2638
+ if (builds !== undefined) {
2639
+ if (!Array.isArray(builds) || builds.length === 0) throw new TypeError('bundleCommand: builds must be a non-empty array')
2640
+ const given = Object.entries(options).find(([key, value]) => !['cwd', 'env'].includes(key) && value !== undefined && value !== false && value !== null && !(Array.isArray(value) && value.length === 0))
2641
+ if (given) throw new TypeError(`bundleCommand: ${given[0]} can't be combined with builds (each build sets its own)`)
2642
+ }
2643
+ // Each build's options, the defaults above where it leaves one out.
2644
+ const plans = (builds ?? [{}]).map((build) => ({ ...options, ...build, cwd, env }))
2645
+ const kinds = plans.map((plan) => classifyEntries('bundleCommand', { ...plan, lockfile }))
2646
+
2647
+ // The files this build writes, never taken into `extra`, given the root its paths are relative to:
2648
+ // the bundle (by default at that root, see bundleCommand's `target`) and the lockfile.
2649
+ const outputsAt = (root) => [output ?? join(root, DEFAULT_BUNDLE_FILE), lockfile].filter((file) => file !== undefined && file !== '-').map((file) => resolve(cwd, file))
2650
+ // With `builds` (stasis.config.json's entries, however many builds they make), `extra` is the
2651
+ // bundle's, not any build's: added to their union below.
2652
+ const bundleExtra = builds !== undefined
2653
+ let union, root
2654
+ if (deptree) {
2655
+ // The disk read through the tree, nothing cached (`cache: false`, and no setCacheDir here).
2656
+ const [{ createClient }, { detectPackageManager }] = await Promise.all([import('@preventive/upstream/github.js'), import('../vfs-bundle/tree.js')])
2657
+ const built = await withNpmToken(home, () => buildFromLockfile('bundleCommand', diskHost, {
2658
+ cwd: resolve(cwd),
2659
+ packageManager: detectPackageManager('bundleCommand', diskHost, resolve(cwd), { why: '--deptree lays out the dependencies from a lockfile' }),
2660
+ lockfile,
2661
+ cache: false,
2662
+ lazy: false,
2663
+ client: createClient({ token: env.GITHUB_TOKEN || null }),
2664
+ }, { env, plan: { builds: plans, bundleExtra }, outputsAt, refuseInstalled: true }))
2665
+ console.warn(unpackedSummary(built.stats))
2666
+ root = built.root
2667
+ union = { bundle: built.bundle, lockfile: () => built.lockfile }
2668
+ } else {
2669
+ const builts = []
2670
+ for (const [i, plan] of plans.entries()) {
2671
+ // eslint-disable-next-line no-await-in-loop -- one build at a time, in the order its warnings read
2672
+ builts.push(await buildOfKind(kinds[i], { ...plan, outputsAt, extra: !bundleExtra }))
2673
+ }
2674
+ // The directory the bundle's paths are relative to: a State-built JS bundle's root, which is the
2675
+ // project's at or above cwd (a workspace package's is the workspace root's), else cwd.
2676
+ const roots = builts.map((built) => built.root ?? resolve(cwd))
2677
+ root = roots[0]
2678
+ // State-built bundles keep the State root's repo (no cwd fallback: their paths are relative to that root).
2679
+ for (const built of builts) if (!built.stateBuilt) built.bundle.repo ??= detectRepo(cwd)
2680
+ union = unionOfBuilds('bundleCommand', plans, builts, roots)
2681
+ if (bundleExtra) {
2682
+ // Once, whatever the builds' order: to their union, whose own record of a file stands, by the
2683
+ // allowlist of the stasis.config.json at the root, not any one entry's `resources`.
2684
+ const withExtras = withExtra(union, root, { outputs: outputsAt(root) })
2685
+ if (withExtras !== union) pinInstalledCommits(withExtras.bundle, root)
2686
+ union = withExtras
2687
+ }
2688
+ }
2689
+ return { ...union, root }
2690
+ }