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

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.
package/src/cmd/bundle.js CHANGED
@@ -1,25 +1,26 @@
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'
9
+ import { getParser, importsTypescriptByOutputName, literalSpec, scan, scanCounts } from '../scan.js'
11
10
  import { parseDefine } from '../define.js'
12
- import { createFieldResolver, resolveConditions } from '../resolve-fields.js'
11
+ import { METRO_ASSET_EXTS, METRO_SOURCE_EXTS, createFieldResolver, isMetroAssetFile, resolveConditions } from '../resolve-fields.js'
13
12
  import { discoverTsconfig, isDir, loadTsconfigPaths, packageTsconfigPaths } 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
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'
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, 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'
24
25
  import {
25
26
  SOLIDITY_PACKAGE_MANIFESTS,
@@ -41,7 +42,7 @@ import {
41
42
  loadComposerPackages,
42
43
  loadLaravelProviderFiles,
43
44
  } from '../loaders/php.js'
44
- import { DEFAULT_BUNDLE_FILE, bundledSummary, packagesLabel, writeBundle, writeFile } from './output.js'
45
+ import { DEFAULT_BUNDLE_FILE, bundledSummary, packagesLabel, unpackedSummary, writeBundle, writeFile } from './output.js'
45
46
 
46
47
  const JS_EXTS = new Set(['.js', '.cjs', '.mjs', '.ts', '.cts', '.mts', '.jsx', '.tsx'])
47
48
  const BASH_EXTS = new Set(['.sh', '.bash'])
@@ -216,9 +217,6 @@ function makeRustClassifier(cargo) {
216
217
  }
217
218
  }
218
219
 
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
220
  // A file of the bundle root (`realBase` its real path), its bytes, by project-relative path, or
223
221
  // null when it isn't a regular file there (a manifest the context saw may have gone since; a FIFO
224
222
  // would hang a read) or, with `within` a directory, doesn't really lie in it (a vendored crate's
@@ -241,7 +239,8 @@ function readFileWithinBase(baseDir, realBase, rel, within) {
241
239
  function executableSources(baseDir, sources, host) {
242
240
  const executable = new Set()
243
241
  for (const path of sources.keys()) {
244
- if (isExecutableFile(join(baseDir, path), host)) executable.add(path)
242
+ // A fresh set: a file that can't be looked at is just not marked.
243
+ if (observeExecutable(join(baseDir, path), host) === true) executable.add(path)
245
244
  }
246
245
  return executable
247
246
  }
@@ -268,11 +267,11 @@ function assertSelfContained(lang, noun, entries, sources, missing) {
268
267
  // (packageLookup by default, which walks past a malformed one). `classifyDep(path)` optionally
269
268
  // places a file directly (non-node_modules ecosystems like Soldeer/github); null defers. `format` tags
270
269
  // 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.
270
+ // a flat target string or a Map<platform,target>; both round-trip untouched. `keyedResolutions`,
271
+ // `consumer` and `consumerFiles` are codeBundle's.
273
272
  function assembleCodeBundle({
274
- baseDir, entries, sources, resolutions, workspaceName, workspaceVersion, format, formats, conditionKey, classifyDep, host,
275
- packageOf = packageLookup(baseDir, { host }), consumer,
273
+ baseDir, entries, sources, resolutions, keyedResolutions, workspaceName, workspaceVersion, format, formats, conditionKey, classifyDep, host,
274
+ packageOf = packageLookup(baseDir, { host }), consumer, consumerFiles,
276
275
  }) {
277
276
  const modules = new Map()
278
277
  // `identity`'s name, version, ecosystem, repo and vendored (moduleInfo takes those alone); a later
@@ -290,35 +289,30 @@ function assembleCodeBundle({
290
289
  continue
291
290
  }
292
291
  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
292
+ // A node_modules file whose nearest package.json is the workspace root (or none at all) is a misconfigured dep -- refuse.
293
+ if (splitNodeModulesPath(path) !== null && !(meta && hasNodeModulesSegment(meta.pkgDir))) {
294
+ throw new Error(`No package.json with name+version found for ${path}`)
302
295
  }
296
+ const [dir, identity] = meta ? [meta.pkgDir, meta] : ['.', { name: workspaceName, version: workspaceVersion }]
297
+ ensureBucket(dir, identity).files[fileInBucket(dir, path)] = content
303
298
  }
304
299
 
305
- return codeBundle({ baseDir, entries, sources, modules, resolutions, format, formats, conditionKey, host, consumer })
300
+ return codeBundle({ baseDir, entries, sources, modules, resolutions, keyedResolutions, format, formats, conditionKey, host, consumer, consumerFiles })
306
301
  }
307
302
 
308
303
  // A full-scope code Bundle of `sources`, bucketed as `modules`: `format` tags every file, `formats`
309
304
  // (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' }) {
305
+ // are keyed under `conditionKey`, beside the `keyedResolutions` (conditions key -> parent -> specifier
306
+ // -> target) under keys of their own. The `consumerFiles` (every file, by default) are attributed to
307
+ // `consumer` in the `reason` map: `bundle`, or `bundle:metro` for --metro.
308
+ function codeBundle({ baseDir, entries, sources, modules, resolutions, keyedResolutions = new Map(), format, formats, conditionKey, host, consumer = 'bundle', consumerFiles }) {
313
309
  const formatsMap = new Map()
314
310
  for (const path of sources.keys()) {
315
311
  const fileFormat = formats?.get(path) ?? format
316
312
  if (fileFormat !== undefined) formatsMap.set(path, fileFormat)
317
313
  }
318
314
 
319
- const importsForKey = new Map()
320
- for (const [parent, specMap] of resolutions) importsForKey.set(parent, specMap)
321
- const imports = new Map([[conditionKey, importsForKey]])
315
+ const imports = new Map([[conditionKey, new Map(resolutions)], ...keyedResolutions])
322
316
 
323
317
  // Executable bits, straight off disk (a synthetic source with no file there is simply not
324
318
  // executable). Shell bundles lean on this most: `stasis extract` puts the +x back on the scripts.
@@ -332,23 +326,13 @@ function codeBundle({ baseDir, entries, sources, modules, resolutions, format, f
332
326
  formats: formatsMap,
333
327
  imports,
334
328
  executable,
335
- }).withReason(consumer)
329
+ }).withReason(consumer, consumerFiles)
336
330
  }
337
331
 
338
332
  // Files never carried, whatever reads them: `.env` files, and Hardhat's config, which is code
339
333
  // (both however they're cased).
340
334
  const neverCarried = (rel) => isDotEnvFile(rel) || posix.basename(rel).toLowerCase().startsWith('hardhat.config.')
341
335
 
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
336
  // The build-description files of a Solidity bundle (--manifests), as Map<path, text>, sorted:
353
337
  // `configFiles` (what discoverSolidityConfig read, whatever they're called), each of which must be
354
338
  // carried -- one outside the root, neverCarried, refused or gone is an error, as the bundle
@@ -398,7 +382,7 @@ function solidityManifests(baseDir, sources, configFiles, { classifyDep, package
398
382
 
399
383
  // An entry `stasis bundle` takes for a directory: one that is, or an extensionless path that
400
384
  // 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)
385
+ const isDirEntry = (abs, host = diskHost) => isDir(abs, host) || (extname(abs) === '' && host.stat(abs) === null)
402
386
 
403
387
  // Whether `entry` (resolved against `cwd`) is a Solidity bundle's: a .sol file or a directory entry.
404
388
  export const isSolidityEntry = (entry, cwd = process.cwd(), host = diskHost) => entry.endsWith('.sol') || isDirEntry(resolve(cwd, entry), host)
@@ -752,9 +736,9 @@ function refuseSplit(split, baseDir) {
752
736
  }
753
737
  }
754
738
 
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).
739
+ // Classify scanner unresolved edges into fatal (broken/divergent at load) vs tolerated (a
740
+ // catchable runtime miss); every parse error is fatal. Shared so the plain and --mainFields JS
741
+ // paths gate identically. `baseDir` relativizes the paths in the emitted messages (see displayPath).
758
742
  function analyzeScanner(scanner, { baseDir }) {
759
743
  const show = (url) => displayPath(url, baseDir)
760
744
  const staticReachable = eagerlyLinked(scanner)
@@ -762,11 +746,9 @@ function analyzeScanner(scanner, { baseDir }) {
762
746
  const fatal = scanner.unresolved
763
747
  .filter((u) => fatalUnresolved(u))
764
748
  .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
- }
749
+ // A file the parser couldn't read cleanly, however it is reached: its edges are unknown (a module
750
+ // file's are discarded) or partial (those of a script's recovered AST), never a graph to record.
751
+ for (const p of scanner.parseErrors) fatal.push(`parse error in ${show(p.url)}: ${p.message}`)
770
752
  // Edge resolving to a file a source bundle can't carry (.node/.wasm/extensionless): scan
771
753
  // records it but never queues the child, so load would die.
772
754
  for (const [, byParent] of scanner.imports) {
@@ -779,13 +761,12 @@ function analyzeScanner(scanner, { baseDir }) {
779
761
  }
780
762
  }
781
763
  const tolerated = scanner.unresolved.filter((u) => !fatalUnresolved(u))
782
- const toleratedParse = scanner.parseErrors.filter((p) => !fatalParse(p))
783
- return { fatal, tolerated, toleratedParse }
764
+ return { fatal, tolerated }
784
765
  }
785
766
 
786
767
  // Throw on fatal scan issues; warn on tolerated ones. `label` tags the scan pass; `baseDir`
787
768
  // relativizes the paths in the warnings (see displayPath), matching analyzeScanner's messages.
788
- function reportScanIssues({ fatal, tolerated, toleratedParse, leftOut = [] }, { label = '', baseDir, root = '' }) {
769
+ function reportScanIssues({ fatal, tolerated, leftOut = [] }, { label = '', baseDir, root = '' }) {
789
770
  const where = label ? ` (${label})` : ''
790
771
  const show = (url) => displayPath(url, baseDir)
791
772
  // The first 10 of `items` as indented lines (`line` renders one), then how many more there are.
@@ -800,10 +781,6 @@ function reportScanIssues({ fatal, tolerated, toleratedParse, leftOut = [] }, {
800
781
  const summary = listed(tolerated, (u) => describeUnresolved(u, show))
801
782
  console.warn(`[stasis] Bundle has ${tolerated.length} unresolved import(s)${where}; they will fall through at load time:\n${summary}`)
802
783
  }
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
784
  if (leftOut.length > 0) {
808
785
  // The file outside the root by its path from the cwd, as the refusal of one linked eagerly names it.
809
786
  const summary = listed(leftOut, (u) => `${u.kind} ${u.spec} from ${show(u.parentURL)} (at ${relative(baseDir, fileURLToPath(u.outsideURL))})`)
@@ -840,9 +817,11 @@ function typescriptPathsFor(typescript, baseDir, tsconfig, host, env) {
840
817
  // root above it, where that holds no stasis file of its own. With `extra`, the files its
841
818
  // stasis.config.json's `extra` names are added as `stasis run` adds them (State#includeExtra), never
842
819
  // 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 = () => [] } = {}) {
820
+ // relative to), resources by `resources` where given, else by the config's; read through `host`, as
821
+ // Node's glob reads the disk. `memo` is what the build's scans keep across the rounds a lazy tree is
822
+ // read in, by each scan's label (vfs-bundle/lazy.js): the scan keeps what it reads there, and a round
823
+ // that read a package not unpacked yet (`host.touches()`) stops after it.
824
+ 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
825
  if (!Array.isArray(entries) || entries.length === 0) {
847
826
  throw new Error('buildJsBundle: at least one entry .js/.cjs/.mjs/.ts/.cts/.mts/.jsx/.tsx file is required')
848
827
  }
@@ -860,7 +839,9 @@ export async function buildJsBundle({ cwd = process.cwd(), env = process.env, en
860
839
  typescript ||= importsTypescriptByOutputName(absEntries, host)
861
840
  const typescriptPaths = typescriptPathsFor(typescript, baseDir, tsconfig, host, env)
862
841
 
863
- const scanned = scan(absEntries, { conditions: scanConditions, jsx, flow, typescript, typescriptPaths, resources: resourceSet, define, host })
842
+ const scanned = scan(absEntries, { conditions: scanConditions, jsx, flow, typescript, typescriptPaths, resources: resourceSet, define, host, memo: memo ? nestedMap(memo, 'js') : null })
843
+ // A round of a lazy tree that asked for a package already is void: the rest is moot.
844
+ if (memo && host.touches() > 0) throw new Error('buildJsBundle: this round reads a package not unpacked yet')
864
845
 
865
846
  // Materialise via a non-preload State: addFile bucketizes + records sources/formats,
866
847
  // addImport replays the edge map; serialize emits the runtime loader's v1 layout.
@@ -878,7 +859,7 @@ export async function buildJsBundle({ cwd = process.cwd(), env = process.env, en
878
859
  // Only what lies within the root is carried (see withinRoot). One the entries link eagerly is
879
860
  // refused, named here rather than as the State's bare assertion: a stasis file in a directory below
880
861
  // 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))))
862
+ const { scanner, outside, leftOut } = withinRoot(scanned, (url) => isPathWithin(state.root, fileURLToPath(url)))
882
863
  const markers = STASIS_ROOT_FILES.filter((name) => host.stat(join(state.root, name)) !== null)
883
864
  const rootedBy = markers.length === 0 ? '' : ` (rooted there by its ${markers.join(' and ')}, which can be removed if stale)`
884
865
  const root = ` at ${relative(baseDir, state.root) || '.'}${rootedBy}`
@@ -909,12 +890,7 @@ export async function buildJsBundle({ cwd = process.cwd(), env = process.env, en
909
890
  const byParent = new Map()
910
891
  for (const [key, parents] of scanner.imports) {
911
892
  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
- }
893
+ for (const [spec, childURL] of specs) nestedMap(nestedMap(byParent, parentURL), spec).set(key, childURL)
918
894
  }
919
895
  }
920
896
  for (const [parentURL, bySpec] of byParent) {
@@ -957,14 +933,48 @@ function innermostRootOf(baseDir, files, root, host) {
957
933
  // resolves a file the bundle would reject; an extensionless `import './Foo'` can land on
958
934
  // Foo.jsx/Foo.tsx, as it does under Metro. Like Metro's default, this doesn't track a project's
959
935
  // own sourceExts.
960
- const SOURCE_EXTS = ['js', 'jsx', 'json', 'ts', 'tsx']
936
+ const SOURCE_EXTS = METRO_SOURCE_EXTS
961
937
  // React Native preset mainFields for `--metro` (which also sets the RN conditions + platform suffixes).
962
938
  const METRO_MAIN_FIELDS = ['react-native', 'browser', 'main']
963
939
 
964
940
  // The config files a React Native app's build loads from its project root, though nothing the app
965
941
  // 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']
942
+ const METRO_CONFIG_FILE = 'metro.config.js'
943
+ const METRO_CONFIG_FILES = [METRO_CONFIG_FILE, 'react-native.config.js']
967
944
  const BABEL_CONFIG_FILE = 'babel.config.js'
945
+ // What else a React Native app's build runs in Node, which neither the app nor the configs import:
946
+ // the CLI that runs Metro and Metro itself, and what they load by a name or path given as a string.
947
+ // Each { name, file, via }: the package `name` as the packages `via` find it, each the next in turn
948
+ // (as each requires it), else as fewer of them do, the last ones, down to the project alone; `file`
949
+ // in it, else its entry, as a require() of it from the last of `via` resolves.
950
+ // Metro, as React Native's CLI plugin runs it (`react-native bundle`, `start`).
951
+ const METRO_VIA = ['react-native', '@react-native/community-cli-plugin', 'metro']
952
+ const METRO_TOOLCHAIN = [
953
+ // React Native's CLI, which an app runs Metro through.
954
+ { name: 'react-native', file: 'cli.js', via: [] },
955
+ { name: 'metro', via: METRO_VIA.slice(0, -1) },
956
+ // Metro's worker, which its workers run (require.resolve'd, to fork), its transform worker
957
+ // (metro-config's default `transformerPath`), the child process jest-worker forks them in, and
958
+ // metro-file-map's Haste worker: each loaded by a path.
959
+ { name: 'metro', file: 'src/DeltaBundler/Worker.js', via: METRO_VIA.slice(0, -1) },
960
+ { name: 'metro-transform-worker', via: METRO_VIA },
961
+ { name: 'jest-worker', file: 'build/workers/processChild.js', via: METRO_VIA },
962
+ { name: 'metro-file-map', file: 'src/plugins/haste/worker.js', via: METRO_VIA },
963
+ // And its minifier, metro-config's default `minifierPath` (DEFAULT_METRO_MINIFIER_PATH), which the
964
+ // worker requires from its own directory, by that name, as it minifies: what a pnpm override puts there
965
+ // in its stead too (`module.exports = {}`).
966
+ { name: 'metro-minify-terser', via: [...METRO_VIA, 'metro-transform-worker'] },
967
+ ]
968
+ // With --babel(-aliases), the Babel transformer React Native's Metro config names
969
+ // (`babelTransformerPath`, require.resolve'd from @react-native/metro-config), which runs @babel/core
970
+ // and React Native's Babel preset, with its plugins.
971
+ const BABEL_TOOLCHAIN = [{ name: '@react-native/metro-babel-transformer', via: ['@react-native/metro-config'] }]
972
+ // The transformers stasis's Metro plugin (src/metro.js) points Metro at by path, beside it, which
973
+ // Metro's workers run when a config wraps its own in withStasis().
974
+ const STASIS_METRO_TRANSFORMERS = ['metro-transformer.js', 'metro-babel-transformer.js']
975
+ // The conditions a require() resolves with in Node, and the key its edges take (the scan's).
976
+ const NODE_REQUIRE_CONDITIONS = new Set(['require', 'node', 'node-addons', 'module-sync'])
977
+ const REQUIRE_KEY = [...NODE_REQUIRE_CONDITIONS].join(', ')
968
978
  // What esbuild and webpack 5 both assert on every platform besides `import`/`require` and
969
979
  // `default`, and so `--mainFields` too: `module`, the bundler-only condition a package's ESM build
970
980
  // sits under, from an import and a require alike. (esbuild drops it when given conditions of its
@@ -976,7 +986,8 @@ const BUNDLER_CONDITIONS = ['module']
976
986
  // it adds to the bundler base (`import`/`require` + `default`, never Node's `node`; see
977
987
  // resolveConditions): --metro asserts the RN conditions (+ browser on web), Metro's own set;
978
988
  // --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 }) {
989
+ // Under --metro, `assetExts` are the extensions Metro takes as assets (its defaults where not given).
990
+ export function fieldResolverFor(platform, { mainFields, metro = false, assetExts, conditions = [], typescript = false, typescriptPaths = null, host = diskHost, kept }) {
980
991
  const extras = metro ? ['react-native', ...(platform === 'web' ? ['browser'] : [])] : [...new Set([...BUNDLER_CONDITIONS, ...conditions])]
981
992
  const fields = metro ? METRO_MAIN_FIELDS : mainFields
982
993
  const resolver = createFieldResolver({
@@ -987,28 +998,38 @@ export function fieldResolverFor(platform, { mainFields, metro = false, conditio
987
998
  conditions: resolveConditions('commonjs', extras),
988
999
  // Opt into Metro's package-entry browser-field quirks only on the --metro path.
989
1000
  metro,
1001
+ assetExts,
990
1002
  // --typescript: tsc's mapping, inside the field resolver (the scanner's own fallback
991
1003
  // only backs the built-in resolver). Unreachable under --metro-resolver
992
1004
  // (classifyEntries rejects the combination -- metro-resolver can't substitute).
993
1005
  typescript,
994
1006
  typescriptPaths,
995
1007
  host,
1008
+ kept,
996
1009
  })
997
1010
  return { extras, mainFields: fields, resolver }
998
1011
  }
999
1012
 
1000
1013
  const CONFIG_FILE = 'stasis.config.json'
1001
1014
 
1002
- // The stasis.config.json in `cwd`, parsed; null where there is none.
1003
- function readConfigFile(name, cwd, host = diskHost) {
1015
+ // The stasis.config.json in `cwd`, parsed; null where there is none. `name`, if any, prefixes errors.
1016
+ export function readConfigFile(name, cwd, host = diskHost) {
1004
1017
  const text = readFileSyncMaybe(resolve(cwd), CONFIG_FILE, 'utf8', host)
1005
1018
  try {
1006
1019
  return text === null ? null : JSON.parse(text)
1007
1020
  } catch (cause) {
1008
- throw new Error(`${name}: ${CONFIG_FILE} is not valid JSON`, { cause })
1021
+ throw new Error(`${name === undefined ? '' : `${name}: `}${CONFIG_FILE} is not valid JSON`, { cause })
1009
1022
  }
1010
1023
  }
1011
1024
 
1025
+ // Whether the tree laid out from the lockfile for the project `host` holds at `cwd` installs
1026
+ // devDependencies: the `installDevDependencies` of the stasis.config.json there, true without one.
1027
+ export const installsDevDependencies = (name, cwd, host) => parseInstallDevDependencies(CONFIG_FILE, readConfigFile(name, cwd, host)?.installDevDependencies)
1028
+
1029
+ // Whether the tree laid out from the lockfile for the project `host` holds at `cwd` may be lazy: the
1030
+ // `lazyDependencies` of the stasis.config.json there, true without one.
1031
+ const lazesDependencies = (name, cwd, host) => parseLazyDependencies(CONFIG_FILE, readConfigFile(name, cwd, host)?.lazyDependencies)
1032
+
1012
1033
  // --babel-aliases: the alias map of the JSON file `file` (a path from `baseDir`, in the project, read through
1013
1034
  // `host`), babel-plugin-module-resolver's `alias` option as data: each alias to the string it maps to.
1014
1035
  function readAliasesFile(baseDir, file, host = diskHost) {
@@ -1079,11 +1100,338 @@ function metroEmptyModule(baseDir, host) {
1079
1100
  return isUtf8(buf) && EMPTY_MODULE_SOURCE.test(buf.toString('utf8').trim()) ? real : null
1080
1101
  }
1081
1102
 
1103
+ // The directory of package `name`, by its real path, in the nearest node_modules up from `dir` that
1104
+ // holds it, as Node looks for one; through each package of `via` first, one that depends on it, for
1105
+ // an install (pnpm) that links it only beside that one. -> undefined where none does
1106
+ function packageDirFrom(dir, name, host, via = []) {
1107
+ let from = dir
1108
+ for (const step of [...via, name]) {
1109
+ let found
1110
+ for (let d = from; found === undefined; d = dirname(d)) {
1111
+ const candidate = join(d, 'node_modules', step)
1112
+ if (host.stat(join(candidate, 'package.json'))?.isFile()) found = host.realpath(candidate)
1113
+ else if (dirname(d) === d) return undefined
1114
+ }
1115
+ from = found
1116
+ }
1117
+ return from
1118
+ }
1119
+
1120
+ // What a require() of `spec` from the directory `dir` resolves to, as Node resolves it. -> absolute
1121
+ // path, or undefined where none does
1122
+ function requireFrom(dir, spec, host) {
1123
+ try {
1124
+ return host.resolve(join(dir, 'package.json'), spec, NODE_REQUIRE_CONDITIONS)
1125
+ } catch {
1126
+ return undefined
1127
+ }
1128
+ }
1129
+
1130
+ // The AST of the module `file`, read through `host` (`source` where read already).
1131
+ const parseModule = (file, host, source = host.readFile(file).toString('utf8')) => getParser().parseSync(file, source, { sourceType: 'unambiguous' }).program
1132
+
1133
+ // Calls `fn` on each node of the AST `node`, depth first.
1134
+ function walkAst(node, fn) {
1135
+ if (Array.isArray(node)) {
1136
+ for (const child of node) walkAst(child, fn)
1137
+ return
1138
+ }
1139
+ if (!node || typeof node !== 'object' || typeof node.type !== 'string') return
1140
+ fn(node)
1141
+ for (const key of Object.keys(node)) if (key !== 'type') walkAst(node[key], fn)
1142
+ }
1143
+
1144
+ // The method `node` calls on an object (`x.concat(...)` -> 'concat'), on the one named `object` where
1145
+ // given (`require.resolve(...)`), or undefined.
1146
+ function memberCall(node, object) {
1147
+ if (node?.type !== 'CallExpression' || node.callee.type !== 'MemberExpression' || node.callee.computed) return undefined
1148
+ if (object !== undefined && (node.callee.object.type !== 'Identifier' || node.callee.object.name !== object)) return undefined
1149
+ return node.callee.property.name
1150
+ }
1151
+
1152
+ // What the module `file` names by `require.resolve('x')`, read off its source, never run, each where
1153
+ // Node resolves it (babelPathCall: from the module, or the `paths` it gives): the polyfills
1154
+ // @react-native/js-polyfills' index.js lists for React Native's Metro config to prepend
1155
+ // (`getPolyfills`), the Babel plugins a plugin runs Babel with itself. A specifier resolved more than
1156
+ // once (in other `paths` too) is each of its resolutions.
1157
+ // -> [specifier, absolute path][] of those that resolve, in source order
1158
+ function requireResolutions(file, host) {
1159
+ const resolved = []
1160
+ const source = host.readFile(file).toString('utf8')
1161
+ // Parsed only where both words occur, however the call spaces them (`require\n .resolve(`).
1162
+ if (!source.includes('require') || !source.includes('resolve')) return resolved
1163
+ walkAst(parseModule(file, host, source), (node) => {
1164
+ const spec = memberCall(node, 'require') === 'resolve' ? literalSpec(node.arguments[0]) : null
1165
+ const path = spec === null ? null : babelPathCall(node, file, host)
1166
+ if (path !== null) resolved.push([spec, path])
1167
+ })
1168
+ return resolved
1169
+ }
1170
+
1171
+ // The name Babel requires a plugin or preset its config names by (@babel/core's standardizeName):
1172
+ // a `module:` name as written, else a short one prefixed (`module-resolver` ->
1173
+ // babel-plugin-module-resolver, `@babel/env` -> @babel/preset-env, `@scope/x` -> @scope/babel-plugin-x,
1174
+ // `@scope` -> @scope/babel-plugin); a path as is.
1175
+ const BABEL_PREFIX = { plugin: /^(?!@|module:|[^/]+\/|babel-plugin-)/u, preset: /^(?!@|module:|[^/]+\/|babel-preset-)/u }
1176
+ const BABEL_ORG = { plugin: /^(@babel\/)(?!plugin-|[^/]+\/)/u, preset: /^(@babel\/)(?!preset-|[^/]+\/)/u }
1177
+ const OTHER_ORG = { plugin: /^(@(?!babel\/)[^/]+\/)(?![^/]*babel-plugin(?:-|\/|$)|[^/]+\/)/u, preset: /^(@(?!babel\/)[^/]+\/)(?![^/]*babel-preset(?:-|\/|$)|[^/]+\/)/u }
1178
+ const OTHER_ORG_DEFAULT = /^(@(?!babel$)[^/]+)$/u
1179
+ const standardizeBabelName = (type, name) => (isAbsolute(name) ? name : name
1180
+ .replace(BABEL_PREFIX[type], `babel-${type}-`)
1181
+ .replace(BABEL_ORG[type], `$1${type}-`)
1182
+ .replace(OTHER_ORG[type], `$1babel-${type}-`)
1183
+ .replace(OTHER_ORG_DEFAULT, `$1/babel-${type}`)
1184
+ .replace(/^module:/u, ''))
1185
+
1186
+ // A string that names a Babel plugin or preset by its form, a package or a path: `module:x`,
1187
+ // `@babel/plugin-x`, `babel-plugin-x`, `@react-native/babel-preset`,
1188
+ // `node_modules/@org/x-babel/babel/plugin-inline.js`.
1189
+ const BABEL_MODULE_STRING = /^module:|babel[-/](?:plugin|preset)/u
1190
+ // A module of a Babel config by its name: `babel` and `config` in its file's name (babel.config.js,
1191
+ // create-babel-config.js), or in its package's (a shared config, @org/babel-config).
1192
+ const BABEL_CONFIG_NAME = /babel.*config|config.*babel/iu
1193
+ const isBabelConfigModule = (rel) => BABEL_CONFIG_NAME.test(posix.basename(rel)) || BABEL_CONFIG_NAME.test(splitNodeModulesPath(rel)?.name ?? '')
1194
+
1195
+ // The lists a Babel config names its plugins and presets in, by the name of the key, variable or object
1196
+ // holding one, and what Babel takes each of them as.
1197
+ const BABEL_LISTS = new Map([['plugins', 'plugin'], ['presets', 'preset']])
1198
+ // And the list of extensions a Metro config gives Metro to take as assets (`resolver.assetExts`).
1199
+ const METRO_ASSET_LISTS = new Map([['assetExts', 'asset']])
1200
+ // The array methods making a list of the one they're called on: `concat` (with its arguments'),
1201
+ // `filter`, `flat`, `slice`.
1202
+ const LIST_METHODS = new Set(['concat', 'filter', 'flat', 'slice'])
1203
+ // A path the module `file` computes from its own directory and strings: `__dirname`, a string, or a
1204
+ // `path.resolve(...)` or `path.join(...)` of them. -> the path (a relative one from Babel's cwd), or null
1205
+ function staticPath(node, file) {
1206
+ if (node?.type === 'Identifier' && node.name === '__dirname') return dirname(file)
1207
+ const string = literalSpec(node)
1208
+ if (string !== null || node?.type !== 'CallExpression' || memberCall(node, 'require') !== undefined) return string
1209
+ const name = node.callee.type === 'Identifier' ? node.callee.name : memberCall(node)
1210
+ if (name !== 'resolve' && name !== 'join') return null
1211
+ const parts = node.arguments.map((arg) => staticPath(arg, file))
1212
+ if (parts.length === 0 || parts.includes(null)) return null
1213
+ return name === 'resolve' && parts.some((part) => isAbsolute(part)) ? resolve(...parts) : join(...parts)
1214
+ }
1215
+
1216
+ // The path a call computes, as Babel takes a plugin or preset named by path:
1217
+ // `path.resolve(__dirname, 'node_modules/@org/x/plugin')`, `path.join(__dirname, 'x')` (staticPath), or
1218
+ // what `require.resolve('x')` resolves to as Node resolves it: from the module, else from each of the
1219
+ // `paths` it gives (`{ paths: [path.join(__dirname, 'shared')] }`) in turn. -> it, or null
1220
+ function babelPathCall(node, file, host) {
1221
+ if (memberCall(node, 'require') !== 'resolve') {
1222
+ const path = node?.type === 'CallExpression' ? staticPath(node, file) : null
1223
+ return path === null || isAbsolute(path) || path.startsWith('.') ? path : `./${path}`
1224
+ }
1225
+ const spec = literalSpec(node.arguments[0])
1226
+ const option = node.arguments[1]
1227
+ const paths = option?.type === 'ObjectExpression'
1228
+ ? option.properties.find((property) => property.type === 'Property' && !property.computed && (property.key.name ?? property.key.value) === 'paths')
1229
+ : undefined
1230
+ const dirs = paths === undefined ? [dirname(file)] : paths.value.type === 'ArrayExpression' ? paths.value.elements.map((element) => staticPath(element, file)) : [null]
1231
+ // One only the run knows, or one relative to its cwd: no telling where Node looks.
1232
+ if (spec === null || dirs.some((dir) => dir === null || !isAbsolute(dir))) return null
1233
+ for (const dir of dirs) {
1234
+ try {
1235
+ return host.resolve(join(dir, 'index.js'), spec, NODE_REQUIRE_CONDITIONS)
1236
+ } catch { /* not from here: the next */ }
1237
+ }
1238
+ return null
1239
+ }
1240
+
1241
+ // What the lists of `lists` (the name of the key, variable or object holding one -> what each entry is
1242
+ // taken as) in the AST `program` hold, read off its source, never run: each list (an array under such a
1243
+ // key at any depth, a variable or property so named or assigned, one pushed onto either), through
1244
+ // spreads, variables, branches and the array methods making a list of another (LIST_METHODS); each
1245
+ // entry (or the first of a `[name, options]` pair) passed to `entry(type, node)`.
1246
+ function readLists(program, lists, entry) {
1247
+ // Each variable's declarators with an initializer, by its name.
1248
+ const declarators = []
1249
+ walkAst(program, (node) => {
1250
+ if (node.type === 'VariableDeclarator' && node.id.type === 'Identifier' && node.init) declarators.push(node)
1251
+ })
1252
+ const inits = Map.groupBy(declarators, (node) => node.id.name)
1253
+ const seen = new Map()
1254
+ // Read `node` for the `type`s it lists: `depth` levels of arrays in it lists (1 a list, 2 one `.flat()`
1255
+ // flattens), and an array below them an entry's `[name, options]` pair. A spread joins its elements to
1256
+ // the array spreading it; a variable is its initializers; a branch (`a ? b : c`, `a && b`) either side.
1257
+ const read = (node, type, depth) => {
1258
+ if (!node || depth > MAX_LIST_DEPTH) return
1259
+ const key = `${type}${depth}`
1260
+ if (!seen.has(node)) seen.set(node, new Set())
1261
+ if (seen.get(node).has(key)) return
1262
+ seen.get(node).add(key)
1263
+ if (node.type === 'ArrayExpression') {
1264
+ if (depth === 0) return read(node.elements[0], type, 0)
1265
+ for (const element of node.elements) {
1266
+ if (element?.type === 'SpreadElement') read(element.argument, type, depth)
1267
+ else read(element, type, depth - 1)
1268
+ }
1269
+ } else if (node.type === 'Identifier') {
1270
+ for (const { init } of inits.get(node.name) ?? []) read(init, type, depth)
1271
+ } else if (node.type === 'ConditionalExpression' || node.type === 'LogicalExpression') {
1272
+ for (const side of node.type === 'LogicalExpression' ? [node.left, node.right] : [node.consequent, node.alternate]) read(side, type, depth)
1273
+ } else if (depth > 0 && LIST_METHODS.has(memberCall(node))) {
1274
+ // `base.concat(more)` of both, `[a, b].filter(Boolean)` of its own, `[[a], b].flat()` of its own
1275
+ // flattened, as many levels as it says (`flat(2)`, `flat(Infinity)`).
1276
+ const method = memberCall(node)
1277
+ const levels = method === 'flat' ? flatDepth(node.arguments[0]) : 0
1278
+ read(node.callee.object, type, Math.min(depth + levels, MAX_LIST_DEPTH))
1279
+ if (method === 'concat') for (const arg of node.arguments) read(arg.argument ?? arg, type, arg.type === 'SpreadElement' ? depth + 1 : depth)
1280
+ } else {
1281
+ entry(type, node)
1282
+ }
1283
+ }
1284
+ const listName = (node) => (node.type === 'Identifier' ? node.name : node.type === 'MemberExpression' && !node.computed ? node.property.name : undefined)
1285
+ walkAst(program, (node) => {
1286
+ // A list: under a key, held in a variable or assigned to a property, so named.
1287
+ const [name, value] = node.type === 'Property' && !node.computed ? [node.key.name ?? node.key.value, node.value]
1288
+ : node.type === 'VariableDeclarator' ? [listName(node.id), node.init]
1289
+ : node.type === 'AssignmentExpression' ? [listName(node.left), node.right]
1290
+ : []
1291
+ if (lists.has(name)) read(value, lists.get(name), 1)
1292
+ // Or added to one: `push` and `unshift` each argument an entry, `concat` as it concatenates.
1293
+ const method = memberCall(node)
1294
+ const type = method && lists.get(listName(node.callee.object))
1295
+ if (!type) return
1296
+ if (method === 'concat') read(node, type, 1)
1297
+ else if (method === 'push' || method === 'unshift') for (const arg of node.arguments) read(arg.argument ?? arg, type, arg.type === 'SpreadElement' ? 1 : 0)
1298
+ })
1299
+ }
1300
+
1301
+ // The plugins and presets the module `file` names in the lists of them (readLists): by a string,
1302
+ // standardized as Babel requires it, or by a path the module computes (babelPathCall), as is. With
1303
+ // `anyString`, also any string naming one by its form (BABEL_MODULE_STRING) wherever it is, as
1304
+ // written. -> the names
1305
+ function babelNamedModules(file, host, { anyString = false } = {}) {
1306
+ const program = parseModule(file, host)
1307
+ const named = new Set()
1308
+ readLists(program, BABEL_LISTS, (type, node) => {
1309
+ const name = literalSpec(node)
1310
+ const path = name ? null : babelPathCall(node, file, host)
1311
+ if (name) named.add(standardizeBabelName(type, name))
1312
+ else if (path) named.add(path)
1313
+ })
1314
+ if (anyString) {
1315
+ walkAst(program, (node) => {
1316
+ const string = literalSpec(node)
1317
+ if (string && BABEL_MODULE_STRING.test(string)) named.add(string.replace(/^module:/u, ''))
1318
+ })
1319
+ }
1320
+ return named
1321
+ }
1322
+
1323
+ // The extensions Metro takes as assets (`resolver.assetExts`), as the project's Metro config gives them:
1324
+ // Metro's defaults, and each string its metro.config.js lists among them, read off its source, never
1325
+ // run (readLists: `assetExts: [...defaultConfig.resolver.assetExts, 'riv']`,
1326
+ // `config.resolver.assetExts.push('riv')`). One it filters out is still taken as one.
1327
+ function metroAssetExts(baseDir, host) {
1328
+ const exts = new Set(METRO_ASSET_EXTS)
1329
+ const config = join(baseDir, METRO_CONFIG_FILE)
1330
+ if (!host.stat(config)?.isFile()) return exts
1331
+ readLists(parseModule(config, host), METRO_ASSET_LISTS, (_, node) => {
1332
+ const ext = literalSpec(node)
1333
+ if (ext) exts.add(ext)
1334
+ })
1335
+ return exts
1336
+ }
1337
+
1338
+ // How deep a Babel config's lists nest, as read: arrays of arrays `.flat()` flattens.
1339
+ const MAX_LIST_DEPTH = 8
1340
+ // The levels `.flat(arg)` flattens: 1 by default, else the number written (Infinity as deep as read).
1341
+ function flatDepth(arg) {
1342
+ if (arg === undefined) return 1
1343
+ if (arg.type === 'Identifier' && arg.name === 'Infinity') return MAX_LIST_DEPTH
1344
+ return arg.type === 'Literal' && typeof arg.value === 'number' ? arg.value : 1
1345
+ }
1346
+
1347
+ const JSX_FILE_EXTS = new Set(['.jsx', '.tsx'])
1348
+ // Metro's asyncRequire (metro-config's default `asyncRequireModulePath`), which Metro adds to a build
1349
+ // with an `import()`, as the Metro plugin's graph then holds it.
1350
+ const ASYNC_REQUIRE = 'metro-runtime/src/modules/asyncRequire.js'
1351
+
1352
+ // A get-or-read cache for the rounds a lazy tree's build runs in (vfs-bundle/lazy.js), in its `memo`
1353
+ // under `label`: what `read()` gives for a key, kept where reading it through `host` read no package
1354
+ // not unpacked yet (`host.touches()`), as a scan keeps what it reads of a file; without a `memo`,
1355
+ // read each time.
1356
+ function keptAcross(label, host, memo) {
1357
+ const kept = memo ? nestedMap(memo, label) : null
1358
+ return (key, read) => {
1359
+ if (kept?.has(key)) return kept.get(key)
1360
+ const before = kept === null ? 0 : host.touches()
1361
+ const value = read()
1362
+ if (kept !== null && host.touches() === before) kept.set(key, value)
1363
+ return value
1364
+ }
1365
+ }
1366
+
1367
+ // `fn()` on `host`, its reads probes of what files packages hold, which read none of their code,
1368
+ // where the host tells them apart (a lazy tree's, vfs-bundle/tree.js's treeHost).
1369
+ const probing = (host, fn) => (host.probing === undefined ? fn() : host.probing(fn))
1370
+
1371
+ // What Babel and Metro add to a React Native build beyond what its source imports (#99 records no
1372
+ // edge to one, as Metro bundles them unasserted), each resolved as `resolver` resolves it (Babel's
1373
+ // aliases applied), or `platformResolver` where Babel never runs, from a file of `files` (absolute
1374
+ // paths of code files) or `assets` (of the assets reached) it is added to: under
1375
+ // --babel or --babel-aliases, the Babel helpers (every @babel/runtime/helpers/*.js of each
1376
+ // @babel/runtime a file finds, which ones Babel adds hanging on the syntax the file uses), Metro's
1377
+ // module system (metro-runtime), and React Native's polyfills (the files @react-native/js-polyfills
1378
+ // lists); with an `import()` (`dynamicImports`), Metro's asyncRequire (ASYNC_REQUIRE); with JSX (`jsx`, else a
1379
+ // .jsx/.tsx file), the JSX runtime Babel imports for it; for an asset, the asset registry
1380
+ // (ASSET_REGISTRY) the module Metro builds of it, `require(registry).registerAsset({...})`, requires,
1381
+ // which Metro builds past Babel, so no alias of Babel's applies to it (platformResolver). -> absolute
1382
+ // paths
1383
+ function metroInsertedFiles(files, { assets, baseDir, babelled, dynamicImports, jsx, resolver, platformResolver, conditions: conds, host, kept = (key, read) => read() }) {
1384
+ const inserted = new Set()
1385
+ // One file of each directory of `list`'s: a specifier resolves alike from every file of one, and
1386
+ // is kept by it (`kept`, keptAcross's).
1387
+ const onePerDir = (list) => [...new Map(list.map((file) => [dirname(file), file])).values()]
1388
+ const resolveFrom = (from, spec, by = resolver) => kept(`${by === resolver ? '' : 'platform\0'}${dirname(from)}\0${spec}`, () => {
1389
+ const url = by(from, spec, conds)?.url
1390
+ return url === undefined ? undefined : fileURLToPath(url)
1391
+ })
1392
+ if (babelled) {
1393
+ const helpersDirs = new Set()
1394
+ for (const from of onePerDir(files)) {
1395
+ const probe = resolveFrom(from, '@babel/runtime/helpers/interopRequireDefault')
1396
+ if (probe !== undefined) helpersDirs.add(dirname(probe))
1397
+ }
1398
+ for (const dir of helpersDirs) {
1399
+ for (const ent of host.readdir(dir)) if (ent.isFile() && ent.name.endsWith('.js')) inserted.add(join(dir, ent.name))
1400
+ }
1401
+ const metroRuntime = packageDirFrom(baseDir, 'metro-runtime', host) ?? packageDirFrom(baseDir, 'metro-runtime', host, ['metro'])
1402
+ const moduleSystem = metroRuntime === undefined ? undefined : join(metroRuntime, 'src/polyfills/require.js')
1403
+ if (moduleSystem !== undefined && host.stat(moduleSystem)?.isFile()) inserted.add(moduleSystem)
1404
+ const polyfills = packageDirFrom(baseDir, '@react-native/js-polyfills', host) ?? packageDirFrom(baseDir, '@react-native/js-polyfills', host, ['react-native'])
1405
+ if (polyfills !== undefined && host.stat(join(polyfills, 'index.js'))?.isFile()) {
1406
+ for (const [, file] of requireResolutions(join(polyfills, 'index.js'), host)) inserted.add(file)
1407
+ }
1408
+ }
1409
+ // Metro adds asyncRequire as a dependency of a module with an `import()`: from the project, else
1410
+ // through Metro, which depends on metro-runtime (an install that nests it, or a pnpm one, links it
1411
+ // only beside Metro).
1412
+ if (dynamicImports) {
1413
+ const metroDir = packageDirFrom(baseDir, 'metro', host)
1414
+ const file = requireFrom(baseDir, ASYNC_REQUIRE, host) ?? (metroDir === undefined ? undefined : requireFrom(metroDir, ASYNC_REQUIRE, host))
1415
+ if (file !== undefined) inserted.add(file)
1416
+ }
1417
+ // Babel's JSX transform imports the runtime into a file with JSX: under --jsx any one, else a
1418
+ // .jsx/.tsx one.
1419
+ for (const from of onePerDir(jsx ? files : files.filter((file) => JSX_FILE_EXTS.has(extname(file))))) {
1420
+ const runtime = resolveFrom(from, 'react/jsx-runtime')
1421
+ if (runtime !== undefined) inserted.add(runtime)
1422
+ }
1423
+ for (const from of onePerDir(assets)) {
1424
+ const registry = resolveFrom(from, ASSET_REGISTRY, platformResolver)
1425
+ if (registry !== undefined) inserted.add(registry)
1426
+ }
1427
+ return inserted
1428
+ }
1429
+
1082
1430
  // Build a JS/TS Bundle + companion Lockfile via the legacy-field resolver (`--mainFields`/
1083
1431
  // `--metro`). Scanned once per platform; each edge is recorded flat when the platforms that
1084
1432
  // have it agree, or as a `{ platform: target }` map where they diverge. `scope` and `resources`
1085
1433
  // 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 }) {
1434
+ 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
1435
  const baseDir = resolve(cwd)
1088
1436
  const absEntries = entries.map((e) => resolve(baseDir, e))
1089
1437
  const normalized = normalizeEntries(entries, cwd)
@@ -1093,13 +1441,25 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1093
1441
  // is the root; a file below neither is outside it. (Realpathed only when needed, so a missing cwd
1094
1442
  // is still the scan's missing entry.)
1095
1443
  let realRoot
1444
+ // Each file's path from the root, and the path of each file URL, which the scans and what follows
1445
+ // them take again for each file they reach, and each edge: worked out once a build, or for a lazy
1446
+ // tree once across its rounds, in its `memo` (each is the path's alone).
1447
+ const fromRoot = memo ? nestedMap(memo, 'paths from the root') : new Map()
1096
1448
  const relWithin = (abs) => {
1449
+ if (fromRoot.has(abs)) return fromRoot.get(abs)
1097
1450
  let rel = toPosix(relative(baseDir, abs))
1098
1451
  if (relativeEscapes(rel)) {
1099
1452
  realRoot ??= host.realpath(baseDir)
1100
1453
  rel = toPosix(relative(realRoot, abs))
1101
1454
  }
1102
- return relativeEscapes(rel) ? null : rel
1455
+ fromRoot.set(abs, relativeEscapes(rel) ? null : rel)
1456
+ return fromRoot.get(abs)
1457
+ }
1458
+ const pathsOf = memo ? nestedMap(memo, 'file paths') : new Map()
1459
+ const pathOf = (url) => {
1460
+ let path = pathsOf.get(url)
1461
+ if (path === undefined) pathsOf.set(url, (path = fileURLToPath(url)))
1462
+ return path
1103
1463
  }
1104
1464
  const toRel = (abs) => {
1105
1465
  const rel = relWithin(abs)
@@ -1132,58 +1492,270 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1132
1492
 
1133
1493
  // --babel-aliases: the same aliasing as --babel's, from a JSON file of the project, for every platform alike.
1134
1494
  const fileAliases = aliasesFile === undefined ? null : createAliases(readAliasesFile(baseDir, aliasesFile, host), baseDir)
1495
+ // --babel or --babel-aliases: a build Metro runs through Babel, which adds its helpers to each file.
1496
+ const babelled = babel || fileAliases !== null
1135
1497
  // The edges each platform's scan left out above the root (withinRoot's leftOut), across platforms.
1136
1498
  const leftOutEdges = []
1499
+ // --metro: the extensions Metro takes as assets, its defaults and those the project's Metro config
1500
+ // adds (metroAssetExts: `assetExts: [...defaultConfig.resolver.assetExts, 'riv']`), which it resolves
1501
+ // by its asset rule, scale variants and all.
1502
+ const assetExts = metro ? metroAssetExts(baseDir, host) : METRO_ASSET_EXTS
1503
+ // The resources the app imports that are no asset of Metro's are no module of Metro's, which can't
1504
+ // resolve one: one reaches the build only as a transform's input, read and inlined by a Babel plugin
1505
+ // (`require('./messages.po')`, @exodus/i18n's plugin-inline-po), as the Metro plugin records it
1506
+ // (`metro`, no edge): no edge to one, and no asset registry for one.
1507
+ const isTransformInput = (abs, info) => metro && info?.resource === true && !isMetroAssetFile(abs, assetExts)
1508
+ // A round of a lazy tree that asked for a package already is void (vfs-bundle/lazy.js): it reads
1509
+ // on, for all else it asks for, but reports and refuses nothing, which the round that asks for
1510
+ // none does, and stops before it reads the files in.
1511
+ const voided = () => Boolean(memo) && host.touches() > 0
1512
+ // Scan from `roots` with `options`, keeping what lies within the root (one the entries link eagerly
1513
+ // is refused, see withinRoot), its issues reported under `label`, by which a lazy tree's `memo`
1514
+ // keeps what each of its scans read. -> { scanner, leftOut }
1515
+ const scanWithin = (roots, options, label) => {
1516
+ const { scanner, outside, leftOut } = withinRoot(scan(roots, { ...options, memo: memo ? nestedMap(memo, label) : null }), (url) => relWithin(pathOf(url)) !== null)
1517
+ for (const url of outside) toRel(pathOf(url))
1518
+ if (!voided()) reportScanIssues({ ...analyzeScanner(scanner, { baseDir }), leftOut }, { baseDir, label, root: ' at .' })
1519
+ return { scanner, leftOut }
1520
+ }
1137
1521
  for (const platform of platforms) {
1138
- const field = fieldResolverFor(platform, { mainFields, metro, conditions: scanConditions, typescript, typescriptPaths, host })
1522
+ // What each resolves, kept across a lazy tree's rounds where it read nothing not unpacked yet.
1523
+ const keptFor = (label) => (memo ? nestedMap(memo, `${label} ${platform ?? '*'}`) : undefined)
1524
+ const field = fieldResolverFor(platform, { mainFields, metro, assetExts, conditions: scanConditions, typescript, typescriptPaths, host, kept: keptFor('resolutions') })
1139
1525
  const { extras } = field
1140
1526
  // --metro --metro-resolver delegates to the project's own metro-resolver for byte-for-byte Metro
1141
1527
  // fidelity; otherwise the built-in field/suffix resolver reproduces it. metro-resolver derives
1142
1528
  // default/require|import/platform conditions itself, so it takes only the extra `react-native`
1143
1529
  // condition (browser comes from its per-platform map, keyed on `web`).
1144
1530
  const resolver = metroResolver
1145
- ? createMetroResolver({ projectDir: baseDir, platform, sourceExts: SOURCE_EXTS, mainFields, conditionNames: ['react-native'], host })
1531
+ ? createMetroResolver({ projectDir: baseDir, platform, sourceExts: SOURCE_EXTS, mainFields, conditionNames: ['react-native'], assetExts, host })
1146
1532
  : field.resolver
1147
1533
  // Without --typescript, the field resolver --typescript would build names the file a miss would
1148
1534
  // 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
1535
+ const typescriptResolve = typescript || metroResolver ? null : fieldResolverFor(platform, { mainFields, metro, assetExts, conditions: scanConditions, typescript: true, typescriptPaths, host, kept: keptFor('typescript resolutions') }).resolver
1150
1536
  // --babel: a specifier the project's babel-plugin-module-resolver rewrites resolves as rewritten,
1151
1537
  // as Metro resolves it after Babel; its edge stays keyed by the specifier the source holds.
1152
1538
  const aliases = babel ? createBabelAliases({ projectDir: baseDir, platform }) : fileAliases
1539
+ // --babel: and whether it keeps a TypeScript import listing types alone (onlyRemoveTypeImports).
1540
+ const keepsTypeImports = babel ? createBabelTypeImportsKept({ projectDir: baseDir, platform }) : null
1153
1541
  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
-
1542
+ // --metro: built through Babel, which erases an import listing types alone (Scan's `babel`).
1543
+ const scanOptions = { conditions: extras, resolve: aliased(resolver), typescriptResolve: aliased(typescriptResolve), jsx, flow, resources: resourceSet, define, babel: metro, keepsTypeImports, host }
1161
1544
  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)
1545
+ // Record what scanning from `roots` reaches: its files, their formats and edges. -> its scanner
1546
+ const absorb = (roots) => {
1547
+ const { scanner, leftOut } = scanWithin(roots, scanOptions, platform ?? 'mainFields')
1548
+ leftOutEdges.push(...leftOut)
1549
+ for (const [url, info] of scanner.files) {
1550
+ const abs = pathOf(url)
1551
+ const rel = toRel(abs)
1552
+ reached.add(abs)
1553
+ if (info.resource) resourceRels.add(rel)
1554
+ // Under --metro a code file has no format, as the Metro plugin records it: Metro transforms
1555
+ // it from source, and Node's module/commonjs isn't what it runs. But the one its name alone
1556
+ // gives it (.json, .mjs, .cjs: nodeExtFormat), as Node loads it too.
1557
+ formatsByRel.set(rel, metro && !info.resource ? nodeExtFormat(abs) : info.format)
1558
+ for (const e of info.edges) {
1559
+ if (e.builtin || e.dynamic) continue
1560
+ let target
1561
+ if (e.empty) { usesEmpty = true; target = emptyTarget }
1562
+ else if (e.child) target = toRel(pathOf(e.child))
1563
+ else continue // unresolved (already warned/thrown by reportScanIssues)
1564
+ // A transform's input is no dependency of Metro's: the import is gone before Metro sees it.
1565
+ if (e.child && isTransformInput(pathOf(e.child), scanner.files.get(e.child))) continue
1566
+ nestedMap(nestedMap(edges, rel), e.spec).set(platformKey, target)
1567
+ }
1180
1568
  }
1569
+ return scanner
1570
+ }
1571
+ const scanner = staged('scan', () => absorb(absEntries))
1572
+ // --metro: and what Babel and Metro add, which the source doesn't import, carried as Metro's code
1573
+ // and scanned for what each imports in turn; one above the root, passed over, as the Metro plugin
1574
+ // passes one over.
1575
+ if (metro) {
1576
+ const metroAt = performance.now()
1577
+ const code = [...scanner.files].filter(([, info]) => !info.resource).map(([url]) => pathOf(url))
1578
+ const assets = [...scanner.files].filter(([url, info]) => info.resource && !isTransformInput(pathOf(url), info)).map(([url]) => pathOf(url))
1579
+ // As a require(): Babel's CommonJS transform makes one of each it adds, as Metro writes its own.
1580
+ const required = new Set(['require', 'default', ...extras])
1581
+ // And each asset's files Metro reads to build its module (`scales`, `hash`) and copies out with
1582
+ // it, its other scales (`logo@2x.png`, `logo@3x.png`), by the name Metro reads it by (one linked
1583
+ // in, its target's bytes; one linked from outside the root passed over, as the Metro plugin
1584
+ // passes it over): no import names one, so no edge, carried as the asset is.
1585
+ const scales = assets.flatMap((file) => metroAssetFiles(file, platform, host)).filter((file) => relWithin(host.realpath(file)) !== null)
1586
+ const dynamicImports = [...scanner.files.values()].some((info) => !info.resource && info.edges.some((e) => e.kind === 'dynamic-import'))
1587
+ const kept = keptAcross(`Metro's additions ${platformKey}`, host, memo)
1588
+ const added = [...new Set([...metroInsertedFiles(code, { assets, baseDir, babelled, dynamicImports, jsx, resolver: aliased(resolver), platformResolver: resolver, conditions: required, host, kept }), ...scales])]
1589
+ .filter((file) => !scanner.files.has(pathToFileURL(file).toString()) && relWithin(file) !== null)
1590
+ stageTimes.metro += performance.now() - metroAt
1591
+ if (added.length > 0) staged('scan', () => absorb(added))
1181
1592
  }
1182
1593
  }
1183
1594
 
1184
1595
  // One platform leaving out an edge another resolves within the root is a split too: collapsed
1185
1596
  // 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)
1597
+ if (!voided()) refuseSplit(leftOutEdges.filter((e) => edges.get(toRel(pathOf(e.parentURL)))?.has(e.spec)), baseDir)
1598
+
1599
+ // --metro: and the project's configs a React Native build loads, though nothing the app imports
1600
+ // reaches them: Metro's and the React Native CLI's, and with --babel or --babel-aliases the Babel
1601
+ // config Metro's transformer runs; and the toolchain Metro loads by name (METRO_TOOLCHAIN, and
1602
+ // under either BABEL_TOOLCHAIN), as Node resolves a require() of it from the package naming it, else
1603
+ // from the project. Node loads each, not Metro, so each is traced as a Node entry is
1604
+ // (buildJsBundle): with all it imports, resolved as Node resolves it, each file in the format Node
1605
+ // loads it in, which one the app's build reaches too keeps (Metro needs none).
1606
+ // Their edges, each under the conditions Node resolves it with, as the Metro plugin's run records
1607
+ // them (Node's loader keys every edge so), apart from Metro's under '*' in a file both reach, though
1608
+ // they agree: under '*', they'd be none of the edges the run records.
1609
+ // conditions key -> parentRel -> specifier -> targetRel, beside the bundle's '*' edges.
1610
+ const keyedResolutions = new Map()
1611
+ // The edges from `parent` (rel) keyed `key`: specifier -> targetRel.
1612
+ const keyedFrom = (key, parent) => nestedMap(nestedMap(keyedResolutions, key), parent)
1613
+ // The configs' edges some context misses or leaves out: `${parentRel}\0${specifier}` -> the miss.
1614
+ const configMissed = new Map()
1615
+ // The files the configs reach (rel), and of them those the app's build doesn't: the bundler's,
1616
+ // attributed to `bundle`, not to `bundle:metro` with the app's source Metro bundles.
1617
+ const configReached = new Set()
1618
+ const configOnly = new Set()
1619
+ const configsAt = performance.now()
1620
+ if (metro) {
1621
+ const configs = [...METRO_CONFIG_FILES, ...(babelled ? [BABEL_CONFIG_FILE] : [])].map((rel) => join(baseDir, rel)).filter((abs) => host.stat(abs)?.isFile())
1622
+ // A METRO_TOOLCHAIN entry's file. -> absolute path, or undefined
1623
+ const toolchainFile = ({ name, file, via }) => {
1624
+ for (let i = 0; i <= via.length; i++) {
1625
+ const chain = via.slice(i)
1626
+ if (file !== undefined) {
1627
+ const dir = packageDirFrom(baseDir, name, host, chain)
1628
+ if (dir !== undefined && host.stat(join(dir, file))?.isFile()) return join(dir, file)
1629
+ continue
1630
+ }
1631
+ const from = chain.length === 0 ? baseDir : packageDirFrom(baseDir, chain.at(-1), host, chain.slice(0, -1))
1632
+ const found = from === undefined ? undefined : requireFrom(from, name, host)
1633
+ if (found !== undefined) return found
1634
+ }
1635
+ return undefined
1636
+ }
1637
+ for (const entry of [...METRO_TOOLCHAIN, ...(babelled ? BABEL_TOOLCHAIN : [])]) {
1638
+ const file = toolchainFile(entry)
1639
+ if (file !== undefined && relWithin(file) !== null) configs.push(file)
1640
+ }
1641
+ // And the React Native configs of the app's packages, which React Native's CLI requires as it
1642
+ // loads its own (`react-native config`'s packages, read as data).
1643
+ // Each by its real path, as Node loads it (a pnpm install's package linked into node_modules
1644
+ // requires what lies beside it in the store).
1645
+ for (const dir of probing(host, () => linkedPackages(baseDir, host))) {
1646
+ const file = join(dir, 'react-native.config.js')
1647
+ if (!probing(host, () => host.stat(file)?.isFile())) continue
1648
+ const real = host.realpath(file)
1649
+ if (relWithin(real) !== null) configs.push(real)
1650
+ }
1651
+ // Trace `roots` as Node entries, recording what they reach. -> the scan
1652
+ const traceNode = (roots) => {
1653
+ const { scanner, leftOut } = scanWithin(roots, { resources: resourceSet, host }, 'configs and toolchain')
1654
+ for (const [url, info] of scanner.files) {
1655
+ const abs = pathOf(url)
1656
+ const rel = toRel(abs)
1657
+ configReached.add(rel)
1658
+ if (!reached.has(abs)) configOnly.add(rel)
1659
+ reached.add(abs)
1660
+ if (info.resource) resourceRels.add(rel)
1661
+ formatsByRel.set(rel, info.format)
1662
+ }
1663
+ for (const u of [...scanner.unresolved, ...leftOut]) {
1664
+ if (u.spec !== undefined) configMissed.set(`${toRel(pathOf(u.parentURL))}\0${u.spec}`, u)
1665
+ }
1666
+ for (const [key, byParent] of scanner.imports) {
1667
+ for (const [parentURL, specs] of byParent) {
1668
+ const keyed = keyedFrom(key, toRel(pathOf(parentURL)))
1669
+ for (const [spec, childURL] of specs) keyed.set(spec, toRel(pathOf(childURL)))
1670
+ }
1671
+ }
1672
+ return scanner
1673
+ }
1674
+ const traced = configs.length > 0 ? traceNode(configs) : null
1675
+ // And the transformers of stasis's Metro plugin beside it, where a config requires it, in turn.
1676
+ const transformers = [...(traced?.files.keys() ?? [])].map((url) => pathOf(url))
1677
+ .filter((file) => basename(file) === 'metro.js' && basename(dirname(file)) === 'src' &&
1678
+ readJson(join(dirname(dirname(file)), 'package.json'), host)?.name === '@exodus/stasis-plugins')
1679
+ .flatMap((file) => STASIS_METRO_TRANSFORMERS.map((name) => join(dirname(file), name)))
1680
+ .filter((file) => host.stat(file)?.isFile() && relWithin(file) !== null && !traced.files.has(pathToFileURL(file).toString()))
1681
+ if (transformers.length > 0) traceNode(transformers)
1682
+ // And the plugins and presets the Babel config names, which @babel/core requires by name: as each
1683
+ // module the config reaches names them, in `plugins` and `presets` where it is the project's own or a
1684
+ // Babel config's by its name (isBabelConfigModule), and anywhere, by their form, where it is a Babel
1685
+ // config's. Each resolved as @babel/core resolves it, from the config's directory, else from the
1686
+ // module naming it, a path from either; and what each reaches, in turn. And what any module it
1687
+ // reaches (a plugin's own too) names by `require.resolve('x')`, which Babel loads: a plugin running
1688
+ // Babel with plugins of its own (react-native-reanimated's, `require.resolve('@babel/plugin-…')`),
1689
+ // with the edge Node's resolution makes, under the conditions it resolves with.
1690
+ const babelConfig = pathToFileURL(join(baseDir, BABEL_CONFIG_FILE)).toString()
1691
+ // What each file names, read once across a lazy tree's rounds (keptAcross).
1692
+ const resolutionsOf = keptAcross('require.resolve', host, memo)
1693
+ const namedOf = keptAcross('babel names', host, memo)
1694
+ if (traced?.files.has(babelConfig)) {
1695
+ const files = new Map(traced.files)
1696
+ const visited = new Set()
1697
+ const resolveNamed = (name, from) => {
1698
+ const pathLike = name.startsWith('.') || isAbsolute(name) || name.startsWith('node_modules/')
1699
+ for (const dir of [baseDir, dirname(from)]) {
1700
+ const file = requireFrom(dir, pathLike ? resolve(dir, name) : name, host)
1701
+ if (file !== undefined) return file
1702
+ }
1703
+ return undefined
1704
+ }
1705
+ for (let frontier = [babelConfig]; frontier.length > 0;) {
1706
+ const named = new Set()
1707
+ for (let i = 0; i < frontier.length; i++) {
1708
+ const url = frontier[i]
1709
+ const info = files.get(url)
1710
+ if (visited.has(url) || info === undefined) continue
1711
+ visited.add(url)
1712
+ for (const e of info.edges) if (e.child) frontier.push(e.child)
1713
+ const file = pathOf(url)
1714
+ const rel = toRel(file)
1715
+ if (info.resource || !JS_EXTS.has(extname(file))) continue
1716
+ for (const [spec, target] of resolutionsOf(file, () => requireResolutions(file, host))) {
1717
+ if (!JS_EXTS.has(extname(target)) || relWithin(target) === null) continue
1718
+ named.add(target)
1719
+ // A specifier keeps one edge, the first recorded, as a run records a resolution only where no
1720
+ // edge is: one the module also require()s keeps that edge, Node's loader's (traced, so
1721
+ // recorded, before the module is read), and one resolved again in other `paths` its first.
1722
+ // Every copy they find (`require.resolve('x', { paths })` another than `require('x')`) is
1723
+ // still traced.
1724
+ const keyed = keyedFrom(REQUIRE_KEY, rel)
1725
+ if (!keyed.has(spec)) keyed.set(spec, toRel(target))
1726
+ }
1727
+ const configModule = isBabelConfigModule(rel)
1728
+ if (!configModule && splitNodeModulesPath(rel)) continue
1729
+ for (const name of namedOf(`${configModule}\0${file}`, () => babelNamedModules(file, host, { anyString: configModule }))) {
1730
+ const found = resolveNamed(name, file)
1731
+ if (found !== undefined && JS_EXTS.has(extname(found)) && relWithin(found) !== null) named.add(found)
1732
+ }
1733
+ }
1734
+ const fresh = [...named].filter((file) => !files.has(pathToFileURL(file).toString()))
1735
+ if (fresh.length > 0) for (const [url, info] of traceNode(fresh).files) files.set(url, info)
1736
+ frontier = [...named].map((file) => pathToFileURL(file).toString())
1737
+ }
1738
+ }
1739
+ // And the files of the toolchain the Metro plugin carries for every build, which nothing here
1740
+ // reaches (AUTO_INCLUDES): setup_env.sh, which React Native's CLI runs as each command of it starts.
1741
+ // As the plugin resolves and tags each: from the project, by the shared classifier, a resource
1742
+ // where it names no format.
1743
+ for (const specifier of AUTO_INCLUDES) {
1744
+ const file = requireFrom(baseDir, specifier, host)
1745
+ if (file === undefined || reached.has(file) || relWithin(file) === null) continue
1746
+ const rel = toRel(file)
1747
+ const format = classifyFormat(file, { content: host.readFile(file) })
1748
+ reached.add(file)
1749
+ configReached.add(rel)
1750
+ configOnly.add(rel)
1751
+ if (format === undefined) resourceRels.add(rel)
1752
+ else formatsByRel.set(rel, format ?? undefined)
1753
+ }
1754
+ }
1755
+
1756
+ stageTimes.configs += performance.now() - configsAt
1757
+ if (voided()) throw new Error('buildResolvedJsBundle: this round reads a package not unpacked yet')
1758
+ const carryingAt = performance.now()
1187
1759
 
1188
1760
  // Read each reached file once: bytes for the bundle, integrity for the lockfile. Source is
1189
1761
  // stored as UTF-8 text, so non-UTF-8 bytes would diverge from the hashed bytes -- reject them.
@@ -1198,6 +1770,11 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1198
1770
  // A resource carries bytes, not source: UTF-8 verbatim or base64 when binary, tagged with the
1199
1771
  // byte-derived format (here and in the --metro native capture below).
1200
1772
  const putResource = (rel, buf) => (isUtf8(buf) ? put(rel, buf.toString('utf8'), 'resource', buf) : put(rel, buf.toString('base64'), 'resource:base64', buf))
1773
+ // Code carries its source, UTF-8 (`what` it is, refused where it isn't).
1774
+ const putCode = (rel, buf, format, what = 'JS bundle source') => {
1775
+ if (!isUtf8(buf)) throw new Error(`${what} is not valid UTF-8: ${rel}`)
1776
+ put(rel, buf.toString('utf8'), format, buf)
1777
+ }
1201
1778
  const realBase = host.realpath(baseDir)
1202
1779
  // Metro's empty module is carried as the file it is, a code file of Metro's (no format).
1203
1780
  if (usesEmpty && metroEmpty !== null) reached.add(metroEmpty)
@@ -1209,13 +1786,9 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1209
1786
  // fail closed.
1210
1787
  assertRealPathWithinBase(realBase, baseDir, rel, host)
1211
1788
  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
- }
1789
+ // A code file's format is the scan's.
1790
+ if (resourceRels.has(rel)) putResource(rel, buf)
1791
+ else putCode(rel, buf, formatsByRel.get(rel))
1219
1792
  }
1220
1793
  if (usesEmpty && metroEmpty === null) {
1221
1794
  // Refuse to shadow a real reached file sitting at the reserved empty-module path.
@@ -1236,11 +1809,12 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1236
1809
  if (metro) {
1237
1810
  const pkgDirs = new Map() // package dir -> its package's name
1238
1811
  // 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.
1812
+ // not a linked native dependency, so it must not drag in its ios/android surface. Nor is one
1813
+ // only the configs reach, which Node runs at build time, as the Metro plugin's graph holds none.
1240
1814
  const isCode = (rel) => !resourceRels.has(rel)
1241
1815
  for (const abs of reached) {
1242
1816
  const rel = toRel(abs)
1243
- const nm = isCode(rel) && splitNodeModulesPath(rel)
1817
+ const nm = isCode(rel) && !configOnly.has(rel) && splitNodeModulesPath(rel)
1244
1818
  if (nm) pkgDirs.set(nm.dir, nm.name)
1245
1819
  }
1246
1820
  // A package linked into node_modules from out of it (a workspace package) is reached by its real
@@ -1260,22 +1834,15 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1260
1834
  if (!splitNodeModulesPath(rel)) links.add(rel)
1261
1835
  } catch { /* a dangling link, or one out of the root, whose files the scan pass refused already */ }
1262
1836
  }
1263
- const listing = (abs) => {
1264
- try {
1265
- return host.readdir(abs)
1266
- } catch {
1267
- return []
1268
- }
1269
- }
1270
1837
  // As locatePackage: a directory named node_modules holds no node_modules of its own to look in.
1271
1838
  if (basename(dir) !== 'node_modules') {
1272
1839
  const nodeModules = join(dir, 'node_modules')
1273
- for (const ent of listing(nodeModules)) {
1840
+ for (const ent of listing(nodeModules, host)) {
1274
1841
  const abs = join(nodeModules, ent.name)
1275
1842
  if (ent.name.startsWith('@')) {
1276
1843
  // A scope holds packages, a directory or a link to one (a whole scope linked, where
1277
1844
  // every package in it is reached through the link).
1278
- for (const scoped of listing(abs)) {
1845
+ for (const scoped of listing(abs, host)) {
1279
1846
  if (ent.isSymbolicLink() || scoped.isSymbolicLink()) link(join(abs, scoped.name))
1280
1847
  }
1281
1848
  } else if (ent.isSymbolicLink()) {
@@ -1304,14 +1871,9 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1304
1871
  }
1305
1872
  // And the app's packages React Native links whether JS imports them or not (`react-native config`'s
1306
1873
  // 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)
1874
+ for (const dir of probing(host, () => linkedNativePackages(baseDir, host))) {
1875
+ const rel = relWithin(host.realpath(dir))
1876
+ if (rel !== null && !pkgDirs.has(rel)) pkgDirs.set(rel, readPackageJson(baseDir, `${rel}/package.json`, { host })?.name)
1315
1877
  }
1316
1878
  // Each one's native build inputs, walked whole (nativeInputs, shared with the plugin), read only
1317
1879
  // once its real path is held to the root.
@@ -1328,47 +1890,21 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1328
1890
  // react-native core isn't a Pod (config reports it via reactNativePath, not `dependencies`),
1329
1891
  // but its native source (React/, ReactCommon/, ReactAndroid/, ...) is walked all the same.
1330
1892
  const isRnCore = name === 'react-native'
1331
- if (!isRnCore && !looksNative(pkgAbs, nativeFs)) continue
1893
+ if (!isRnCore && !probing(host, () => looksNative(pkgAbs, nativeFs))) continue
1332
1894
  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
- }
1895
+ if (action === 'code') putCode(toRel(file), source, format, 'native source')
1896
+ else putResource(toRel(file), source)
1340
1897
  }
1341
1898
  // RN core's `.js` build scripts the classify loop skips (Metro-owned by extension, but never in
1342
1899
  // the graph): force-include as code so the podspec/Ruby that invokes them at pod-install resolves.
1343
1900
  if (isRnCore) {
1344
1901
  for (const file of RN_CORE_INCLUDE_FILES) {
1345
1902
  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)
1903
+ if (host.stat(abs) === null || sources.has(toRel(abs))) continue
1904
+ putCode(toRel(abs), nativeFs.readFile(abs), packageType(abs, host) === 'module' ? 'module' : 'commonjs', 'native source')
1353
1905
  }
1354
1906
  }
1355
1907
  }
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
1908
  }
1373
1909
 
1374
1910
  // --package-json (or the config's packageJSON): fold each bundled module's package.json into
@@ -1381,20 +1917,23 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1381
1917
  if (withManifests) {
1382
1918
  const found = new Map()
1383
1919
  const exists = (rel) => found.get(rel) ?? found.set(rel, packageJSONStat(host, join(baseDir, rel))?.isFile() ?? false).get(rel)
1384
- const manifests = new Set()
1920
+ const manifests = new Map() // manifest -> the reached files it serves
1385
1921
  for (const abs of reached) {
1386
1922
  const rel = toRel(abs)
1387
1923
  const pkgDir = packageOf(rel)?.pkgDir ?? (splitNodeModulesPath(rel) ? undefined : '.')
1388
1924
  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)
1925
+ for (const manifest of [moduleFileKey(pkgDir, 'package.json'), scopeManifestOf(rel, pkgDir, exists)]) {
1926
+ if (manifest !== undefined) (manifests.get(manifest) ?? manifests.set(manifest, []).get(manifest)).push(rel)
1927
+ }
1392
1928
  }
1393
- for (const rel of manifests) {
1929
+ for (const [rel, of] of manifests) {
1394
1930
  if (sources.has(rel)) continue
1395
1931
  const buf = readModuleManifest({ baseDir, realBase, rel, host })
1396
1932
  if (!buf) continue
1397
1933
  put(rel, buf.toString('utf8'), 'json', buf)
1934
+ // Attributed as the files it serves are, as State#includePackageJson attributes one.
1935
+ if (of.some((file) => configReached.has(file))) configReached.add(rel)
1936
+ if (of.every((file) => configOnly.has(file))) configOnly.add(rel)
1398
1937
  }
1399
1938
  }
1400
1939
 
@@ -1404,21 +1943,28 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1404
1943
  const specMap = new Map()
1405
1944
  for (const [spec, byPlatform] of bySpec) {
1406
1945
  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
- }
1946
+ specMap.set(spec, distinct.size === 1 ? [...distinct][0] : new Map([...byPlatform].toSorted((a, b) => (a[0] < b[0] ? -1 : 1))))
1412
1947
  }
1413
1948
  resolutions.set(parent, specMap)
1414
1949
  }
1950
+ // A config's import Node misses or leaves out in a context, from a file the app's build reaches
1951
+ // too, where Metro resolves it: no miss is recorded, so the app's edge under '*' would serve that
1952
+ // context Metro's file in its place -- refused, as refuseSplit refuses a split.
1953
+ for (const [key, { parentURL, spec }] of configMissed) {
1954
+ if (!resolutions.get(key.slice(0, key.indexOf('\0')))?.has(spec)) continue
1955
+ throw new Error(
1956
+ `${spec} from ${displayPath(parentURL, baseDir)} resolves under Metro, for the app, and not under Node, for the ` +
1957
+ "project's configs: a bundle can't keep Node's miss without serving Node Metro's file in its place"
1958
+ )
1959
+ }
1415
1960
 
1416
1961
  const rootPkg = readJson(join(baseDir, 'package.json'), host) ?? {}
1417
- const bundle = assembleCodeBundle({
1962
+ const assembled = assembleCodeBundle({
1418
1963
  baseDir,
1419
1964
  entries: normalized,
1420
1965
  sources,
1421
1966
  resolutions,
1967
+ keyedResolutions,
1422
1968
  formats: formatsByRel,
1423
1969
  workspaceName: rootPkg.name ?? 'workspace',
1424
1970
  workspaceVersion: rootPkg.version ?? '0.0.0',
@@ -1426,26 +1972,14 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1426
1972
  host,
1427
1973
  packageOf,
1428
1974
  consumer: metro ? 'bundle:metro' : 'bundle',
1975
+ consumerFiles: [...sources.keys()].filter((rel) => !configOnly.has(rel)),
1429
1976
  })
1977
+ // What the configs reach is the bundler's, not the app's: `bundle`, as a Node build's files are.
1978
+ const bundle = configReached.size === 0 ? assembled : assembled.withReason('bundle', [...configReached])
1430
1979
 
1431
1980
  // 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 }
1981
+ stageTimes.carrying += performance.now() - carryingAt
1982
+ return { bundle, lockfile: Lockfile.fromBundle(bundle, (file) => integrities.get(file)) }
1449
1983
  }
1450
1984
 
1451
1985
  // Classify entries into their single shared language and check option applicability; `name` prefixes
@@ -1598,19 +2132,23 @@ async function buildJs({ mainFields, platforms, metro, metroResolver, babel, bab
1598
2132
  // there names in `extra`, as `stasis add` adds them (extraArtifacts), into its bundle and its
1599
2133
  // lockfile; never one of `outputs` (the absolute paths the build writes), and resources by the
1600
2134
  // 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)
2135
+ // State instead, as `stasis run` does (State#includeExtra). The project is read through `host`,
2136
+ // `name`'s.
2137
+ function withExtra(built, cwd, { outputs = [], resources, host = diskHost, name = 'bundle' }) {
2138
+ const config = readConfigFile(name, cwd, host)
1604
2139
  const patterns = parseExtraOption(CONFIG_FILE, config?.extra)
1605
2140
  if (patterns.length === 0) return built
1606
2141
  // The union of `builds` (see bundleCommand) may be State-built in node_modules scope, which the
1607
2142
  // 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`)
2143
+ 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
2144
  const baseDir = resolve(cwd)
1610
2145
  const written = new Set(outputs.map((file) => toPosix(relative(baseDir, file))))
1611
2146
  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 })
2147
+ const added = extraArtifacts({ baseDir, patterns, resources: allowlist, bundle: built.bundle, outputs: written, host })
1613
2148
  if (added.bundle === null && added.carried.length === 0) return built
2149
+ // The files are the build's own, at its root, so they are where it was built: its `repo`, which a
2150
+ // merge with a side lacking one would drop.
2151
+ if (added.bundle !== null) added.bundle.repo = built.bundle.repo
1614
2152
  // Each file attributed: to the build's own consumer (`bundle`, or `bundle:metro`, as codeBundle
1615
2153
  // stamps it), `extra`, or both, for one `extra` names that the build carries itself.
1616
2154
  const merged = added.bundle === null ? built.bundle : built.bundle.merge(added.bundle)
@@ -1627,7 +2165,7 @@ function withExtra(built, cwd, outputs, resources) {
1627
2165
  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
2166
  const outputs = outputsAt(resolve(cwd))
1629
2167
  // 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)
2168
+ const withExtraOf = (built, buildResources) => (extra ? withExtra(built, cwd, { outputs, resources: buildResources }) : built)
1631
2169
  if (kind === 'sol') return withExtraOf({ bundle: await buildSolidityBundle({ cwd, env, entries, mappingFile, manifests }) })
1632
2170
  if (kind === 'php') return withExtraOf({ bundle: await buildPhpBundle({ cwd, entries }) })
1633
2171
  if (kind === 'bash') return withExtraOf({ bundle: await buildBashBundle({ cwd, entries }) })
@@ -1638,6 +2176,16 @@ async function buildOfKind(kind, { cwd, env, entries, mappingFile, manifests, sc
1638
2176
  return built
1639
2177
  }
1640
2178
 
2179
+ // Several builds' bundles (and lockfiles) as one, the i-th of `builts` built of `builds[i]` and
2180
+ // rooted at `roots[i]`: their strict union, rooted where every one of them is.
2181
+ function unionOfBuilds(name, builds, builts, roots) {
2182
+ const other = roots.findIndex((root) => root !== roots[0])
2183
+ if (other !== -1) {
2184
+ 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]})`)
2185
+ }
2186
+ return builts.reduce((a, b) => ({ bundle: a.bundle.merge(b.bundle), lockfile: a.lockfile && (() => a.lockfile().merge(b.lockfile())) }))
2187
+ }
2188
+
1641
2189
  // buildVfsBundle's checks of `options`, which hold before anything is fetched (`host` the
1642
2190
  // project's, or an empty tree's with `fetched: false`): no metro-resolver, which reads the disk;
1643
2191
  // `generate` only for JS.
@@ -1661,7 +2209,10 @@ export function checkVfsOptions(name, options) {
1661
2209
  // EXODUS_STASIS_* setting is read. The entries the stasis.config.json in cwd names in its `entries`
1662
2210
  // (every one, where none are given) are built with their options there, as `stasis bundle` builds
1663
2211
  // them, but none that runs a program; an option given has to agree with theirs, one left out is
1664
- // theirs (configuredBuilds).
2212
+ // theirs (configuredBuilds). The files its `extra` names are added as `stasis bundle` adds them,
2213
+ // globbed in the Vfs as Node globs the disk (vfs-bundle/glob.js), which needs the optional minimatch.
2214
+ // Where its `installDevDependencies` is false, what devDependencies alone reach is left out of the
2215
+ // tree, as the package manager's production install leaves it out.
1665
2216
  // `repo`, the informational `{ github, directory, commit }`,
1666
2217
  // is the Bundle's, over what is detected in the Vfs as `stasis bundle` detects it on disk. `os`,
1667
2218
  // `cpu` and `libc` are loadNodeModules'. Without a `packageManager`, it is the one whose lockfile
@@ -1673,27 +2224,61 @@ export function checkVfsOptions(name, options) {
1673
2224
  // `cache` is where npm packages' tarballs and version documents are kept (checkCache). Each
1674
2225
  // dependency is recorded at the commit the tree installs it from, where deptree names one
1675
2226
  // (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.
2227
+ // JS bundle built through a State, the State's root, which is at or above it. A lazy tree, pnpm's
2228
+ // or npm's, is laid out with nothing fetched, and each package is fetched and unpacked once the
2229
+ // build reads under it, the build run again until it reads none it has not (vfs-bundle/lazy.js):
2230
+ // the bundle the full tree gives, its warnings said once, with `stats` counting what is unpacked. A
2231
+ // tarball the build never reaches is never fetched, nor held to the lockfile, nor refused. The tree
2232
+ // is lazy where it can be (lazyTaken) and the stasis.config.json in cwd leaves `lazyDependencies`
2233
+ // true; `lazy: false` installs every package, and `lazy: true` is refused where the tree cannot be
2234
+ // lazy, or `lazyDependencies` is false. `stats` are deptree's, and `stats.unpacked` the packages
2235
+ // unpacked: each one installed, or those a lazy tree unpacked.
1677
2236
  // -> { 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')
2237
+ export async function buildVfsBundle(options = {}) {
2238
+ const { checkVfs, vfsHost } = await import('../vfs-bundle/tree.js')
2239
+ checkVfs('buildVfsBundle', options.vfs)
2240
+ return buildFromLockfile('buildVfsBundle', vfsHost(options.vfs), options)
2241
+ }
2242
+
2243
+ // buildVfsBundle's build, `name`'s, of the project `project` (a host, @exodus/stasis-core/host)
2244
+ // reads: of a Vfs, which `vfs` is (for `generate` alone), or of the disk, for `stasis bundle
2245
+ // --deptree`. As buildVfsBundle builds it, with `options` as it takes them, but for what bundleCommand
2246
+ // gives besides (none of which buildVfsBundle takes): `plan`, `{ builds, bundleExtra }`, the builds
2247
+ // (each `{ entries, ...options }`) bundleCommand plans (bundle-options.js) in place of those
2248
+ // configuredBuilds makes, and whether their union's `extra` is the bundle's, as bundleCommand adds
2249
+ // it; `env`, what the builds read their settings from (none by default); `outputsAt(root)`, the
2250
+ // absolute paths written, which `extra` never takes; and `refuseInstalled`, layOutTree's.
2251
+ 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 } = {}) {
2252
+ const { checkCache, checkKind, checkLazy, checkTarget, lazyTaken, loadTree, packageManagerFor, packageManagerOf, pinCommits, treeHost, vfsHost } = await import('../vfs-bundle/tree.js')
1680
2253
  const { configuredBuilds } = await import('./bundle-options.js')
1681
- checkVfs('buildVfsBundle', vfs)
1682
- checkTarget('buildVfsBundle', { os, cpu, libc })
1683
- checkCache('buildVfsBundle', cache)
2254
+ const { checkGlob } = await import('../vfs-bundle/glob.js')
2255
+ checkTarget(name, { os, cpu, libc })
2256
+ checkCache(name, cache)
2257
+ if (lazy !== undefined && typeof lazy !== 'boolean') throw new TypeError(`${name}: lazy must be a boolean`)
1684
2258
  // Checked as the Bundle checks it, before anything is fetched.
1685
2259
  if (repo !== undefined) repo = new Bundle({ repo }).repo
1686
- const project = vfsHost(vfs)
1687
2260
  // A real path, as every file the scan reaches is.
1688
2261
  cwd = project.realpath(posix.resolve('/', cwd))
1689
- packageManager = packageManagerFor('buildVfsBundle', project, cwd, { packageManager, packageManagerVersion, os })
1690
- const pm = packageManagerOf('buildVfsBundle', packageManager)
2262
+ packageManager = packageManagerFor(name, project, cwd, { packageManager, packageManagerVersion, os })
2263
+ const pm = packageManagerOf(name, packageManager)
2264
+ if (lazy) checkLazy(name, packageManager)
1691
2265
  // The entries stasis.config.json's `entries` names, every one where none are given, each built
1692
2266
  // 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])
2267
+ // the options given. Each build is checked with the options it is built with. With the builds its
2268
+ // `entries` make, `extra` is the bundle's, added once to their union, as bundleCommand adds it;
2269
+ // else the build's own, as buildOfKind adds it.
2270
+ const configured = plan === undefined ? configuredBuilds(name, { cwd, host: project, entries: options.entries, options }) : null
2271
+ const { builds, bundleExtra } = plan ?? { builds: configured?.builds ?? [options], bundleExtra: configured !== null }
2272
+ if (builds.length === 0) throw new Error(`${name}: no entries given, and ${CONFIG_FILE}'s entries name none`)
2273
+ // That the Vfs can be globbed for `extra` is checked before anything is fetched (for a State
2274
+ // rooted above cwd, where it reads the config there).
2275
+ if (parseExtraOption(CONFIG_FILE, readConfigFile(name, cwd, project)?.extra).some((pattern) => !pattern.startsWith('!'))) checkGlob()
2276
+ const installDevDependencies = installsDevDependencies(name, cwd, project)
2277
+ // Lazy where the tree can be, unless stasis.config.json's `lazyDependencies` or `lazy` says not.
2278
+ const lazyDependencies = lazesDependencies(name, cwd, project)
2279
+ if (lazy && !lazyDependencies) throw new Error(`${name}: lazy is given, and ${CONFIG_FILE}'s lazyDependencies is false`)
2280
+ lazy ??= lazyDependencies && lazyTaken(project, packageManager, cwd)
2281
+ for (const build of builds) checkKind(name, checkVfsOptions(name, { ...build, lockfile: options.lockfile, generate, cwd, host: project }), [packageManager])
1697
2282
  // Checked before anything is fetched: an entry out of what the tree installs is in the project
1698
2283
  // already. (A Solidity entry that is no .sol file is a directory, skipped where it is missing.)
1699
2284
  for (const entry of builds.flatMap((build) => build.entries)) {
@@ -1701,32 +2286,122 @@ export async function buildVfsBundle({ vfs, packageManager, cwd = '/', packageMa
1701
2286
  if (pm.kind === 'sol' && !abs.endsWith('.sol')) continue
1702
2287
  if (!posix.relative(cwd, abs).split('/').includes(pm.installs) && project.stat(abs) === null) throw new Error(`entry not found: ${abs}`)
1703
2288
  }
1704
- const tree = await loadTree({ project, packageManager, cwd, packageManagerVersion, os, cpu, libc, client, cache })
1705
- let { host } = tree
2289
+ 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 }))
2290
+ const { inRounds } = await import('../vfs-bundle/lazy.js')
2291
+ // The project the builds read: with the Prisma Clients `generate` writes, generated once before
2292
+ // any build, for a lazy tree in rounds of their own (they read the tree's `prisma`), so that each
2293
+ // of the build's rounds reads the same project, as what its scans keep across them takes it to.
2294
+ let outside = project
1706
2295
  if (generate.includes('prisma')) {
1707
2296
  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})`)
1725
- }
1726
- const { bundle, lockfile } = builts.reduce((a, b) => ({ bundle: a.bundle.merge(b.bundle), lockfile: a.lockfile && (() => a.lockfile().merge(b.lockfile())) }))
2297
+ const generating = (reads) => generatePrismaClients({ host: treeHost(tree, project, reads), root: tree.root, projects: tree.projects })
2298
+ const outputs = await debugTimed('Generated the Prisma Clients', () => (tree.unpack === undefined ? generating() : inRounds(generating, tree.unpack)))
2299
+ if (outputs.length > 0) outside = vfsHost(withPrismaClients(vfs, outputs))
2300
+ }
2301
+ // What follows reads the tree through a host of `outside`: once, or for a lazy tree, in rounds,
2302
+ // each reading into its `reads`, each build keeping what its scans read across them in a memo of
2303
+ // its own.
2304
+ const memos = tree.unpack === undefined ? undefined : builds.map(() => new Map())
2305
+ const attempt = async (reads) => {
2306
+ const host = treeHost(tree, outside, reads)
2307
+ const args = { cwd, host, env }
2308
+ const builts = []
2309
+ // A build of a round that asked for a package already is void, whatever it threw: the builds
2310
+ // after it are run all the same, for all else they ask for, and the round is set aside after.
2311
+ let voided
2312
+ for (const [i, build] of builds.entries()) {
2313
+ let built
2314
+ try {
2315
+ // eslint-disable-next-line no-await-in-loop -- one build at a time, in the order its warnings read
2316
+ built = pm.kind === 'sol' ? { bundle: await buildSolidityBundle({ ...build, ...args }), root: cwd } : await buildJs({ ...build, ...args, innermostRoot, extra: !bundleExtra, outputsAt, memo: memos?.[i] })
2317
+ } catch (error) {
2318
+ if (reads === undefined || reads.asked.size === 0) throw error
2319
+ voided ??= { error }
2320
+ continue
2321
+ }
2322
+ // Rooted at cwd, where `stasis bundle` detects its repo.
2323
+ if (repo === undefined && !built.stateBuilt) built.bundle.repo ??= detectRepo(cwd, host)
2324
+ builts.push(built)
2325
+ }
2326
+ if (voided !== undefined) throw voided.error
2327
+ // Merged as bundleCommand merges its builds.
2328
+ const { root } = builts[0]
2329
+ const union = unionOfBuilds(name, builds, builts, builts.map((built) => built.root))
2330
+ // Once: to the union, by the allowlist of the stasis.config.json at the root, as bundleCommand adds
2331
+ // it; else to the one build given, by its `resources`, as buildOfKind adds it, unless its State
2332
+ // took it. Never one of the files written (none, into a Vfs).
2333
+ return { root, ...bundleExtra || !builts[0].stateBuilt ? withExtra(union, root, { outputs: outputsAt(root), resources: bundleExtra ? undefined : builds[0].resources, host, name }) : union }
2334
+ }
2335
+ const since = counts()
2336
+ const built = tree.unpack === undefined
2337
+ ? await debugTimed('Built', () => attempt(), () => builtSince(since))
2338
+ : await inRounds(...debugging() ? reportedRounds(attempt, tree.unpack) : [attempt, tree.unpack])
2339
+ const { bundle, lockfile, root } = built
1727
2340
  if (repo !== undefined) bundle.repo = repo
1728
2341
  pinCommits(bundle, root, tree)
1729
- return { bundle, lockfile: lockfile?.(), stats: tree.stats, packageManager, root }
2342
+ const unpacked = tree.lazy?.unpacked.size ?? tree.stats.installed ?? tree.stats.dependencies
2343
+ return { bundle, lockfile: lockfile?.(), stats: { ...tree.stats, unpacked }, packageManager, root }
2344
+ }
2345
+
2346
+ // EXODUS_STASIS_DEBUG=1 or true.
2347
+ export const debugging = () => process.env.EXODUS_STASIS_DEBUG === '1' || process.env.EXODUS_STASIS_DEBUG === 'true'
2348
+
2349
+ // The time a --metro build's stages took, all told, in milliseconds: its scans of what the app
2350
+ // imports, what Metro and Babel add to it, the configs and toolchain, and carrying the files reached
2351
+ // into the bundle. What EXODUS_STASIS_DEBUG says of each build.
2352
+ const stageTimes = { scan: 0, metro: 0, configs: 0, carrying: 0 }
2353
+ const staged = (stage, fn) => {
2354
+ const at = performance.now()
2355
+ try {
2356
+ return fn()
2357
+ } finally {
2358
+ stageTimes[stage] += performance.now() - at
2359
+ }
2360
+ }
2361
+ const seconds = (ms) => `${(ms / 1000).toFixed(2)} s`
2362
+
2363
+ // `run()`, said on stderr once what it gives settles where EXODUS_STASIS_DEBUG asks for it: `what`, in
2364
+ // how long, and what `detail()` adds. Written, not warned: inRounds holds back what a round it sets
2365
+ // aside warns. -> what `run()` gives
2366
+ export async function debugTimed(what, run, detail = () => '') {
2367
+ if (!debugging()) return run()
2368
+ const at = performance.now()
2369
+ try {
2370
+ return await run()
2371
+ } finally {
2372
+ process.stderr.write(`[stasis] ${what} in ${seconds(performance.now() - at)}${detail()}\n`)
2373
+ }
2374
+ }
2375
+
2376
+ // scanCounts and stageTimes as they are, for builtSince.
2377
+ const counts = () => ({ files: { ...scanCounts }, stages: { ...stageTimes } })
2378
+ const STAGES = { scan: 'scan', metro: "Metro's additions", configs: 'configs and toolchain', carrying: 'carrying' }
2379
+
2380
+ // What builds did since `since` (counts()'): the files their scans parsed, resolved again from a
2381
+ // parse kept and took as kept, and the time of each stage they went through.
2382
+ function builtSince(since) {
2383
+ const files = Object.keys(scanCounts).map((key) => `${scanCounts[key] - since.files[key]} ${key}`).join(', ')
2384
+ const times = Object.keys(STAGES).filter((key) => stageTimes[key] > since.stages[key]).map((key) => `${STAGES[key]} ${seconds(stageTimes[key] - since.stages[key])}`)
2385
+ return ` (files ${files}${times.length > 0 ? `; ${times.join(', ')}` : ''})`
2386
+ }
2387
+
2388
+ // A package's name, as its directory in a tree says it: what follows its last node_modules.
2389
+ const packageName = (dir) => dir.slice(dir.lastIndexOf('/node_modules/') + '/node_modules/'.length)
2390
+
2391
+ // `attempt` and `unpack` as inRounds takes them, each round said on stderr as it ends (debugTimed):
2392
+ // what it built (builtSince), the packages it asked for, the first of them named, and how long
2393
+ // unpacking them took.
2394
+ function reportedRounds(attempt, unpack) {
2395
+ let round = 0
2396
+ return [
2397
+ (reads) => {
2398
+ round++
2399
+ const since = counts()
2400
+ 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 ? ', …' : ''}`)
2401
+ return debugTimed(`Lazy round ${round}: built`, () => attempt(reads), () => `${builtSince(since)}${asked()}`)
2402
+ },
2403
+ (asked, read) => debugTimed(`Lazy round ${round}: unpacked`, () => unpack(asked, read)),
2404
+ ]
1730
2405
  }
1731
2406
 
1732
2407
  // Programmatic equivalent of `stasis bundle`: build and return an in-memory Bundle without
@@ -1750,7 +2425,17 @@ export async function buildBundle({ cwd = process.cwd(), env = process.env, entr
1750
2425
  // of `entries` and the options building them, is several builds' (each `{ entries, ...options }`,
1751
2426
  // as stasis.config.json's `bundle` gives an entry its own, see bundle-options.js), whose bundles
1752
2427
  // (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 } = {}) {
2428
+ // With `deptree`, the dependencies are not read from disk but laid out in memory from the lockfile
2429
+ // that installs cwd, as buildVfsBundle lays them out with `lazy: false` (for JS, pnpm, yarn 1 or
2430
+ // npm's; for Solidity, Soldeer's), and the rest of the project read from disk through them:
2431
+ // refused where no lockfile of those installs cwd, or more than one does, and where anything is
2432
+ // installed already (layOutTree's `refuseInstalled`), before anything is fetched. The tarballs are fetched (or read
2433
+ // from a cache that holds them, held to the lockfile all the same) and nothing is cached: nothing
2434
+ // is written but the bundle and the lockfile. A Soldeer git dependency is fetched from GitHub with
2435
+ // `env`'s GITHUB_TOKEN where it is set. Where process.env gives the npm client no token, it is given
2436
+ // `<home>/.audit/config.json`'s `npmToken` (the user's home by default), else the one `<home>/.npmrc`
2437
+ // gives npm's registry, while the tree is laid out and the bundle built (tokens.js's withNpmToken).
2438
+ export async function bundleCommand({ 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, brotliQuality, add = false, builds, deptree = false } = {}) {
1754
2439
  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
2440
  if (builds !== undefined) {
1756
2441
  if (!Array.isArray(builds) || builds.length === 0) throw new TypeError('bundleCommand: builds must be a non-empty array')
@@ -1758,8 +2443,7 @@ export async function bundleCommand({ cwd = process.cwd(), env = process.env, en
1758
2443
  if (given) throw new TypeError(`bundleCommand: ${given[0]} can't be combined with builds (each build sets its own)`)
1759
2444
  }
1760
2445
  // 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 }))
2446
+ const plans = (builds ?? [{}]).map((build) => ({ ...options, ...build, cwd, env }))
1763
2447
  const kinds = plans.map((plan) => classifyEntries('bundleCommand', { ...plan, lockfile }))
1764
2448
 
1765
2449
  // --add has nothing to merge into on stdout (write-only).
@@ -1773,34 +2457,47 @@ export async function bundleCommand({ cwd = process.cwd(), env = process.env, en
1773
2457
  // With `builds` (stasis.config.json's entries, however many builds they make), `extra` is the
1774
2458
  // bundle's, not any build's: added to their union below.
1775
2459
  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]})`)
2460
+ let union, root
2461
+ if (deptree) {
2462
+ // The disk read through the tree, nothing cached (`cache: false`, and no setCacheDir here).
2463
+ const [{ createClient }, { detectPackageManager }, { withNpmToken }] = await Promise.all([import('@preventive/upstream/github.js'), import('../vfs-bundle/tree.js'), import('./tokens.js')])
2464
+ const built = await withNpmToken({ home }, () => buildFromLockfile('bundleCommand', diskHost, {
2465
+ cwd: resolve(cwd),
2466
+ packageManager: detectPackageManager('bundleCommand', diskHost, resolve(cwd), { why: '--deptree lays out the dependencies from a lockfile' }),
2467
+ lockfile,
2468
+ cache: false,
2469
+ lazy: false,
2470
+ client: createClient({ token: env.GITHUB_TOKEN || null }),
2471
+ }, { env, plan: { builds: plans, bundleExtra }, outputsAt, refuseInstalled: true }))
2472
+ console.warn(unpackedSummary(built.stats))
2473
+ root = built.root
2474
+ union = { bundle: built.bundle, lockfile: () => built.lockfile }
2475
+ } else {
2476
+ const builts = []
2477
+ for (const [i, plan] of plans.entries()) {
2478
+ // eslint-disable-next-line no-await-in-loop -- one build at a time, in the order its warnings read
2479
+ builts.push(await buildOfKind(kinds[i], { ...plan, outputsAt, extra: !bundleExtra }))
2480
+ }
2481
+ // The directory the bundle's paths are relative to: a State-built JS bundle's root, which is the
2482
+ // project's at or above cwd (a workspace package's is the workspace root's), else cwd.
2483
+ const roots = builts.map((built) => built.root ?? resolve(cwd))
2484
+ root = roots[0]
2485
+ // State-built bundles keep the State root's repo (no cwd fallback: their paths are relative to that root).
2486
+ for (const built of builts) if (!built.stateBuilt) built.bundle.repo ??= detectRepo(cwd)
2487
+ union = unionOfBuilds('bundleCommand', plans, builts, roots)
2488
+ if (bundleExtra) {
2489
+ // Once, whatever the builds' order: to their union, whose own record of a file stands, by the
2490
+ // allowlist of the stasis.config.json at the root, not any one entry's `resources`.
2491
+ const withExtras = withExtra(union, root, { outputs: outputsAt(root) })
2492
+ if (withExtras !== union) pinInstalledCommits(withExtras.bundle, root)
2493
+ union = withExtras
2494
+ }
1788
2495
  }
1789
2496
  // By default the bundle goes there, where `stasis run --bundle=load` discovers it. In cwd below
1790
2497
  // the root it would load as rooted at cwd, and root the next `stasis bundle` there too, out of the
1791
2498
  // files above it.
1792
2499
  const target = output ?? relative(resolve(cwd), join(root, DEFAULT_BUNDLE_FILE))
1793
2500
 
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
2501
  let { bundle } = union
1805
2502
  // Only a JS bundle has a lockfile (classifyEntries refuses one for the others).
1806
2503
  let lockData = lockfile ? union.lockfile().serialize() : undefined
@@ -1813,7 +2510,7 @@ export async function bundleCommand({ cwd = process.cwd(), env = process.env, en
1813
2510
  if (add && existsSync(outAbs)) {
1814
2511
  let existing
1815
2512
  try {
1816
- existing = Bundle.parse(brotliDecompressSync(readFileSync(outAbs)).toString('utf8'))
2513
+ existing = parseBundleBytes(readFileSync(outAbs))
1817
2514
  } catch (cause) {
1818
2515
  throw new Error(`bundleCommand: --add failed to read the existing bundle at ${target}`, { cause })
1819
2516
  }