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

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