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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +2 -1
  2. package/bin/stasis.js +99 -16
  3. package/package.json +22 -9
  4. package/src/audit-corrections.js +11 -42
  5. package/src/audit.js +241 -190
  6. package/src/babel.js +39 -122
  7. package/src/bundle-from-lockfile.js +9 -21
  8. package/src/bundle-reader.js +5 -9
  9. package/src/bundle.js +0 -3
  10. package/src/cmd/build.js +82 -174
  11. package/src/cmd/bundle-options.js +113 -167
  12. package/src/cmd/bundle.js +942 -1294
  13. package/src/cmd/diff.js +1 -4
  14. package/src/cmd/github-bundle.js +21 -26
  15. package/src/cmd/output.js +15 -12
  16. package/src/cmd/sbom.js +3 -8
  17. package/src/cmd/tokens.js +16 -23
  18. package/src/define.js +36 -94
  19. package/src/diff.js +72 -128
  20. package/src/esbuild.js +0 -4
  21. package/src/flow.js +18 -13
  22. package/src/installed.js +265 -0
  23. package/src/json-stream.js +4 -9
  24. package/src/loader-mock.js +3 -8
  25. package/src/loader.js +1 -5
  26. package/src/loaders/bash.js +41 -93
  27. package/src/loaders/cargo.js +298 -805
  28. package/src/loaders/foundry.js +109 -285
  29. package/src/loaders/paths.js +13 -13
  30. package/src/loaders/php.js +210 -489
  31. package/src/loaders/rust.js +507 -1430
  32. package/src/loaders/solidity-ownership.js +48 -128
  33. package/src/loaders/solidity.js +39 -122
  34. package/src/loaders/toml.js +7 -17
  35. package/src/lockfile.js +0 -3
  36. package/src/metro-assets.js +10 -384
  37. package/src/metro-resolver.js +58 -134
  38. package/src/metro-transformer.js +0 -5
  39. package/src/metro.js +0 -4
  40. package/src/mock.js +32 -80
  41. package/src/parse.js +4 -4
  42. package/src/prune.js +0 -3
  43. package/src/resolve-fields.js +137 -532
  44. package/src/resolve-node.js +27 -45
  45. package/src/resolve-typescript.js +166 -298
  46. package/src/rollup.js +0 -4
  47. package/src/run-installed.js +10 -0
  48. package/src/sbom.js +52 -128
  49. package/src/scan.js +142 -284
  50. package/src/sixel.js +310 -0
  51. package/src/table.js +63 -31
  52. package/src/validate-notes.d.ts +19 -0
  53. package/src/validate-notes.js +72 -0
  54. package/src/validate.d.ts +159 -0
  55. package/src/validate.js +596 -522
  56. package/src/vfs-bundle/entries.js +40 -57
  57. package/src/vfs-bundle/github.js +80 -122
  58. package/src/vfs-bundle/glob.js +4 -45
  59. package/src/vfs-bundle/lazy.js +27 -56
  60. package/src/vfs-bundle/prisma.js +235 -105
  61. package/src/vfs-bundle/soldeer.js +28 -49
  62. package/src/vfs-bundle/tree.js +295 -318
  63. package/src/vfs-bundle/yarnclean.js +10 -31
  64. package/src/vfs-bundle.js +23 -64
  65. package/src/webpack.js +0 -4
  66. package/src/why.js +78 -232
  67. package/src/installed-commits.js +0 -184
package/src/cmd/bundle.js CHANGED
@@ -2,26 +2,32 @@ import { isUtf8 } from 'node:buffer'
2
2
  import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs'
3
3
  import { basename, dirname, extname, isAbsolute, join, posix, relative, resolve } from 'node:path'
4
4
  import { fileURLToPath, pathToFileURL } from 'node:url'
5
+ import { brotliCompressSync, constants as zlibConstants } from 'node:zlib'
5
6
 
6
7
  import { Bundle, isValidRepoField } from '@exodus/stasis-core/bundle'
8
+ import { Config, assertOption } from '@exodus/stasis-core/config'
7
9
  import { Lockfile } from '@exodus/stasis-core/lockfile'
8
- import { extraArtifacts } from '@exodus/stasis-core/add'
9
- import { getParser, importsTypescriptByOutputName, literalSpec, scan, scanCounts } from '../scan.js'
10
- import { parseDefine } from '../define.js'
11
- import { METRO_ASSET_EXTS, METRO_SOURCE_EXTS, createFieldResolver, isMetroAssetFile, resolveConditions } from '../resolve-fields.js'
12
- import { discoverTsconfig, isDir, loadTsconfigPaths, packageTsconfigPaths } from '../resolve-typescript.js'
10
+ import { assembleBundle, expandExtra, validateFiles } from '@exodus/stasis-core/add'
11
+ import { getParser, importsTypescriptByOutputName, isResolvableExt, literalSpec, scan, scanCounts } from '../scan.js'
12
+ import { METRO_RELEASE_DEFINE, parseDefine } from '../define.js'
13
+ import { METRO_ASSET_EXTS, METRO_ASSET_RESOLUTIONS, METRO_SOURCE_EXTS, createFieldResolver, resolveConditions } from '../resolve-fields.js'
14
+ import { discoverTsconfig, isDir, isFile, loadTsconfigPaths, packageTsconfigPaths, tsconfigOutputsOf } from '../resolve-typescript.js'
13
15
  import { createMetroResolver } from '../metro-resolver.js'
14
16
  import { ASSET_REGISTRY } from '../metro-assets.js'
15
17
  import { createAliases, createBabelAliases, createBabelTypeImportsKept } from '../babel.js'
16
18
  import { AUTO_INCLUDES, RN_CORE_INCLUDE_FILES, linkedNativePackages, linkedPackages, listing, looksNative, metroAssetFiles, nativeInputs } from '@exodus/stasis-plugins/metro-native'
17
- import { pinInstalledCommits } from '../installed-commits.js'
19
+ import { installedRecords, pinInstalled } from '../installed.js'
18
20
  import { State } from '@exodus/stasis-core/state'
19
- import { readFileSyncMaybe, sha512integrity } from '@exodus/stasis-core/state-util'
21
+ import { bundledIntegrity, readFileSyncMaybe } from '@exodus/stasis-core/state-util'
20
22
  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'
23
+ import { attestsAny, BUNDLE_FILE, CONFIG_FILE, EMPTY_MODULE_PATH, assertRealPathWithinBase, classifyFormat, dependencyEcosystem, fileInBucket, archiveIntegrity, hasNodeModulesSegment, heldToAccounts, isDotEnvFile, isPathWithin, isPlainObject, JS_EXTENSIONS, LOCK_FILE, memoized, moduleFileKey, moduleFileKeys, moduleInfo, nestedMap, nodeExtFormat, observeExecutable, parseExtraOption, parseLockfilesOption, parseResourcesOption, posixPathEscapes, relativeEscapes, splitNodeModulesPath, toPosix, upsert, withAttestedOf, withMetadataOf } from '@exodus/stasis-core/util'
22
24
  import { diskHost } from '@exodus/stasis-core/host'
25
+ import { linksOn, parsePolicy, recordedIn } from '@exodus/stasis-core/policy'
23
26
  import { parseBundleBytes } from '@exodus/stasis-core/brotli'
24
27
  import { parseSoldeerLockfile } from '@preventive/lockfile/soldeer.js'
28
+ import { isAssetFile } from '@preventive/rn/metro-assets.js'
29
+ import { added as addedTo } from '@preventive/rn/src/rn-config.js'
30
+ import { globRegExp, isPathGlob, packagesOf, restrictionsOf } from '@preventive/rn/src/restrictions.js'
25
31
  import {
26
32
  SOLIDITY_PACKAGE_MANIFESTS,
27
33
  SOLIDITY_ROOT_MANIFESTS,
@@ -30,10 +36,11 @@ import {
30
36
  discoverSolidityConfig,
31
37
  expandSolidityEntries,
32
38
  } from '../loaders/solidity.js'
33
- import { decodeUtf8 } from '../loaders/solidity-ownership.js'
39
+ import { decodeUtf8, solidityOwnership } from '../loaders/solidity-ownership.js'
34
40
  import { buildBashTree, collectBashFilesFromDisk } from '../loaders/bash.js'
35
41
  import { boundaryOf, collectRustBundle, withinRealDir } from '../loaders/rust.js'
36
42
  import { createCargoContext } from '../loaders/cargo.js'
43
+ import { ancestors } from '../loaders/paths.js'
37
44
  import {
38
45
  bucketizePhpSources,
39
46
  buildPhpTree,
@@ -42,185 +49,100 @@ import {
42
49
  loadComposerPackages,
43
50
  loadLaravelProviderFiles,
44
51
  } from '../loaders/php.js'
45
- import { DEFAULT_BUNDLE_FILE, bundledSummary, packagesLabel, unpackedSummary, writeBundle, writeFile } from './output.js'
52
+ import { bundledSummary, lockedSummary, lockfileOnlyError, packagesLabel, unpackedSummary, writeBundle, writeFile } from './output.js'
53
+ import { withNpmToken } from './tokens.js'
46
54
 
47
- const JS_EXTS = new Set(['.js', '.cjs', '.mjs', '.ts', '.cts', '.mts', '.jsx', '.tsx'])
48
55
  const BASH_EXTS = new Set(['.sh', '.bash'])
49
56
  const RUST_EXTS = new Set(['.rs'])
57
+ const nonEmpty = (list) => Array.isArray(list) && list.length > 0
50
58
 
51
- // Fallback identity for the workspace bucket; Bundle.parse requires every bucket to attest name+version.
52
- const SOLIDITY_WORKSPACE_NAME = 'solidity-bundle'
53
- const SOLIDITY_WORKSPACE_VERSION = '0.0.0'
54
- const SOLIDITY_FORMAT = 'solidity'
55
- const BASH_WORKSPACE_NAME = 'bash-bundle'
56
- const BASH_WORKSPACE_VERSION = '0.0.0'
57
- const SHELL_FORMAT = 'shell'
58
-
59
- const RUST_WORKSPACE_NAME = 'rust-bundle'
60
- const RUST_WORKSPACE_VERSION = '0.0.0'
61
- const RUST_FORMAT = 'rust'
62
-
63
- const PHP_WORKSPACE_NAME = 'php-bundle'
64
- const PHP_WORKSPACE_VERSION = '0.0.0'
65
- const PHP_FORMAT = 'php'
66
-
67
- // Deepest common parent dir of `paths`, relative to `cwd`; starts with `..` when entries
68
- // escape cwd (e.g. via remapping), or "." when it is cwd itself.
59
+ // The deepest dir holding every one of `paths`, relative to `cwd` (`..` where they escape it, by remapping).
69
60
  export function outermostDir(paths, cwd) {
70
61
  if (paths.length === 0) return '.'
71
62
  const cwdAbs = posix.resolve(toPosix(cwd))
72
- const absDirs = paths.map((p) => posix.dirname(posix.resolve(cwdAbs, p)))
73
- const partsList = absDirs.map((d) => d.split('/'))
74
- const common = []
75
- for (let i = 0; i < partsList[0].length; i++) {
76
- const c = partsList[0][i]
77
- if (!partsList.every((parts) => parts[i] === c)) break
78
- common.push(c)
79
- }
80
- const absCommon = common.join('/') || '/'
81
- const rel = posix.relative(cwdAbs, absCommon)
82
- return rel === '' ? '.' : rel
63
+ const [first, ...rest] = paths.map((p) => posix.dirname(posix.resolve(cwdAbs, p)).split('/'))
64
+ let depth = 0
65
+ while (depth < first.length && rest.every((parts) => parts[depth] === first[depth])) depth++
66
+ return posix.relative(cwdAbs, first.slice(0, depth).join('/') || '/') || '.'
83
67
  }
84
68
 
85
- // Split a Soldeer dep dir `<name>-<version>` (its authoritative identity); falls back to
86
- // version 0.0.0 when it doesn't end in a semver (every bucket must attest a version).
69
+ // A Soldeer dependency's folder `<name>-<version>`; 0.0.0 where it ends in no semver, as every bucket attests one.
87
70
  function parseSoldeerDir(seg) {
88
71
  const m = /^(.+)-(\d+\.\d+\.\d+(?:[-+][\w.-]+)?)$/u.exec(seg)
89
72
  return m ? { name: m[1], version: m[2] } : { name: seg, version: '0.0.0' }
90
73
  }
91
74
 
92
- // A url as git fetches from a host: scp's `[user@]host:path`, with no scheme and a colon before its
93
- // first slash, or a URL of a transport git has itself (`http`, `https`, `git`, `ssh` and its
94
- // `git+ssh`/`ssh+git` aliases, lowercase, as git spells them), its authority, `[userinfo@]host[:port]`,
95
- // ending at the first slash after it. Not, then, what a package.json spells GitHub's that git reads
96
- // another way or not at all: `ssh://git@github.com:owner/name`, whose host is `github.com:owner` to
97
- // git; `git+https://` or `HTTPS://`, transports git doesn't have; nor `owner/name`, a path, as is any
98
- // url with no colon before its first slash.
75
+ // A url as git fetches from a host: scp's `[user@]host:path`, or a URL of a transport of git's own (lowercase).
76
+ // Not `ssh://git@github.com:owner/name` (host `github.com:owner` to git), `git+https://`, nor a bare path.
99
77
  const GIT_URL = /^(?:[^/]*:(?!\/\/)|(?:https?|git|ssh|git\+ssh|ssh\+git):\/\/(?:[^/@]*@)?[^/:@]*(?::\d*)?\/)/u
100
78
 
101
- // The GitHub repository, `owner/name`, a .gitmodules `url` names (parseGithubRepository), else null:
102
- // none for a host merely holding `github.com` (`https://notgithub.com/o/n`), nor for a url git
103
- // doesn't fetch as one (GIT_URL), nor for npm's `github:owner/name`, the host `github`'s path to git,
104
- // whatever an ssh alias or `insteadOf` makes of it, nor one with a `#`, which git keeps in the path
105
- // it asks for (`owner/name.git#v1`) where npm takes a committish. git's `ssh+git://`, which a
106
- // package.json doesn't spell, is its `ssh://`.
79
+ // Not npm's `github:owner/name` (the host `github` to git), nor one with a `#`, which git keeps in the path.
107
80
  const submoduleGithub = (url) => (GIT_URL.test(url) && !/^github:/iu.test(url) && !url.includes('#') ? parseGithubRepository(url.replace(/^ssh\+git:/u, 'ssh:')) : null)
108
81
 
109
- // The GitHub ones of `submodules` (readGitmodules), as Map<submodulePath, { name, branch }>.
110
82
  function githubSubmodules(submodules) {
111
- const byPath = new Map()
112
- for (const { path, url, branch } of submodules) {
113
- const name = url && submoduleGithub(url)
114
- if (name) byPath.set(path, { name, branch })
115
- }
116
- return byPath
83
+ const named = submodules.map(({ path, url, branch }) => [path, { name: url && submoduleGithub(url), branch }])
84
+ return new Map(named.filter(([, { name }]) => name))
117
85
  }
118
86
 
119
- // The root of the GitHub repository `github` at `commit`, where both are valid; else undefined.
120
87
  const repoRootAt = (github, commit) => (isValidRepoField('github', github) && isValidRepoField('commit', commit) ? { github, directory: '', commit } : undefined)
121
88
 
122
- // The text of `rel`, a file of the project root, read as `ownership` vouches for it; null where it
123
- // refuses it, nothing is there, or it isn't UTF-8.
124
- function ownedText(baseDir, rel, ownership, host) {
125
- const { reason, real, outside } = ownership.of(rel)
126
- if (reason || real === null || outside) return null
127
- try {
128
- const buf = readRegularFileOrNull(join(host.realpath(baseDir), real), rel, host)
129
- return buf === null ? null : decodeUtf8(buf, rel)
130
- } catch {
131
- return null
132
- }
133
- }
134
-
135
- // Each git dependency the root's soldeer.lock installs from GitHub, by its folder under
136
- // `dependencies/` (`<name>-<version>`, as Soldeer names it): the repository's root at the lockfile's
137
- // `rev`, which Soldeer checks out there. None where the lockfile can't be read or parsed: it is
138
- // metadata, which never stops a build.
139
- function soldeerGitRepos(baseDir, ownership, host) {
140
- const text = ownedText(baseDir, 'soldeer.lock', ownership, host)
89
+ // None where `ownership` refuses soldeer.lock or it can't be read: metadata never stops a build.
90
+ function soldeerLocks(baseDir, ownership, host) {
91
+ const { reason, real, outside } = ownership.of('soldeer.lock')
141
92
  let lock = null
142
93
  try {
143
- lock = text === null ? null : parseSoldeerLockfile(text)
144
- } catch {
145
- // Not one Soldeer writes: nothing is recorded from it.
146
- }
147
- const repos = new Map()
94
+ const buf = reason || real === null || outside ? null : readRegularFileOrNull(join(host.realpath(baseDir), real), 'soldeer.lock', host)
95
+ lock = buf === null ? null : parseSoldeerLockfile(decodeUtf8(buf, 'soldeer.lock'))
96
+ } catch { /* unreadable, or not one Soldeer writes: nothing is recorded of it */ }
97
+ const locks = new Map()
148
98
  for (const dep of Object.values(lock?.dependencies ?? {})) {
149
- const repo = dep.type === 'git' ? repoRootAt(parseGithubRepository(dep.git), dep.rev) : undefined
150
- if (repo !== undefined) repos.set(`${dep.name}-${dep.version}`, repo)
99
+ locks.set(`${dep.name}-${dep.version}`, dep.type === 'git' ? { repo: repoRootAt(parseGithubRepository(dep.git), dep.rev) } : { integrity: archiveIntegrity('soldeer', dep.checksum) })
151
100
  }
152
- return repos
101
+ return locks
153
102
  }
154
103
 
155
- // Classify a Solidity file's dep bucket: Soldeer (`dependencies/<name>-<version>/`) or a
156
- // github submodule (`lib/`, via the `.gitmodules` `ownership` read), else null to defer to the
157
- // node_modules/workspace logic. `ownership.assert` vets a package.json path before it is read
158
- // through `host`. A bucket's `repo` is the GitHub repository it is checked out from, at its root, at
159
- // the commit its checkout's HEAD is at, as the bundle's own `repo` reads one: a submodule's, from
160
- // the GitHub repository its .gitmodules url names; a Soldeer git dependency's, where it is its
161
- // soldeer.lock `rev` (soldeerGitRepos), as Soldeer holds a checkout to the lockfile before taking it
162
- // for installed: a folder left at another commit since the lockfile changed, or no checkout, records
163
- // none.
104
+ // A bucket's `repo` is its checkout's HEAD; a Soldeer git dependency's only at its soldeer.lock `rev`.
164
105
  function makeSolidityClassifier(baseDir, ownership, host) {
165
106
  const submodules = githubSubmodules(ownership.submodules)
166
- const check = ownership.assert
167
- const versions = new Map() // a submodule's package.json version, read once
168
- const commits = new Map() // a bucket's checkout's commit, read once
169
- const commitOf = (dir) => {
170
- if (!commits.has(dir)) commits.set(dir, checkoutCommit(join(baseDir, dir), host))
171
- return commits.get(dir)
172
- }
173
- let soldeer // soldeerGitRepos, read at the first Soldeer file
107
+ const versionOf = memoized((sub) => readPackageJson(baseDir, moduleFileKey(sub, 'package.json'), { strict: true, check: ownership.assert, host })?.version)
108
+ const commitOf = memoized((dir) => checkoutCommit(join(baseDir, dir), host))
109
+ let soldeer
174
110
  return (path) => {
175
- if (path.startsWith('dependencies/')) {
176
- const seg = path.slice('dependencies/'.length).split('/')[0]
177
- if (seg) {
178
- const { name, version } = parseSoldeerDir(seg)
179
- const bucketDir = `dependencies/${seg}`
180
- soldeer ??= soldeerGitRepos(baseDir, ownership, host)
181
- const locked = soldeer.get(seg)
182
- const repo = locked !== undefined && commitOf(bucketDir) === locked.commit ? locked : undefined
183
- return { bucketDir, name, version, ecosystem: 'soldeer', ...(repo === undefined ? {} : { repo }) }
184
- }
111
+ const seg = path.startsWith('dependencies/') ? path.slice('dependencies/'.length).split('/')[0] : ''
112
+ if (seg) {
113
+ const { name, version } = parseSoldeerDir(seg)
114
+ const bucketDir = `dependencies/${seg}`
115
+ soldeer ??= soldeerLocks(baseDir, ownership, host)
116
+ const { repo: locked, integrity } = soldeer.get(seg) ?? {}
117
+ const repo = locked !== undefined && commitOf(bucketDir) === locked.commit ? locked : undefined
118
+ return { bucketDir, name, version, ecosystem: 'soldeer', integrity, ...(repo && { repo }) }
185
119
  }
186
120
  for (const [sub, { name, branch }] of submodules) {
187
121
  if (path === sub || path.startsWith(`${sub}/`)) {
188
- if (!versions.has(sub)) versions.set(sub, readPackageJson(baseDir, moduleFileKey(sub, 'package.json'), { strict: true, check, host })?.version)
122
+ const version = versionOf(sub)
189
123
  const repo = repoRootAt(name, commitOf(sub))
190
- return { bucketDir: sub, name, version: versions.get(sub) ?? branch ?? '0.0.0', ecosystem: 'github', ...(repo === undefined ? {} : { repo }) }
124
+ return { bucketDir: sub, name, version: version ?? branch ?? '0.0.0', ecosystem: 'github', ...(repo && { repo }) }
191
125
  }
192
126
  }
193
127
  return null
194
128
  }
195
129
  }
196
130
 
197
- // A vendored crate's ecosystem, by where `cargo vendor` copied it from (vendoredFrom): a registry's
198
- // copy is `cargo`, which `stasis audit` asks OSV about as a crates.io crate's; a git checkout's is
199
- // `cargo-git`, and a copy with no `.cargo-checksum.json`, which could be either, `cargo-unknown`.
200
- // Neither is ever sent to a public advisory database: a private git dependency's name has no place
201
- // there, and a crates.io crate of the same name isn't it.
202
- const vendoredEcosystem = (from) => {
203
- if (from === 'registry') return 'cargo'
204
- return from === 'git' ? 'cargo-git' : 'cargo-unknown'
205
- }
131
+ // Only a registry's copy is `cargo`, which `stasis audit` asks OSV about: a git checkout's or one with no
132
+ // `.cargo-checksum.json` is never sent to a public advisory database.
133
+ const vendoredEcosystem = (from) => (from === 'registry' ? 'cargo' : from === 'git' ? 'cargo-git' : 'cargo-unknown')
206
134
 
207
- // Classify a Rust file by the nearest Cargo.toml `[package]`: a `cargo vendor`ed crate under the
208
- // vendor dir (`vendor/<dir>/`, or where .cargo/config.toml points) is a dependency (tagged
209
- // `cargo`, `cargo-git` or `cargo-unknown`: see vendoredEcosystem); any other package (the crate
210
- // itself, a workspace member reached through a `path` dependency) is first-party, so no
211
- // ecosystem. Null (no manifest above the file) defers to the package.json/placeholder logic.
212
135
  function makeRustClassifier(cargo) {
213
136
  return (path) => {
214
137
  const pkg = cargo.packageInfo(path)
215
138
  if (!pkg) return null
216
- return { bucketDir: pkg.dir, name: pkg.name, version: pkg.version, ecosystem: cargo.isVendored(path) ? vendoredEcosystem(cargo.vendoredFrom(path)) : undefined }
139
+ const ecosystem = cargo.isVendored(path) ? vendoredEcosystem(cargo.vendoredFrom(path)) : undefined
140
+ return { bucketDir: pkg.dir, name: pkg.name, version: pkg.version, ecosystem, ...(ecosystem === 'cargo' && { integrity: cargo.vendoredIntegrity(path) }) }
217
141
  }
218
142
  }
219
143
 
220
- // A file of the bundle root (`realBase` its real path), its bytes, by project-relative path, or
221
- // null when it isn't a regular file there (a manifest the context saw may have gone since; a FIFO
222
- // would hang a read) or, with `within` a directory, doesn't really lie in it (a vendored crate's
223
- // manifest linked to a project file).
144
+ // Null where `rel` isn't a regular file (a FIFO would hang a read) or doesn't really lie in `within` (a
145
+ // vendored crate's manifest linked to a project file).
224
146
  function readFileWithinBase(baseDir, realBase, rel, within) {
225
147
  try {
226
148
  assertRealPathWithinBase(realBase, baseDir, rel)
@@ -233,49 +155,23 @@ function readFileWithinBase(baseDir, realBase, rel, within) {
233
155
  }
234
156
  }
235
157
 
236
- // Project-relative paths in `sources` whose on-disk file carries a POSIX execute bit -- the
237
- // `executable` list both artifacts record. The State-driven path derives this in addFile; the
238
- // static builders never touch a State, so they stat here.
239
- function executableSources(baseDir, sources, host) {
240
- const executable = new Set()
241
- for (const path of sources.keys()) {
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)
244
- }
245
- return executable
158
+ function assertEntries(name, entries, what, isEntry, noun = what) {
159
+ if (!nonEmpty(entries)) throw new Error(`${name}: at least one entry ${what} is required`)
160
+ for (const e of entries) if (!isEntry(e)) throw new Error(`${name}: not a ${noun}: ${e}`)
246
161
  }
247
162
 
248
- // Bundles must be self-contained: throw on a missing entry or an unresolved `noun` (import, script,
249
- // module) of a `lang` bundle -- `missing`'s { spec, from }, with the `reason` it was refused if given.
250
163
  function assertSelfContained(lang, noun, entries, sources, missing) {
251
- const issues = []
252
- for (const entry of entries) {
253
- if (!sources.has(entry)) issues.push(`Missing entry: ${entry}`)
254
- }
255
- for (const { spec, from, reason } of missing) {
256
- issues.push(`Unresolved ${noun}: ${spec} from ${from}${reason ? ` (refused: ${reason})` : ''}`)
257
- }
258
- if (issues.length > 0) {
259
- throw new Error(`${lang} bundle has unresolved ${noun}s:\n${issues.map((s) => ` ${s}`).join('\n')}`)
260
- }
164
+ const issues = [...entries].filter((entry) => !sources.has(entry)).map((entry) => `Missing entry: ${entry}`)
165
+ for (const { spec, from, reason } of missing) issues.push(`Unresolved ${noun}: ${spec} from ${from}${reason ? ` (refused: ${reason})` : ''}`)
166
+ if (issues.length > 0) throw new Error(`${lang} bundle has unresolved ${noun}s:\n${issues.map((s) => ` ${s}`).join('\n')}`)
261
167
  }
262
168
 
263
- // Assemble a full-scope code Bundle shared by the non-JS bundlers. Files are bucketed by
264
- // nearest package.json (node_modules -> `npm`-tagged bucket, workspace -> its dir, none ->
265
- // "." with the placeholder identity); a node_modules file whose nearest package.json is the
266
- // workspace root is rejected, not mislabeled; `packageOf(path)` finds that package.json
267
- // (packageLookup by default, which walks past a malformed one). `classifyDep(path)` optionally
268
- // places a file directly (non-node_modules ecosystems like Soldeer/github); null defers. `format` tags
269
- // every file; `formats` (Map<path,format>) overrides it per file. `resolutions` values are
270
- // a flat target string or a Map<platform,target>; both round-trip untouched. `keyedResolutions`,
271
- // `consumer` and `consumerFiles` are codeBundle's.
169
+ // A file of no package goes in "." under the workspace identity: Bundle.parse needs every bucket to attest one.
272
170
  function assembleCodeBundle({
273
- baseDir, entries, sources, resolutions, keyedResolutions, workspaceName, workspaceVersion, format, formats, conditionKey, classifyDep, host,
171
+ baseDir, entries, sources, resolutions, keyedResolutions, workspaceName, workspaceVersion = '0.0.0', format, formats, conditionKey, classifyDep, host,
274
172
  packageOf = packageLookup(baseDir, { host }), consumer, consumerFiles,
275
173
  }) {
276
174
  const modules = new Map()
277
- // `identity`'s name, version, ecosystem, repo and vendored (moduleInfo takes those alone); a later
278
- // file's adds the vendored package it is in.
279
175
  const ensureBucket = (dir, identity) => {
280
176
  const bucket = modules.get(dir)
281
177
  modules.set(dir, bucket === undefined ? moduleInfo({ ...identity, files: Object.create(null) }) : withMetadataOf(bucket, identity))
@@ -289,7 +185,7 @@ function assembleCodeBundle({
289
185
  continue
290
186
  }
291
187
  const meta = packageOf(path)
292
- // A node_modules file whose nearest package.json is the workspace root (or none at all) is a misconfigured dep -- refuse.
188
+ // One of node_modules whose nearest package.json is the workspace root's (or none) is a misconfigured dep.
293
189
  if (splitNodeModulesPath(path) !== null && !(meta && hasNodeModulesSegment(meta.pkgDir))) {
294
190
  throw new Error(`No package.json with name+version found for ${path}`)
295
191
  }
@@ -300,59 +196,37 @@ function assembleCodeBundle({
300
196
  return codeBundle({ baseDir, entries, sources, modules, resolutions, keyedResolutions, format, formats, conditionKey, host, consumer, consumerFiles })
301
197
  }
302
198
 
303
- // A full-scope code Bundle of `sources`, bucketed as `modules`: `format` tags every file, `formats`
304
- // (Map<path,format>) overrides it per file (a file neither tags has none), and the `resolutions` edges
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
199
  function codeBundle({ baseDir, entries, sources, modules, resolutions, keyedResolutions = new Map(), format, formats, conditionKey, host, consumer = 'bundle', consumerFiles }) {
309
200
  const formatsMap = new Map()
310
201
  for (const path of sources.keys()) {
311
202
  const fileFormat = formats?.get(path) ?? format
312
203
  if (fileFormat !== undefined) formatsMap.set(path, fileFormat)
313
204
  }
314
-
315
- const imports = new Map([[conditionKey, new Map(resolutions)], ...keyedResolutions])
316
-
317
- // Executable bits, straight off disk (a synthetic source with no file there is simply not
318
- // executable). Shell bundles lean on this most: `stasis extract` puts the +x back on the scripts.
319
- const executable = executableSources(baseDir, sources, host)
320
-
321
- // Attribute files to the static build's consumer (static builders skip State's per-file tagging).
322
205
  return new Bundle({
323
206
  config: { scope: 'full' },
324
207
  entries: new Set(entries),
325
208
  modules,
326
209
  formats: formatsMap,
327
- imports,
328
- executable,
210
+ imports: new Map([[conditionKey, new Map(resolutions)], ...keyedResolutions]),
211
+ // Off disk: a synthetic source with no file is not executable.
212
+ executable: new Set([...sources.keys()].filter((path) => observeExecutable(join(baseDir, path), host) === true)),
329
213
  }).withReason(consumer, consumerFiles)
330
214
  }
331
215
 
332
- // Files never carried, whatever reads them: `.env` files, and Hardhat's config, which is code
333
- // (both however they're cased).
216
+ // Never carried, whatever reads them, however cased: `.env` files, and Hardhat's config, which is code.
334
217
  const neverCarried = (rel) => isDotEnvFile(rel) || posix.basename(rel).toLowerCase().startsWith('hardhat.config.')
335
218
 
336
- // The build-description files of a Solidity bundle (--manifests), as Map<path, text>, sorted:
337
- // `configFiles` (what discoverSolidityConfig read, whatever they're called), each of which must be
338
- // carried -- one outside the root, neverCarried, refused or gone is an error, as the bundle
339
- // couldn't reproduce the resolution without it -- plus the SOLIDITY_*_MANIFESTS that exist, for the
340
- // root and for each package dir `classifyDep`/`packageOf` places a bundled source in, but none
341
- // whose path `ownership` refuses (see solidityOwnership). Carried as written: whatever they hold
342
- // (an RPC URL with its API key, an Etherscan key, a URL's credentials) is in the bundle too, as
343
- // with --package-json. Read through `host`.
219
+ // --manifests: carried as written, secrets too.
344
220
  function solidityManifests(baseDir, sources, configFiles, { classifyDep, packageOf, ownership, host }) {
345
221
  const realBase = host.realpath(baseDir)
346
- // `{ text }`, or `{ why }` it can't be carried (null: nothing is there). Read by the real path
347
- // `ownership` resolved `rel` to, so what's carried is the file it vouched for.
222
+ // `{ text }`, or `{ why }` it can't be carried (null: nothing is there).
348
223
  const carry = (rel) => {
349
224
  const { reason, real, outside } = ownership.of(rel)
350
225
  if (reason) return { why: reason }
351
226
  if (real === null) return { why: null }
352
227
  if (outside) throw new Error(`Refusing to follow symlink escaping bundle root: ${rel} -> ${resolve(realBase, real)}`)
353
228
  const buf = readRegularFileOrNull(join(realBase, real), rel, host)
354
- if (buf === null) return { why: null } // a directory
355
- return { text: decodeUtf8(buf, rel) }
229
+ return buf === null ? { why: null } : { text: decodeUtf8(buf, rel) }
356
230
  }
357
231
  const unreproducible = (rel, why) => new Error(`--manifests can't carry ${rel}, which the Solidity resolution read: ${why}`)
358
232
  const out = new Map()
@@ -380,18 +254,12 @@ function solidityManifests(baseDir, sources, configFiles, { classifyDep, package
380
254
  return new Map([...out.keys()].toSorted().map((rel) => [rel, out.get(rel)]))
381
255
  }
382
256
 
383
- // An entry `stasis bundle` takes for a directory: one that is, or an extensionless path that
384
- // doesn't exist (a project without `script/` still bundles with `src test script`).
385
- const isDirEntry = (abs, host = diskHost) => isDir(abs, host) || (extname(abs) === '' && host.stat(abs) === null)
257
+ // Or an extensionless path that doesn't exist: a project without `script/` still bundles with `src test script`.
258
+ const isDirEntry = (abs, host) => isDir(abs, host) || (extname(abs) === '' && host.stat(abs) === null)
386
259
 
387
- // Whether `entry` (resolved against `cwd`) is a Solidity bundle's: a .sol file or a directory entry.
388
260
  export const isSolidityEntry = (entry, cwd = process.cwd(), host = diskHost) => entry.endsWith('.sol') || isDirEntry(resolve(cwd, entry), host)
389
261
 
390
- // What's wrong with `entries`' directory entries (resolved against `cwd`), or null: a directory
391
- // entry stands for the .sol files under it, so it goes with Solidity entries only; and entries
392
- // that are all missing extensionless paths are a mistyped file, not a project without those dirs.
393
- // `fetched` false: `host` is an empty tree standing in for one not fetched yet, which can't say
394
- // what is missing.
262
+ // `fetched` false: `host` is an empty tree standing in for one not fetched yet, which can't say what is missing.
395
263
  export function directoryEntryError(entries, cwd = process.cwd(), host = diskHost, { fetched = true } = {}) {
396
264
  const dirs = entries.filter((e) => isDirEntry(resolve(cwd, e), host))
397
265
  const absent = fetched ? dirs.filter((e) => host.stat(resolve(cwd, e)) === null) : []
@@ -402,35 +270,19 @@ export function directoryEntryError(entries, cwd = process.cwd(), host = diskHos
402
270
  : `a directory entry is only supported for Solidity bundles (it stands for the .sol files under it): ${dirs[0]}`
403
271
  }
404
272
 
405
- // Build an in-memory Bundle from entry .sol files and directories (a directory stands for the .sol
406
- // files under it, as forge's src/test/script dirs and Hardhat's contracts dir do; a missing or
407
- // empty one is skipped). Imports resolve through the remappings the project's build uses
408
- // (discoverSolidityConfig: forge's discovery for a Foundry project); `mappingFile`
409
- // (foundry.toml/remappings.txt) pins them to exactly what it lists. An import is refused when it
410
- // reaches a non-.sol file or leaves the root, or when a dependency's reaches the project's own
411
- // files. Config files are read, and bundled only with `manifests` (see solidityManifests). `env`
412
- // supplies FOUNDRY_PROFILE / FOUNDRY_REMAPPINGS, reported when they apply. The project is read
413
- // through `host`.
273
+ // A missing directory entry is skipped; `env` supplies FOUNDRY_PROFILE / FOUNDRY_REMAPPINGS.
414
274
  export async function buildSolidityBundle({ cwd = process.cwd(), entries, mappingFile, manifests = false, env = process.env, host = diskHost } = {}) {
415
- if (!Array.isArray(entries) || entries.length === 0) {
416
- throw new Error('buildSolidityBundle: at least one entry .sol file or directory is required')
417
- }
418
-
275
+ if (!nonEmpty(entries)) throw new Error('buildSolidityBundle: at least one entry .sol file or directory is required')
419
276
  const baseDir = resolve(cwd)
420
277
  const normalized = normalizeEntries(entries, cwd)
421
- for (const e of normalized) {
422
- if (!isSolidityEntry(e, baseDir, host)) throw new Error(`buildSolidityBundle: not a .sol file or directory: ${e}`)
423
- }
278
+ for (const e of normalized) if (!isSolidityEntry(e, baseDir, host)) throw new Error(`buildSolidityBundle: not a .sol file or directory: ${e}`)
424
279
  const expanded = expandSolidityEntries(baseDir, normalized, host)
425
-
426
280
  const { remappings, libs, ownership, files: configFiles, envUsed } = discoverSolidityConfig(baseDir, { mappingFile, env, host })
427
281
  // The bundle doesn't record the environment, so say when it shaped the resolution.
428
282
  if (envUsed.length > 0) console.warn(`[stasis] Solidity imports resolved with ${envUsed.join(', ')} from the environment`)
429
283
  const sources = collectSolidityFilesFromDisk(baseDir, expanded, remappings, { libs, ownership, host })
430
284
  const { resolutions, missing } = buildSolidityTree(sources, { remappings, baseDir, libs, ownership, host })
431
-
432
285
  assertSelfContained('Solidity', 'import', expanded, sources, missing)
433
-
434
286
  const classifyDep = makeSolidityClassifier(baseDir, ownership, host)
435
287
  const packageOf = packageLookup(baseDir, { strict: true, check: ownership.assert, host })
436
288
  const bundled = new Map(sources)
@@ -441,98 +293,49 @@ export async function buildSolidityBundle({ cwd = process.cwd(), entries, mappin
441
293
  formats.set(path, posix.basename(path) === 'package.json' ? 'json' : 'resource')
442
294
  }
443
295
  }
444
-
445
296
  return assembleCodeBundle({
446
- baseDir,
447
- entries: expanded,
448
- sources: bundled,
449
- resolutions,
450
- workspaceName: SOLIDITY_WORKSPACE_NAME,
451
- workspaceVersion: SOLIDITY_WORKSPACE_VERSION,
452
- format: SOLIDITY_FORMAT,
453
- formats,
454
- conditionKey: 'solidity',
455
- classifyDep,
456
- packageOf,
457
- host,
297
+ baseDir, entries: expanded, sources: bundled, resolutions, workspaceName: 'solidity-bundle', format: 'solidity', formats,
298
+ conditionKey: 'solidity', classifyDep, packageOf, host,
458
299
  })
459
300
  }
460
301
 
461
- // Build an in-memory Bundle from entry .sh/.bash files by walking the source/exec graph.
462
- // A local `source` inside the bundle root that resolves to no bundled script is fatal;
463
- // external refs (PATH commands, `$VAR`, absolute, `../`-escaping) are dropped.
302
+ // A local `source` resolving to no bundled script is fatal; an external one (PATH, `$VAR`, out of the root) dropped.
464
303
  export async function buildBashBundle({ cwd = process.cwd(), entries } = {}) {
465
- if (!Array.isArray(entries) || entries.length === 0) {
466
- throw new Error('buildBashBundle: at least one entry .sh/.bash file is required')
467
- }
468
- for (const e of entries) {
469
- if (!BASH_EXTS.has(extname(e))) throw new Error(`buildBashBundle: not a .sh/.bash file: ${e}`)
470
- }
471
-
304
+ assertEntries('buildBashBundle', entries, '.sh/.bash file', (e) => BASH_EXTS.has(extname(e)))
472
305
  const baseDir = resolve(cwd)
473
306
  const normalized = normalizeEntries(entries, cwd)
474
-
475
307
  const sources = await collectBashFilesFromDisk(baseDir, normalized)
476
308
  const { resolutions, missing } = buildBashTree(sources)
477
-
478
- // Self-contained (external refs never reach `missing`).
479
309
  assertSelfContained('Bash', 'script', normalized, sources, missing)
480
-
481
- return assembleCodeBundle({
482
- baseDir,
483
- entries: normalized,
484
- sources,
485
- resolutions,
486
- workspaceName: BASH_WORKSPACE_NAME,
487
- workspaceVersion: BASH_WORKSPACE_VERSION,
488
- format: SHELL_FORMAT,
489
- conditionKey: SHELL_FORMAT,
490
- })
310
+ return assembleCodeBundle({ baseDir, entries: normalized, sources, resolutions, workspaceName: 'bash-bundle', format: 'shell', conditionKey: 'shell' })
491
311
  }
492
312
 
493
- // Build an in-memory Bundle from entry .rs files (crate roots) by walking `mod` declarations and
494
- // references to in-tree crates (the package's own lib, Cargo `path` deps, `cargo vendor`ed
495
- // crates). An unresolvable unconditional `mod` is fatal; path edges (`crate::`/`self::`/`super::`/
496
- // relative `use`s) are recorded best-effort and never widen the file set. Registry deps that
497
- // aren't vendored can't be bundled: they're reported, with a `cargo vendor` hint when there is no
498
- // `vendor/` dir at all. Each crate's features are resolved from the manifests (Cargo.toml +
499
- // Cargo.lock) the way `cargo build` of the entries' packages would, so `#[cfg(feature = …)]` code
500
- // that is off stays out; `cargo` takes them from `cargo metadata` instead (opt-in: it runs cargo).
501
- // `cargoFeatures` / `cargoNoDefaultFeatures` / `cargoAllFeatures` are cargo's `--features` /
502
- // `--no-default-features` / `--all-features` for the entries' packages, in either mode.
503
- // `cargoTarget` (a triple, or `host`) names the build's target: its cfgs, asked of rustc, decide
504
- // `#[cfg(unix)]`-style code and target-specific dependency tables, which are otherwise all kept.
505
- // `cargoManifests` also carries what describes each bundled package's build: its Cargo.toml (and
506
- // the workspace's), its build script -- walked like a crate root, so its modules and the in-tree
507
- // build-dependencies it reaches come along -- plus the root's Cargo.lock and .cargo/config.toml.
508
- export async function buildRustBundle({ cwd = process.cwd(), entries, cargo = false, cargoFeatures = [], cargoNoDefaultFeatures = false, cargoAllFeatures = false, cargoTarget = null, cargoManifests = false } = {}) {
509
- if (!Array.isArray(entries) || entries.length === 0) {
510
- throw new Error('buildRustBundle: at least one entry .rs file is required')
511
- }
512
- for (const e of entries) {
513
- if (!RUST_EXTS.has(extname(e))) throw new Error(`buildRustBundle: not a .rs file: ${e}`)
313
+ // The entries alone, as written: a TEAL program includes no other file.
314
+ export async function buildTealBundle({ cwd = process.cwd(), entries } = {}) {
315
+ assertEntries('buildTealBundle', entries, '.teal file', (e) => e.endsWith('.teal'))
316
+ const baseDir = resolve(cwd)
317
+ const realBase = realpathSync(baseDir)
318
+ const normalized = normalizeEntries(entries, cwd)
319
+ const sources = new Map()
320
+ for (const rel of new Set(normalized)) {
321
+ const buf = readFileWithinBase(baseDir, realBase, rel, '.')
322
+ if (buf !== null) sources.set(rel, decodeUtf8(buf, rel))
514
323
  }
324
+ assertSelfContained('TEAL', 'program', normalized, sources, [])
325
+ return assembleCodeBundle({ baseDir, entries: normalized, sources, workspaceName: 'teal-bundle', format: 'teal', conditionKey: 'teal' })
326
+ }
515
327
 
328
+ export async function buildRustBundle({ cwd = process.cwd(), entries, cargo = false, cargoFeatures = [], cargoNoDefaultFeatures = false, cargoAllFeatures = false, cargoTarget = null, cargoManifests = false } = {}) {
329
+ assertEntries('buildRustBundle', entries, '.rs file', (e) => RUST_EXTS.has(extname(e)))
516
330
  const baseDir = resolve(cwd)
517
331
  const normalized = normalizeEntries(entries, cwd)
518
-
519
- // One Cargo context for the walk, the edge pass and the bucketing: manifests are read once, and
520
- // the walk's crate resolution and the tree's feature decisions agree.
332
+ // One Cargo context for the walk, the edges and the bucketing, so their crate and feature decisions agree.
521
333
  const cargoCtx = createCargoContext(baseDir, {
522
- entries: normalized,
523
- cargo,
524
- features: cargoFeatures,
525
- noDefaultFeatures: cargoNoDefaultFeatures,
526
- allFeatures: cargoAllFeatures,
527
- target: cargoTarget,
334
+ entries: normalized, cargo, features: cargoFeatures, noDefaultFeatures: cargoNoDefaultFeatures, allFeatures: cargoAllFeatures, target: cargoTarget,
528
335
  })
529
- // `include_str!` / `include_bytes!` assets ride along as resources: the walk records their
530
- // format. With `cargoManifests`, each package's build script is walked too (collectRustBundle).
531
336
  const { sources, formats, tree } = await collectRustBundle(baseDir, normalized, { cargo: cargoCtx, buildScripts: cargoManifests })
532
337
  if (cargoManifests) {
533
- // Each bundled package's manifests, and its workspace's lockfile and cargo config, carried as
534
- // written: whatever they hold (a registry token, a `git` URL's credentials) is in the bundle
535
- // too, as with --package-json. A file that isn't UTF-8 text is refused, not altered.
338
+ // Carried as written, credentials too; a file that isn't UTF-8 is refused, not altered.
536
339
  const files = new Set()
537
340
  for (const path of sources.keys()) for (const f of cargoCtx.buildFilesFor(path)) files.add(f.path)
538
341
  const realBase = realpathSync(baseDir)
@@ -547,19 +350,12 @@ export async function buildRustBundle({ cwd = process.cwd(), entries, cargo = fa
547
350
  }
548
351
  }
549
352
  const { resolutions, missing, unresolvedCrates } = tree
550
-
551
353
  assertSelfContained('Rust', 'module', normalized, sources, missing)
552
-
553
- // Where the features the cfg decisions rest on come from: cargo's resolver, or -- something it
554
- // takes missing -- a replay of the manifests, said so (a project without Cargo has neither).
555
354
  const resolution = cargoCtx.resolution()
556
355
  if (resolution.mode === 'replay' && normalized.some((e) => cargoCtx.packageInfo(e) !== null)) {
557
356
  console.warn(`[stasis] Rust features from a replay of the manifests, not cargo's resolver: ${resolution.why}`)
558
357
  }
559
- // EXODUS_STASIS_DEBUG=1: show the feature resolution the cfg decisions came from, per package and
560
- // context, so the replay, cargo's resolver and `cargo metadata` (which unifies dev/build deps
561
- // like resolver 1) can be compared.
562
- if (process.env.EXODUS_STASIS_DEBUG === '1' || process.env.EXODUS_STASIS_DEBUG === 'true') {
358
+ if (debugging()) {
563
359
  const mode = { cargo: "cargo's resolver", metadata: 'cargo metadata', replay: 'manifest replay' }[resolution.mode]
564
360
  for (const context of ['target', 'host']) {
565
361
  const resolved = [...cargoCtx.resolvedFeatures(context)].toSorted(([a], [b]) => (a < b ? -1 : 1))
@@ -570,12 +366,6 @@ export async function buildRustBundle({ cwd = process.cwd(), entries, cargo = fa
570
366
  }
571
367
  }
572
368
  }
573
-
574
- // A crate the code names that the bundle doesn't hold: a registry dependency that isn't vendored
575
- // (or not in a version its requirement allows), a path or patch outside the bundle root (the
576
- // loader warned of those), or a crate root the walk refused -- and any other dependency a bundled
577
- // package's build links that nothing in-tree answers, however the code names it, if at all.
578
- // Reported whatever the vendor dir holds; with none, the fix is one command.
579
369
  const notLoaded = tree.wantedRoots.filter((r) => !sources.has(r)).map((r) => `${cargoCtx.packageInfo(r)?.name ?? r} (${r})`)
580
370
  const bundledPackages = new Set([...sources.keys()].map((p) => cargoCtx.packageInfo(p)?.dir).filter((d) => d !== undefined))
581
371
  const lacking = cargoCtx.lackingDependencies(bundledPackages, { buildScripts: cargoManifests })
@@ -587,90 +377,48 @@ export async function buildRustBundle({ cwd = process.cwd(), entries, cargo = fa
587
377
  console.warn(`[stasis] ${names.length} crate${names.length === 1 ? '' : 's'} referenced but not in the bundle: ${shown}`)
588
378
  if (!existsSync(join(baseDir, cargoCtx.vendorDir))) console.warn('[stasis] Registry dependencies are bundled only when vendored in-tree: run `cargo vendor` first.')
589
379
  }
590
-
591
380
  return assembleCodeBundle({
592
- baseDir,
593
- entries: normalized,
594
- sources,
595
- resolutions,
596
- workspaceName: RUST_WORKSPACE_NAME,
597
- workspaceVersion: RUST_WORKSPACE_VERSION,
598
- format: RUST_FORMAT,
599
- formats,
600
- conditionKey: 'rust',
381
+ baseDir, entries: normalized, sources, resolutions, workspaceName: 'rust-bundle', format: 'rust', formats, conditionKey: 'rust',
601
382
  classifyDep: makeRustClassifier(cargoCtx),
602
383
  })
603
384
  }
604
385
 
605
- // Build an in-memory Bundle from entry .php files by statically scanning the
606
- // require/include graph (and Composer's class-autoload graph when present); no PHP is
607
- // executed. Autoload resolution is best-effort; explicit require/include of a literal path
608
- // is strict, and a missing entry or unresolved explicit include is fatal.
386
+ // Scanned statically, no PHP executed: the require/include graph strictly, Composer's autoload best-effort.
609
387
  export async function buildPhpBundle({ cwd = process.cwd(), entries } = {}) {
610
- if (!Array.isArray(entries) || entries.length === 0) {
611
- throw new Error('buildPhpBundle: at least one entry .php file is required')
612
- }
613
- for (const e of entries) {
614
- if (!e.endsWith('.php')) throw new Error(`buildPhpBundle: not a .php file: ${e}`)
615
- }
616
-
388
+ assertEntries('buildPhpBundle', entries, '.php file', (e) => e.endsWith('.php'))
617
389
  const baseDir = resolve(cwd)
618
390
  const normalized = normalizeEntries(entries, cwd)
619
391
  const autoload = loadComposerAutoload(baseDir)
620
- // The Composer packages, of composer.lock (vendor/composer/installed.json held identical to it
621
- // where both are), or of installed.json alone: read once, before anything is scanned.
622
392
  const packages = loadComposerPackages(baseDir)
623
-
624
- // Laravel auto-discovers service providers rather than referencing them statically; seed
625
- // them as extra roots so their config/route/view files get bundled.
393
+ // Laravel auto-discovers service providers: seeded as roots so their config/route/view files come along.
626
394
  const providerRoots = loadLaravelProviderFiles(baseDir, autoload, packages)
627
-
628
395
  const sources = await collectPhpFilesFromDisk(baseDir, [...normalized, ...providerRoots], { autoload })
629
396
  const { resolutions, missing } = buildPhpTree(sources, { baseDir, autoload })
630
-
631
397
  assertSelfContained('PHP', 'import', normalized, sources, missing)
632
-
633
- return codeBundle({
634
- baseDir,
635
- entries: normalized,
636
- sources,
637
- // Group per Composer package (vendor/<pkg>), not the node_modules bucketizer.
638
- modules: bucketizePhpSources(baseDir, sources, PHP_WORKSPACE_NAME, PHP_WORKSPACE_VERSION, packages),
639
- resolutions,
640
- format: PHP_FORMAT,
641
- // PHP includes don't vary by Node condition; key edges under "php", not the JS wildcard "*".
642
- conditionKey: 'php',
643
- })
398
+ const modules = bucketizePhpSources(baseDir, sources, 'php-bundle', '0.0.0', packages)
399
+ return codeBundle({ baseDir, entries: normalized, sources, modules, resolutions, format: 'php', conditionKey: 'php' })
644
400
  }
645
401
 
646
- // Render a scanned file URL for diagnostics: project-relative when inside `baseDir`, else absolute.
647
- // The scan issue checks run on the LIVE scan, whose files/imports are keyed by absolute file: URLs
648
- // (the resolver returns absolute paths); relativizing here matches the project-relative paths the
649
- // bundle itself stores (toRel), rather than leaking the machine's absolute paths into the message.
650
402
  function displayPath(url, baseDir) {
651
403
  const abs = fileURLToPath(url)
652
404
  const rel = relative(baseDir, abs)
653
405
  return rel && !relativeEscapes(rel) ? rel : abs
654
406
  }
655
407
 
656
- // One unresolved scan edge for diagnostics, naming the file --typescript would resolve it to when
657
- // the scan found one (Scan#typescriptCandidate).
658
408
  function describeUnresolved(u, show) {
659
409
  const typescript = u.typescriptURL ? `; resolves to ${show(u.typescriptURL)} under --typescript` : ''
660
410
  return `${u.kind} ${u.spec ?? '<dynamic>'} from ${show(u.parentURL)} (${u.reason}${typescript})`
661
411
  }
662
412
 
663
- // Static ESM import/export-from edges load eagerly before user code, so a failure there is
664
- // uncatchable; one below a require()/dynamic-import() boundary is catchable.
413
+ // Static ESM edges load before user code, so a failure there is uncatchable, unlike one past a require()/import().
665
414
  const STATIC_KINDS = new Set(['import', 'export-from'])
666
415
 
667
- // The files the entries link eagerly, through static edges alone.
668
- function eagerlyLinked(scanner) {
669
- const linked = new Set(scanner.entries)
670
- const walk = [...scanner.entries]
671
- while (walk.length > 0) {
672
- for (const e of scanner.files.get(walk.shift())?.edges ?? []) {
673
- if (STATIC_KINDS.has(e.kind) && e.child && !linked.has(e.child)) {
416
+ function linkedThrough(scanner, roots, follows) {
417
+ const linked = new Set(roots)
418
+ const walk = [...linked]
419
+ for (let i = 0; i < walk.length; i++) {
420
+ for (const e of scanner.files.get(walk[i])?.edges ?? []) {
421
+ if (e.child && !linked.has(e.child) && follows(e)) {
674
422
  linked.add(e.child)
675
423
  walk.push(e.child)
676
424
  }
@@ -679,24 +427,14 @@ function eagerlyLinked(scanner) {
679
427
  return linked
680
428
  }
681
429
 
682
- // The scan within the project root (`within(url)`), since a bundle can't carry a file outside it.
683
- // One the entries link eagerly is returned in `outside`, for the caller to refuse. One reached
684
- // only past a require()/import() boundary is left out, with whatever only it reaches, and its edges
685
- // are returned in `leftOut`, misses that load can catch as it does an unresolved one: an optional
686
- // peer dependency's lazy require() reaching a copy hoisted above a root that a stasis file pins, say.
430
+ const eagerlyLinked = (scanner) => linkedThrough(scanner, scanner.entries, (e) => STATIC_KINDS.has(e.kind))
431
+
432
+ // A file outside the root (`within`) the entries link eagerly is `outside`, for the caller to refuse; one reached
433
+ // only past a require()/import() is left out with what only it reaches, its edges `leftOut`, misses load can catch.
687
434
  function withinRoot(scanner, within) {
688
435
  const outside = [...eagerlyLinked(scanner)].filter((url) => scanner.files.has(url) && !within(url))
689
436
  if (outside.length > 0) return { scanner, outside, leftOut: [] }
690
- const kept = new Set([...scanner.entries].filter((url) => scanner.files.has(url)))
691
- const walk = [...kept]
692
- while (walk.length > 0) {
693
- for (const e of scanner.files.get(walk.shift())?.edges ?? []) {
694
- if (e.child && scanner.files.has(e.child) && !kept.has(e.child) && within(e.child)) {
695
- kept.add(e.child)
696
- walk.push(e.child)
697
- }
698
- }
699
- }
437
+ const kept = linkedThrough(scanner, [...scanner.entries].filter((url) => scanner.files.has(url)), (e) => scanner.files.has(e.child) && within(e.child))
700
438
  if (kept.size === scanner.files.size) return { scanner, outside, leftOut: [] }
701
439
  const leftOut = []
702
440
  const files = new Map()
@@ -725,130 +463,83 @@ function withinRoot(scanner, within) {
725
463
  return { scanner: { entries: scanner.entries, files, imports, unresolved, parseErrors }, outside, leftOut }
726
464
  }
727
465
 
728
- // Refuse an edge one --metro platform resolves within the root and another leaves out (see
729
- // withinRoot): collapsed to the one target, it would serve that platform the other's file.
730
- function refuseSplit(split, baseDir) {
731
- for (const { parentURL, spec, outsideURL } of split) {
732
- throw new Error(
733
- `${spec} from ${displayPath(parentURL, baseDir)} resolves within the project root in one context and outside it, ` +
734
- `at ${relative(baseDir, fileURLToPath(outsideURL))}, in another: a bundle can't leave out the one without serving the other in its place`
735
- )
736
- }
466
+ const listed = (items, line = (s) => s) => {
467
+ const more = items.length > 10 ? `\n ... and ${items.length - 10} more` : ''
468
+ return items.slice(0, 10).map((x) => ` ${line(x)}`).join('\n') + more
737
469
  }
738
470
 
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).
742
- function analyzeScanner(scanner, { baseDir }) {
471
+ // `fatal`: broken at load, a parse error, or a miss `generated(u)` says a generator would write; else
472
+ // `tolerated`, a catchable runtime miss. `typescript`: whether a fatal miss resolves under --typescript.
473
+ function analyzeScanner(scanner, { baseDir, generated }) {
743
474
  const show = (url) => displayPath(url, baseDir)
744
475
  const staticReachable = eagerlyLinked(scanner)
745
- const fatalUnresolved = (u) => STATIC_KINDS.has(u.kind) && staticReachable.has(u.parentURL)
746
- const fatal = scanner.unresolved
747
- .filter((u) => fatalUnresolved(u))
748
- .map((u) => `unresolved ${describeUnresolved(u, show)}`)
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.
476
+ const fatal = []
477
+ const tolerated = []
478
+ let typescript = false
479
+ for (const u of scanner.unresolved) {
480
+ const why = generated?.(u)
481
+ if (why === undefined && !(STATIC_KINDS.has(u.kind) && staticReachable.has(u.parentURL))) tolerated.push(u)
482
+ else {
483
+ fatal.push(`unresolved ${describeUnresolved(u, show)}${why === undefined ? '' : `: ${why}`}`)
484
+ typescript ||= u.typescriptURL !== undefined
485
+ }
486
+ }
487
+ // A file the parser couldn't read cleanly has unknown or partial edges, never a graph to record.
751
488
  for (const p of scanner.parseErrors) fatal.push(`parse error in ${show(p.url)}: ${p.message}`)
752
- // Edge resolving to a file a source bundle can't carry (.node/.wasm/extensionless): scan
753
- // records it but never queues the child, so load would die.
754
- for (const [, byParent] of scanner.imports) {
489
+ // An edge to a file a source bundle can't carry (.node/.wasm/extensionless), which load would die on.
490
+ for (const byParent of scanner.imports.values()) {
755
491
  for (const [parentURL, specMap] of byParent) {
756
492
  for (const [spec, childURL] of specMap) {
757
- if (!scanner.files.has(childURL)) {
758
- fatal.push(`${spec} from ${show(parentURL)} resolves to ${show(childURL)}, which a source bundle can't carry`)
759
- }
493
+ if (!scanner.files.has(childURL)) fatal.push(`${spec} from ${show(parentURL)} resolves to ${show(childURL)}, which a source bundle can't carry`)
760
494
  }
761
495
  }
762
496
  }
763
- const tolerated = scanner.unresolved.filter((u) => !fatalUnresolved(u))
764
- return { fatal, tolerated }
497
+ return { fatal, tolerated, typescript }
765
498
  }
766
499
 
767
- // Throw on fatal scan issues; warn on tolerated ones. `label` tags the scan pass; `baseDir`
768
- // relativizes the paths in the warnings (see displayPath), matching analyzeScanner's messages.
769
- function reportScanIssues({ fatal, tolerated, leftOut = [] }, { label = '', baseDir, root = '' }) {
500
+ function reportScanIssues({ fatal, tolerated, typescript, leftOut }, { label = '', baseDir, root }) {
770
501
  const where = label ? ` (${label})` : ''
771
502
  const show = (url) => displayPath(url, baseDir)
772
- // The first 10 of `items` as indented lines (`line` renders one), then how many more there are.
773
- const listed = (items, line = (s) => s) => {
774
- const more = items.length > 10 ? `\n ... and ${items.length - 10} more` : ''
775
- return items.slice(0, 10).map((x) => ` ${line(x)}`).join('\n') + more
776
- }
777
503
  if (fatal.length > 0) {
778
- throw new Error(`JS bundle would be broken at load time${where}:\n${listed(fatal)}`)
504
+ throw Object.assign(new Error(`JS bundle would be broken at load time${where}:\n${listed(fatal)}`), { resolvesUnderTypescript: typescript })
779
505
  }
780
506
  if (tolerated.length > 0) {
781
507
  const summary = listed(tolerated, (u) => describeUnresolved(u, show))
782
508
  console.warn(`[stasis] Bundle has ${tolerated.length} unresolved import(s)${where}; they will fall through at load time:\n${summary}`)
783
509
  }
784
510
  if (leftOut.length > 0) {
785
- // The file outside the root by its path from the cwd, as the refusal of one linked eagerly names it.
786
511
  const summary = listed(leftOut, (u) => `${u.kind} ${u.spec} from ${show(u.parentURL)} (at ${relative(baseDir, fileURLToPath(u.outsideURL))})`)
787
512
  console.warn(`[stasis] Bundle leaves out ${leftOut.length} import(s) of files outside the project root${root}${where}; they will fall through at load time:\n${summary}`)
788
513
  }
789
514
  }
790
515
 
791
- // Normalize conditions (trim, drop empties) so a sloppy programmatic caller can't push a bogus
792
- // token into the resolver.
516
+ // Trimmed, empties dropped, so a sloppy programmatic caller can't push a bogus token into the resolver.
793
517
  const cleanConditions = (conditions) => conditions.map((c) => (typeof c === 'string' ? c.trim() : c)).filter(Boolean)
794
518
 
795
- // --typescript honours tsconfig `paths` aliases: an explicit --tsconfig's for every file (it must
796
- // exist), else those of the tsconfig.json beside each importing file's package.json
797
- // (packageTsconfigPaths, the project read as the State reads it, to `env`'s PROJECT_CWD; null
798
- // matcher = no aliases). Without --typescript they only hint a miss (Scan#typescriptCandidate), so a
799
- // config that doesn't load is none.
519
+ // Without --typescript the `paths` only hint a miss, so a tsconfig that doesn't load is none.
800
520
  function typescriptPathsFor(typescript, baseDir, tsconfig, host, env) {
801
- if (typescript && tsconfig !== undefined) return loadTsconfigPaths(discoverTsconfig(baseDir, tsconfig, host), host)
521
+ if (typescript && tsconfig !== undefined) {
522
+ const matcher = loadTsconfigPaths(discoverTsconfig(baseDir, tsconfig, host), host)
523
+ return { matchPaths: (spec) => matcher?.matchPaths(spec) ?? [], outputs: tsconfigOutputsOf({ host }) }
524
+ }
802
525
  return packageTsconfigPaths(baseDir, { host, lenient: !typescript, projectCwd: env.PROJECT_CWD })
803
526
  }
804
527
 
805
- // Build a JS/TS Bundle (in-memory) by statically scanning the require/import graph; no
806
- // user code is executed, and TS is stored verbatim (Node strips types at load). Scope comes
807
- // from stasis.config.json / `EXODUS_STASIS_SCOPE` unless `scope` overrides. `conditions` are
808
- // extra `exports`/`imports` conditions; on their own they don't honour legacy mainFields or
809
- // platform suffixes (see `--mainFields` / buildResolvedJsBundle). `typescript` maps a failed
810
- // resolution to its on-disk TS source (tsc's rules; see resolve-typescript.js), honouring the
811
- // `paths` aliases of `tsconfig` (an explicit config path, default each package's tsconfig.json); it
812
- // is taken as given where the entries import TS sources by output name (importsTypescriptByOutputName).
813
- // `define` gives globals their values, a branch they keep from running reaching nothing (define.js).
814
- // Files are read through `host` (@exodus/stasis-core/host), the disk by default; EXODUS_STASIS_*
815
- // settings from `env`. With `innermostRoot`, the State is rooted at the innermost package at or
816
- // above cwd that holds every file the scan reaches (innermostRootOf), rather than at the project's
817
- // root above it, where that holds no stasis file of its own. With `extra`, the files its
818
- // stasis.config.json's `extra` names are added as `stasis run` adds them (State#includeExtra), never
819
- // one of `outputsAt(root)` (the absolute paths the build writes, given the root its paths are
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 } = {}) {
825
- if (!Array.isArray(entries) || entries.length === 0) {
826
- throw new Error('buildJsBundle: at least one entry .js/.cjs/.mjs/.ts/.cts/.mts/.jsx/.tsx file is required')
827
- }
828
- for (const e of entries) {
829
- if (!JS_EXTS.has(extname(e))) throw new Error(`buildJsBundle: not a JS/TS file: ${e}`)
830
- }
831
-
528
+ // Through a State, the graph scanned statically: no user code runs. `memo` keeps scans across a lazy tree's rounds.
529
+ 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, generated } = {}) {
530
+ assertEntries('buildJsBundle', entries, '.js/.cjs/.mjs/.ts/.cts/.mts/.jsx/.tsx file', (e) => JS_EXTENSIONS.has(extname(e)), 'JS/TS file')
832
531
  const baseDir = resolve(cwd)
833
532
  const absEntries = entries.map((e) => resolve(baseDir, e))
834
-
835
533
  const scanConditions = cleanConditions(conditions)
836
- // --resources: extensions/filenames carried as opaque assets instead of failing "can't carry".
837
534
  const resourceSet = parseResourcesOption('buildJsBundle', resources)
838
-
839
535
  typescript ||= importsTypescriptByOutputName(absEntries, host)
840
536
  const typescriptPaths = typescriptPathsFor(typescript, baseDir, tsconfig, host, env)
841
-
842
- const scanned = scan(absEntries, { conditions: scanConditions, jsx, flow, typescript, typescriptPaths, resources: resourceSet, define, host, memo: memo ? nestedMap(memo, 'js') : null })
537
+ const scanned = scan(absEntries, { conditions: scanConditions, jsx, flow, typescript, typescriptPaths, resources: resourceSet, define, host, memo: memo ? nestedMap(memo, 'js') : null, missTargets: generated !== undefined })
843
538
  // A round of a lazy tree that asked for a package already is void: the rest is moot.
844
539
  if (memo && host.touches() > 0) throw new Error('buildJsBundle: this round reads a package not unpacked yet')
845
-
846
- // Materialise via a non-preload State: addFile bucketizes + records sources/formats,
847
- // addImport replays the edge map; serialize emits the runtime loader's v1 layout.
848
- // bundle:'replace' skips reading any on-disk stasis.code.br (bundle:'add' would leak stale
849
- // entries); lock:'ignore' tolerates a pre-existing lockfile without consuming it; and it is never
850
- // written, so it claims no write target.
851
- const stateOptions = { bundle: 'replace', lock: 'ignore', ...(scope ? { scope } : {}), host, claim: false }
540
+ // bundle:'replace' reads no stasis.code.br on disk; lock:'ignore' leaves the lockfile to heldToLockfile;
541
+ // never written, the State claims nothing, and assertPolicies checks the bundle.
542
+ const stateOptions = { bundle: 'replace', lock: 'ignore', ownLock: true, ...(scope && { scope }), host, claim: false, checkPolicy: false }
852
543
  let state = new State(baseDir, { ...stateOptions, env })
853
544
  // A root holding a stasis file is the project's own choice, whose config the build takes.
854
545
  if (innermostRoot && !STASIS_ROOT_FILES.some((file) => host.stat(join(state.root, file)) !== null)) {
@@ -856,36 +547,15 @@ export async function buildJsBundle({ cwd = process.cwd(), env = process.env, en
856
547
  // The State's walk up from cwd stops at PROJECT_CWD, as it does where yarn sets it.
857
548
  if (root !== state.root) state = new State(baseDir, { ...stateOptions, env: { ...env, PROJECT_CWD: root } })
858
549
  }
859
- // Only what lies within the root is carried (see withinRoot). One the entries link eagerly is
860
- // refused, named here rather than as the State's bare assertion: a stasis file in a directory below
861
- // the project's root (a stray stasis.code.br there, say) roots the State at it, out of the files above.
862
550
  const { scanner, outside, leftOut } = withinRoot(scanned, (url) => isPathWithin(state.root, fileURLToPath(url)))
863
- const markers = STASIS_ROOT_FILES.filter((name) => host.stat(join(state.root, name)) !== null)
864
- const rootedBy = markers.length === 0 ? '' : ` (rooted there by its ${markers.join(' and ')}, which can be removed if stale)`
865
- const root = ` at ${relative(baseDir, state.root) || '.'}${rootedBy}`
866
- for (const url of outside) {
867
- throw new Error(`buildJsBundle: ${relative(baseDir, fileURLToPath(url))} is outside the project root${root}`)
868
- }
869
- // Fail closed where the bundle is guaranteed broken at load; warn on catchable misses (see analyzeScanner).
870
- reportScanIssues({ ...analyzeScanner(scanner, { baseDir }), leftOut }, { baseDir, root })
551
+ const root = ` at ${relative(baseDir, state.root) || '.'}${rootedBy(state.root, host, ', which can be removed if stale')}`
552
+ for (const url of outside) throw new Error(`buildJsBundle: ${relative(baseDir, fileURLToPath(url))} is outside the project root${root}`)
553
+ reportScanIssues({ ...analyzeScanner(scanner, { baseDir, generated }), leftOut }, { baseDir, root })
871
554
  for (const [url, info] of scanner.files) {
872
- // A resource carries bytes only: addFile derives resource vs resource:base64 from the content
873
- // and stores it under `resources` (a resource can't be an entry, so no isEntry).
874
- // Recorded as the `bundle` consumer's, which `extra` files are not.
875
- if (info.resource) {
876
- state.addFile(url, { resource: true, reason: 'bundle' })
877
- continue
878
- }
879
- const isEntry = scanner.entries.has(url)
880
- state.addFile(url, { format: info.format, isEntry, reason: 'bundle' })
881
- }
882
- // Edges where every context agrees keep the wildcard '*' key (getImport's fallback when a
883
- // condition lookup misses). Where the require()- and import()-context resolutions of one
884
- // (parent, specifier) DIVERGE, each target keeps its real condition key -- one '*' entry
885
- // would serve one context the other's file; an unmatched set fails closed via resolveBundled.
886
- // So does one that a context resolves and another misses or leaves out (an `exports` map with
887
- // a `require` branch alone, say): getImport serves no context the other's key where conditions
888
- // decide the resolution, so the miss stays one, as under Node.
555
+ state.addFile(url, info.resource ? { resource: true, reason: 'bundle' } : { format: info.format, isEntry: scanner.entries.has(url), reason: 'bundle' })
556
+ }
557
+ // An edge every context resolves alike is keyed '*'; one they resolve apart, misses or leave out keeps each
558
+ // target under its own conditions, so no context is served another's file.
889
559
  const missed = new Set([...scanner.unresolved, ...leftOut].filter((u) => u.spec !== undefined).map((u) => `${u.parentURL}\0${u.spec}`))
890
560
  const byParent = new Map()
891
561
  for (const [key, parents] of scanner.imports) {
@@ -899,28 +569,24 @@ export async function buildJsBundle({ cwd = process.cwd(), env = process.env, en
899
569
  if (targets.size === 1 && !missed.has(`${parentURL}\0${spec}`)) {
900
570
  state.addImport(parentURL, spec, [...targets][0], { conditions: '*' })
901
571
  } else {
902
- for (const [key, childURL] of byKey) {
903
- state.addImport(parentURL, spec, childURL, { conditions: key.split(', ') })
904
- }
572
+ for (const [key, childURL] of byKey) state.addImport(parentURL, spec, childURL, { conditions: key.split(', ') })
905
573
  }
906
574
  }
907
575
  }
908
- // Its own targets are none of the State's: it writes nothing.
909
576
  if (extra) state.includeExtra({ outputs: outputsAt(state.root), resources: resources === undefined ? undefined : resourceSet })
910
- // --package-json, or the `packageJSON` of the stasis.config.json the State took, as `stasis run` takes
911
- // it: fold each bundled module's package.json into the State's buckets (idempotent -- no-ops any
912
- // manifest the scan already reached). State's bundle=replace makes writeBundle true, so addFile accepts them.
913
577
  if (packageJSON || state.config.packageJSON) state.includePackageJson()
914
578
  return state
915
579
  }
916
580
 
917
581
  // The files that root a State in the directory holding one (state.js's discovery).
918
- const STASIS_ROOT_FILES = ['stasis.config.json', 'stasis.lock.json', DEFAULT_BUNDLE_FILE]
582
+ const STASIS_ROOT_FILES = [CONFIG_FILE, LOCK_FILE, BUNDLE_FILE]
919
583
 
920
- // The innermost directory at or above `baseDir`, up to `root`, holding every one of `files` (real
921
- // paths) and a package.json with a name, which a State can be rooted at; `root` where none below it
922
- // does. One without a name, such as a `{"type":"module"}` marker, is no root: the State takes such a
923
- // file's package to be the named one above it, which it then must hold.
584
+ function rootedBy(dir, host = diskHost, hint = '') {
585
+ const markers = STASIS_ROOT_FILES.filter((name) => host.stat(join(dir, name)) !== null)
586
+ return markers.length === 0 ? '' : ` (rooted there by its ${markers.join(' and ')}${hint})`
587
+ }
588
+
589
+ // A package.json with no name (a `{"type":"module"}` marker) roots no State.
924
590
  function innermostRootOf(baseDir, files, root, host) {
925
591
  for (let dir = baseDir; dir !== root && dirname(dir) !== dir; dir = dirname(dir)) {
926
592
  if (files.every((file) => isPathWithin(dir, file)) && readPackageJson(dir, 'package.json', { host })?.name !== undefined) return dir
@@ -928,123 +594,86 @@ function innermostRootOf(baseDir, files, root, host) {
928
594
  return root
929
595
  }
930
596
 
931
- // Extensions probed when a resolved target names none, in Metro's default sourceExts order.
932
- // Limited to what a source bundle can carry (scan's RESOLVABLE_EXTS) so the resolver never
933
- // resolves a file the bundle would reject; an extensionless `import './Foo'` can land on
934
- // Foo.jsx/Foo.tsx, as it does under Metro. Like Metro's default, this doesn't track a project's
935
- // own sourceExts.
936
- const SOURCE_EXTS = METRO_SOURCE_EXTS
937
- // React Native preset mainFields for `--metro` (which also sets the RN conditions + platform suffixes).
938
597
  const METRO_MAIN_FIELDS = ['react-native', 'browser', 'main']
939
598
 
940
- // The config files a React Native app's build loads from its project root, though nothing the app
941
- // imports reaches them: Metro's and the React Native CLI's, and with --babel(-aliases) Babel's.
599
+ // What a React Native app's build loads from its root, though nothing the app imports reaches it.
942
600
  const METRO_CONFIG_FILE = 'metro.config.js'
943
601
  const METRO_CONFIG_FILES = [METRO_CONFIG_FILE, 'react-native.config.js']
602
+ const RN_CONFIG_FILE = 'rn.config.json'
944
603
  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`).
604
+ // What else a React Native app's build runs in Node: each package `name` as the packages `via` find it in turn
605
+ // (else fewer of them, down to the project), and `file` in it, else its entry, as React Native's CLI runs Metro.
951
606
  const METRO_VIA = ['react-native', '@react-native/community-cli-plugin', 'metro']
952
607
  const METRO_TOOLCHAIN = [
953
- // React Native's CLI, which an app runs Metro through.
954
608
  { name: 'react-native', file: 'cli.js', via: [] },
955
609
  { 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.
610
+ // Loaded by a path: Metro's worker, its transform worker, jest-worker's child process and the Haste worker.
959
611
  { name: 'metro', file: 'src/DeltaBundler/Worker.js', via: METRO_VIA.slice(0, -1) },
960
612
  { name: 'metro-transform-worker', via: METRO_VIA },
961
613
  { name: 'jest-worker', file: 'build/workers/processChild.js', via: METRO_VIA },
962
614
  { 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 = {}`).
615
+ // metro-config's default `minifierPath`, which the transform worker requires.
966
616
  { name: 'metro-minify-terser', via: [...METRO_VIA, 'metro-transform-worker'] },
967
617
  ]
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.
618
+ // With --babel(-aliases), the `babelTransformerPath` React Native's Metro config names.
971
619
  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().
620
+ // Beside stasis's Metro plugin (src/metro.js), run by Metro's workers under withStasis().
974
621
  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
622
  const NODE_REQUIRE_CONDITIONS = new Set(['require', 'node', 'node-addons', 'module-sync'])
977
623
  const REQUIRE_KEY = [...NODE_REQUIRE_CONDITIONS].join(', ')
978
- // What esbuild and webpack 5 both assert on every platform besides `import`/`require` and
979
- // `default`, and so `--mainFields` too: `module`, the bundler-only condition a package's ESM build
980
- // sits under, from an import and a require alike. (esbuild drops it when given conditions of its
981
- // own; webpack keeps it, as `--conditions` adds to it here.) Node and Metro never assert it.
624
+ // What esbuild and webpack 5 both assert besides `import`/`require` and `default`, which Node and Metro never do.
982
625
  const BUNDLER_CONDITIONS = ['module']
983
626
 
984
- // The built-in field/suffix resolver (resolve-fields.js) the legacy-field build resolves with on
985
- // `platform` (null for --mainFields), its `mainFields` (Metro's under --metro), and the conditions
986
- // it adds to the bundler base (`import`/`require` + `default`, never Node's `node`; see
987
- // resolveConditions): --metro asserts the RN conditions (+ browser on web), Metro's own set;
988
- // --mainFields asserts esbuild's and webpack's `module` (BUNDLER_CONDITIONS) and the user's --conditions.
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 }) {
627
+ export function fieldResolverFor(platform, { mainFields, metro = false, assetExts, sourceExts = METRO_SOURCE_EXTS, assetResolutions, conditions = [], typescript = false, typescriptPaths = null, host = diskHost, kept }) {
991
628
  const extras = metro ? ['react-native', ...(platform === 'web' ? ['browser'] : [])] : [...new Set([...BUNDLER_CONDITIONS, ...conditions])]
992
629
  const fields = metro ? METRO_MAIN_FIELDS : mainFields
993
630
  const resolver = createFieldResolver({
994
- mainFields: fields,
995
- platform,
996
- preferNative: platform !== null && platform !== 'web',
997
- sourceExts: SOURCE_EXTS,
998
- conditions: resolveConditions('commonjs', extras),
999
- // Opt into Metro's package-entry browser-field quirks only on the --metro path.
1000
- metro,
1001
- assetExts,
1002
- // --typescript: tsc's mapping, inside the field resolver (the scanner's own fallback
1003
- // only backs the built-in resolver). Unreachable under --metro-resolver
1004
- // (classifyEntries rejects the combination -- metro-resolver can't substitute).
1005
- typescript,
1006
- typescriptPaths,
1007
- host,
1008
- kept,
631
+ mainFields: fields, platform, preferNative: platform !== null && platform !== 'web', sourceExts, conditions: resolveConditions('commonjs', extras),
632
+ metro, assetExts, assetResolutions, typescript, typescriptPaths, host, kept,
1009
633
  })
1010
634
  return { extras, mainFields: fields, resolver }
1011
635
  }
1012
636
 
1013
- const CONFIG_FILE = 'stasis.config.json'
1014
-
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) {
1017
- const text = readFileSyncMaybe(resolve(cwd), CONFIG_FILE, 'utf8', host)
637
+ function parseJson(text, message) {
1018
638
  try {
1019
- return text === null ? null : JSON.parse(text)
639
+ return JSON.parse(text)
1020
640
  } catch (cause) {
1021
- throw new Error(`${name === undefined ? '' : `${name}: `}${CONFIG_FILE} is not valid JSON`, { cause })
641
+ throw new Error(message, { cause })
1022
642
  }
1023
643
  }
1024
644
 
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)
645
+ export function readConfigFile(name, cwd, host = diskHost) {
646
+ const text = readFileSyncMaybe(resolve(cwd), CONFIG_FILE, 'utf8', host)
647
+ return text === null ? null : parseJson(text, `${name === undefined ? '' : `${name}: `}${CONFIG_FILE} is not valid JSON`)
648
+ }
1028
649
 
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)
650
+ // Each build held to its entries' Policy, `bundle` to the root's; links followed through `files`, the builds' host.
651
+ function assertPolicies(name, { builds = [], builts = [], bundle, root, host = diskHost, files = host, label = CONFIG_FILE }) {
652
+ const links = linksOn(root, files)
653
+ for (const [i, build] of builds.entries()) build.policy?.assertArtifact(builts[i].bundle, { links })
654
+ parsePolicy(label, readConfigFile(name, root, host))?.assertArtifact(bundle, { links })
655
+ }
656
+
657
+ // The stasis.config.json `key` in `cwd`, true where it is left out.
658
+ function configFlag(key, name, cwd, host) {
659
+ const value = readConfigFile(name, cwd, host)?.[key]
660
+ if (value === undefined) return true
661
+ if (typeof value !== 'boolean') throw new TypeError(`${CONFIG_FILE}: ${key} must be true or false, got ${JSON.stringify(value)}`)
662
+ return value
663
+ }
1032
664
 
1033
- // --babel-aliases: the alias map of the JSON file `file` (a path from `baseDir`, in the project, read through
1034
- // `host`), babel-plugin-module-resolver's `alias` option as data: each alias to the string it maps to.
1035
- function readAliasesFile(baseDir, file, host = diskHost) {
665
+ export const installsDevDependencies = (name, cwd, host) => configFlag('installDevDependencies', name, cwd, host)
666
+
667
+ export const configuredLockfiles = (name, cwd, host) => parseLockfilesOption(`${name}: ${CONFIG_FILE}`, readConfigFile(name, cwd, host)?.lockfiles)
668
+
669
+ // --babel-aliases: babel-plugin-module-resolver's `alias` option as data, from a JSON file in the project.
670
+ function readAliasesFile(baseDir, file, host) {
1036
671
  const rel = relative(baseDir, resolve(baseDir, file))
1037
672
  if (relativeEscapes(rel)) throw new Error(`stasis bundle: --babel-aliases must name a file in the project: ${file}`)
1038
673
  const text = readFileSyncMaybe(baseDir, toPosix(rel), 'utf8', host)
1039
674
  if (text === null) throw new Error(`stasis bundle: --babel-aliases file not found: ${file}`)
1040
- // A link to a file out of the project is refused too.
1041
675
  assertRealPathWithinBase(host.realpath(baseDir), baseDir, toPosix(rel), host)
1042
- let alias
1043
- try {
1044
- alias = JSON.parse(text)
1045
- } catch (cause) {
1046
- throw new Error(`stasis bundle: --babel-aliases file ${file} is not valid JSON`, { cause })
1047
- }
676
+ const alias = parseJson(text, `stasis bundle: --babel-aliases file ${file} is not valid JSON`)
1048
677
  if (!isPlainObject(alias) || !Object.values(alias).every((value) => typeof value === 'string')) {
1049
678
  throw new Error(`stasis bundle: --babel-aliases file ${file} must be an object mapping each alias to a string`)
1050
679
  }
@@ -1059,13 +688,8 @@ function readAliasesFile(baseDir, file, host = diskHost) {
1059
688
  return alias
1060
689
  }
1061
690
 
1062
- // The `scope`, `resources` and `packageJSON` of a --mainFields/--metro build: as given (an empty
1063
- // `resources` too, for no allowlist; `packageJSON` true), else as the stasis.config.json in `cwd`
1064
- // sets them (the directory these bundles' paths are relative to, and where one is written by
1065
- // default). The field resolver builds a full-scope bundle alone, so a narrower scope, from either,
1066
- // is refused rather than silently ignored.
1067
- // -> { resources: Set<string> (parseResourcesOption's), packageJSON: boolean }
1068
- function fieldBuildOptions(name, { cwd = process.cwd(), scope, resources, packageJSON = false, host = diskHost }) {
691
+ // A --mainFields/--metro build is full-scope alone, so a narrower scope is refused rather than silently ignored.
692
+ function fieldBuildOptions(name, { cwd, scope, resources, packageJSON = false, host }) {
1069
693
  const resourcesGiven = resources !== undefined
1070
694
  const config = scope === undefined || !resourcesGiven || !packageJSON ? readConfigFile(name, cwd, host) : null
1071
695
  if (scope !== undefined && scope !== 'full') {
@@ -1083,14 +707,10 @@ function fieldBuildOptions(name, { cwd = process.cwd(), scope, resources, packag
1083
707
  }
1084
708
  }
1085
709
 
1086
- // Metro's own empty module, which it resolves a disabled import to (its default
1087
- // resolver.emptyModulePath), where the project installs metro-runtime at its root.
710
+ // Metro's default resolver.emptyModulePath, which it resolves a disabled import to.
1088
711
  const METRO_EMPTY_MODULE = 'node_modules/metro-runtime/src/modules/empty-module.js'
1089
- // An empty module's source, trimmed: nothing, or a lone "use strict" directive.
1090
712
  const EMPTY_MODULE_SOURCE = /^(?:(["'])use strict\1;?)?$/u
1091
713
 
1092
- // The real path of METRO_EMPTY_MODULE under `baseDir`, where it is a file in the project that is in
1093
- // fact empty (EMPTY_MODULE_SOURCE), else null.
1094
714
  function metroEmptyModule(baseDir, host) {
1095
715
  const abs = join(baseDir, METRO_EMPTY_MODULE)
1096
716
  if (!host.stat(abs)?.isFile()) return null
@@ -1100,25 +720,20 @@ function metroEmptyModule(baseDir, host) {
1100
720
  return isUtf8(buf) && EMPTY_MODULE_SOURCE.test(buf.toString('utf8').trim()) ? real : null
1101
721
  }
1102
722
 
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
723
+ // Through each of `via` first, for an install (pnpm) that links `name` only beside the one depending on it.
1106
724
  function packageDirFrom(dir, name, host, via = []) {
1107
725
  let from = dir
1108
726
  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
727
+ let d = from
728
+ while (!host.stat(join(d, 'node_modules', step, 'package.json'))?.isFile()) {
729
+ if (dirname(d) === d) return undefined
730
+ d = dirname(d)
1114
731
  }
1115
- from = found
732
+ from = host.realpath(join(d, 'node_modules', step))
1116
733
  }
1117
734
  return from
1118
735
  }
1119
736
 
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
737
  function requireFrom(dir, spec, host) {
1123
738
  try {
1124
739
  return host.resolve(join(dir, 'package.json'), spec, NODE_REQUIRE_CONDITIONS)
@@ -1127,10 +742,8 @@ function requireFrom(dir, spec, host) {
1127
742
  }
1128
743
  }
1129
744
 
1130
- // The AST of the module `file`, read through `host` (`source` where read already).
1131
745
  const parseModule = (file, host, source = host.readFile(file).toString('utf8')) => getParser().parseSync(file, source, { sourceType: 'unambiguous' }).program
1132
746
 
1133
- // Calls `fn` on each node of the AST `node`, depth first.
1134
747
  function walkAst(node, fn) {
1135
748
  if (Array.isArray(node)) {
1136
749
  for (const child of node) walkAst(child, fn)
@@ -1141,20 +754,14 @@ function walkAst(node, fn) {
1141
754
  for (const key of Object.keys(node)) if (key !== 'type') walkAst(node[key], fn)
1142
755
  }
1143
756
 
1144
- // The method `node` calls on an object (`x.concat(...)` -> 'concat'), on the one named `object` where
1145
- // given (`require.resolve(...)`), or undefined.
757
+ // The method `node` calls (`x.concat(...)` -> 'concat'), on the object named `object` where given.
1146
758
  function memberCall(node, object) {
1147
759
  if (node?.type !== 'CallExpression' || node.callee.type !== 'MemberExpression' || node.callee.computed) return undefined
1148
760
  if (object !== undefined && (node.callee.object.type !== 'Identifier' || node.callee.object.name !== object)) return undefined
1149
761
  return node.callee.property.name
1150
762
  }
1151
763
 
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
764
+ // What `file` names by `require.resolve('x')`, read off its source, never run. -> [specifier, path][]
1158
765
  function requireResolutions(file, host) {
1159
766
  const resolved = []
1160
767
  const source = host.readFile(file).toString('utf8')
@@ -1168,10 +775,7 @@ function requireResolutions(file, host) {
1168
775
  return resolved
1169
776
  }
1170
777
 
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.
778
+ // The name Babel requires a plugin or preset by, as @babel/core's standardizeName has it.
1175
779
  const BABEL_PREFIX = { plugin: /^(?!@|module:|[^/]+\/|babel-plugin-)/u, preset: /^(?!@|module:|[^/]+\/|babel-preset-)/u }
1176
780
  const BABEL_ORG = { plugin: /^(@babel\/)(?!plugin-|[^/]+\/)/u, preset: /^(@babel\/)(?!preset-|[^/]+\/)/u }
1177
781
  const OTHER_ORG = { plugin: /^(@(?!babel\/)[^/]+\/)(?![^/]*babel-plugin(?:-|\/|$)|[^/]+\/)/u, preset: /^(@(?!babel\/)[^/]+\/)(?![^/]*babel-preset(?:-|\/|$)|[^/]+\/)/u }
@@ -1183,25 +787,25 @@ const standardizeBabelName = (type, name) => (isAbsolute(name) ? name : name
1183
787
  .replace(OTHER_ORG_DEFAULT, `$1/babel-${type}`)
1184
788
  .replace(/^module:/u, ''))
1185
789
 
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`.
790
+ // A string naming a Babel plugin or preset by its form, a package or a path.
1189
791
  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).
792
+ // `babel` and `config` in its file's name (create-babel-config.js), or its package's (@org/babel-config).
1192
793
  const BABEL_CONFIG_NAME = /babel.*config|config.*babel/iu
1193
794
  const isBabelConfigModule = (rel) => BABEL_CONFIG_NAME.test(posix.basename(rel)) || BABEL_CONFIG_NAME.test(splitNodeModulesPath(rel)?.name ?? '')
1194
795
 
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.
796
+ // The name of a key, variable or object holding a list -> what an entry of it is taken as.
1197
797
  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
798
  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`.
799
+ // The array methods making a list of the one they're called on (`concat` with its arguments').
1202
800
  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
801
+ const MAX_LIST_DEPTH = 8
802
+ function flatDepth(arg) {
803
+ if (arg === undefined) return 1
804
+ if (arg.type === 'Identifier' && arg.name === 'Infinity') return MAX_LIST_DEPTH
805
+ return arg.type === 'Literal' && typeof arg.value === 'number' ? arg.value : 1
806
+ }
807
+
808
+ // `__dirname`, a string, or a `path.resolve(...)` or `path.join(...)` of them. -> the path, or null
1205
809
  function staticPath(node, file) {
1206
810
  if (node?.type === 'Identifier' && node.name === '__dirname') return dirname(file)
1207
811
  const string = literalSpec(node)
@@ -1213,10 +817,7 @@ function staticPath(node, file) {
1213
817
  return name === 'resolve' && parts.some((part) => isAbsolute(part)) ? resolve(...parts) : join(...parts)
1214
818
  }
1215
819
 
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
820
+ // A staticPath, as Babel takes a plugin named by path, or what `require.resolve('x')` resolves to. -> it, or null
1220
821
  function babelPathCall(node, file, host) {
1221
822
  if (memberCall(node, 'require') !== 'resolve') {
1222
823
  const path = node?.type === 'CallExpression' ? staticPath(node, file) : null
@@ -1238,28 +839,23 @@ function babelPathCall(node, file, host) {
1238
839
  return null
1239
840
  }
1240
841
 
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)`.
842
+ // `entry(type, node)` on each entry (or the first of a `[name, options]` pair) of the `lists` in `program`, read
843
+ // through spreads, variables, branches and LIST_METHODS, never run.
1246
844
  function readLists(program, lists, entry) {
1247
- // Each variable's declarators with an initializer, by its name.
1248
845
  const declarators = []
1249
846
  walkAst(program, (node) => {
1250
847
  if (node.type === 'VariableDeclarator' && node.id.type === 'Identifier' && node.init) declarators.push(node)
1251
848
  })
1252
849
  const inits = Map.groupBy(declarators, (node) => node.id.name)
1253
850
  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.
851
+ // `depth` levels of arrays in `node` list (2: one `.flat()` flattens), and an array below them is an entry's
852
+ // `[name, options]` pair.
1257
853
  const read = (node, type, depth) => {
1258
854
  if (!node || depth > MAX_LIST_DEPTH) return
1259
855
  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)
856
+ const keys = upsert(seen, node, () => new Set())
857
+ if (keys.has(key)) return
858
+ keys.add(key)
1263
859
  if (node.type === 'ArrayExpression') {
1264
860
  if (depth === 0) return read(node.elements[0], type, 0)
1265
861
  for (const element of node.elements) {
@@ -1271,8 +867,6 @@ function readLists(program, lists, entry) {
1271
867
  } else if (node.type === 'ConditionalExpression' || node.type === 'LogicalExpression') {
1272
868
  for (const side of node.type === 'LogicalExpression' ? [node.left, node.right] : [node.consequent, node.alternate]) read(side, type, depth)
1273
869
  } 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
870
  const method = memberCall(node)
1277
871
  const levels = method === 'flat' ? flatDepth(node.arguments[0]) : 0
1278
872
  read(node.callee.object, type, Math.min(depth + levels, MAX_LIST_DEPTH))
@@ -1283,7 +877,7 @@ function readLists(program, lists, entry) {
1283
877
  }
1284
878
  const listName = (node) => (node.type === 'Identifier' ? node.name : node.type === 'MemberExpression' && !node.computed ? node.property.name : undefined)
1285
879
  walkAst(program, (node) => {
1286
- // A list: under a key, held in a variable or assigned to a property, so named.
880
+ // A list under a key, held in a variable or assigned to a property, so named.
1287
881
  const [name, value] = node.type === 'Property' && !node.computed ? [node.key.name ?? node.key.value, node.value]
1288
882
  : node.type === 'VariableDeclarator' ? [listName(node.id), node.init]
1289
883
  : node.type === 'AssignmentExpression' ? [listName(node.left), node.right]
@@ -1298,10 +892,7 @@ function readLists(program, lists, entry) {
1298
892
  })
1299
893
  }
1300
894
 
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
895
+ // The plugins and presets `file` lists, as Babel requires them; with `anyString`, any BABEL_MODULE_STRING too.
1305
896
  function babelNamedModules(file, host, { anyString = false } = {}) {
1306
897
  const program = parseModule(file, host)
1307
898
  const named = new Set()
@@ -1320,13 +911,10 @@ function babelNamedModules(file, host, { anyString = false } = {}) {
1320
911
  return named
1321
912
  }
1322
913
 
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) {
914
+ // Metro's default assetExts and each the Metro config `file` lists, read off its source (one it filters out too).
915
+ function metroAssetExts(baseDir, host, file) {
1328
916
  const exts = new Set(METRO_ASSET_EXTS)
1329
- const config = join(baseDir, METRO_CONFIG_FILE)
917
+ const config = resolve(baseDir, file)
1330
918
  if (!host.stat(config)?.isFile()) return exts
1331
919
  readLists(parseModule(config, host), METRO_ASSET_LISTS, (_, node) => {
1332
920
  const ext = literalSpec(node)
@@ -1335,24 +923,79 @@ function metroAssetExts(baseDir, host) {
1335
923
  return exts
1336
924
  }
1337
925
 
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
926
+ // rn.config.json's options stasis reads, each [valid, what], as @preventive/rn's rn-config.js checks them.
927
+ const listOf = (valid) => (value) => Array.isArray(value) && value.every(valid)
928
+ const EXTENSIONS = [listOf((value) => typeof value === 'string' && /^[^./\\][^/\\]*$/u.test(value)), 'a list of extensions, each without its dot ("riv")']
929
+ const isScale = (value) => (typeof value === 'number' && Number.isFinite(value) && value > 0) || (typeof value === 'string' && /^\d+(?:\.\d+)?$/u.test(value) && Number(value) > 0)
930
+ const RN_CONFIG_LISTS = {
931
+ metroConfig: [(value) => typeof value === 'boolean' || (typeof value === 'string' && value !== ''), 'true, false or the path of a Metro config'],
932
+ assetExts: EXTENSIONS,
933
+ sourceExts: EXTENSIONS,
934
+ assetResolutions: [listOf(isScale), 'a list of scales ("0.75", or 0.75)'],
935
+ }
936
+ const PATH_GLOBS = [listOf((value) => typeof value === 'string' && isPathGlob(value)), 'a list of globs of paths from the project root, each a directory\'s files too ("src", "src/**/*.js", "node_modules")']
937
+ const isPackageGlob = (value) => typeof value === 'string' && /^(?:@[^/]+\/[^/]+|[^@/.][^/]*)$/u.test(value) && !value.includes('\\') && globRegExp(value) !== null
938
+ const PACKAGE_GLOBS = [listOf(isPackageGlob), 'a list of globs of packages\' names ("react-native", "@react-native/*")']
939
+ const RN_CONFIG_RESTRICTIONS = { allowedPaths: PATH_GLOBS, disallowedPaths: PATH_GLOBS, allowedModules: PACKAGE_GLOBS, disallowedModules: PACKAGE_GLOBS }
940
+
941
+ // Throws where it isn't a JSON object, as rn-bundle refuses to build.
942
+ function readRnConfig(baseDir, host) {
943
+ const text = readFileSyncMaybe(baseDir, RN_CONFIG_FILE, 'utf8', host)
944
+ if (text === null) return null
945
+ const config = parseJson(text, `${RN_CONFIG_FILE} is not valid JSON`)
946
+ if (!isPlainObject(config)) throw new Error(`${RN_CONFIG_FILE} is not an object of options`)
947
+ return config
948
+ }
949
+
950
+ function rnOptions(config, options, prefix = '') {
951
+ const taken = {}
952
+ for (const [key, [valid, what]] of Object.entries(options)) {
953
+ if (config[key] === undefined) continue
954
+ if (!valid(config[key])) throw new Error(`${RN_CONFIG_FILE}: ${prefix}${key} is ${what}, not ${JSON.stringify(config[key])}`)
955
+ taken[key] = config[key]
956
+ }
957
+ return taken
958
+ }
959
+
960
+ function rnRestrictions(config) {
961
+ const restrictions = rnOptions(config, RN_CONFIG_RESTRICTIONS)
962
+ for (const mode of ['development', 'production']) {
963
+ if (config[mode] === undefined) continue
964
+ if (!isPlainObject(config[mode])) throw new Error(`${RN_CONFIG_FILE}: ${mode} is an object of options, not ${JSON.stringify(config[mode])}`)
965
+ restrictions[mode] = rnOptions(config[mode], RN_CONFIG_RESTRICTIONS, `${mode}.`)
966
+ }
967
+ return restrictions
968
+ }
969
+
970
+ // --metro: Metro's resolver lists as rn-bundle takes them from rn.config.json and the Metro config it loads
971
+ // (unless rn.config.json has assetExts or metroConfig false), and the `configs` they came of, to carry.
972
+ function metroLists(baseDir, host, config) {
973
+ const rn = config === null ? {} : rnOptions(config, RN_CONFIG_LISTS)
974
+ const metroFile = rn.assetExts !== undefined || rn.metroConfig === false ? null : typeof rn.metroConfig === 'string' ? rn.metroConfig : METRO_CONFIG_FILE
975
+ const assetExts = metroFile === null ? new Set(addedTo(METRO_ASSET_EXTS, rn.assetExts ?? [])) : metroAssetExts(baseDir, host, metroFile)
976
+ const sourceExts = addedTo(METRO_SOURCE_EXTS, rn.sourceExts ?? [])
977
+ // Metro takes a file of an extension in both for an asset, and rn.config.json removes none.
978
+ const both = rn.sourceExts?.find((ext) => assetExts.has(ext)) ?? rn.assetExts?.find((ext) => sourceExts.includes(ext))
979
+ if (both !== undefined) throw new Error(`${RN_CONFIG_FILE}: ${both} is in both assetExts and sourceExts, which Metro takes a file of as an asset`)
980
+ return {
981
+ assetExts,
982
+ sourceExts: sourceExts.filter((ext) => isResolvableExt(`.${ext}`)),
983
+ assetResolutions: addedTo(METRO_ASSET_RESOLUTIONS, (rn.assetResolutions ?? []).map(String)),
984
+ configs: [join(baseDir, RN_CONFIG_FILE), ...(metroFile === null ? [] : [resolve(baseDir, metroFile)])],
985
+ }
986
+ }
987
+
988
+ function refuseRestricted(files, restrict, host) {
989
+ const packageOf = packagesOf({ isFile: (path) => host.stat(path)?.isFile() ?? false, readFile: (path) => host.readFile(path) })
990
+ const errors = [...new Set([...files].map((file) => host.realpath(file)).flatMap((file) => restrict(file, packageOf(file))))].toSorted()
991
+ if (errors.length > 0) throw new Error(`stasis bundle: the --metro build takes what ${RN_CONFIG_FILE} forbids:\n${listed(errors)}`)
1345
992
  }
1346
993
 
1347
994
  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.
995
+ // metro-config's default `asyncRequireModulePath`, which Metro adds to a build with an `import()`.
1350
996
  const ASYNC_REQUIRE = 'metro-runtime/src/modules/asyncRequire.js'
1351
997
 
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.
998
+ // A cache across a lazy tree's rounds: what `read()` gives, kept where it read no package not unpacked yet.
1356
999
  function keptAcross(label, host, memo) {
1357
1000
  const kept = memo ? nestedMap(memo, label) : null
1358
1001
  return (key, read) => {
@@ -1364,32 +1007,20 @@ function keptAcross(label, host, memo) {
1364
1007
  }
1365
1008
  }
1366
1009
 
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).
1010
+ // `fn()`, its reads probes of what files packages hold, which read none of their code, where `host` (a lazy
1011
+ // tree's) tells them apart.
1369
1012
  const probing = (host, fn) => (host.probing === undefined ? fn() : host.probing(fn))
1370
1013
 
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() }) {
1014
+ function metroInsertedFiles(files, { assets, baseDir, babelled, dynamicImports, jsx, resolver, platformResolver, conditions: conds, host, kept }) {
1384
1015
  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).
1016
+ // A specifier resolves alike from every file of a directory.
1387
1017
  const onePerDir = (list) => [...new Map(list.map((file) => [dirname(file), file])).values()]
1388
1018
  const resolveFrom = (from, spec, by = resolver) => kept(`${by === resolver ? '' : 'platform\0'}${dirname(from)}\0${spec}`, () => {
1389
1019
  const url = by(from, spec, conds)?.url
1390
1020
  return url === undefined ? undefined : fileURLToPath(url)
1391
1021
  })
1392
1022
  if (babelled) {
1023
+ // Every helper of each @babel/runtime a file finds: which ones Babel adds hangs on the syntax.
1393
1024
  const helpersDirs = new Set()
1394
1025
  for (const from of onePerDir(files)) {
1395
1026
  const probe = resolveFrom(from, '@babel/runtime/helpers/interopRequireDefault')
@@ -1406,20 +1037,17 @@ function metroInsertedFiles(files, { assets, baseDir, babelled, dynamicImports,
1406
1037
  for (const [, file] of requireResolutions(join(polyfills, 'index.js'), host)) inserted.add(file)
1407
1038
  }
1408
1039
  }
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).
1040
+ // From the project, else through Metro, beside which a pnpm install links metro-runtime.
1412
1041
  if (dynamicImports) {
1413
1042
  const metroDir = packageDirFrom(baseDir, 'metro', host)
1414
1043
  const file = requireFrom(baseDir, ASYNC_REQUIRE, host) ?? (metroDir === undefined ? undefined : requireFrom(metroDir, ASYNC_REQUIRE, host))
1415
1044
  if (file !== undefined) inserted.add(file)
1416
1045
  }
1417
- // Babel's JSX transform imports the runtime into a file with JSX: under --jsx any one, else a
1418
- // .jsx/.tsx one.
1419
1046
  for (const from of onePerDir(jsx ? files : files.filter((file) => JSX_FILE_EXTS.has(extname(file))))) {
1420
1047
  const runtime = resolveFrom(from, 'react/jsx-runtime')
1421
1048
  if (runtime !== undefined) inserted.add(runtime)
1422
1049
  }
1050
+ // Metro's module of an asset requires it past Babel, so with no alias of its.
1423
1051
  for (const from of onePerDir(assets)) {
1424
1052
  const registry = resolveFrom(from, ASSET_REGISTRY, platformResolver)
1425
1053
  if (registry !== undefined) inserted.add(registry)
@@ -1427,40 +1055,27 @@ function metroInsertedFiles(files, { assets, baseDir, babelled, dynamicImports,
1427
1055
  return inserted
1428
1056
  }
1429
1057
 
1430
- // Build a JS/TS Bundle + companion Lockfile via the legacy-field resolver (`--mainFields`/
1431
- // `--metro`). Scanned once per platform; each edge is recorded flat when the platforms that
1432
- // have it agree, or as a `{ platform: target }` map where they diverge. `scope` and `resources`
1433
- // are fieldBuildOptions'. Returns { bundle, lockfile }.
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 }) {
1058
+ // A JS/TS Bundle via the legacy-field resolver (`--mainFields`/`--metro`), scanned once per platform: an edge
1059
+ // is flat where the platforms agree, else a `{ platform: target }` map. -> { bundle, lockfile: () => Lockfile }
1060
+ 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, generated }) {
1435
1061
  const baseDir = resolve(cwd)
1436
1062
  const absEntries = entries.map((e) => resolve(baseDir, e))
1437
1063
  const normalized = normalizeEntries(entries, cwd)
1438
1064
 
1439
- // A resolved file arrives by its real path, an entry as spelled: under a cwd named through a link,
1440
- // the files the scan reaches lie below the root's real path, not below `baseDir` as spelled. Either
1441
- // is the root; a file below neither is outside it. (Realpathed only when needed, so a missing cwd
1442
- // is still the scan's missing entry.)
1065
+ // A resolved file arrives by its real path, an entry as spelled: below either is within the root. (Realpathed
1066
+ // only when needed, so a missing cwd is still the scan's missing entry.)
1443
1067
  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
1068
  const fromRoot = memo ? nestedMap(memo, 'paths from the root') : new Map()
1448
- const relWithin = (abs) => {
1449
- if (fromRoot.has(abs)) return fromRoot.get(abs)
1069
+ const relWithin = (abs) => upsert(fromRoot, abs, () => {
1450
1070
  let rel = toPosix(relative(baseDir, abs))
1451
1071
  if (relativeEscapes(rel)) {
1452
1072
  realRoot ??= host.realpath(baseDir)
1453
1073
  rel = toPosix(relative(realRoot, abs))
1454
1074
  }
1455
- fromRoot.set(abs, relativeEscapes(rel) ? null : rel)
1456
- return fromRoot.get(abs)
1457
- }
1075
+ return relativeEscapes(rel) ? null : rel
1076
+ })
1458
1077
  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
1463
- }
1078
+ const pathOf = (url) => upsert(pathsOf, url, () => fileURLToPath(url))
1464
1079
  const toRel = (abs) => {
1465
1080
  const rel = relWithin(abs)
1466
1081
  if (rel === null) throw new Error(`Bundle would reach a file outside the project root: ${abs}`)
@@ -1468,81 +1083,65 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1468
1083
  }
1469
1084
 
1470
1085
  const scanConditions = cleanConditions(conditions)
1471
-
1472
- // Taken as given where the entries import TS sources by output name, but under --metro-resolver,
1473
- // which has no tsc mapping (classifyEntries rejects --typescript there).
1086
+ // Not under --metro-resolver, which has no tsc mapping.
1474
1087
  if (!metroResolver) typescript ||= importsTypescriptByOutputName(absEntries, host)
1475
- // --typescript's tsconfig `paths` matcher, shared by every per-platform resolver below.
1476
1088
  const typescriptPaths = typescriptPathsFor(typescript, baseDir, tsconfig, host, env)
1477
- // --resources (else the config's): extensions/filenames carried as opaque assets instead of
1478
- // failing "can't carry". --package-json, else the config's `packageJSON`: see withManifests below.
1479
1089
  const { resources: resourceSet, packageJSON: withManifests } = fieldBuildOptions('buildResolvedJsBundle', { cwd, scope, resources, packageJSON, host })
1480
1090
 
1481
1091
  const formatsByRel = new Map()
1482
- // Reached files the scanner tagged as resources (rel keys); read as bytes below, not UTF-8 source.
1483
1092
  const resourceRels = new Set()
1484
1093
  // parentRel -> specifier -> Map<platformKey, targetRel>; collapsed after all platforms scanned.
1485
1094
  const edges = new Map()
1486
- const reached = new Set() // absolute paths reached on any platform
1095
+ const reached = new Set()
1487
1096
  let usesEmpty = false
1488
- // What a disabled import resolves to: under --metro Metro's own empty module where the project
1489
- // has it (metroEmptyModule), as Metro resolves it; else the reserved synthetic one.
1097
+ // A disabled import resolves to Metro's own empty module where the project has it, else the reserved one.
1490
1098
  const metroEmpty = metro ? metroEmptyModule(baseDir, host) : null
1491
1099
  const emptyTarget = metroEmpty === null ? EMPTY_MODULE_PATH : toRel(metroEmpty)
1492
1100
 
1493
- // --babel-aliases: the same aliasing as --babel's, from a JSON file of the project, for every platform alike.
1494
1101
  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.
1102
+ // Metro runs the build through Babel, which adds its helpers to each file.
1496
1103
  const babelled = babel || fileAliases !== null
1497
- // The edges each platform's scan left out above the root (withinRoot's leftOut), across platforms.
1498
1104
  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.
1105
+ const rnConfig = metro ? readRnConfig(baseDir, host) : null
1106
+ const { assetExts, sourceExts, assetResolutions, configs: listConfigs } = metro ? metroLists(baseDir, host, rnConfig) : { assetExts: METRO_ASSET_EXTS, sourceExts: METRO_SOURCE_EXTS, assetResolutions: METRO_ASSET_RESOLUTIONS, configs: [] }
1107
+ // A --metro bundle builds as a release build does, so production's restrictions apply, not development's.
1108
+ const restrict = rnConfig === null ? null : restrictionsOf(rnRestrictions(rnConfig), host.realpath(baseDir), false)
1109
+ const metroTaken = new Set()
1110
+ // A resource that is no asset of Metro's reaches the build only as a transform's input, inlined by a Babel
1111
+ // plugin (`require('./messages.po')`): no edge to one, nor asset registry.
1112
+ const isTransformInput = (abs, info) => metro && info?.resource === true && !isAssetFile(abs, assetExts)
1113
+ // A round of a lazy tree that asked for a package already is void: it reads on, for all else it asks for,
1114
+ // but reports and refuses nothing, and stops before it reads the files in.
1511
1115
  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
1116
  const scanWithin = (roots, options, label) => {
1516
1117
  const { scanner, outside, leftOut } = withinRoot(scan(roots, { ...options, memo: memo ? nestedMap(memo, label) : null }), (url) => relWithin(pathOf(url)) !== null)
1517
1118
  for (const url of outside) toRel(pathOf(url))
1518
- if (!voided()) reportScanIssues({ ...analyzeScanner(scanner, { baseDir }), leftOut }, { baseDir, label, root: ' at .' })
1119
+ if (!voided()) reportScanIssues({ ...analyzeScanner(scanner, { baseDir, generated }), leftOut }, { baseDir, label, root: ' at .' })
1519
1120
  return { scanner, leftOut }
1520
1121
  }
1521
1122
  for (const platform of platforms) {
1522
- // What each resolves, kept across a lazy tree's rounds where it read nothing not unpacked yet.
1523
1123
  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') })
1124
+ const field = fieldResolverFor(platform, { mainFields, metro, assetExts, sourceExts, assetResolutions, conditions: scanConditions, typescript, typescriptPaths, host, kept: keptFor('resolutions') })
1525
1125
  const { extras } = field
1526
- // --metro --metro-resolver delegates to the project's own metro-resolver for byte-for-byte Metro
1527
- // fidelity; otherwise the built-in field/suffix resolver reproduces it. metro-resolver derives
1528
- // default/require|import/platform conditions itself, so it takes only the extra `react-native`
1529
- // condition (browser comes from its per-platform map, keyed on `web`).
1126
+ // The project's own metro-resolver derives every condition but the extra `react-native` itself.
1530
1127
  const resolver = metroResolver
1531
- ? createMetroResolver({ projectDir: baseDir, platform, sourceExts: SOURCE_EXTS, mainFields, conditionNames: ['react-native'], assetExts, host })
1128
+ ? createMetroResolver({ projectDir: baseDir, platform, sourceExts, mainFields, conditionNames: ['react-native'], assetExts, assetResolutions, host })
1532
1129
  : field.resolver
1533
- // Without --typescript, the field resolver --typescript would build names the file a miss would
1534
- // land on: a hint in the unresolved report, never an edge (see Scan#typescriptCandidate).
1535
- const typescriptResolve = typescript || metroResolver ? null : fieldResolverFor(platform, { mainFields, metro, assetExts, conditions: scanConditions, typescript: true, typescriptPaths, host, kept: keptFor('typescript resolutions') }).resolver
1536
- // --babel: a specifier the project's babel-plugin-module-resolver rewrites resolves as rewritten,
1537
- // as Metro resolves it after Babel; its edge stays keyed by the specifier the source holds.
1130
+ // Without --typescript, the file a miss would land on under it: a hint in the unresolved report, never an edge.
1131
+ const typescriptResolve = typescript || metroResolver ? null : fieldResolverFor(platform, { mainFields, metro, assetExts, sourceExts, assetResolutions, conditions: scanConditions, typescript: true, typescriptPaths, host, kept: keptFor('typescript resolutions') }).resolver
1132
+ // --babel: a specifier babel-plugin-module-resolver rewrites resolves as rewritten, as Metro resolves it
1133
+ // after Babel; its edge stays keyed by the specifier the source holds.
1538
1134
  const aliases = babel ? createBabelAliases({ projectDir: baseDir, platform }) : fileAliases
1539
- // --babel: and whether it keeps a TypeScript import listing types alone (onlyRemoveTypeImports).
1540
1135
  const keepsTypeImports = babel ? createBabelTypeImportsKept({ projectDir: baseDir, platform }) : null
1541
- const aliased = (resolveFn) => (aliases && resolveFn ? (file, spec, conds) => resolveFn(file, aliases(file, spec) ?? spec, conds) : resolveFn)
1136
+ const aliased = (resolveFn) => {
1137
+ if (!aliases || !resolveFn) return resolveFn
1138
+ const spelled = (file, spec) => aliases(file, spec) ?? spec
1139
+ return Object.assign((file, spec, conds) => resolveFn(file, spelled(file, spec), conds), resolveFn.missTargets && { missTargets: (file, spec, conds) => resolveFn.missTargets(file, spelled(file, spec), conds) })
1140
+ }
1542
1141
  // --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 }
1544
- const platformKey = platform ?? '*' // '*' is a private placeholder for the single mainFields pass; it never unflattens
1545
- // Record what scanning from `roots` reaches: its files, their formats and edges. -> its scanner
1142
+ const scanOptions = { conditions: extras, resolve: aliased(resolver), typescriptResolve: aliased(typescriptResolve), jsx, flow, resources: resourceSet, define, babel: metro, keepsTypeImports, host, missTargets: generated !== undefined }
1143
+ // '*' is a private placeholder for the single mainFields pass; it never unflattens.
1144
+ const platformKey = platform ?? '*'
1546
1145
  const absorb = (roots) => {
1547
1146
  const { scanner, leftOut } = scanWithin(roots, scanOptions, platform ?? 'mainFields')
1548
1147
  leftOutEdges.push(...leftOut)
@@ -1550,17 +1149,16 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1550
1149
  const abs = pathOf(url)
1551
1150
  const rel = toRel(abs)
1552
1151
  reached.add(abs)
1152
+ if (restrict !== null && !isTransformInput(abs, info)) metroTaken.add(abs)
1553
1153
  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.
1154
+ // Under --metro a code file has only the format its name gives it, as the Metro plugin records it.
1557
1155
  formatsByRel.set(rel, metro && !info.resource ? nodeExtFormat(abs) : info.format)
1558
1156
  for (const e of info.edges) {
1559
1157
  if (e.builtin || e.dynamic) continue
1560
1158
  let target
1561
1159
  if (e.empty) { usesEmpty = true; target = emptyTarget }
1562
1160
  else if (e.child) target = toRel(pathOf(e.child))
1563
- else continue // unresolved (already warned/thrown by reportScanIssues)
1161
+ else continue // unresolved: reportScanIssues warned or threw
1564
1162
  // A transform's input is no dependency of Metro's: the import is gone before Metro sees it.
1565
1163
  if (e.child && isTransformInput(pathOf(e.child), scanner.files.get(e.child))) continue
1566
1164
  nestedMap(nestedMap(edges, rel), e.spec).set(platformKey, target)
@@ -1569,19 +1167,14 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1569
1167
  return scanner
1570
1168
  }
1571
1169
  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.
1170
+ // --metro: and what Babel and Metro add, scanned in turn; one above the root passed over, as the Metro plugin does.
1575
1171
  if (metro) {
1576
1172
  const metroAt = performance.now()
1577
1173
  const code = [...scanner.files].filter(([, info]) => !info.resource).map(([url]) => pathOf(url))
1578
1174
  const assets = [...scanner.files].filter(([url, info]) => info.resource && !isTransformInput(pathOf(url), info)).map(([url]) => pathOf(url))
1579
1175
  // As a require(): Babel's CommonJS transform makes one of each it adds, as Metro writes its own.
1580
1176
  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.
1177
+ // Each asset's other scales (`logo@2x.png`), which Metro reads to build its module: no import names one.
1585
1178
  const scales = assets.flatMap((file) => metroAssetFiles(file, platform, host)).filter((file) => relWithin(host.realpath(file)) !== null)
1586
1179
  const dynamicImports = [...scanner.files.values()].some((info) => !info.resource && info.edges.some((e) => e.kind === 'dynamic-import'))
1587
1180
  const kept = keptAcross(`Metro's additions ${platformKey}`, host, memo)
@@ -1592,34 +1185,29 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1592
1185
  }
1593
1186
  }
1594
1187
 
1595
- // One platform leaving out an edge another resolves within the root is a split too: collapsed
1596
- // below, the edge would serve that platform the other's file in its place.
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.
1188
+ // Collapsed, an edge one platform resolves in the root and another leaves out would serve the one the other's file.
1189
+ const [split] = voided() ? [] : leftOutEdges.filter((e) => edges.get(toRel(pathOf(e.parentURL)))?.has(e.spec))
1190
+ if (split !== undefined) {
1191
+ throw new Error(
1192
+ `${split.spec} from ${displayPath(split.parentURL, baseDir)} resolves within the project root in one context and outside it, ` +
1193
+ `at ${relative(baseDir, fileURLToPath(split.outsideURL))}, in another: a bundle can't leave out the one without serving the other in its place`
1194
+ )
1195
+ }
1196
+ if (restrict !== null && !voided()) refuseRestricted(usesEmpty && metroEmpty !== null ? [...metroTaken, metroEmpty] : metroTaken, restrict, host)
1197
+
1198
+ // --metro: and the configs and toolchain a React Native build loads in Node, traced as Node entries: their
1199
+ // edges keyed by Node's conditions, as the Metro plugin's run records them, apart from the app's '*' ones.
1610
1200
  const keyedResolutions = new Map()
1611
- // The edges from `parent` (rel) keyed `key`: specifier -> targetRel.
1612
1201
  const keyedFrom = (key, parent) => nestedMap(nestedMap(keyedResolutions, key), parent)
1613
- // The configs' edges some context misses or leaves out: `${parentRel}\0${specifier}` -> the miss.
1202
+ // `${parentRel}\0${specifier}` -> the configs' miss.
1614
1203
  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.
1204
+ // What the configs reach, and of it what the app's build doesn't: attributed to `bundle`, not `bundle:metro`.
1617
1205
  const configReached = new Set()
1618
1206
  const configOnly = new Set()
1619
1207
  const configsAt = performance.now()
1620
1208
  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
1209
+ const rootConfigs = [...METRO_CONFIG_FILES, ...(babelled ? [BABEL_CONFIG_FILE] : [])].map((rel) => join(baseDir, rel))
1210
+ const configs = [...new Set([...rootConfigs, ...listConfigs.filter((abs) => relWithin(abs) !== null)])].filter((abs) => host.stat(abs)?.isFile())
1623
1211
  const toolchainFile = ({ name, file, via }) => {
1624
1212
  for (let i = 0; i <= via.length; i++) {
1625
1213
  const chain = via.slice(i)
@@ -1638,17 +1226,13 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1638
1226
  const file = toolchainFile(entry)
1639
1227
  if (file !== undefined && relWithin(file) !== null) configs.push(file)
1640
1228
  }
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).
1229
+ // And the React Native configs of the app's packages, which its CLI requires, by their real path.
1645
1230
  for (const dir of probing(host, () => linkedPackages(baseDir, host))) {
1646
1231
  const file = join(dir, 'react-native.config.js')
1647
1232
  if (!probing(host, () => host.stat(file)?.isFile())) continue
1648
1233
  const real = host.realpath(file)
1649
1234
  if (relWithin(real) !== null) configs.push(real)
1650
1235
  }
1651
- // Trace `roots` as Node entries, recording what they reach. -> the scan
1652
1236
  const traceNode = (roots) => {
1653
1237
  const { scanner, leftOut } = scanWithin(roots, { resources: resourceSet, host }, 'configs and toolchain')
1654
1238
  for (const [url, info] of scanner.files) {
@@ -1660,9 +1244,7 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1660
1244
  if (info.resource) resourceRels.add(rel)
1661
1245
  formatsByRel.set(rel, info.format)
1662
1246
  }
1663
- for (const u of [...scanner.unresolved, ...leftOut]) {
1664
- if (u.spec !== undefined) configMissed.set(`${toRel(pathOf(u.parentURL))}\0${u.spec}`, u)
1665
- }
1247
+ for (const u of [...scanner.unresolved, ...leftOut]) if (u.spec !== undefined) configMissed.set(`${toRel(pathOf(u.parentURL))}\0${u.spec}`, u)
1666
1248
  for (const [key, byParent] of scanner.imports) {
1667
1249
  for (const [parentURL, specs] of byParent) {
1668
1250
  const keyed = keyedFrom(key, toRel(pathOf(parentURL)))
@@ -1672,23 +1254,16 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1672
1254
  return scanner
1673
1255
  }
1674
1256
  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.
1257
+ // And the transformers beside stasis's Metro plugin, where a config requires it.
1676
1258
  const transformers = [...(traced?.files.keys() ?? [])].map((url) => pathOf(url))
1677
1259
  .filter((file) => basename(file) === 'metro.js' && basename(dirname(file)) === 'src' &&
1678
1260
  readJson(join(dirname(dirname(file)), 'package.json'), host)?.name === '@exodus/stasis-plugins')
1679
1261
  .flatMap((file) => STASIS_METRO_TRANSFORMERS.map((name) => join(dirname(file), name)))
1680
1262
  .filter((file) => host.stat(file)?.isFile() && relWithin(file) !== null && !traced.files.has(pathToFileURL(file).toString()))
1681
1263
  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.
1264
+ // And the plugins and presets the Babel config's modules name, and what any module reached names by
1265
+ // `require.resolve('x')`, which Babel loads: each traced in turn.
1690
1266
  const babelConfig = pathToFileURL(join(baseDir, BABEL_CONFIG_FILE)).toString()
1691
- // What each file names, read once across a lazy tree's rounds (keptAcross).
1692
1267
  const resolutionsOf = keptAcross('require.resolve', host, memo)
1693
1268
  const namedOf = keptAcross('babel names', host, memo)
1694
1269
  if (traced?.files.has(babelConfig)) {
@@ -1712,15 +1287,11 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1712
1287
  for (const e of info.edges) if (e.child) frontier.push(e.child)
1713
1288
  const file = pathOf(url)
1714
1289
  const rel = toRel(file)
1715
- if (info.resource || !JS_EXTS.has(extname(file))) continue
1290
+ if (info.resource || !JS_EXTENSIONS.has(extname(file))) continue
1716
1291
  for (const [spec, target] of resolutionsOf(file, () => requireResolutions(file, host))) {
1717
- if (!JS_EXTS.has(extname(target)) || relWithin(target) === null) continue
1292
+ if (!JS_EXTENSIONS.has(extname(target)) || relWithin(target) === null) continue
1718
1293
  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.
1294
+ // A specifier keeps the first edge recorded, as a run records a resolution only where no edge is.
1724
1295
  const keyed = keyedFrom(REQUIRE_KEY, rel)
1725
1296
  if (!keyed.has(spec)) keyed.set(spec, toRel(target))
1726
1297
  }
@@ -1728,7 +1299,7 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1728
1299
  if (!configModule && splitNodeModulesPath(rel)) continue
1729
1300
  for (const name of namedOf(`${configModule}\0${file}`, () => babelNamedModules(file, host, { anyString: configModule }))) {
1730
1301
  const found = resolveNamed(name, file)
1731
- if (found !== undefined && JS_EXTS.has(extname(found)) && relWithin(found) !== null) named.add(found)
1302
+ if (found !== undefined && JS_EXTENSIONS.has(extname(found)) && relWithin(found) !== null) named.add(found)
1732
1303
  }
1733
1304
  }
1734
1305
  const fresh = [...named].filter((file) => !files.has(pathToFileURL(file).toString()))
@@ -1736,10 +1307,7 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1736
1307
  frontier = [...named].map((file) => pathToFileURL(file).toString())
1737
1308
  }
1738
1309
  }
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.
1310
+ // And the toolchain files the Metro plugin carries for every build, tagged as it does them.
1743
1311
  for (const specifier of AUTO_INCLUDES) {
1744
1312
  const file = requireFrom(baseDir, specifier, host)
1745
1313
  if (file === undefined || reached.has(file) || relWithin(file) === null) continue
@@ -1757,77 +1325,54 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1757
1325
  if (voided()) throw new Error('buildResolvedJsBundle: this round reads a package not unpacked yet')
1758
1326
  const carryingAt = performance.now()
1759
1327
 
1760
- // Read each reached file once: bytes for the bundle, integrity for the lockfile. Source is
1761
- // stored as UTF-8 text, so non-UTF-8 bytes would diverge from the hashed bytes -- reject them.
1328
+ // Each reached file carried as the bytes read, encoded without loss, for the lockfile to hash once wanted.
1762
1329
  const sources = new Map()
1763
- const integrities = new Map()
1764
- // Carry `rel`: `content` in the bundle as `format`, the integrity of its bytes `buf` in the lockfile.
1765
- const put = (rel, content, format, buf) => {
1330
+ const put = (rel, content, format) => {
1766
1331
  sources.set(rel, content)
1767
1332
  formatsByRel.set(rel, format)
1768
- integrities.set(rel, sha512integrity(buf))
1769
1333
  }
1770
- // A resource carries bytes, not source: UTF-8 verbatim or base64 when binary, tagged with the
1771
- // byte-derived format (here and in the --metro native capture below).
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).
1334
+ const putResource = (rel, buf) => (isUtf8(buf) ? put(rel, buf.toString('utf8'), 'resource') : put(rel, buf.toString('base64'), 'resource:base64'))
1335
+ // Code carries its source, UTF-8, which non-UTF-8 bytes would not survive: refused.
1774
1336
  const putCode = (rel, buf, format, what = 'JS bundle source') => {
1775
1337
  if (!isUtf8(buf)) throw new Error(`${what} is not valid UTF-8: ${rel}`)
1776
- put(rel, buf.toString('utf8'), format, buf)
1338
+ put(rel, buf.toString('utf8'), format)
1777
1339
  }
1778
1340
  const realBase = host.realpath(baseDir)
1779
1341
  // Metro's empty module is carried as the file it is, a code file of Metro's (no format).
1780
1342
  if (usesEmpty && metroEmpty !== null) reached.add(metroEmpty)
1781
1343
  for (const abs of reached) {
1782
1344
  const rel = toRel(abs)
1783
- // Security: a resolved file arrives by its real path, which toRel already holds to the root,
1784
- // but an entry is taken as spelled (and --metro-resolver keeps a path it can't realpath), so an
1785
- // in-tree-named symlink escaping the root would slip past toRel's textual check -- realpath and
1786
- // fail closed.
1345
+ // Security: an entry is taken as spelled (and --metro-resolver keeps a path it can't realpath), so a
1346
+ // symlink escaping the root would slip past toRel's textual check: realpath and fail closed.
1787
1347
  assertRealPathWithinBase(realBase, baseDir, rel, host)
1788
1348
  const buf = host.readFile(abs)
1789
- // A code file's format is the scan's.
1790
1349
  if (resourceRels.has(rel)) putResource(rel, buf)
1791
1350
  else putCode(rel, buf, formatsByRel.get(rel))
1792
1351
  }
1793
1352
  if (usesEmpty && metroEmpty === null) {
1794
- // Refuse to shadow a real reached file sitting at the reserved empty-module path.
1795
1353
  if (formatsByRel.has(EMPTY_MODULE_PATH)) {
1796
1354
  throw new Error(`Bundle needs the reserved empty-module path ${EMPTY_MODULE_PATH}, but the project has a real file there`)
1797
1355
  }
1798
- put(EMPTY_MODULE_PATH, '', metro ? undefined : 'commonjs', Buffer.alloc(0))
1356
+ put(EMPTY_MODULE_PATH, '', metro ? undefined : 'commonjs')
1799
1357
  }
1800
1358
 
1801
- // The package a bundled file is in (findPackageMetadata: its pkgDir and identity, null where no
1802
- // manifest names one), memoized per directory so a package's many files don't each re-walk to it.
1803
1359
  const packageOf = packageLookup(baseDir, { host })
1804
1360
 
1805
- // --metro also carries the native build inputs of each native package the app links, as the Metro
1806
- // plugin does: the bundled packages installed or linked into node_modules, and those React Native
1807
- // links unimported. Native source is stored as code under a language tag; other assets as
1808
- // 'resource'/'resource:base64'.
1361
+ // --metro also carries the native build inputs of each native package the app links, as the Metro plugin does.
1809
1362
  if (metro) {
1810
- const pkgDirs = new Map() // package dir -> its package's name
1811
- // Follow the CODE/module graph only: a package reached solely for an asset (--resources) is
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.
1363
+ const pkgDirs = new Map()
1364
+ // A package reached only for an asset, or only by the configs Node runs at build time, is no linked native one.
1814
1365
  const isCode = (rel) => !resourceRels.has(rel)
1815
1366
  for (const abs of reached) {
1816
1367
  const rel = toRel(abs)
1817
1368
  const nm = isCode(rel) && !configOnly.has(rel) && splitNodeModulesPath(rel)
1818
1369
  if (nm) pkgDirs.set(nm.dir, nm.name)
1819
1370
  }
1820
- // A package linked into node_modules from out of it (a workspace package) is reached by its real
1821
- // path, out of node_modules, but is a dependency all the same, however an import reaches it (by
1822
- // name or alias, through a browser/react-native map, by a relative path): one linked, under any
1823
- // name, into a node_modules its importer finds packages in (the directories the resolver looks
1824
- // in). One linked nowhere there is the project's own source.
1825
- const linksIn = new Map() // directory -> Set of the real package dirs linked into a node_modules it finds packages in
1826
- const linkedFrom = (dir) => {
1827
- let links = linksIn.get(dir)
1828
- if (links !== undefined) return links
1371
+ // A workspace package linked into node_modules is reached by its real path, but is a dependency all the same
1372
+ // where it is linked, under any name, into a node_modules its importer finds packages in.
1373
+ const linkedFrom = memoized((dir) => {
1829
1374
  const parent = dirname(dir)
1830
- links = new Set(parent === dir ? [] : linkedFrom(parent))
1375
+ const links = new Set(parent === dir ? [] : linkedFrom(parent))
1831
1376
  const link = (abs) => {
1832
1377
  try {
1833
1378
  const rel = toRel(host.realpath(abs))
@@ -1840,43 +1385,35 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1840
1385
  for (const ent of listing(nodeModules, host)) {
1841
1386
  const abs = join(nodeModules, ent.name)
1842
1387
  if (ent.name.startsWith('@')) {
1843
- // A scope holds packages, a directory or a link to one (a whole scope linked, where
1844
- // every package in it is reached through the link).
1845
- for (const scoped of listing(abs, host)) {
1846
- if (ent.isSymbolicLink() || scoped.isSymbolicLink()) link(join(abs, scoped.name))
1847
- }
1388
+ // A whole scope may be linked, every package in it reached through the link.
1389
+ for (const scoped of listing(abs, host)) if (ent.isSymbolicLink() || scoped.isSymbolicLink()) link(join(abs, scoped.name))
1848
1390
  } else if (ent.isSymbolicLink()) {
1849
1391
  link(abs)
1850
1392
  }
1851
1393
  }
1852
1394
  }
1853
- linksIn.set(dir, links)
1854
1395
  return links
1855
- }
1396
+ })
1856
1397
  for (const [parent, bySpec] of edges) {
1857
1398
  for (const byPlatform of bySpec.values()) {
1858
1399
  for (const target of byPlatform.values()) {
1859
1400
  if (!isCode(target) || splitNodeModulesPath(target)) continue
1860
1401
  const links = linkedFrom(dirname(join(baseDir, parent)))
1861
- // The linked root holding the target, nearest first: its entry may lie below a manifest of
1862
- // its own (a named dist/package.json), as a node_modules path names the package it's in.
1402
+ // The nearest linked root: the target may lie below a manifest of its own (a named dist/package.json).
1863
1403
  let root = posix.dirname(target)
1864
1404
  while (root !== '.' && !links.has(root)) root = posix.dirname(root)
1865
- // The project's own source isn't a dependency, nor is an import within the package itself.
1866
1405
  if (root === '.' || parent.startsWith(`${root}/`) || pkgDirs.has(root)) continue
1867
1406
  // Named by its own manifest, whatever name it's linked under.
1868
1407
  pkgDirs.set(root, readPackageJson(baseDir, `${root}/package.json`, { host })?.name)
1869
1408
  }
1870
1409
  }
1871
1410
  }
1872
- // And the app's packages React Native links whether JS imports them or not (`react-native config`'s
1873
- // list, read as data), by their real path, but one above the root, which the plugin passes over too.
1411
+ // And those React Native links whether JS imports them or not, but one above the root, as the plugin does.
1874
1412
  for (const dir of probing(host, () => linkedNativePackages(baseDir, host))) {
1875
1413
  const rel = relWithin(host.realpath(dir))
1876
1414
  if (rel !== null && !pkgDirs.has(rel)) pkgDirs.set(rel, readPackageJson(baseDir, `${rel}/package.json`, { host })?.name)
1877
1415
  }
1878
- // Each one's native build inputs, walked whole (nativeInputs, shared with the plugin), read only
1879
- // once its real path is held to the root.
1416
+ // A file read only once its real path is held to the root.
1880
1417
  const nativeFs = {
1881
1418
  readdir: (abs) => host.readdir(abs),
1882
1419
  stat: (abs) => host.stat(abs),
@@ -1887,16 +1424,14 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1887
1424
  }
1888
1425
  for (const [pkgDir, name] of [...pkgDirs].toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
1889
1426
  const pkgAbs = join(baseDir, pkgDir)
1890
- // react-native core isn't a Pod (config reports it via reactNativePath, not `dependencies`),
1891
- // but its native source (React/, ReactCommon/, ReactAndroid/, ...) is walked all the same.
1427
+ // react-native core isn't a Pod (config reports it via reactNativePath), but its native source is walked too.
1892
1428
  const isRnCore = name === 'react-native'
1893
1429
  if (!isRnCore && !probing(host, () => looksNative(pkgAbs, nativeFs))) continue
1894
1430
  for (const { file, source, action, format } of nativeInputs(pkgAbs, nativeFs, { resources: resourceSet, skip: (abs) => sources.has(toRel(abs)) })) {
1895
1431
  if (action === 'code') putCode(toRel(file), source, format, 'native source')
1896
1432
  else putResource(toRel(file), source)
1897
1433
  }
1898
- // RN core's `.js` build scripts the classify loop skips (Metro-owned by extension, but never in
1899
- // the graph): force-include as code so the podspec/Ruby that invokes them at pod-install resolves.
1434
+ // RN core's `.js` build scripts, never in the graph, which the podspec runs at pod-install.
1900
1435
  if (isRnCore) {
1901
1436
  for (const file of RN_CORE_INCLUDE_FILES) {
1902
1437
  const abs = join(pkgAbs, file)
@@ -1907,49 +1442,37 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1907
1442
  }
1908
1443
  }
1909
1444
 
1910
- // --package-json (or the config's packageJSON): fold each bundled module's package.json into
1911
- // `sources` (and its integrity into the companion lockfile) even when the scan never reached it,
1912
- // and the nearer one a reached file of it has (scopeManifestOf: a `{"type":"module"}` marker such
1913
- // as `esm/package.json`), which Metro's resolver reads for the file, as the State path does.
1914
- // Buckets are the ones assembleCodeBundle derives, from the same `packageOf` (its pkgDir, else the
1915
- // '.' workspace bucket). readModuleManifest applies the read/validate rules shared with the State
1916
- // path (containment, UTF-8-aborts); no identity check here -- these buckets are all fresh from disk.
1445
+ // --package-json: each bundled module's package.json, and the nearer one a reached file of it has (a
1446
+ // `{"type":"module"}` marker), which Metro's resolver reads for the file, as the State path does.
1917
1447
  if (withManifests) {
1918
- const found = new Map()
1919
- const exists = (rel) => found.get(rel) ?? found.set(rel, packageJSONStat(host, join(baseDir, rel))?.isFile() ?? false).get(rel)
1920
- const manifests = new Map() // manifest -> the reached files it serves
1448
+ const exists = memoized((rel) => packageJSONStat(host, join(baseDir, rel))?.isFile() ?? false)
1449
+ const manifests = new Map()
1921
1450
  for (const abs of reached) {
1922
1451
  const rel = toRel(abs)
1923
1452
  const pkgDir = packageOf(rel)?.pkgDir ?? (splitNodeModulesPath(rel) ? undefined : '.')
1924
1453
  if (pkgDir === undefined) continue
1925
1454
  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)
1455
+ if (manifest !== undefined) upsert(manifests, manifest, () => []).push(rel)
1927
1456
  }
1928
1457
  }
1929
1458
  for (const [rel, of] of manifests) {
1930
1459
  if (sources.has(rel)) continue
1931
1460
  const buf = readModuleManifest({ baseDir, realBase, rel, host })
1932
1461
  if (!buf) continue
1933
- put(rel, buf.toString('utf8'), 'json', buf)
1462
+ put(rel, buf.toString('utf8'), 'json')
1934
1463
  // Attributed as the files it serves are, as State#includePackageJson attributes one.
1935
1464
  if (of.some((file) => configReached.has(file))) configReached.add(rel)
1936
1465
  if (of.every((file) => configOnly.has(file))) configOnly.add(rel)
1937
1466
  }
1938
1467
  }
1939
1468
 
1940
- // Collapse each edge: one distinct target across platforms -> flat string; else a sorted Map<platform, target>.
1941
- const resolutions = new Map()
1942
- for (const [parent, bySpec] of edges) {
1943
- const specMap = new Map()
1944
- for (const [spec, byPlatform] of bySpec) {
1945
- const distinct = new Set(byPlatform.values())
1946
- specMap.set(spec, distinct.size === 1 ? [...distinct][0] : new Map([...byPlatform].toSorted((a, b) => (a[0] < b[0] ? -1 : 1))))
1947
- }
1948
- resolutions.set(parent, specMap)
1469
+ // One target across the platforms is flat; else a Map<platform, target>, sorted.
1470
+ const collapsed = (byPlatform) => {
1471
+ const distinct = new Set(byPlatform.values())
1472
+ return distinct.size === 1 ? [...distinct][0] : new Map([...byPlatform].toSorted((a, b) => (a[0] < b[0] ? -1 : 1)))
1949
1473
  }
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.
1474
+ const resolutions = new Map([...edges].map(([parent, bySpec]) => [parent, new Map([...bySpec].map(([spec, to]) => [spec, collapsed(to)]))]))
1475
+ // Where Node misses a config's import Metro resolves for the app, the app's '*' edge would serve Node Metro's file.
1953
1476
  for (const [key, { parentURL, spec }] of configMissed) {
1954
1477
  if (!resolutions.get(key.slice(0, key.indexOf('\0')))?.has(spec)) continue
1955
1478
  throw new Error(
@@ -1960,296 +1483,261 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1960
1483
 
1961
1484
  const rootPkg = readJson(join(baseDir, 'package.json'), host) ?? {}
1962
1485
  const assembled = assembleCodeBundle({
1963
- baseDir,
1964
- entries: normalized,
1965
- sources,
1966
- resolutions,
1967
- keyedResolutions,
1968
- formats: formatsByRel,
1969
- workspaceName: rootPkg.name ?? 'workspace',
1970
- workspaceVersion: rootPkg.version ?? '0.0.0',
1971
- conditionKey: '*',
1972
- host,
1973
- packageOf,
1974
- consumer: metro ? 'bundle:metro' : 'bundle',
1486
+ baseDir, entries: normalized, sources, resolutions, keyedResolutions, formats: formatsByRel, workspaceName: rootPkg.name ?? 'workspace',
1487
+ workspaceVersion: rootPkg.version ?? '0.0.0', conditionKey: '*', host, packageOf, consumer: metro ? 'bundle:metro' : 'bundle',
1975
1488
  consumerFiles: [...sources.keys()].filter((rel) => !configOnly.has(rel)),
1976
1489
  })
1977
1490
  // What the configs reach is the bundler's, not the app's: `bundle`, as a Node build's files are.
1978
1491
  const bundle = configReached.size === 0 ? assembled : assembled.withReason('bundle', [...configReached])
1979
-
1980
- // The companion lockfile mirrors the bundle, swapping file content for its integrity.
1981
1492
  stageTimes.carrying += performance.now() - carryingAt
1982
- return { bundle, lockfile: Lockfile.fromBundle(bundle, (file) => integrities.get(file)) }
1493
+ return { bundle, lockfile: deferredLockfile(bundle) }
1983
1494
  }
1984
1495
 
1985
- // Classify entries into their single shared language and check option applicability; `name` prefixes
1986
- // errors. A directory entry (resolved against `cwd`, on `host`) stands for the .sol files under it: Solidity only.
1987
- // `partial`: the options may yet be completed by a stasis.config.json entry's (configuredBuilds), so
1988
- // what one needs of another one there could set (`--platforms` of `--metro`) is left to the build.
1496
+ // The one language of `entries`, the options checked against it; `name` prefixes errors. `partial`: a
1497
+ // stasis.config.json entry may yet complete the options, so what one needs of another is left to the build.
1989
1498
  function classifyEntries(name, { cwd = process.cwd(), entries, mappingFile, manifests, scope, lockfile, conditions, mainFields, platforms, metro, metroResolver, babel, babelAliases, jsx, flow, typescript, tsconfig, resources, define, packageJSON, cargo, cargoFeatures, cargoNoDefaultFeatures, cargoAllFeatures, cargoTarget, cargoManifests, host = diskHost, fetched, partial = false }) {
1990
- if (!Array.isArray(entries) || entries.length === 0) {
1991
- throw new Error(`${name}: at least one entry file is required`)
1992
- }
1499
+ if (!nonEmpty(entries)) throw new Error(`${name}: at least one entry file is required`)
1993
1500
  const dirError = directoryEntryError(entries, cwd, host, { fetched })
1994
1501
  if (dirError !== null) throw new Error(`${name}: ${dirError}`)
1995
- let kind
1996
- if (entries.every((e) => isSolidityEntry(e, cwd, host))) kind = 'sol'
1997
- else if (entries.every((e) => e.endsWith('.php'))) kind = 'php'
1998
- else if (entries.every((e) => JS_EXTS.has(extname(e)))) kind = 'js'
1999
- else if (entries.every((e) => BASH_EXTS.has(extname(e)))) kind = 'bash'
2000
- else if (entries.every((e) => RUST_EXTS.has(extname(e)))) kind = 'rust'
2001
- else {
2002
- throw new Error(`${name}: entries must all be .sol, all be .php, all be .js/.cjs/.mjs/.ts/.cts/.mts/.jsx/.tsx, all be .sh/.bash, or all be .rs (no mixing)`)
2003
- }
2004
- if (mappingFile && kind !== 'sol') {
2005
- throw new Error(`${name}: --mapping is only valid for .sol bundles`)
2006
- }
2007
- // --manifests carries the Solidity build's description files (foundry.toml, remappings.txt, ...).
2008
- if (manifests && kind !== 'sol') {
2009
- throw new Error(`${name}: --manifests is only valid for .sol bundles`)
2010
- }
2011
- // --cargo runs `cargo metadata` for the Rust feature/dependency resolution and the --cargo-*
2012
- // flags steer that resolution; nothing else reads Cargo.
2013
- if (kind !== 'rust') {
2014
- const given = { cargo, 'cargo-features': Array.isArray(cargoFeatures) && cargoFeatures.length > 0, 'cargo-no-default-features': cargoNoDefaultFeatures, 'cargo-all-features': cargoAllFeatures, 'cargo-target': cargoTarget, 'cargo-manifests': cargoManifests }
2015
- for (const [flag, on] of Object.entries(given)) if (on) throw new Error(`${name}: --${flag} is only valid for Rust bundles`)
2016
- }
2017
- if (scope !== undefined && kind !== 'js') {
2018
- throw new Error(`${name}: --scope is only valid for JS bundles`)
2019
- }
2020
- if (lockfile !== undefined && kind !== 'js') {
2021
- throw new Error(`${name}: --lockfile is only valid for JS bundles`)
2022
- }
2023
- // Reject --conditions for non-JS: their resolvers don't consult exports/imports conditions, so it would silently do nothing.
2024
- if (Array.isArray(conditions) && conditions.length > 0 && kind !== 'js') {
2025
- throw new Error(`${name}: --conditions is only valid for JS bundles`)
2026
- }
2027
- // --flow strips Flow type syntax before the JS parser; the non-JS loaders never parse with oxc.
2028
- if (flow && kind !== 'js') {
2029
- throw new Error(`${name}: --flow is only valid for JS bundles`)
2030
- }
2031
- // --typescript maps JS-output specifiers to their on-disk TS sources; only the JS resolvers do it.
2032
- if (typescript && kind !== 'js') {
2033
- throw new Error(`${name}: --typescript is only valid for JS bundles`)
2034
- }
2035
- // --tsconfig names the config whose `paths` aliases --typescript honours; meaningless without it.
2036
- if (tsconfig !== undefined && !typescript && !partial) {
2037
- throw new Error(`${name}: --tsconfig is only valid with --typescript`)
2038
- }
2039
- // --resources carries allowlisted assets reached through the JS import graph; JS-only. The list
2040
- // is validated (parseResourcesOption) in the builders, but reject a code extension here too so a
2041
- // typo surfaces with the command name rather than deep in the scan.
2042
- if (Array.isArray(resources) && resources.length > 0) {
1502
+ const kinds = {
1503
+ sol: (e) => isSolidityEntry(e, cwd, host), php: (e) => e.endsWith('.php'), js: (e) => JS_EXTENSIONS.has(extname(e)),
1504
+ bash: (e) => BASH_EXTS.has(extname(e)), rust: (e) => RUST_EXTS.has(extname(e)), teal: (e) => e.endsWith('.teal'),
1505
+ }
1506
+ const kind = Object.keys(kinds).find((key) => entries.every((e) => kinds[key](e)))
1507
+ if (kind === undefined) {
1508
+ throw new Error(`${name}: entries must all be .sol, all be .php, all be .js/.cjs/.mjs/.ts/.cts/.mts/.jsx/.tsx, all be .sh/.bash, all be .rs, or all be .teal (no mixing)`)
1509
+ }
1510
+ // Refuse each of the options `given` that is on, for the `only` (`what`) bundles alone, in order.
1511
+ const onlyFor = (only, what, given) => {
1512
+ if (kind === only) return
1513
+ for (const [flag, on] of Object.entries(given)) if (on) throw new Error(`${name}: --${flag} is only valid for ${what} bundles`)
1514
+ }
1515
+ onlyFor('sol', '.sol', { mapping: mappingFile, manifests })
1516
+ onlyFor('rust', 'Rust', { cargo, 'cargo-features': nonEmpty(cargoFeatures), 'cargo-no-default-features': cargoNoDefaultFeatures, 'cargo-all-features': cargoAllFeatures, 'cargo-target': cargoTarget, 'cargo-manifests': cargoManifests })
1517
+ onlyFor('js', 'JS', { scope: scope !== undefined, lockfile: lockfile !== undefined, conditions: nonEmpty(conditions), flow, typescript })
1518
+ if (tsconfig !== undefined && !typescript && !partial) throw new Error(`${name}: --tsconfig is only valid with --typescript`)
1519
+ // Checked here too, so a typo surfaces with the command name rather than deep in the scan.
1520
+ if (nonEmpty(resources)) {
2043
1521
  if (kind !== 'js') throw new Error(`${name}: --resources is only valid for JS bundles`)
2044
1522
  parseResourcesOption(name, resources)
2045
1523
  }
2046
- // --mainFields and --metro/--platforms are legacy-/platform-resolution knobs; JS-only.
2047
- if (mainFields !== undefined && kind !== 'js') {
2048
- throw new Error(`${name}: --mainFields is only valid for JS bundles`)
2049
- }
2050
- // --jsx toggles the scanner's JSX parsing for the .js family (.jsx/.tsx need no flag); only JS
2051
- // entries are scanned.
2052
- if (jsx && kind !== 'js') {
2053
- throw new Error(`${name}: --jsx is only valid for JS bundles`)
2054
- }
2055
- // --define folds the globals it gives in the JS scan's branch tests; no other language is scanned for them.
2056
- if (parseDefine(name, define).size > 0 && kind !== 'js') {
2057
- throw new Error(`${name}: --define is only valid for JS bundles`)
2058
- }
2059
- // --package-json folds each bundled module's npm package.json in; JS-only (the sol/php/bash/rust
2060
- // bucketizers group by their own ecosystems' manifests, not an npm package.json).
2061
- if (packageJSON && kind !== 'js') {
2062
- throw new Error(`${name}: --package-json is only valid for JS bundles`)
2063
- }
2064
- if ((metro || (Array.isArray(platforms) && platforms.length > 0)) && kind !== 'js') {
2065
- throw new Error(`${name}: --metro is only valid for JS bundles`)
2066
- }
2067
- // --metro presets conditions + mainFields + platform suffixes, so it can't combine with
2068
- // explicit --conditions/--mainFields and requires --platforms; --platforms is meaningless without it.
1524
+ // `define` is parsed (checked) whatever the kind.
1525
+ onlyFor('js', 'JS', { mainFields: mainFields !== undefined, jsx, define: parseDefine(name, define).size > 0, 'package-json': packageJSON, metro: metro || nonEmpty(platforms) })
2069
1526
  if (metro) {
2070
- if (Array.isArray(conditions) && conditions.length > 0) {
2071
- throw new Error(`${name}: --conditions can't be combined with --metro (it sets its own conditions)`)
2072
- }
2073
- if (mainFields !== undefined) {
2074
- throw new Error(`${name}: --mainFields can't be combined with --metro (it sets its own mainFields)`)
2075
- }
2076
- if ((!Array.isArray(platforms) || platforms.length === 0) && !partial) {
2077
- throw new Error(`${name}: --metro requires --platforms (e.g. --platforms=ios,android)`)
2078
- }
2079
- // A platform name becomes an edge key: reject '/' (Bundle/Lockfile parse refuse it) and
2080
- // the reserved '*' placeholder, so the writer never emits a map the readers reject.
1527
+ if (nonEmpty(conditions)) throw new Error(`${name}: --conditions can't be combined with --metro (it sets its own conditions)`)
1528
+ if (mainFields !== undefined) throw new Error(`${name}: --mainFields can't be combined with --metro (it sets its own mainFields)`)
1529
+ if (!nonEmpty(platforms) && !partial) throw new Error(`${name}: --metro requires --platforms (e.g. --platforms=ios,android)`)
1530
+ // A platform name becomes an edge key, which Bundle/Lockfile parse refuse with a '/', and '*' is reserved.
2081
1531
  for (const p of Array.isArray(platforms) ? platforms : []) {
2082
1532
  if (typeof p !== 'string' || p.length === 0 || p === '*' || p.includes('/')) {
2083
1533
  throw new Error(`${name}: invalid platform '${p}' (a platform name can't be empty, contain '/', or be '*')`)
2084
1534
  }
2085
1535
  }
2086
- } else if (Array.isArray(platforms) && platforms.length > 0 && !partial) {
1536
+ } else if (nonEmpty(platforms) && !partial) {
2087
1537
  throw new Error(`${name}: --platforms is only valid with --metro`)
2088
1538
  }
2089
- // --metro-resolver swaps in the project's real metro-resolver; it only means something under --metro.
2090
- if (metroResolver && !metro) {
2091
- throw new Error(`${name}: --metro-resolver is only valid with --metro`)
2092
- }
2093
- // --babel takes the import aliases of the project's Babel config, which Metro runs; not otherwise.
2094
- if (babel && !metro) {
2095
- throw new Error(`${name}: --babel is only valid with --metro`)
2096
- }
2097
- // --babel-aliases takes the same aliases from a JSON file instead, so the two don't combine.
1539
+ if (metroResolver && !metro) throw new Error(`${name}: --metro-resolver is only valid with --metro`)
1540
+ if (babel && !metro) throw new Error(`${name}: --babel is only valid with --metro`)
2098
1541
  if (babelAliases !== undefined) {
2099
1542
  if (typeof babelAliases !== 'string' || babelAliases === '') throw new TypeError(`${name}: babelAliases must be a path to a JSON file`)
2100
1543
  if (!metro && !partial) throw new Error(`${name}: --babel-aliases is only valid with --metro`)
2101
1544
  if (babel) throw new Error(`${name}: --babel-aliases can't be combined with --babel (both set the import aliases)`)
2102
1545
  }
2103
- // metro-resolver has no TS extension substitution, so --typescript would silently not apply -- reject it.
2104
1546
  if (typescript && metroResolver) {
2105
1547
  throw new Error(`${name}: --typescript is not supported with --metro-resolver (the project's metro-resolver doesn't substitute .js -> .ts)`)
2106
1548
  }
2107
- // The field resolver always emits full-scope: a narrower --scope, or one from stasis.config.json,
2108
- // would be silently ignored -- reject it here, before anything is built (or fetched).
1549
+ // Refused before anything is built (or fetched).
2109
1550
  if (mainFields !== undefined || metro) fieldBuildOptions(name, { cwd, scope, resources, host })
2110
1551
  return kind
2111
1552
  }
2112
1553
 
2113
- // A JS bundle as `stasis bundle` builds it: by the legacy-field resolver with `mainFields` or
2114
- // `metro`, whose per-platform edges and synthetic empty module a State can't hold, else through a
2115
- // State, which detects the bundle's repo itself. `root` is the directory the bundle's paths are
2116
- // relative to: cwd for the resolver's, the State's root for the State's. `extra` and `outputsAt` are
2117
- // buildJsBundle's, for the State's alone (see withExtra).
2118
- // -> { bundle, lockfile: () => Lockfile, stateBuilt, root }
1554
+ // By the legacy-field resolver with `mainFields` or `metro`, whose per-platform edges a State can't hold.
2119
1555
  async function buildJs({ mainFields, platforms, metro, metroResolver, babel, babelAliases, innermostRoot, extra, outputsAt, ...options }) {
1556
+ if (metro) options = { ...options, jsx: true, flow: true, define: { ...METRO_RELEASE_DEFINE, ...options.define } }
2120
1557
  if (metro || mainFields !== undefined) {
2121
1558
  const built = await buildResolvedJsBundle({ ...options, mainFields: metro ? METRO_MAIN_FIELDS : mainFields, platforms: metro ? platforms : [null], metro: Boolean(metro), metroResolver: Boolean(metroResolver), babel: Boolean(babel), babelAliases })
2122
- return { bundle: built.bundle, lockfile: () => built.lockfile, stateBuilt: false, root: resolve(options.cwd) }
1559
+ return { bundle: built.bundle, lockfile: built.lockfile, stateBuilt: false, root: resolve(options.cwd) }
2123
1560
  }
2124
1561
  const state = await buildJsBundle({ ...options, innermostRoot, extra, outputsAt })
2125
1562
  const bundle = state.sourceBundle
2126
- // Stamp the `bundle` consumer (the static build carries none), unless `extra` added files: then
2127
- // the State attributes each file, to `bundle` or `extra`.
1563
+ // The `bundle` consumer, unless `extra` added files: then the State attributes each, to `bundle` or `extra`.
2128
1564
  return { bundle: bundle.reason === undefined ? bundle.withReason('bundle') : bundle, lockfile: () => state.lockfile, stateBuilt: true, root: state.root }
2129
1565
  }
2130
1566
 
2131
- // `built` (buildOfKind's, built in `cwd` without a State, or the union of bundleCommand's `builds` rooted there) with the files the stasis.config.json
2132
- // there names in `extra`, as `stasis add` adds them (extraArtifacts), into its bundle and its
2133
- // lockfile; never one of `outputs` (the absolute paths the build writes), and resources by the
2134
- // build's `resources` where given, else by the config's. A State-built bundle takes them in its
2135
- // State instead, as `stasis run` does (State#includeExtra). The project is read through `host`,
2136
- // `name`'s.
1567
+ // `bundle`'s lockfile, built on the first call, so a file is hashed only once it is wanted. Its records are
1568
+ // taken now: one the bundle replaces after (a commit pinned onto a module) is not the lockfile's.
1569
+ function deferredLockfile(bundle) {
1570
+ const { config, entries, imports, formats, executable } = bundle
1571
+ const now = { config, entries, modules: new Map(bundle.modules), imports, formats, executable }
1572
+ let lockfile
1573
+ return () => (lockfile ??= Lockfile.fromBundle(now, (file, content) => bundledIntegrity(content, formats.get(file))))
1574
+ }
1575
+
1576
+ // `built` (without a State) with stasis.config.json's `extra` files but `outputs`, packed as `stasis add` packs them.
2137
1577
  function withExtra(built, cwd, { outputs = [], resources, host = diskHost, name = 'bundle' }) {
2138
1578
  const config = readConfigFile(name, cwd, host)
2139
1579
  const patterns = parseExtraOption(CONFIG_FILE, config?.extra)
2140
1580
  if (patterns.length === 0) return built
2141
- // The union of `builds` (see bundleCommand) may be State-built in node_modules scope, which the
2142
- // full-scope files `extra` adds can't join.
1581
+ // The union of `builds` may be State-built in node_modules scope, which the full-scope files can't join.
2143
1582
  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`)
2144
1583
  const baseDir = resolve(cwd)
2145
1584
  const written = new Set(outputs.map((file) => toPosix(relative(baseDir, file))))
2146
1585
  const allowlist = resources === undefined ? parseResourcesOption(CONFIG_FILE, config.resources) : parseResourcesOption('--resources', resources)
2147
- const added = extraArtifacts({ baseDir, patterns, resources: allowlist, bundle: built.bundle, outputs: written, host })
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
2152
- // Each file attributed: to the build's own consumer (`bundle`, or `bundle:metro`, as codeBundle
2153
- // stamps it), `extra`, or both, for one `extra` names that the build carries itself.
2154
- const merged = added.bundle === null ? built.bundle : built.bundle.merge(added.bundle)
2155
- const bundle = added.carried.length === 0 ? merged : merged.withReason('extra', added.carried)
1586
+ const named = expandExtra(baseDir, patterns, written, host).files
1587
+ // Bundle#sources builds its Map afresh on each read.
1588
+ const bundled = built.bundle.sources
1589
+ // A file `extra` names that the build carries itself is attributed to both.
1590
+ const carried = [...named.keys()].filter((rel) => bundled.has(rel))
1591
+ const files = new Map([...named].filter(([rel]) => !bundled.has(rel)))
1592
+ if (files.size === 0 && carried.length === 0) return built
1593
+ let { bundle } = built
1594
+ let extraLock
1595
+ if (files.size > 0) {
1596
+ const { codeFiles, resourceFiles } = validateFiles({ baseDir, realBase: host.realpath(baseDir), files, resources: allowlist, label: 'extra', host })
1597
+ const rootPkg = readJson(join(baseDir, 'package.json'), host) ?? {}
1598
+ const extra = assembleBundle(baseDir, new Map([...codeFiles, ...resourceFiles]), rootPkg.name ?? 'workspace', rootPkg.version ?? '0.0.0', undefined, { reason: 'extra', buckets: bundle.modules, host })
1599
+ extraLock = deferredLockfile(extra)
1600
+ // The files are the build's own, so its `repo`, which a merge with a side lacking one would drop.
1601
+ extra.repo = bundle.repo
1602
+ bundle = bundle.merge(extra)
1603
+ }
1604
+ if (carried.length > 0) bundle = bundle.withReason('extra', carried)
2156
1605
  const { lockfile } = built
2157
- return { ...built, bundle, ...(lockfile && added.lockfile && { lockfile: () => lockfile().merge(added.lockfile) }) }
1606
+ return { ...built, bundle, ...(lockfile && extraLock && { lockfile: () => lockfile().merge(extraLock()) }) }
2158
1607
  }
2159
1608
 
2160
- // The bundle of `kind` (classifyEntries') built from buildBundle's options, from disk, with the files
2161
- // stasis.config.json's `extra` names: a JS bundle's git dependencies at the commits their package
2162
- // managers' records name (pinInstalledCommits). `outputsAt(root)` are the absolute paths the build
2163
- // writes, given the root its paths are relative to.
2164
- // -> { bundle, lockfile: () => Lockfile, stateBuilt } (the last two of a JS bundle alone)
1609
+ // From disk, with stasis.config.json's `extra` unless `extra` is false (one of several builds), not yet pinned.
2165
1610
  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 }) {
2166
1611
  const outputs = outputsAt(resolve(cwd))
2167
- // Without `extra`, built as if the config named none (one of several builds of a bundle, see bundleCommand).
2168
1612
  const withExtraOf = (built, buildResources) => (extra ? withExtra(built, cwd, { outputs, resources: buildResources }) : built)
2169
1613
  if (kind === 'sol') return withExtraOf({ bundle: await buildSolidityBundle({ cwd, env, entries, mappingFile, manifests }) })
2170
1614
  if (kind === 'php') return withExtraOf({ bundle: await buildPhpBundle({ cwd, entries }) })
2171
1615
  if (kind === 'bash') return withExtraOf({ bundle: await buildBashBundle({ cwd, entries }) })
1616
+ if (kind === 'teal') return withExtraOf({ bundle: await buildTealBundle({ cwd, entries }) })
2172
1617
  if (kind === 'rust') return withExtraOf({ bundle: await buildRustBundle({ cwd, entries, cargo, cargoFeatures, cargoNoDefaultFeatures, cargoAllFeatures, cargoTarget, cargoManifests }) })
2173
- let built = await buildJs({ cwd, env, entries, scope, conditions, mainFields, platforms, metro, metroResolver, babel, babelAliases, jsx, flow, typescript, tsconfig, resources, define, packageJSON, extra, outputsAt })
2174
- if (!built.stateBuilt) built = withExtraOf(built, resources)
2175
- pinInstalledCommits(built.bundle, built.root)
2176
- return built
1618
+ const built = await buildJs({ cwd, env, entries, scope, conditions, mainFields, platforms, metro, metroResolver, babel, babelAliases, jsx, flow, typescript, tsconfig, resources, define, packageJSON, extra, outputsAt })
1619
+ return built.stateBuilt ? built : withExtraOf(built, resources)
1620
+ }
1621
+
1622
+ // How each Soldeer, Composer and vendored Cargo dependency is installed, where what its integrity is read of tells.
1623
+ function archiveAccounts(modules, root) {
1624
+ let soldeer, composer
1625
+ const told = {
1626
+ soldeer: (dir) => (soldeer ??= soldeerLocks(root, solidityOwnership(root), diskHost)).get(dir.slice('dependencies/'.length)),
1627
+ composer: (dir) => (composer ??= loadComposerPackages(root)).find((pkg) => pkg.dir === dir),
1628
+ cargo: (dir, sums = readJson(join(root, dir, '.cargo-checksum.json'))) => sums && { integrity: archiveIntegrity('cargo', sums.package) },
1629
+ }
1630
+ return new Map([...modules].map(([dir, { ecosystem }]) => [dir, Object.hasOwn(told, ecosystem) ? told[ecosystem](dir) : undefined]).filter(([, account]) => account))
1631
+ }
1632
+
1633
+ // `built` with its npm dependencies as their install records name them, whatever its kind (a Solidity import
1634
+ // out of node_modules is one): their `accounts`, `accountsOf(modules)` any other's, its lockfile attesting so.
1635
+ function pinnedOf(built, root) {
1636
+ const accounts = pinInstalled(built.bundle, root)
1637
+ return { ...built, accounts, accountsOf: (modules) => new Map([...archiveAccounts(modules, root), ...installedRecords(modules, root)]), ...(built.lockfile && { lockfile: () => withAttested(built.lockfile(), built.bundle) }) }
2177
1638
  }
2178
1639
 
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
1640
  function unionOfBuilds(name, builds, builts, roots) {
2182
1641
  const other = roots.findIndex((root) => root !== roots[0])
2183
1642
  if (other !== -1) {
2184
1643
  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
1644
  }
2186
- return builts.reduce((a, b) => ({ bundle: a.bundle.merge(b.bundle), lockfile: a.lockfile && (() => a.lockfile().merge(b.lockfile())) }))
1645
+ return builts.reduce((a, b) => ({ bundle: a.bundle.merge(b.bundle), lockfile: a.lockfile && b.lockfile && (() => a.lockfile().merge(b.lockfile())) }))
2187
1646
  }
2188
1647
 
2189
- // buildVfsBundle's checks of `options`, which hold before anything is fetched (`host` the
2190
- // project's, or an empty tree's with `fetched: false`): no metro-resolver, which reads the disk;
2191
- // `generate` only for JS.
2192
- // -> the kind of bundle its entries make
1648
+ function bundleUnder(bundle, dir) {
1649
+ const at = (path) => posix.join(dir, path)
1650
+ const target = (to) => (typeof to === 'string' ? at(to) : new Map([...to].map(([platform, file]) => [platform, at(file)])))
1651
+ const { version, config, entries, modules, formats, imports, executable, reason, repo, package: pkg } = bundle
1652
+ return new Bundle({
1653
+ version, config, repo, package: pkg,
1654
+ entries: new Set([...entries].map(at)),
1655
+ modules: new Map([...modules].map(([bucket, info]) => [at(bucket), info])),
1656
+ formats: new Map([...formats].map(([file, format]) => [at(file), format])),
1657
+ imports: new Map([...imports].map(([conditions, byParent]) => [conditions, new Map([...byParent].map(([parent, specifiers]) => [at(parent), new Map([...specifiers].map(([specifier, to]) => [specifier, target(to)]))]))])),
1658
+ executable: new Set([...executable].map(at)),
1659
+ reason: reason && Object.fromEntries(Object.entries(reason).map(([consumer, files]) => [consumer, files.map(at)])),
1660
+ })
1661
+ }
1662
+
1663
+ // `build` (its paths from `cwd`) built from `from`, a directory under cwd, as from there, its paths put under `from`.
1664
+ async function solidityFrom(build, from, { cwd, ...args }) {
1665
+ if (from === cwd) return buildSolidityBundle({ ...build, ...args, cwd })
1666
+ const fromThere = (path) => posix.relative(from, posix.resolve(cwd, path)) || '.'
1667
+ const { entries, mappingFile } = build
1668
+ const bundle = await buildSolidityBundle({ ...build, ...args, cwd: from, entries: entries.map(fromThere), mappingFile: mappingFile && fromThere(mappingFile) })
1669
+ return bundleUnder(bundle, posix.relative(cwd, from))
1670
+ }
1671
+
1672
+ // Before anything is fetched (`host` an empty tree's, with `fetched: false`). -> the kind of bundle
2193
1673
  export function checkVfsOptions(name, options) {
2194
1674
  const kind = classifyEntries(name, options)
2195
1675
  if (options.metroResolver) throw new Error(`${name}: metroResolver is not supported`)
2196
1676
  // The project's Babel config is code, run from the disk.
2197
1677
  if (options.babel) throw new Error(`${name}: babel is not supported`)
2198
- const { generate = [] } = options
2199
- if (!Array.isArray(generate) || generate.some((step) => step !== 'prisma')) throw new TypeError(`${name}: generate must be an array of 'prisma'`)
2200
- if (generate.length > 0 && kind !== 'js') throw new Error(`${name}: --generate is only valid for JS bundles`)
1678
+ const { generate } = options
1679
+ if (generate !== undefined && (!Array.isArray(generate) || generate.some((step) => step !== 'prisma'))) throw new TypeError(`${name}: generate must be an array of 'prisma'`)
1680
+ if (generate?.length > 0 && kind !== 'js') throw new Error(`${name}: --generate is only valid for JS bundles`)
2201
1681
  return kind
2202
1682
  }
2203
1683
 
2204
- // A bundle from the lockfile of the project held in `vfs` alone (@exodus/stasis/vfs-bundle), `cwd` a
2205
- // path there: buildBundle's JS options, resolved through the node_modules 'pnpm', 'yarn1' or 'npm'
2206
- // would install, or its Solidity options, through the dependencies folder 'soldeer' would install; with
2207
- // nothing read from disk but tarballs and zips, nor from the environment: a Solidity bundle is
2208
- // built with foundry.toml's default profile, whatever FOUNDRY_PROFILE or remappings one sets, and no
2209
- // EXODUS_STASIS_* setting is read. The entries the stasis.config.json in cwd names in its `entries`
2210
- // (every one, where none are given) are built with their options there, as `stasis bundle` builds
2211
- // them, but none that runs a program; an option given has to agree with theirs, one left out is
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.
2216
- // `repo`, the informational `{ github, directory, commit }`,
2217
- // is the Bundle's, over what is detected in the Vfs as `stasis bundle` detects it on disk. `os`,
2218
- // `cpu` and `libc` are loadNodeModules'. Without a `packageManager`, it is the one whose lockfile
2219
- // installs cwd, where only one's does. `innermostRoot` is buildJsBundle's, for a JS bundle built
2220
- // through a State. `generate: ['prisma']` writes each project's Prisma Client into the laid-out
2221
- // tree before the scan, leaving `vfs` as it is (vfs-bundle/prisma.js). `client`, a
2222
- // @preventive/upstream/github.js client, is what a Soldeer git dependency is fetched through; without
2223
- // one, a soldeer.lock holding one is refused, naming deptree's `github`, before anything is fetched.
2224
- // `cache` is where npm packages' tarballs and version documents are kept (checkCache). Each
2225
- // dependency is recorded at the commit the tree installs it from, where deptree names one
2226
- // (pinCommits). `root` is the directory in the Vfs the bundle's paths are relative to: cwd, or for a
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.
2236
- // -> { bundle: Bundle, lockfile: Lockfile (of a JS bundle), stats, packageManager, root }
2237
- export async function buildVfsBundle(options = {}) {
1684
+ // Nothing read from disk but tarballs and zips, nor from the environment (doc/github-bundle.md).
1685
+ export async function buildVfsBundle({ lock, ...options } = {}) {
2238
1686
  const { checkVfs, vfsHost } = await import('../vfs-bundle/tree.js')
2239
1687
  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')
1688
+ if (lock !== undefined) assertOption('lock', lock)
1689
+ const host = vfsHost(options.vfs)
1690
+ const built = await buildFromLockfile('buildVfsBundle', host, options)
1691
+ const mode = lockModeOf(built.root, { EXODUS_STASIS_LOCK: lock }, host).lock
1692
+ const at = posix.join(built.root, LOCK_FILE)
1693
+ const label = posix.relative('/', at)
1694
+ if (heldToLockfile(() => built.lockfile, at, label, mode, { host, bundle: built.bundle, accounts: built.accounts }) !== undefined && mode === 'frozen') {
1695
+ loadsBeside(built.bundle.serialize(), { at: posix.join(built.root, BUNDLE_FILE), lockAbs: at, label, root: built.root, scope: built.bundle.config.scope, host })
1696
+ }
1697
+ return built
1698
+ }
1699
+
1700
+ // One tree of each lockfile stasis.config.json's `lockfiles` names, else of `packageManager`, or the one detected.
1701
+ async function treeSpecs(name, project, cwd, { packageManager, packageManagerVersion, os, why }) {
1702
+ const { checkVersion, lockfileSpecs, packageManagerFor } = await import('../vfs-bundle/tree.js')
1703
+ const lockfiles = configuredLockfiles(name, cwd, project)
1704
+ if (lockfiles === undefined) return [{ packageManager: packageManagerFor(name, project, cwd, { packageManager, packageManagerVersion, os, why }), cwd, packageManagerVersion }]
1705
+ if (packageManager !== undefined) throw new Error(`${name}: packageManager is given, and ${CONFIG_FILE}'s lockfiles names the lockfiles to lay out`)
1706
+ checkVersion(name, { packageManager, packageManagerVersion })
1707
+ return lockfileSpecs(name, project, cwd, lockfiles, os)
1708
+ }
1709
+
1710
+ async function layLazily(name, project, cwd, specs, lazy) {
1711
+ const { lazyTaken } = await import('../vfs-bundle/tree.js')
1712
+ const lazyDependencies = configFlag('lazyDependencies', name, cwd, project)
1713
+ if (lazy && !lazyDependencies) throw new Error(`${name}: lazy is given, and ${CONFIG_FILE}'s lazyDependencies is false`)
1714
+ for (const spec of specs) spec.lazy = lazy ?? (lazyDependencies && lazyTaken(project, spec.packageManager, spec.cwd))
1715
+ }
1716
+
1717
+ async function prismaOutputs(tree, project, options) {
1718
+ const [{ generatePrismaClients }, { treeHost }, { inRounds }] = await Promise.all([import('../vfs-bundle/prisma.js'), import('../vfs-bundle/tree.js'), import('../vfs-bundle/lazy.js')])
1719
+ const generating = (reads) => generatePrismaClients({ ...options, host: treeHost(tree, project, reads), root: tree.root })
1720
+ return tree.unpack === undefined ? generating() : inRounds(generating, tree.unpack)
1721
+ }
1722
+
1723
+ // As buildVfsBundle generates them under `generate: ['prisma']`, but with devDependencies whatever the config says.
1724
+ export async function prismaClientsOf({ vfs, cwd = '/', packageManager, client, cache } = {}) {
1725
+ const name = 'prismaClientsOf'
1726
+ const { layOutTrees, packageManagerOf, plannedTrees, vfsHost } = await import('../vfs-bundle/tree.js')
1727
+ const { loadGenerator, schemaProjects } = await import('../vfs-bundle/prisma.js')
1728
+ const project = vfsHost(vfs)
1729
+ cwd = project.realpath(posix.resolve('/', cwd))
1730
+ const specs = (await treeSpecs(name, project, cwd, { packageManager })).filter((spec) => packageManagerOf(name, spec.packageManager).kind === 'js')
1731
+ await layLazily(name, project, cwd, specs)
1732
+ if (!plannedTrees({ project, specs }).some(({ root, projects }) => schemaProjects(project, root, projects).size > 0)) return []
1733
+ loadGenerator()
1734
+ const tree = await layOutTrees({ project, specs, client, cache, installDevDependencies: true })
1735
+ return (await prismaOutputs(tree, project, { projects: schemaProjects(project, tree.root, tree.projects), strict: false, shared: true })).outputs
1736
+ }
1737
+
1738
+ // `project` is a Vfs, which `vfs` is, or the disk, for `stasis bundle --deptree`, which gives the `plan` of builds.
1739
+ 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, why } = {}) {
1740
+ const { checkCache, checkKind, checkLazy, checkTarget, layOutTrees, packageManagerOf, pinFromTree, treeHost, unpackedOf, vfsHost } = await import('../vfs-bundle/tree.js')
2253
1741
  const { configuredBuilds } = await import('./bundle-options.js')
2254
1742
  const { checkGlob } = await import('../vfs-bundle/glob.js')
2255
1743
  checkTarget(name, { os, cpu, libc })
@@ -2259,96 +1747,154 @@ async function buildFromLockfile(name, project, { vfs, packageManager, cwd = '/'
2259
1747
  if (repo !== undefined) repo = new Bundle({ repo }).repo
2260
1748
  // A real path, as every file the scan reaches is.
2261
1749
  cwd = project.realpath(posix.resolve('/', cwd))
2262
- packageManager = packageManagerFor(name, project, cwd, { packageManager, packageManagerVersion, os })
2263
- const pm = packageManagerOf(name, packageManager)
2264
- if (lazy) checkLazy(name, packageManager)
2265
- // The entries stasis.config.json's `entries` names, every one where none are given, each built
2266
- // with its options there, which the options given agree with (configuredBuilds); else built with
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.
1750
+ const specs = await treeSpecs(name, project, cwd, { packageManager, packageManagerVersion, os, why })
1751
+ const packageManagers = specs.map((spec) => spec.packageManager)
1752
+ const pms = packageManagers.map((one) => packageManagerOf(name, one))
1753
+ if (lazy) for (const one of packageManagers) checkLazy(name, one)
1754
+ // The builds stasis.config.json's `entries` makes, whose union takes `extra` once; else the options given.
2270
1755
  const configured = plan === undefined ? configuredBuilds(name, { cwd, host: project, entries: options.entries, options }) : null
2271
1756
  const { builds, bundleExtra } = plan ?? { builds: configured?.builds ?? [options], bundleExtra: configured !== null }
2272
1757
  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).
1758
+ // That the Vfs can be globbed for `extra` is checked before anything is fetched.
2275
1759
  if (parseExtraOption(CONFIG_FILE, readConfigFile(name, cwd, project)?.extra).some((pattern) => !pattern.startsWith('!'))) checkGlob()
2276
1760
  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])
2282
- // Checked before anything is fetched: an entry out of what the tree installs is in the project
2283
- // already. (A Solidity entry that is no .sol file is a directory, skipped where it is missing.)
2284
- for (const entry of builds.flatMap((build) => build.entries)) {
2285
- const abs = posix.resolve(cwd, entry)
2286
- if (pm.kind === 'sol' && !abs.endsWith('.sol')) continue
2287
- if (!posix.relative(cwd, abs).split('/').includes(pm.installs) && project.stat(abs) === null) throw new Error(`entry not found: ${abs}`)
2288
- }
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.
1761
+ await layLazily(name, project, cwd, specs, lazy)
1762
+ // Prisma Clients: asked for, every project's, strictly; else, of a JS build of a Vfs, those the entries reach,
1763
+ // where `generatePrisma` leaves it; or none, their outputs read all the same.
1764
+ const generatePrisma = configFlag('generatePrisma', name, cwd, project)
1765
+ if (generate?.length > 0 && !generatePrisma) throw new Error(`${name}: generate is given, and ${CONFIG_FILE}'s generatePrisma is false`)
1766
+ const vfsJs = vfs !== undefined && pms.some((pm) => pm.kind === 'js')
1767
+ const prismaGeneration = generate?.includes('prisma') ? 'asked' : !vfsJs ? null : generate === undefined && generatePrisma ? 'detected' : 'off'
1768
+ const kinds = builds.map((build) => {
1769
+ const kind = checkVfsOptions(name, { ...build, lockfile: options.lockfile, generate, cwd, host: project })
1770
+ checkKind(name, kind, packageManagers)
1771
+ return kind
1772
+ })
1773
+ // An entry out of what the trees install is in the project already: checked before anything is fetched. (A
1774
+ // Solidity entry that is no .sol file is a directory, skipped where it is missing.)
1775
+ const installs = new Set(pms.map((pm) => pm.installs))
1776
+ for (const [i, build] of builds.entries()) {
1777
+ for (const entry of build.entries) {
1778
+ const abs = posix.resolve(cwd, entry)
1779
+ if (kinds[i] === 'sol' && !abs.endsWith('.sol')) continue
1780
+ if (!posix.relative(cwd, abs).split('/').some((part) => installs.has(part)) && project.stat(abs) === null) throw new Error(`entry not found: ${abs}`)
1781
+ }
1782
+ }
1783
+ // A Solidity build is built from cwd, but where a Soldeer project the trees lay out is below it: then each .sol
1784
+ // file is built from the innermost project holding it, with its foundry.toml and remappings.
1785
+ const solRoots = specs.filter((spec, i) => pms[i].kind === 'sol' && spec.cwd !== cwd).map((spec) => spec.cwd)
1786
+ const projectOf = (file) => solRoots.filter((dir) => isPathWithin(dir, posix.resolve(cwd, file))).reduce((inner, dir) => (isPathWithin(inner, dir) ? dir : inner), cwd)
1787
+ const solidityBuilt = async (build, args) => {
1788
+ if (solRoots.length === 0) return [{ bundle: await buildSolidityBundle({ ...build, ...args }), root: cwd }]
1789
+ const out = []
1790
+ for (const [from, entries] of Map.groupBy(expandSolidityEntries(cwd, normalizeEntries(build.entries, cwd), args.host), projectOf)) {
1791
+ // eslint-disable-next-line no-await-in-loop -- one project at a time, in the order its warnings read
1792
+ out.push({ bundle: await solidityFrom({ ...build, entries }, from, args), root: cwd })
1793
+ }
1794
+ return out
1795
+ }
1796
+ const laidOut = specs.length > 1 ? 'Laid out the trees' : specs[0].lazy ? 'Laid out the tree' : 'Installed the tree'
1797
+ const tree = await debugTimed(laidOut, () => layOutTrees({ project, specs, os, cpu, libc, client, cache, installDevDependencies, refuseInstalled }))
1798
+ const { inRounds, warningsHeld } = await import('../vfs-bundle/lazy.js')
1799
+ // The Prisma Clients generated once before any build, so that each of a lazy tree's rounds reads the same project.
2294
1800
  let outside = project
2295
- if (generate.includes('prisma')) {
2296
- const { generatePrismaClients, withPrismaClients } = await import('../vfs-bundle/prisma.js')
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.
1801
+ let expected = []
1802
+ const prisma = prismaGeneration === null ? null : await import('../vfs-bundle/prisma.js')
1803
+ if (prisma !== null) {
1804
+ // Before any `prisma` is read. Nothing done, nor said, where no project has a schema.
1805
+ const withSchema = prisma.schemaProjects(project, tree.root, tree.projects)
1806
+ const strict = prismaGeneration === 'asked'
1807
+ // By its real path, as the scan reaches it, where the project holds it.
1808
+ const entryPath = (entry) => {
1809
+ const path = posix.resolve(cwd, entry)
1810
+ try {
1811
+ return project.realpath(path)
1812
+ } catch {
1813
+ return path
1814
+ }
1815
+ }
1816
+ const reached = () => prisma.reachedProjects(project, tree.root, tree.projects, builds.flatMap((build) => build.entries.map(entryPath)))
1817
+ const projects = strict || withSchema.size === 0 ? withSchema : withSchema.intersection(reached())
1818
+ if (projects.size > 0) {
1819
+ const off = prismaGeneration === 'off' ? `generation is off: ${generate === undefined ? `${CONFIG_FILE}'s generatePrisma is false` : 'generate is [] (--no-generate)'}` : undefined
1820
+ const clients = off === undefined ? await debugTimed('Generated the Prisma Clients', () => prismaOutputs(tree, project, { projects, strict })) : { outputs: [] }
1821
+ // What their generators would write, read from the project alone: a lazy tree fetches nothing for it.
1822
+ expected = prisma.expectedOutputs({ host: project, root: tree.root, projects, ...clients, off })
1823
+ if (clients.outputs.length > 0) outside = vfsHost(prisma.withPrismaClients(vfs, clients.outputs))
1824
+ }
1825
+ }
1826
+ const generated = expected.length === 0 ? undefined : prisma.generatedMiss(expected)
1827
+ // For a lazy tree, in rounds, each build keeping what its scans read across them in a memo of its own.
2304
1828
  const memos = tree.unpack === undefined ? undefined : builds.map(() => new Map())
2305
1829
  const attempt = async (reads) => {
2306
1830
  const host = treeHost(tree, outside, reads)
2307
1831
  const args = { cwd, host, env }
1832
+ // A Vfs holds nothing built: a build broken at load by a miss --typescript resolves is built with it
1833
+ // instead, saying nothing of what the first warned.
1834
+ const jsBuilt = async (build, memo) => {
1835
+ const js = (typescript, kept = memo) => buildJs({ ...build, ...args, typescript, innermostRoot, extra: !bundleExtra, outputsAt, memo: kept, generated })
1836
+ if (vfs === undefined || build.typescript) return js(build.typescript)
1837
+ const first = await warningsHeld(() => js(false))
1838
+ if (first.error?.resolvesUnderTypescript) return js(true, memo && nestedMap(memo, 'typescript'))
1839
+ for (const warning of first.warnings) console.warn(...warning)
1840
+ if (first.error) throw first.error
1841
+ return first.value
1842
+ }
2308
1843
  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.
1844
+ // A build of a round that asked for a package already is void, whatever it threw: the builds after it
1845
+ // are run all the same, for all else they ask for, and the round is set aside after.
2311
1846
  let voided
1847
+ const ofBuilds = []
2312
1848
  for (const [i, build] of builds.entries()) {
2313
1849
  let built
2314
1850
  try {
2315
1851
  // 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] })
1852
+ built = kinds[i] === 'sol' ? await solidityBuilt(build, args) : [await jsBuilt(build, memos?.[i])]
2317
1853
  } catch (error) {
2318
1854
  if (reads === undefined || reads.asked.size === 0) throw error
2319
1855
  voided ??= { error }
2320
1856
  continue
2321
1857
  }
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)
1858
+ for (const one of built) {
1859
+ // Rooted at cwd, where `stasis bundle` detects its repo.
1860
+ if (repo === undefined && !one.stateBuilt) one.bundle.repo ??= detectRepo(cwd, host)
1861
+ builts.push(one)
1862
+ ofBuilds.push(build)
1863
+ }
2325
1864
  }
2326
1865
  if (voided !== undefined) throw voided.error
2327
- // Merged as bundleCommand merges its builds.
2328
1866
  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 }
1867
+ const union = unionOfBuilds(name, ofBuilds, builts, builts.map((built) => built.root))
1868
+ // `extra` once: to the union; else to the one build given, unless its State took it.
1869
+ return { root, builts, host, ...bundleExtra || !builts[0].stateBuilt ? withExtra(union, root, { outputs: outputsAt(root), resources: bundleExtra ? undefined : builds[0].resources, host, name }) : union }
2334
1870
  }
2335
1871
  const since = counts()
2336
1872
  const built = tree.unpack === undefined
2337
1873
  ? await debugTimed('Built', () => attempt(), () => builtSince(since))
2338
1874
  : await inRounds(...debugging() ? reportedRounds(attempt, tree.unpack) : [attempt, tree.unpack])
2339
1875
  const { bundle, lockfile, root } = built
1876
+ // After the lazy rounds, so only the full tree's build is checked.
1877
+ assertPolicies(name, { builds, builts: built.builts, bundle, root, host: project, files: built.host, label: `${name}: ${CONFIG_FILE}` })
1878
+ // Said where the bundle carries what generators stasis didn't run would write, as the repo has it.
1879
+ if (prismaGeneration === 'detected') for (const warning of prisma.committedWarnings(expected, bundle, root)) console.warn(warning)
2340
1880
  if (repo !== undefined) bundle.repo = repo
2341
- pinCommits(bundle, root, tree)
2342
- const unpacked = tree.lazy?.unpacked.size ?? tree.stats.installed ?? tree.stats.dependencies
2343
- return { bundle, lockfile: lockfile?.(), stats: { ...tree.stats, unpacked }, packageManager, root }
1881
+ const accounts = pinFromTree(bundle, root, tree)
1882
+ const unpacked = unpackedOf(tree)
1883
+ // Built when first read: `stasis github-bundle` and `stasis bundle --deptree` read it for --lockfile alone.
1884
+ let lock
1885
+ return { bundle, get lockfile() { return (lock ??= lockfile && withAttested(lockfile(), bundle)) }, stats: { ...tree.stats, unpacked }, packageManager: packageManagers.length === 1 ? packageManagers[0] : undefined, packageManagers, root, accounts, accountsOf: (modules) => pinFromTree({ modules }, root, tree) }
1886
+ }
1887
+
1888
+ // `to` (a Lockfile or a Bundle) with the ATTESTED_METADATA `from`'s records of the same buckets carry, in place
1889
+ // of its own: a build's lockfile, taken before its bundle was pinned, with the bundle's.
1890
+ function withAttested(to, from) {
1891
+ for (const [dir, info] of to.modules) if (from.modules.has(dir)) to.modules.set(dir, withAttestedOf(info, from.modules.get(dir)))
1892
+ return to
2344
1893
  }
2345
1894
 
2346
- // EXODUS_STASIS_DEBUG=1 or true.
2347
1895
  export const debugging = () => process.env.EXODUS_STASIS_DEBUG === '1' || process.env.EXODUS_STASIS_DEBUG === 'true'
2348
1896
 
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.
1897
+ // The time a --metro build's stages took, all told, in milliseconds, for EXODUS_STASIS_DEBUG.
2352
1898
  const stageTimes = { scan: 0, metro: 0, configs: 0, carrying: 0 }
2353
1899
  const staged = (stage, fn) => {
2354
1900
  const at = performance.now()
@@ -2360,9 +1906,8 @@ const staged = (stage, fn) => {
2360
1906
  }
2361
1907
  const seconds = (ms) => `${(ms / 1000).toFixed(2)} s`
2362
1908
 
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
1909
+ // `run()`, said on stderr once it settles under EXODUS_STASIS_DEBUG. Written, not warned: inRounds holds back
1910
+ // what a round it sets aside warns.
2366
1911
  export async function debugTimed(what, run, detail = () => '') {
2367
1912
  if (!debugging()) return run()
2368
1913
  const at = performance.now()
@@ -2373,24 +1918,17 @@ export async function debugTimed(what, run, detail = () => '') {
2373
1918
  }
2374
1919
  }
2375
1920
 
2376
- // scanCounts and stageTimes as they are, for builtSince.
2377
1921
  const counts = () => ({ files: { ...scanCounts }, stages: { ...stageTimes } })
2378
1922
  const STAGES = { scan: 'scan', metro: "Metro's additions", configs: 'configs and toolchain', carrying: 'carrying' }
2379
1923
 
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
1924
  function builtSince(since) {
2383
1925
  const files = Object.keys(scanCounts).map((key) => `${scanCounts[key] - since.files[key]} ${key}`).join(', ')
2384
1926
  const times = Object.keys(STAGES).filter((key) => stageTimes[key] > since.stages[key]).map((key) => `${STAGES[key]} ${seconds(stageTimes[key] - since.stages[key])}`)
2385
1927
  return ` (files ${files}${times.length > 0 ? `; ${times.join(', ')}` : ''})`
2386
1928
  }
2387
1929
 
2388
- // A package's name, as its directory in a tree says it: what follows its last node_modules.
2389
1930
  const packageName = (dir) => dir.slice(dir.lastIndexOf('/node_modules/') + '/node_modules/'.length)
2390
1931
 
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
1932
  function reportedRounds(attempt, unpack) {
2395
1933
  let round = 0
2396
1934
  return [
@@ -2404,108 +1942,37 @@ function reportedRounds(attempt, unpack) {
2404
1942
  ]
2405
1943
  }
2406
1944
 
2407
- // Programmatic equivalent of `stasis bundle`: build and return an in-memory Bundle without
2408
- // writing to disk. Files are attributed to the `bundle` consumer (`bundle:metro` with `metro`).
2409
- // Option applicability (--mapping|--manifests/.sol, --scope|--conditions|--mainFields|--metro|--jsx|
2410
- // --flow|--typescript|--define/JS) is enforced by classifyEntries. `define` gives globals their
2411
- // values (`{ __DEV__: false, 'process.env.NODE_ENV': 'production' }`), whose dead branches' imports
2412
- // are left out (see define.js).
1945
+ // `stasis bundle`'s Bundle, not written, a JS one held to the project's stasis.lock.json as `stasis bundle` holds it.
2413
1946
  export async function buildBundle({ cwd = process.cwd(), env = process.env, entries, mappingFile, manifests = false, scope, 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 } = {}) {
2414
1947
  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 }
2415
- return (await buildOfKind(classifyEntries('buildBundle', options), options)).bundle
2416
- }
2417
-
2418
- // Run `stasis bundle`: build a brotli-compressed bundle and write it to `output`
2419
- // (stasis.code.br by default, `-` for stdout; the summary goes to stderr so it never
2420
- // interleaves with binary stdout). An optional JS `lockfile` attests every bundled file;
2421
- // with `--conditions` it attests the conditions-selected graph, so a plain
2422
- // `stasis run --lock=frozen` (which doesn't replay them) fails closed -- pair it with
2423
- // `--bundle=load` or replay the conditions. `add` unions the fresh build into the bundle
2424
- // already on disk (strict; a conflicting file throws) and can't target stdout. `builds`, in place
2425
- // of `entries` and the options building them, is several builds' (each `{ entries, ...options }`,
2426
- // as stasis.config.json's `bundle` gives an entry its own, see bundle-options.js), whose bundles
2427
- // (and lockfiles) are one's: their strict union, rooted where every one of them is.
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 } = {}) {
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 }
2440
- if (builds !== undefined) {
2441
- if (!Array.isArray(builds) || builds.length === 0) throw new TypeError('bundleCommand: builds must be a non-empty array')
2442
- const given = Object.entries(options).find(([key, value]) => !['cwd', 'env'].includes(key) && value !== undefined && value !== false && value !== null && !(Array.isArray(value) && value.length === 0))
2443
- if (given) throw new TypeError(`bundleCommand: ${given[0]} can't be combined with builds (each build sets its own)`)
2444
- }
2445
- // Each build's options, the defaults above where it leaves one out.
2446
- const plans = (builds ?? [{}]).map((build) => ({ ...options, ...build, cwd, env }))
2447
- const kinds = plans.map((plan) => classifyEntries('bundleCommand', { ...plan, lockfile }))
2448
-
2449
- // --add has nothing to merge into on stdout (write-only).
2450
- if (add && output === '-') {
2451
- throw new Error('bundleCommand: --add cannot be combined with --output=- (nothing to merge into on stdout)')
2452
- }
2453
-
2454
- // The files this build writes, never taken into `extra`, given the root its paths are relative to:
2455
- // the bundle (by default at that root, see `target` below) and the lockfile.
2456
- const outputsAt = (root) => [output ?? join(root, DEFAULT_BUNDLE_FILE), lockfile].filter((file) => file !== undefined && file !== '-').map((file) => resolve(cwd, file))
2457
- // With `builds` (stasis.config.json's entries, however many builds they make), `extra` is the
2458
- // bundle's, not any build's: added to their union below.
2459
- const bundleExtra = builds !== undefined
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
- }
2495
- }
2496
- // By default the bundle goes there, where `stasis run --bundle=load` discovers it. In cwd below
2497
- // the root it would load as rooted at cwd, and root the next `stasis bundle` there too, out of the
2498
- // files above it.
2499
- const target = output ?? relative(resolve(cwd), join(root, DEFAULT_BUNDLE_FILE))
1948
+ const built = await buildOfKind(classifyEntries('buildBundle', options), options)
1949
+ const root = built.root ?? resolve(cwd)
1950
+ assertPolicies('buildBundle', { bundle: built.bundle, root })
1951
+ heldOf(pinnedOf(built, root), root, { cwd, env })?.loads(built.bundle)
1952
+ return built.bundle
1953
+ }
2500
1954
 
2501
- let { bundle } = union
1955
+ // Run `stasis bundle` (doc/bundle.md).
1956
+ export async function bundleCommand({ cwd = process.cwd(), output, lockfile, lockfileOnly = false, brotliQuality, add = false, ...options } = {}) {
1957
+ if (add && output === '-') throw new Error('bundleCommand: --add cannot be combined with --output=- (nothing to merge into on stdout)')
1958
+ const lockfileOnlyRefused = lockfileOnly ? lockfileOnlyError({ lockfile, output, add, brotliQuality }) : null
1959
+ if (lockfileOnlyRefused !== null) throw new Error(`bundleCommand: ${lockfileOnlyRefused}`)
1960
+ const built = await buildOfCommand({ ...options, cwd, output, lockfile })
1961
+ const { root } = built
1962
+ // By default at the root, where `stasis run --bundle=load` discovers it: in cwd below the root it would root
1963
+ // the next run and `stasis bundle` there, out of the files above it.
1964
+ const target = output ?? relative(resolve(cwd), join(root, BUNDLE_FILE))
1965
+
1966
+ let { bundle } = built
2502
1967
  // Only a JS bundle has a lockfile (classifyEntries refuses one for the others).
2503
- let lockData = lockfile ? union.lockfile().serialize() : undefined
1968
+ const mode = built.held?.lock
1969
+ if (mode === 'replace' && lockfile === undefined) throw new Error(`bundleCommand: lock = replace rewrites ${built.held.label}: give --lockfile`)
1970
+ if (lockfile !== undefined && ['frozen', 'ignore', 'none'].includes(mode)) throw new Error(`bundleCommand: --lockfile: lock = ${mode} writes no lockfile`)
1971
+ let lockData = lockfile ? built.held.lockfile().serialize() : undefined
2504
1972
 
2505
- // --add: union the fresh build into the existing on-disk bundle; a conflicting file throws. Skipped when nothing is on disk.
2506
1973
  const outAbs = target === '-' ? undefined : resolve(cwd, target)
2507
1974
  const lockAbs = lockData ? resolve(cwd, lockfile) : undefined
2508
- // Files the pre-existing bundle carried; undefined when no merge happened (the summary's merged sentinel).
1975
+ // What the bundle on disk carried; undefined where nothing is merged.
2509
1976
  let mergedFrom
2510
1977
  if (add && existsSync(outAbs)) {
2511
1978
  let existing
@@ -2514,38 +1981,219 @@ export async function bundleCommand({ cwd = process.cwd(), env = process.env, ho
2514
1981
  } catch (cause) {
2515
1982
  throw new Error(`bundleCommand: --add failed to read the existing bundle at ${target}`, { cause })
2516
1983
  }
1984
+ refuseOtherRoot(existing, `the bundle at ${target}`, root, { cwd, at: outAbs })
1985
+ // What the bundle and lockfile on disk carry is written again, so held to the config's Policy too.
1986
+ const policy = parsePolicy(CONFIG_FILE, readConfigFile('bundleCommand', root))
1987
+ const links = linksOn(root)
1988
+ policy?.assertArtifact(existing, { hint: recordedIn(target), links })
2517
1989
  mergedFrom = existing.sources.size
1990
+ // What it carries already is held to the lockfile too, and to how its packages are installed, read anew
1991
+ // where the build has no account of them: as is what the lockfile there records, written again.
1992
+ const heldAsInstalled = (artifact, label) => {
1993
+ const unread = new Map([...artifact.modules].filter(([dir, info]) => !built.accounts.has(dir) && attestsAny(info)))
1994
+ heldToAccounts(artifact.modules, new Map([...built.accountsOf(unread), ...built.accounts]), `${label}: `)
1995
+ }
1996
+ built.held?.hold(deferredLockfile(existing))
1997
+ heldAsInstalled(existing, target)
2518
1998
  bundle = existing.merge(bundle)
2519
- // Keep the companion lockfile consistent with the merged bundle: union an existing one.
2520
- // With none on disk we can't attest the pre-existing bundle's files (their hashes live
2521
- // only in that missing lockfile), so refuse rather than write an under-attesting lockfile.
1999
+ // The lockfile there merged too; in replace, which reads none, the bundle there recorded anew.
2522
2000
  if (lockData) {
2523
- if (!existsSync(lockAbs)) {
2524
- throw new Error(`bundleCommand: --add can't write a complete lockfile at ${lockfile}: the bundle at ${target} already carries files a fresh lockfile wouldn't attest, and there's no existing lockfile to merge into. Write --lockfile alongside the bundle from the first build, or drop --lockfile.`)
2525
- }
2526
2001
  let existingLock
2527
- try {
2528
- existingLock = Lockfile.parse(readFileSync(lockAbs, 'utf8'))
2529
- } catch (cause) {
2530
- throw new Error(`bundleCommand: --add failed to read the existing lockfile at ${lockfile}`, { cause })
2002
+ if (mode === 'replace') {
2003
+ existingLock = deferredLockfile(existing)()
2004
+ } else {
2005
+ // With none on disk the bundle's files can't be attested (their hashes live only in the missing
2006
+ // lockfile): refused rather than written under-attesting.
2007
+ if (!existsSync(lockAbs)) {
2008
+ throw new Error(`bundleCommand: --add can't write a complete lockfile at ${lockfile}: the bundle at ${target} already carries files a fresh lockfile wouldn't attest, and there's no existing lockfile to merge into. Write --lockfile alongside the bundle from the first build, or drop --lockfile.`)
2009
+ }
2010
+ try {
2011
+ existingLock = Lockfile.parse(readFileSync(lockAbs, 'utf8'))
2012
+ } catch (cause) {
2013
+ throw new Error(`bundleCommand: --add failed to read the existing lockfile at ${lockfile}`, { cause })
2014
+ }
2015
+ refuseOtherRoot(existingLock, `the lockfile at ${lockfile}`, root, { cwd, at: lockAbs })
2016
+ // It may record more than the bundle beside it (a broader run's capture).
2017
+ policy?.assertArtifact(existingLock, { hint: recordedIn(lockfile), links })
2018
+ heldAsInstalled(existingLock, lockfile)
2531
2019
  }
2532
2020
  lockData = existingLock.merge(Lockfile.parse(lockData)).serialize()
2533
2021
  }
2022
+ } else if (add && lockData && mode !== 'replace' && existsSync(lockAbs)) {
2023
+ // No bundle to add to, but a lockfile: replaced, as one that can't be read is, unless rooted elsewhere.
2024
+ let existingLock
2025
+ try {
2026
+ existingLock = Lockfile.parse(readFileSync(lockAbs, 'utf8'))
2027
+ } catch { /* replaced */ }
2028
+ if (existingLock !== undefined) refuseOtherRoot(existingLock, `the lockfile at ${lockfile}`, root, { cwd, at: lockAbs })
2534
2029
  }
2535
2030
 
2031
+ // Serialized under --lockfile-only too: never write a lockfile of a bundle that couldn't be written.
2536
2032
  const serialized = bundle.serialize()
2033
+ built.held?.loads(bundle, { text: serialized, at: outAbs })
2537
2034
  const files = [...bundle.sources.keys()]
2538
2035
  const modules = bundle.modules
2539
2036
 
2540
- const dest = writeBundle(cwd, target, serialized, brotliQuality)
2037
+ const dest = lockfileOnly ? undefined : writeBundle(cwd, target, serialized, brotliQuality)
2541
2038
  if (lockData) writeFile(lockAbs, lockData)
2542
2039
  // The bundle's paths are relative to its root; shown relative to cwd, as `dest` is.
2543
2040
  const fromDir = posix.join(toPosix(relative(resolve(cwd), root)), outermostDir(files, root))
2544
- // On a merge, report newly-added files alongside the totals; a fresh write keeps the plain line.
2545
- if (mergedFrom !== undefined) {
2041
+ if (lockfileOnly) {
2042
+ console.warn(lockedSummary(files.length, modules, fromDir, lockfile))
2043
+ } else if (mergedFrom !== undefined) {
2546
2044
  const added = files.length - mergedFrom
2547
2045
  console.warn(`[stasis] Added ${added} file${added === 1 ? '' : 's'} (${files.length} total in ${packagesLabel(modules)}) from ${fromDir} to ${dest}`)
2548
2046
  } else {
2549
2047
  console.warn(bundledSummary(files.length, modules, fromDir, dest))
2550
2048
  }
2049
+ // A stasis file written in a package below the root roots stasis run and stasis bundle there.
2050
+ const shown = (path) => relative(resolve(cwd), path) || '.'
2051
+ for (const file of [lockfileOnly ? undefined : outAbs, lockData ? lockAbs : undefined]) {
2052
+ const dir = file && dirname(file)
2053
+ if (!file || !STASIS_ROOT_FILES.includes(basename(file)) || dir === root || !isPathWithin(root, dir)) continue
2054
+ if (diskHost.stat(join(dir, 'package.json')) === null) continue
2055
+ console.warn(`[stasis] ${shown(file)} roots stasis run and stasis bundle in the package at ${shown(dir)}, but its paths are relative to ${shown(root)}: write it at ${shown(join(root, basename(file)))}`)
2056
+ }
2057
+ }
2058
+
2059
+ // A build's lockfile held to the one `lockfile` names, else its project's, by its runs' lock mode.
2060
+ function heldOf({ bundle, lockfile: built, accounts }, root, { cwd, env, lockfile }) {
2061
+ if (!built) return undefined
2062
+ const { lock, file } = lockModeOf(root, env)
2063
+ const at = lockfile ? resolve(cwd, lockfile) : file ?? join(root, LOCK_FILE)
2064
+ const label = relative(resolve(cwd), at)
2065
+ const hold = (other, options) => heldToLockfile(other, at, label, lock, options)
2066
+ const fresh = hold(built, { bundle, accounts })
2067
+ const loads = (written, { text, at: bundleAt = join(root, BUNDLE_FILE) } = {}) => {
2068
+ if (lock === 'frozen') loadsBeside(text ?? written.serialize(), { at: bundleAt, lockAbs: at, label, root, scope: written.config.scope })
2069
+ }
2070
+ return { lock, label, lockfile: () => fresh ?? built(), hold, loads }
2071
+ }
2072
+
2073
+ // The lock mode and EXODUS_STASIS_LOCK_FILE of a run at `root` that writes a bundle, as a build does.
2074
+ function lockModeOf(root, env = process.env, host = diskHost) {
2075
+ const config = new Config({ env: { EXODUS_STASIS_LOCK: env.EXODUS_STASIS_LOCK, EXODUS_STASIS_LOCK_FILE: env.EXODUS_STASIS_LOCK_FILE }, bundle: 'replace' })
2076
+ const text = readFileSyncMaybe(root, CONFIG_FILE, 'utf-8', host)
2077
+ if (text !== null) config.loadConfig(text)
2078
+ // A relative one of the working directory's, as a run reads it.
2079
+ return { lock: config.lock, file: config.lockFile && resolve(config.lockFile) }
2080
+ }
2081
+
2082
+ // `built()` held to the lockfile at `lockAbs` by `mode`: in add, agreeing with it on all both record; in frozen,
2083
+ // that too, and it must be there and attest every file built; in none, it must not be there.
2084
+ function heldToLockfile(built, lockAbs, label, mode, { host = diskHost, bundle, accounts = [] } = {}) {
2085
+ if (mode !== 'add' && mode !== 'frozen' && mode !== 'none') return undefined
2086
+ const text = readFileSyncMaybe(dirname(lockAbs), basename(lockAbs), 'utf-8', host)
2087
+ if (text === null && mode !== 'frozen') return undefined
2088
+ // A build of none (a Solidity one) is held to nothing.
2089
+ const fresh = built()
2090
+ if (fresh === undefined) return undefined
2091
+ if (text === null) throw new Error(`${label} isn't there, and lock = frozen holds the build to it`)
2092
+ if (mode === 'none') throw new Error(`${label} is there, which lock = none refuses`)
2093
+ let lock
2094
+ try {
2095
+ lock = Lockfile.parse(text)
2096
+ } catch (cause) {
2097
+ throw new Error(`${label}: not a valid stasis lockfile`, { cause })
2098
+ }
2099
+ const merged = lock.mergeAgreeing(fresh, label, { held: true })
2100
+ heldToAccounts(lock.modules, accounts, `${label}: `)
2101
+ // What it alone records of a package no account tells the install of is the build's too: kept.
2102
+ for (const to of bundle ? [fresh, bundle] : [fresh]) withAttested(to, merged)
2103
+ if (mode === 'frozen') {
2104
+ const recorded = moduleFileKeys(lock.modules, { scope: lock.config.scope })
2105
+ for (const file of moduleFileKeys(fresh.modules, { scope: fresh.config.scope })) {
2106
+ if (!recorded.has(file)) throw new Error(`${label} (lock = frozen) doesn't attest ${file}`)
2107
+ }
2108
+ }
2109
+ return fresh
2110
+ }
2111
+
2112
+ // That the bundle of `text`, to be written at `at`, loads beside the lockfile at `lockAbs` as `stasis run
2113
+ // --lock=frozen --bundle=load` loads them; stasis.config.json is read without the bundle paths it names.
2114
+ function loadsBeside(text, { at, lockAbs, label, root, scope, host = diskHost }) {
2115
+ // Not EXODUS_STASIS_BROTLI_QUALITY's, which says how a bundle is written.
2116
+ const bytes = brotliCompressSync(text, { params: { [zlibConstants.BROTLI_PARAM_QUALITY]: 1 } })
2117
+ const readFile = (path) => {
2118
+ if (path === at) return bytes
2119
+ const data = host.readFile(path)
2120
+ if (basename(path) !== CONFIG_FILE) return data
2121
+ const { bundleFile: _bundleFile, resourcesBundleFile: _resourcesBundleFile, ...config } = JSON.parse(data.toString('utf8'))
2122
+ return Buffer.from(JSON.stringify(config))
2123
+ }
2124
+ const served = { ...host, readFile }
2125
+ try {
2126
+ // Rooted where the build is, as a run finding the lockfile there is: given explicitly, it roots nothing.
2127
+ new State(root, { lock: 'frozen', lockFile: lockAbs, bundle: 'load', bundleFile: at, scope, host: served, claim: false, env: { PROJECT_CWD: root } }) // eslint-disable-line no-new -- its constructor is the check
2128
+ } catch (cause) {
2129
+ throw new Error(`${label} (lock = frozen): ${cause.message}`, { cause })
2130
+ }
2131
+ }
2132
+
2133
+ // --add to an artifact whose paths are relative to another root would key its records and the build's apart, never
2134
+ // held to each other; one whose root (where its packages and entries are) can't be told is added to as ever.
2135
+ function refuseOtherRoot(artifact, what, root, { cwd, at }) {
2136
+ const baseDir = resolve(cwd)
2137
+ const named = [...artifact.modules].filter(([, info]) => typeof info.name === 'string')
2138
+ const ecosystemOf = ([dir, { ecosystem }]) => dependencyEcosystem(dir, ecosystem)
2139
+ const workspace = named.filter((bucket) => ecosystemOf(bucket) === undefined)
2140
+ const [packages, entries] = workspace.length > 0 ? [workspace, [...artifact.entries]] : [named.filter((bucket) => ecosystemOf(bucket) === 'npm'), []]
2141
+ if (packages.length === 0) return
2142
+ const holds = (dir) => packages.every(([pkg, { name }]) => readPackageJson(dir, moduleFileKey(pkg, 'package.json'))?.name === name) &&
2143
+ entries.every((entry) => isFile(join(dir, entry)))
2144
+ const theirs = [...new Set([root, ...ancestors(baseDir), ...ancestors(dirname(at))])].find((dir) => holds(dir))
2145
+ if (theirs === undefined || theirs === root) return
2146
+ const shown = (dir) => relative(baseDir, dir) || '.'
2147
+ throw new Error(`bundleCommand: --add can't add to ${what}: its paths are relative to ${shown(theirs)} and this build's to ${shown(root)}${rootedBy(root)}; rebuild it without --add`)
2148
+ }
2149
+
2150
+ // The bundle bundleCommand would write (but for `add`), parsed back as `stasis audit` would read it.
2151
+ export async function buildCommandBundle(options = {}) {
2152
+ const { bundle } = await buildOfCommand(options)
2153
+ return Bundle.parse(bundle.serialize())
2154
+ }
2155
+
2156
+ // bundleCommand's build, not written; `extra` never takes in `output` and `lockfile`, where it would write.
2157
+ async function buildOfCommand({ cwd = process.cwd(), env = process.env, home, entries, mappingFile, manifests = false, output, scope, lockfile, conditions, mainFields, platforms, metro, metroResolver, babel = false, babelAliases, jsx = false, flow = false, typescript = false, tsconfig, resources, define, packageJSON = false, cargo = false, cargoFeatures = [], cargoNoDefaultFeatures = false, cargoAllFeatures = false, cargoTarget = null, cargoManifests = false, builds, deptree = false } = {}) {
2158
+ 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 }
2159
+ if (builds !== undefined) {
2160
+ if (!nonEmpty(builds)) throw new TypeError('bundleCommand: builds must be a non-empty array')
2161
+ const given = Object.entries(options).find(([key, value]) => !['cwd', 'env'].includes(key) && value !== undefined && value !== false && value !== null && !(Array.isArray(value) && value.length === 0))
2162
+ if (given) throw new TypeError(`bundleCommand: ${given[0]} can't be combined with builds (each build sets its own)`)
2163
+ }
2164
+ // Each build's options, the defaults above where it leaves one out.
2165
+ const plans = (builds ?? [{}]).map((build) => ({ ...options, ...build, cwd, env }))
2166
+ const kinds = plans.map((plan) => classifyEntries('bundleCommand', { ...plan, lockfile }))
2167
+ const outputsAt = (root) => [output ?? join(root, BUNDLE_FILE), lockfile].filter((file) => file !== undefined && file !== '-').map((file) => resolve(cwd, file))
2168
+ // With `builds`, `extra` is the bundle's, not any build's: added to their union.
2169
+ const bundleExtra = builds !== undefined
2170
+ let union, root
2171
+ if (deptree) {
2172
+ // The disk read through the tree, nothing cached (`cache: false`, and no setCacheDir here).
2173
+ const { createClient } = await import('@preventive/upstream/github.js')
2174
+ const built = await withNpmToken(home, () => buildFromLockfile('bundleCommand', diskHost, {
2175
+ cwd: resolve(cwd), lockfile, cache: false, lazy: false, client: createClient({ token: env.GITHUB_TOKEN || null }),
2176
+ }, { env, plan: { builds: plans, bundleExtra }, outputsAt, refuseInstalled: true, why: '--deptree lays out the dependencies from a lockfile' }))
2177
+ console.warn(unpackedSummary(built.stats))
2178
+ root = built.root
2179
+ // A lockfile of JS builds alone, as unionOfBuilds has one.
2180
+ union = { bundle: built.bundle, lockfile: kinds.every((kind) => kind === 'js') ? () => built.lockfile : undefined, accounts: built.accounts, accountsOf: built.accountsOf }
2181
+ } else {
2182
+ const builts = []
2183
+ for (const [i, plan] of plans.entries()) {
2184
+ // eslint-disable-next-line no-await-in-loop -- one build at a time, in the order its warnings read
2185
+ builts.push(await buildOfKind(kinds[i], { ...plan, outputsAt, extra: !bundleExtra }))
2186
+ }
2187
+ // A State-built JS bundle's paths are relative to the project's root at or above cwd.
2188
+ const roots = builts.map((built) => built.root ?? resolve(cwd))
2189
+ root = roots[0]
2190
+ // State-built bundles keep the State root's repo (no cwd fallback: their paths are relative to that root).
2191
+ for (const built of builts) if (!built.stateBuilt) built.bundle.repo ??= detectRepo(cwd)
2192
+ union = unionOfBuilds('bundleCommand', plans, builts, roots)
2193
+ // By the allowlist of the stasis.config.json at the root, not any entry's.
2194
+ if (bundleExtra) union = withExtra(union, root, { outputs: outputsAt(root) })
2195
+ union = pinnedOf(union, root)
2196
+ assertPolicies('bundleCommand', { builds: plans, builts, bundle: union.bundle, root })
2197
+ }
2198
+ return { ...union, root, held: heldOf(union, root, { cwd, env, lockfile }) }
2551
2199
  }