@exodus/stasis 1.0.0-beta.9 → 1.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -2
- package/bin/stasis.js +222 -334
- package/package.json +38 -14
- package/src/audit-corrections.js +11 -42
- package/src/audit.js +256 -212
- package/src/babel.js +119 -82
- package/src/bundle-from-lockfile.js +9 -21
- package/src/bundle-reader.js +24 -8
- package/src/bundle.js +0 -3
- package/src/cmd/build.js +211 -117
- package/src/cmd/bundle-options.js +347 -0
- package/src/cmd/bundle.js +1664 -1001
- package/src/cmd/diff.js +1 -4
- package/src/cmd/github-bundle.js +48 -21
- package/src/cmd/output.js +25 -12
- package/src/cmd/sbom.js +6 -9
- package/src/cmd/tokens.js +60 -0
- package/src/define.js +218 -0
- package/src/diff.js +73 -129
- package/src/esbuild.js +0 -4
- package/src/flow.js +27 -0
- package/src/installed.js +265 -0
- package/src/json-stream.js +4 -9
- package/src/loader-mock.js +3 -8
- package/src/loader.js +1 -5
- package/src/loaders/bash.js +43 -120
- package/src/loaders/cargo.js +301 -808
- package/src/loaders/foundry.js +112 -290
- package/src/loaders/paths.js +15 -26
- package/src/loaders/php.js +211 -492
- package/src/loaders/rust.js +518 -1461
- package/src/loaders/solidity-ownership.js +51 -142
- package/src/loaders/solidity.js +42 -188
- package/src/loaders/toml.js +7 -17
- package/src/lockfile.js +0 -3
- package/src/metro-assets.js +41 -0
- package/src/metro-resolver.js +58 -133
- package/src/metro-transformer.js +0 -6
- package/src/metro.js +0 -4
- package/src/mock.js +32 -80
- package/src/parse.js +8 -7
- package/src/prune.js +0 -3
- package/src/resolve-fields.js +152 -249
- package/src/resolve-node.js +29 -45
- package/src/resolve-typescript.js +166 -298
- package/src/rollup.js +0 -4
- package/src/run-installed.js +10 -0
- package/src/sbom.js +70 -97
- package/src/scan.js +240 -282
- package/src/sixel.js +310 -0
- package/src/table.js +95 -0
- package/src/validate-notes.d.ts +19 -0
- package/src/validate-notes.js +72 -0
- package/src/validate.d.ts +159 -0
- package/src/validate.js +1315 -0
- package/src/vfs-bundle/entries.js +50 -46
- package/src/vfs-bundle/github.js +114 -94
- package/src/vfs-bundle/glob.js +379 -0
- package/src/vfs-bundle/lazy.js +99 -0
- package/src/vfs-bundle/prisma.js +235 -105
- package/src/vfs-bundle/soldeer.js +28 -49
- package/src/vfs-bundle/tree.js +418 -277
- package/src/vfs-bundle/yarnclean.js +79 -0
- package/src/vfs-bundle.js +24 -55
- package/src/webpack.js +0 -4
- package/src/why.js +79 -231
- package/src/installed-commits.js +0 -182
package/src/cmd/bundle.js
CHANGED
|
@@ -1,22 +1,33 @@
|
|
|
1
1
|
import { isUtf8 } from 'node:buffer'
|
|
2
2
|
import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs'
|
|
3
|
-
import { basename, dirname, extname, join, posix, relative, resolve } from 'node:path'
|
|
4
|
-
import { fileURLToPath } from 'node:url'
|
|
5
|
-
import {
|
|
3
|
+
import { basename, dirname, extname, isAbsolute, join, posix, relative, resolve } from 'node:path'
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
5
|
+
import { brotliCompressSync, constants as zlibConstants } from 'node:zlib'
|
|
6
6
|
|
|
7
7
|
import { Bundle, isValidRepoField } from '@exodus/stasis-core/bundle'
|
|
8
|
+
import { Config, assertOption } from '@exodus/stasis-core/config'
|
|
8
9
|
import { Lockfile } from '@exodus/stasis-core/lockfile'
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
10
|
+
import { assembleBundle, expandExtra, validateFiles } from '@exodus/stasis-core/add'
|
|
11
|
+
import { getParser, importsTypescriptByOutputName, isResolvableExt, literalSpec, scan, scanCounts } from '../scan.js'
|
|
12
|
+
import { METRO_RELEASE_DEFINE, parseDefine } from '../define.js'
|
|
13
|
+
import { METRO_ASSET_EXTS, METRO_ASSET_RESOLUTIONS, METRO_SOURCE_EXTS, createFieldResolver, resolveConditions } from '../resolve-fields.js'
|
|
14
|
+
import { discoverTsconfig, isDir, isFile, loadTsconfigPaths, packageTsconfigPaths, tsconfigOutputsOf } from '../resolve-typescript.js'
|
|
12
15
|
import { createMetroResolver } from '../metro-resolver.js'
|
|
13
|
-
import {
|
|
16
|
+
import { ASSET_REGISTRY } from '../metro-assets.js'
|
|
17
|
+
import { createAliases, createBabelAliases, createBabelTypeImportsKept } from '../babel.js'
|
|
18
|
+
import { AUTO_INCLUDES, RN_CORE_INCLUDE_FILES, linkedNativePackages, linkedPackages, listing, looksNative, metroAssetFiles, nativeInputs } from '@exodus/stasis-plugins/metro-native'
|
|
19
|
+
import { installedRecords, pinInstalled } from '../installed.js'
|
|
14
20
|
import { State } from '@exodus/stasis-core/state'
|
|
15
|
-
import {
|
|
16
|
-
import { checkoutCommit, detectRepo,
|
|
17
|
-
import {
|
|
21
|
+
import { bundledIntegrity, readFileSyncMaybe } from '@exodus/stasis-core/state-util'
|
|
22
|
+
import { checkoutCommit, detectRepo, normalizeEntries, packageLookup, packageJSONStat, packageType, parseGithubRepository, readJson, readModuleManifest, readPackageJson, readRegularFileOrNull, scopeManifestOf } from '@exodus/stasis-core/bundle-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'
|
|
18
24
|
import { diskHost } from '@exodus/stasis-core/host'
|
|
25
|
+
import { linksOn, parsePolicy, recordedIn } from '@exodus/stasis-core/policy'
|
|
26
|
+
import { parseBundleBytes } from '@exodus/stasis-core/brotli'
|
|
19
27
|
import { parseSoldeerLockfile } from '@preventive/lockfile/soldeer.js'
|
|
28
|
+
import { isAssetFile } from '@preventive/rn/metro-assets.js'
|
|
29
|
+
import { added as addedTo } from '@preventive/rn/src/rn-config.js'
|
|
30
|
+
import { globRegExp, isPathGlob, packagesOf, restrictionsOf } from '@preventive/rn/src/restrictions.js'
|
|
20
31
|
import {
|
|
21
32
|
SOLIDITY_PACKAGE_MANIFESTS,
|
|
22
33
|
SOLIDITY_ROOT_MANIFESTS,
|
|
@@ -25,10 +36,11 @@ import {
|
|
|
25
36
|
discoverSolidityConfig,
|
|
26
37
|
expandSolidityEntries,
|
|
27
38
|
} from '../loaders/solidity.js'
|
|
28
|
-
import { decodeUtf8 } from '../loaders/solidity-ownership.js'
|
|
39
|
+
import { decodeUtf8, solidityOwnership } from '../loaders/solidity-ownership.js'
|
|
29
40
|
import { buildBashTree, collectBashFilesFromDisk } from '../loaders/bash.js'
|
|
30
41
|
import { boundaryOf, collectRustBundle, withinRealDir } from '../loaders/rust.js'
|
|
31
42
|
import { createCargoContext } from '../loaders/cargo.js'
|
|
43
|
+
import { ancestors } from '../loaders/paths.js'
|
|
32
44
|
import {
|
|
33
45
|
bucketizePhpSources,
|
|
34
46
|
buildPhpTree,
|
|
@@ -37,188 +49,100 @@ import {
|
|
|
37
49
|
loadComposerPackages,
|
|
38
50
|
loadLaravelProviderFiles,
|
|
39
51
|
} from '../loaders/php.js'
|
|
40
|
-
import {
|
|
52
|
+
import { bundledSummary, lockedSummary, lockfileOnlyError, packagesLabel, unpackedSummary, writeBundle, writeFile } from './output.js'
|
|
53
|
+
import { withNpmToken } from './tokens.js'
|
|
41
54
|
|
|
42
|
-
const JS_EXTS = new Set(['.js', '.cjs', '.mjs', '.ts', '.cts', '.mts', '.jsx', '.tsx'])
|
|
43
55
|
const BASH_EXTS = new Set(['.sh', '.bash'])
|
|
44
56
|
const RUST_EXTS = new Set(['.rs'])
|
|
57
|
+
const nonEmpty = (list) => Array.isArray(list) && list.length > 0
|
|
45
58
|
|
|
46
|
-
//
|
|
47
|
-
const SOLIDITY_WORKSPACE_NAME = 'solidity-bundle'
|
|
48
|
-
const SOLIDITY_WORKSPACE_VERSION = '0.0.0'
|
|
49
|
-
const SOLIDITY_FORMAT = 'solidity'
|
|
50
|
-
const BASH_WORKSPACE_NAME = 'bash-bundle'
|
|
51
|
-
const BASH_WORKSPACE_VERSION = '0.0.0'
|
|
52
|
-
const SHELL_FORMAT = 'shell'
|
|
53
|
-
|
|
54
|
-
const RUST_WORKSPACE_NAME = 'rust-bundle'
|
|
55
|
-
const RUST_WORKSPACE_VERSION = '0.0.0'
|
|
56
|
-
const RUST_FORMAT = 'rust'
|
|
57
|
-
|
|
58
|
-
const PHP_WORKSPACE_NAME = 'php-bundle'
|
|
59
|
-
const PHP_WORKSPACE_VERSION = '0.0.0'
|
|
60
|
-
const PHP_FORMAT = 'php'
|
|
61
|
-
|
|
62
|
-
// Deepest common parent dir of `paths`, relative to `cwd`; starts with `..` when entries
|
|
63
|
-
// 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).
|
|
64
60
|
export function outermostDir(paths, cwd) {
|
|
65
61
|
if (paths.length === 0) return '.'
|
|
66
62
|
const cwdAbs = posix.resolve(toPosix(cwd))
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const c = partsList[0][i]
|
|
72
|
-
if (!partsList.every((parts) => parts[i] === c)) break
|
|
73
|
-
common.push(c)
|
|
74
|
-
}
|
|
75
|
-
const absCommon = common.join('/') || '/'
|
|
76
|
-
const rel = posix.relative(cwdAbs, absCommon)
|
|
77
|
-
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('/') || '/') || '.'
|
|
78
67
|
}
|
|
79
68
|
|
|
80
|
-
//
|
|
81
|
-
// 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.
|
|
82
70
|
function parseSoldeerDir(seg) {
|
|
83
71
|
const m = /^(.+)-(\d+\.\d+\.\d+(?:[-+][\w.-]+)?)$/u.exec(seg)
|
|
84
72
|
return m ? { name: m[1], version: m[2] } : { name: seg, version: '0.0.0' }
|
|
85
73
|
}
|
|
86
74
|
|
|
87
|
-
// A url as git fetches from a host: scp's `[user@]host:path`,
|
|
88
|
-
//
|
|
89
|
-
// `git+ssh`/`ssh+git` aliases, lowercase, as git spells them), its authority, `[userinfo@]host[:port]`,
|
|
90
|
-
// ending at the first slash after it. Not, then, what a package.json spells GitHub's that git reads
|
|
91
|
-
// another way or not at all: `ssh://git@github.com:owner/name`, whose host is `github.com:owner` to
|
|
92
|
-
// git; `git+https://` or `HTTPS://`, transports git doesn't have; nor `owner/name`, a path, as is any
|
|
93
|
-
// 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.
|
|
94
77
|
const GIT_URL = /^(?:[^/]*:(?!\/\/)|(?:https?|git|ssh|git\+ssh|ssh\+git):\/\/(?:[^/@]*@)?[^/:@]*(?::\d*)?\/)/u
|
|
95
78
|
|
|
96
|
-
//
|
|
97
|
-
// none for a host merely holding `github.com` (`https://notgithub.com/o/n`), nor for a url git
|
|
98
|
-
// doesn't fetch as one (GIT_URL), nor for npm's `github:owner/name`, the host `github`'s path to git,
|
|
99
|
-
// whatever an ssh alias or `insteadOf` makes of it, nor one with a `#`, which git keeps in the path
|
|
100
|
-
// it asks for (`owner/name.git#v1`) where npm takes a committish. git's `ssh+git://`, which a
|
|
101
|
-
// 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.
|
|
102
80
|
const submoduleGithub = (url) => (GIT_URL.test(url) && !/^github:/iu.test(url) && !url.includes('#') ? parseGithubRepository(url.replace(/^ssh\+git:/u, 'ssh:')) : null)
|
|
103
81
|
|
|
104
|
-
// The GitHub ones of `submodules` (readGitmodules), as Map<submodulePath, { name, branch }>.
|
|
105
82
|
function githubSubmodules(submodules) {
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
const name = url && submoduleGithub(url)
|
|
109
|
-
if (name) byPath.set(path, { name, branch })
|
|
110
|
-
}
|
|
111
|
-
return byPath
|
|
83
|
+
const named = submodules.map(({ path, url, branch }) => [path, { name: url && submoduleGithub(url), branch }])
|
|
84
|
+
return new Map(named.filter(([, { name }]) => name))
|
|
112
85
|
}
|
|
113
86
|
|
|
114
|
-
// The root of the GitHub repository `github` at `commit`, where both are valid; else undefined.
|
|
115
87
|
const repoRootAt = (github, commit) => (isValidRepoField('github', github) && isValidRepoField('commit', commit) ? { github, directory: '', commit } : undefined)
|
|
116
88
|
|
|
117
|
-
//
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const { reason, real, outside } = ownership.of(rel)
|
|
121
|
-
if (reason || real === null || outside) return null
|
|
122
|
-
try {
|
|
123
|
-
const buf = readRegularFileOrNull(join(host.realpath(baseDir), real), rel, host)
|
|
124
|
-
return buf === null ? null : decodeUtf8(buf, rel)
|
|
125
|
-
} catch {
|
|
126
|
-
return null
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Each git dependency the root's soldeer.lock installs from GitHub, by its folder under
|
|
131
|
-
// `dependencies/` (`<name>-<version>`, as Soldeer names it): the repository's root at the lockfile's
|
|
132
|
-
// `rev`, which Soldeer checks out there. None where the lockfile can't be read or parsed: it is
|
|
133
|
-
// metadata, which never stops a build.
|
|
134
|
-
function soldeerGitRepos(baseDir, ownership, host) {
|
|
135
|
-
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')
|
|
136
92
|
let lock = null
|
|
137
93
|
try {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
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()
|
|
143
98
|
for (const dep of Object.values(lock?.dependencies ?? {})) {
|
|
144
|
-
|
|
145
|
-
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) })
|
|
146
100
|
}
|
|
147
|
-
return
|
|
101
|
+
return locks
|
|
148
102
|
}
|
|
149
103
|
|
|
150
|
-
//
|
|
151
|
-
// github submodule (`lib/`, via the `.gitmodules` `ownership` read), else null to defer to the
|
|
152
|
-
// node_modules/workspace logic. `ownership.assert` vets a package.json path before it is read
|
|
153
|
-
// through `host`. A bucket's `repo` is the GitHub repository it is checked out from, at its root, at
|
|
154
|
-
// the commit its checkout's HEAD is at, as the bundle's own `repo` reads one: a submodule's, from
|
|
155
|
-
// the GitHub repository its .gitmodules url names; a Soldeer git dependency's, where it is its
|
|
156
|
-
// soldeer.lock `rev` (soldeerGitRepos), as Soldeer holds a checkout to the lockfile before taking it
|
|
157
|
-
// for installed: a folder left at another commit since the lockfile changed, or no checkout, records
|
|
158
|
-
// none.
|
|
104
|
+
// A bucket's `repo` is its checkout's HEAD; a Soldeer git dependency's only at its soldeer.lock `rev`.
|
|
159
105
|
function makeSolidityClassifier(baseDir, ownership, host) {
|
|
160
106
|
const submodules = githubSubmodules(ownership.submodules)
|
|
161
|
-
const
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
const commitOf = (dir) => {
|
|
165
|
-
if (!commits.has(dir)) commits.set(dir, checkoutCommit(join(baseDir, dir), host))
|
|
166
|
-
return commits.get(dir)
|
|
167
|
-
}
|
|
168
|
-
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
|
|
169
110
|
return (path) => {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
return { bucketDir, name, version, ecosystem: 'soldeer', ...(repo === undefined ? {} : { repo }) }
|
|
179
|
-
}
|
|
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 }) }
|
|
180
119
|
}
|
|
181
120
|
for (const [sub, { name, branch }] of submodules) {
|
|
182
121
|
if (path === sub || path.startsWith(`${sub}/`)) {
|
|
183
|
-
|
|
122
|
+
const version = versionOf(sub)
|
|
184
123
|
const repo = repoRootAt(name, commitOf(sub))
|
|
185
|
-
return { bucketDir: sub, name, version:
|
|
124
|
+
return { bucketDir: sub, name, version: version ?? branch ?? '0.0.0', ecosystem: 'github', ...(repo && { repo }) }
|
|
186
125
|
}
|
|
187
126
|
}
|
|
188
127
|
return null
|
|
189
128
|
}
|
|
190
129
|
}
|
|
191
130
|
|
|
192
|
-
//
|
|
193
|
-
//
|
|
194
|
-
|
|
195
|
-
// Neither is ever sent to a public advisory database: a private git dependency's name has no place
|
|
196
|
-
// there, and a crates.io crate of the same name isn't it.
|
|
197
|
-
const vendoredEcosystem = (from) => {
|
|
198
|
-
if (from === 'registry') return 'cargo'
|
|
199
|
-
return from === 'git' ? 'cargo-git' : 'cargo-unknown'
|
|
200
|
-
}
|
|
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')
|
|
201
134
|
|
|
202
|
-
// Classify a Rust file by the nearest Cargo.toml `[package]`: a `cargo vendor`ed crate under the
|
|
203
|
-
// vendor dir (`vendor/<dir>/`, or where .cargo/config.toml points) is a dependency (tagged
|
|
204
|
-
// `cargo`, `cargo-git` or `cargo-unknown`: see vendoredEcosystem); any other package (the crate
|
|
205
|
-
// itself, a workspace member reached through a `path` dependency) is first-party, so no
|
|
206
|
-
// ecosystem. Null (no manifest above the file) defers to the package.json/placeholder logic.
|
|
207
135
|
function makeRustClassifier(cargo) {
|
|
208
136
|
return (path) => {
|
|
209
137
|
const pkg = cargo.packageInfo(path)
|
|
210
138
|
if (!pkg) return null
|
|
211
|
-
|
|
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) }) }
|
|
212
141
|
}
|
|
213
142
|
}
|
|
214
143
|
|
|
215
|
-
//
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
// A file of the bundle root (`realBase` its real path), its bytes, by project-relative path, or
|
|
219
|
-
// null when it isn't a regular file there (a manifest the context saw may have gone since; a FIFO
|
|
220
|
-
// would hang a read) or, with `within` a directory, doesn't really lie in it (a vendored crate's
|
|
221
|
-
// 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).
|
|
222
146
|
function readFileWithinBase(baseDir, realBase, rel, within) {
|
|
223
147
|
try {
|
|
224
148
|
assertRealPathWithinBase(realBase, baseDir, rel)
|
|
@@ -231,48 +155,26 @@ function readFileWithinBase(baseDir, realBase, rel, within) {
|
|
|
231
155
|
}
|
|
232
156
|
}
|
|
233
157
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
function executableSources(baseDir, sources, host) {
|
|
238
|
-
const executable = new Set()
|
|
239
|
-
for (const path of sources.keys()) {
|
|
240
|
-
if (isExecutableFile(join(baseDir, path), host)) executable.add(path)
|
|
241
|
-
}
|
|
242
|
-
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}`)
|
|
243
161
|
}
|
|
244
162
|
|
|
245
|
-
// Bundles must be self-contained: throw on a missing entry or an unresolved `noun` (import, script,
|
|
246
|
-
// module) of a `lang` bundle -- `missing`'s { spec, from }, with the `reason` it was refused if given.
|
|
247
163
|
function assertSelfContained(lang, noun, entries, sources, missing) {
|
|
248
|
-
const issues = []
|
|
249
|
-
for (const
|
|
250
|
-
|
|
251
|
-
}
|
|
252
|
-
for (const { spec, from, reason } of missing) {
|
|
253
|
-
issues.push(`Unresolved ${noun}: ${spec} from ${from}${reason ? ` (refused: ${reason})` : ''}`)
|
|
254
|
-
}
|
|
255
|
-
if (issues.length > 0) {
|
|
256
|
-
throw new Error(`${lang} bundle has unresolved ${noun}s:\n${issues.map((s) => ` ${s}`).join('\n')}`)
|
|
257
|
-
}
|
|
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')}`)
|
|
258
167
|
}
|
|
259
168
|
|
|
260
|
-
//
|
|
261
|
-
// nearest package.json (node_modules -> `npm`-tagged bucket, workspace -> its dir, none ->
|
|
262
|
-
// "." with the placeholder identity); a node_modules file whose nearest package.json is the
|
|
263
|
-
// workspace root is rejected, not mislabeled; `packageOf(path)` finds that package.json
|
|
264
|
-
// (packageLookup by default, which walks past a malformed one). `classifyDep(path)` optionally
|
|
265
|
-
// places a file directly (non-node_modules ecosystems like Soldeer/github); null defers. `format` tags
|
|
266
|
-
// every file; `formats` (Map<path,format>) overrides it per file. `resolutions` values are
|
|
267
|
-
// a flat target string or a Map<platform,target>; both round-trip untouched.
|
|
169
|
+
// A file of no package goes in "." under the workspace identity: Bundle.parse needs every bucket to attest one.
|
|
268
170
|
function assembleCodeBundle({
|
|
269
|
-
baseDir, entries, sources, resolutions, workspaceName, workspaceVersion, format, formats, conditionKey, classifyDep, host,
|
|
270
|
-
packageOf = packageLookup(baseDir, { host }),
|
|
171
|
+
baseDir, entries, sources, resolutions, keyedResolutions, workspaceName, workspaceVersion = '0.0.0', format, formats, conditionKey, classifyDep, host,
|
|
172
|
+
packageOf = packageLookup(baseDir, { host }), consumer, consumerFiles,
|
|
271
173
|
}) {
|
|
272
174
|
const modules = new Map()
|
|
273
|
-
// `identity`'s name, version, ecosystem and repo (moduleInfo takes those alone).
|
|
274
175
|
const ensureBucket = (dir, identity) => {
|
|
275
|
-
|
|
176
|
+
const bucket = modules.get(dir)
|
|
177
|
+
modules.set(dir, bucket === undefined ? moduleInfo({ ...identity, files: Object.create(null) }) : withMetadataOf(bucket, identity))
|
|
276
178
|
return modules.get(dir)
|
|
277
179
|
}
|
|
278
180
|
|
|
@@ -283,80 +185,48 @@ function assembleCodeBundle({
|
|
|
283
185
|
continue
|
|
284
186
|
}
|
|
285
187
|
const meta = packageOf(path)
|
|
286
|
-
|
|
287
|
-
if (meta) {
|
|
288
|
-
|
|
289
|
-
throw new Error(`No package.json with name+version found for ${path}`)
|
|
290
|
-
}
|
|
291
|
-
ensureBucket(meta.pkgDir, meta).files[fileInBucket(meta.pkgDir, path)] = content
|
|
292
|
-
} else {
|
|
293
|
-
if (inNodeModules) throw new Error(`No package.json with name+version found for ${path}`)
|
|
294
|
-
ensureBucket('.', { name: workspaceName, version: workspaceVersion }).files[path] = content
|
|
188
|
+
// One of node_modules whose nearest package.json is the workspace root's (or none) is a misconfigured dep.
|
|
189
|
+
if (splitNodeModulesPath(path) !== null && !(meta && hasNodeModulesSegment(meta.pkgDir))) {
|
|
190
|
+
throw new Error(`No package.json with name+version found for ${path}`)
|
|
295
191
|
}
|
|
192
|
+
const [dir, identity] = meta ? [meta.pkgDir, meta] : ['.', { name: workspaceName, version: workspaceVersion }]
|
|
193
|
+
ensureBucket(dir, identity).files[fileInBucket(dir, path)] = content
|
|
296
194
|
}
|
|
297
195
|
|
|
298
|
-
return codeBundle({ baseDir, entries, sources, modules, resolutions, format, formats, conditionKey, host })
|
|
196
|
+
return codeBundle({ baseDir, entries, sources, modules, resolutions, keyedResolutions, format, formats, conditionKey, host, consumer, consumerFiles })
|
|
299
197
|
}
|
|
300
198
|
|
|
301
|
-
|
|
302
|
-
// (Map<path,format>) overrides it per file, and the `resolutions` edges are keyed under `conditionKey`.
|
|
303
|
-
function codeBundle({ baseDir, entries, sources, modules, resolutions, format, formats, conditionKey, host }) {
|
|
199
|
+
function codeBundle({ baseDir, entries, sources, modules, resolutions, keyedResolutions = new Map(), format, formats, conditionKey, host, consumer = 'bundle', consumerFiles }) {
|
|
304
200
|
const formatsMap = new Map()
|
|
305
|
-
for (const path of sources.keys())
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
const imports = new Map([[conditionKey, importsForKey]])
|
|
310
|
-
|
|
311
|
-
// Executable bits, straight off disk (a synthetic source with no file there is simply not
|
|
312
|
-
// executable). Shell bundles lean on this most: `stasis extract` puts the +x back on the scripts.
|
|
313
|
-
const executable = executableSources(baseDir, sources, host)
|
|
314
|
-
|
|
315
|
-
// Attribute files to the `bundle` consumer (static builders skip State's per-file tagging).
|
|
201
|
+
for (const path of sources.keys()) {
|
|
202
|
+
const fileFormat = formats?.get(path) ?? format
|
|
203
|
+
if (fileFormat !== undefined) formatsMap.set(path, fileFormat)
|
|
204
|
+
}
|
|
316
205
|
return new Bundle({
|
|
317
206
|
config: { scope: 'full' },
|
|
318
207
|
entries: new Set(entries),
|
|
319
208
|
modules,
|
|
320
209
|
formats: formatsMap,
|
|
321
|
-
imports,
|
|
322
|
-
executable
|
|
323
|
-
|
|
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)),
|
|
213
|
+
}).withReason(consumer, consumerFiles)
|
|
324
214
|
}
|
|
325
215
|
|
|
326
|
-
//
|
|
327
|
-
// (both however they're cased).
|
|
216
|
+
// Never carried, whatever reads them, however cased: `.env` files, and Hardhat's config, which is code.
|
|
328
217
|
const neverCarried = (rel) => isDotEnvFile(rel) || posix.basename(rel).toLowerCase().startsWith('hardhat.config.')
|
|
329
218
|
|
|
330
|
-
//
|
|
331
|
-
function packageLookup(baseDir, options) {
|
|
332
|
-
const byDir = new Map()
|
|
333
|
-
return (path) => {
|
|
334
|
-
const dir = posix.dirname(path)
|
|
335
|
-
if (!byDir.has(dir)) byDir.set(dir, findPackageMetadata(baseDir, path, options))
|
|
336
|
-
return byDir.get(dir)
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
// The build-description files of a Solidity bundle (--manifests), as Map<path, text>, sorted:
|
|
341
|
-
// `configFiles` (what discoverSolidityConfig read, whatever they're called), each of which must be
|
|
342
|
-
// carried -- one outside the root, neverCarried, refused or gone is an error, as the bundle
|
|
343
|
-
// couldn't reproduce the resolution without it -- plus the SOLIDITY_*_MANIFESTS that exist, for the
|
|
344
|
-
// root and for each package dir `classifyDep`/`packageOf` places a bundled source in, but none
|
|
345
|
-
// whose path `ownership` refuses (see solidityOwnership). Carried as written: whatever they hold
|
|
346
|
-
// (an RPC URL with its API key, an Etherscan key, a URL's credentials) is in the bundle too, as
|
|
347
|
-
// with --package-json. Read through `host`.
|
|
219
|
+
// --manifests: carried as written, secrets too.
|
|
348
220
|
function solidityManifests(baseDir, sources, configFiles, { classifyDep, packageOf, ownership, host }) {
|
|
349
221
|
const realBase = host.realpath(baseDir)
|
|
350
|
-
// `{ text }`, or `{ why }` it can't be carried (null: nothing is there).
|
|
351
|
-
// `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).
|
|
352
223
|
const carry = (rel) => {
|
|
353
224
|
const { reason, real, outside } = ownership.of(rel)
|
|
354
225
|
if (reason) return { why: reason }
|
|
355
226
|
if (real === null) return { why: null }
|
|
356
227
|
if (outside) throw new Error(`Refusing to follow symlink escaping bundle root: ${rel} -> ${resolve(realBase, real)}`)
|
|
357
228
|
const buf = readRegularFileOrNull(join(realBase, real), rel, host)
|
|
358
|
-
|
|
359
|
-
return { text: decodeUtf8(buf, rel) }
|
|
229
|
+
return buf === null ? { why: null } : { text: decodeUtf8(buf, rel) }
|
|
360
230
|
}
|
|
361
231
|
const unreproducible = (rel, why) => new Error(`--manifests can't carry ${rel}, which the Solidity resolution read: ${why}`)
|
|
362
232
|
const out = new Map()
|
|
@@ -384,18 +254,12 @@ function solidityManifests(baseDir, sources, configFiles, { classifyDep, package
|
|
|
384
254
|
return new Map([...out.keys()].toSorted().map((rel) => [rel, out.get(rel)]))
|
|
385
255
|
}
|
|
386
256
|
|
|
387
|
-
//
|
|
388
|
-
|
|
389
|
-
export 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)
|
|
390
259
|
|
|
391
|
-
// Whether `entry` (resolved against `cwd`) is a Solidity bundle's: a .sol file or a directory entry.
|
|
392
260
|
export const isSolidityEntry = (entry, cwd = process.cwd(), host = diskHost) => entry.endsWith('.sol') || isDirEntry(resolve(cwd, entry), host)
|
|
393
261
|
|
|
394
|
-
//
|
|
395
|
-
// entry stands for the .sol files under it, so it goes with Solidity entries only; and entries
|
|
396
|
-
// that are all missing extensionless paths are a mistyped file, not a project without those dirs.
|
|
397
|
-
// `fetched` false: `host` is an empty tree standing in for one not fetched yet, which can't say
|
|
398
|
-
// 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.
|
|
399
263
|
export function directoryEntryError(entries, cwd = process.cwd(), host = diskHost, { fetched = true } = {}) {
|
|
400
264
|
const dirs = entries.filter((e) => isDirEntry(resolve(cwd, e), host))
|
|
401
265
|
const absent = fetched ? dirs.filter((e) => host.stat(resolve(cwd, e)) === null) : []
|
|
@@ -406,35 +270,19 @@ export function directoryEntryError(entries, cwd = process.cwd(), host = diskHos
|
|
|
406
270
|
: `a directory entry is only supported for Solidity bundles (it stands for the .sol files under it): ${dirs[0]}`
|
|
407
271
|
}
|
|
408
272
|
|
|
409
|
-
//
|
|
410
|
-
// files under it, as forge's src/test/script dirs and Hardhat's contracts dir do; a missing or
|
|
411
|
-
// empty one is skipped). Imports resolve through the remappings the project's build uses
|
|
412
|
-
// (discoverSolidityConfig: forge's discovery for a Foundry project); `mappingFile`
|
|
413
|
-
// (foundry.toml/remappings.txt) pins them to exactly what it lists. An import is refused when it
|
|
414
|
-
// reaches a non-.sol file or leaves the root, or when a dependency's reaches the project's own
|
|
415
|
-
// files. Config files are read, and bundled only with `manifests` (see solidityManifests). `env`
|
|
416
|
-
// supplies FOUNDRY_PROFILE / FOUNDRY_REMAPPINGS, reported when they apply. The project is read
|
|
417
|
-
// through `host`.
|
|
273
|
+
// A missing directory entry is skipped; `env` supplies FOUNDRY_PROFILE / FOUNDRY_REMAPPINGS.
|
|
418
274
|
export async function buildSolidityBundle({ cwd = process.cwd(), entries, mappingFile, manifests = false, env = process.env, host = diskHost } = {}) {
|
|
419
|
-
if (!
|
|
420
|
-
throw new Error('buildSolidityBundle: at least one entry .sol file or directory is required')
|
|
421
|
-
}
|
|
422
|
-
|
|
275
|
+
if (!nonEmpty(entries)) throw new Error('buildSolidityBundle: at least one entry .sol file or directory is required')
|
|
423
276
|
const baseDir = resolve(cwd)
|
|
424
277
|
const normalized = normalizeEntries(entries, cwd)
|
|
425
|
-
for (const e of normalized) {
|
|
426
|
-
if (!isSolidityEntry(e, baseDir, host)) throw new Error(`buildSolidityBundle: not a .sol file or directory: ${e}`)
|
|
427
|
-
}
|
|
278
|
+
for (const e of normalized) if (!isSolidityEntry(e, baseDir, host)) throw new Error(`buildSolidityBundle: not a .sol file or directory: ${e}`)
|
|
428
279
|
const expanded = expandSolidityEntries(baseDir, normalized, host)
|
|
429
|
-
|
|
430
280
|
const { remappings, libs, ownership, files: configFiles, envUsed } = discoverSolidityConfig(baseDir, { mappingFile, env, host })
|
|
431
281
|
// The bundle doesn't record the environment, so say when it shaped the resolution.
|
|
432
282
|
if (envUsed.length > 0) console.warn(`[stasis] Solidity imports resolved with ${envUsed.join(', ')} from the environment`)
|
|
433
283
|
const sources = collectSolidityFilesFromDisk(baseDir, expanded, remappings, { libs, ownership, host })
|
|
434
284
|
const { resolutions, missing } = buildSolidityTree(sources, { remappings, baseDir, libs, ownership, host })
|
|
435
|
-
|
|
436
285
|
assertSelfContained('Solidity', 'import', expanded, sources, missing)
|
|
437
|
-
|
|
438
286
|
const classifyDep = makeSolidityClassifier(baseDir, ownership, host)
|
|
439
287
|
const packageOf = packageLookup(baseDir, { strict: true, check: ownership.assert, host })
|
|
440
288
|
const bundled = new Map(sources)
|
|
@@ -445,98 +293,49 @@ export async function buildSolidityBundle({ cwd = process.cwd(), entries, mappin
|
|
|
445
293
|
formats.set(path, posix.basename(path) === 'package.json' ? 'json' : 'resource')
|
|
446
294
|
}
|
|
447
295
|
}
|
|
448
|
-
|
|
449
296
|
return assembleCodeBundle({
|
|
450
|
-
baseDir,
|
|
451
|
-
|
|
452
|
-
sources: bundled,
|
|
453
|
-
resolutions,
|
|
454
|
-
workspaceName: SOLIDITY_WORKSPACE_NAME,
|
|
455
|
-
workspaceVersion: SOLIDITY_WORKSPACE_VERSION,
|
|
456
|
-
format: SOLIDITY_FORMAT,
|
|
457
|
-
formats,
|
|
458
|
-
conditionKey: 'solidity',
|
|
459
|
-
classifyDep,
|
|
460
|
-
packageOf,
|
|
461
|
-
host,
|
|
297
|
+
baseDir, entries: expanded, sources: bundled, resolutions, workspaceName: 'solidity-bundle', format: 'solidity', formats,
|
|
298
|
+
conditionKey: 'solidity', classifyDep, packageOf, host,
|
|
462
299
|
})
|
|
463
300
|
}
|
|
464
301
|
|
|
465
|
-
//
|
|
466
|
-
// A local `source` inside the bundle root that resolves to no bundled script is fatal;
|
|
467
|
-
// 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.
|
|
468
303
|
export async function buildBashBundle({ cwd = process.cwd(), entries } = {}) {
|
|
469
|
-
|
|
470
|
-
throw new Error('buildBashBundle: at least one entry .sh/.bash file is required')
|
|
471
|
-
}
|
|
472
|
-
for (const e of entries) {
|
|
473
|
-
if (!BASH_EXTS.has(extname(e))) throw new Error(`buildBashBundle: not a .sh/.bash file: ${e}`)
|
|
474
|
-
}
|
|
475
|
-
|
|
304
|
+
assertEntries('buildBashBundle', entries, '.sh/.bash file', (e) => BASH_EXTS.has(extname(e)))
|
|
476
305
|
const baseDir = resolve(cwd)
|
|
477
306
|
const normalized = normalizeEntries(entries, cwd)
|
|
478
|
-
|
|
479
307
|
const sources = await collectBashFilesFromDisk(baseDir, normalized)
|
|
480
308
|
const { resolutions, missing } = buildBashTree(sources)
|
|
481
|
-
|
|
482
|
-
// Self-contained (external refs never reach `missing`).
|
|
483
309
|
assertSelfContained('Bash', 'script', normalized, sources, missing)
|
|
484
|
-
|
|
485
|
-
return assembleCodeBundle({
|
|
486
|
-
baseDir,
|
|
487
|
-
entries: normalized,
|
|
488
|
-
sources,
|
|
489
|
-
resolutions,
|
|
490
|
-
workspaceName: BASH_WORKSPACE_NAME,
|
|
491
|
-
workspaceVersion: BASH_WORKSPACE_VERSION,
|
|
492
|
-
format: SHELL_FORMAT,
|
|
493
|
-
conditionKey: SHELL_FORMAT,
|
|
494
|
-
})
|
|
310
|
+
return assembleCodeBundle({ baseDir, entries: normalized, sources, resolutions, workspaceName: 'bash-bundle', format: 'shell', conditionKey: 'shell' })
|
|
495
311
|
}
|
|
496
312
|
|
|
497
|
-
//
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
// `cargoTarget` (a triple, or `host`) names the build's target: its cfgs, asked of rustc, decide
|
|
508
|
-
// `#[cfg(unix)]`-style code and target-specific dependency tables, which are otherwise all kept.
|
|
509
|
-
// `cargoManifests` also carries what describes each bundled package's build: its Cargo.toml (and
|
|
510
|
-
// the workspace's), its build script -- walked like a crate root, so its modules and the in-tree
|
|
511
|
-
// build-dependencies it reaches come along -- plus the root's Cargo.lock and .cargo/config.toml.
|
|
512
|
-
export async function buildRustBundle({ cwd = process.cwd(), entries, cargo = false, cargoFeatures = [], cargoNoDefaultFeatures = false, cargoAllFeatures = false, cargoTarget = null, cargoManifests = false } = {}) {
|
|
513
|
-
if (!Array.isArray(entries) || entries.length === 0) {
|
|
514
|
-
throw new Error('buildRustBundle: at least one entry .rs file is required')
|
|
515
|
-
}
|
|
516
|
-
for (const e of entries) {
|
|
517
|
-
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))
|
|
518
323
|
}
|
|
324
|
+
assertSelfContained('TEAL', 'program', normalized, sources, [])
|
|
325
|
+
return assembleCodeBundle({ baseDir, entries: normalized, sources, workspaceName: 'teal-bundle', format: 'teal', conditionKey: 'teal' })
|
|
326
|
+
}
|
|
519
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)))
|
|
520
330
|
const baseDir = resolve(cwd)
|
|
521
331
|
const normalized = normalizeEntries(entries, cwd)
|
|
522
|
-
|
|
523
|
-
// One Cargo context for the walk, the edge pass and the bucketing: manifests are read once, and
|
|
524
|
-
// 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.
|
|
525
333
|
const cargoCtx = createCargoContext(baseDir, {
|
|
526
|
-
entries: normalized,
|
|
527
|
-
cargo,
|
|
528
|
-
features: cargoFeatures,
|
|
529
|
-
noDefaultFeatures: cargoNoDefaultFeatures,
|
|
530
|
-
allFeatures: cargoAllFeatures,
|
|
531
|
-
target: cargoTarget,
|
|
334
|
+
entries: normalized, cargo, features: cargoFeatures, noDefaultFeatures: cargoNoDefaultFeatures, allFeatures: cargoAllFeatures, target: cargoTarget,
|
|
532
335
|
})
|
|
533
|
-
// `include_str!` / `include_bytes!` assets ride along as resources: the walk records their
|
|
534
|
-
// format. With `cargoManifests`, each package's build script is walked too (collectRustBundle).
|
|
535
336
|
const { sources, formats, tree } = await collectRustBundle(baseDir, normalized, { cargo: cargoCtx, buildScripts: cargoManifests })
|
|
536
337
|
if (cargoManifests) {
|
|
537
|
-
//
|
|
538
|
-
// written: whatever they hold (a registry token, a `git` URL's credentials) is in the bundle
|
|
539
|
-
// 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.
|
|
540
339
|
const files = new Set()
|
|
541
340
|
for (const path of sources.keys()) for (const f of cargoCtx.buildFilesFor(path)) files.add(f.path)
|
|
542
341
|
const realBase = realpathSync(baseDir)
|
|
@@ -551,19 +350,12 @@ export async function buildRustBundle({ cwd = process.cwd(), entries, cargo = fa
|
|
|
551
350
|
}
|
|
552
351
|
}
|
|
553
352
|
const { resolutions, missing, unresolvedCrates } = tree
|
|
554
|
-
|
|
555
353
|
assertSelfContained('Rust', 'module', normalized, sources, missing)
|
|
556
|
-
|
|
557
|
-
// Where the features the cfg decisions rest on come from: cargo's resolver, or -- something it
|
|
558
|
-
// takes missing -- a replay of the manifests, said so (a project without Cargo has neither).
|
|
559
354
|
const resolution = cargoCtx.resolution()
|
|
560
355
|
if (resolution.mode === 'replay' && normalized.some((e) => cargoCtx.packageInfo(e) !== null)) {
|
|
561
356
|
console.warn(`[stasis] Rust features from a replay of the manifests, not cargo's resolver: ${resolution.why}`)
|
|
562
357
|
}
|
|
563
|
-
|
|
564
|
-
// context, so the replay, cargo's resolver and `cargo metadata` (which unifies dev/build deps
|
|
565
|
-
// like resolver 1) can be compared.
|
|
566
|
-
if (process.env.EXODUS_STASIS_DEBUG === '1' || process.env.EXODUS_STASIS_DEBUG === 'true') {
|
|
358
|
+
if (debugging()) {
|
|
567
359
|
const mode = { cargo: "cargo's resolver", metadata: 'cargo metadata', replay: 'manifest replay' }[resolution.mode]
|
|
568
360
|
for (const context of ['target', 'host']) {
|
|
569
361
|
const resolved = [...cargoCtx.resolvedFeatures(context)].toSorted(([a], [b]) => (a < b ? -1 : 1))
|
|
@@ -574,12 +366,6 @@ export async function buildRustBundle({ cwd = process.cwd(), entries, cargo = fa
|
|
|
574
366
|
}
|
|
575
367
|
}
|
|
576
368
|
}
|
|
577
|
-
|
|
578
|
-
// A crate the code names that the bundle doesn't hold: a registry dependency that isn't vendored
|
|
579
|
-
// (or not in a version its requirement allows), a path or patch outside the bundle root (the
|
|
580
|
-
// loader warned of those), or a crate root the walk refused -- and any other dependency a bundled
|
|
581
|
-
// package's build links that nothing in-tree answers, however the code names it, if at all.
|
|
582
|
-
// Reported whatever the vendor dir holds; with none, the fix is one command.
|
|
583
369
|
const notLoaded = tree.wantedRoots.filter((r) => !sources.has(r)).map((r) => `${cargoCtx.packageInfo(r)?.name ?? r} (${r})`)
|
|
584
370
|
const bundledPackages = new Set([...sources.keys()].map((p) => cargoCtx.packageInfo(p)?.dir).filter((d) => d !== undefined))
|
|
585
371
|
const lacking = cargoCtx.lackingDependencies(bundledPackages, { buildScripts: cargoManifests })
|
|
@@ -591,266 +377,216 @@ export async function buildRustBundle({ cwd = process.cwd(), entries, cargo = fa
|
|
|
591
377
|
console.warn(`[stasis] ${names.length} crate${names.length === 1 ? '' : 's'} referenced but not in the bundle: ${shown}`)
|
|
592
378
|
if (!existsSync(join(baseDir, cargoCtx.vendorDir))) console.warn('[stasis] Registry dependencies are bundled only when vendored in-tree: run `cargo vendor` first.')
|
|
593
379
|
}
|
|
594
|
-
|
|
595
380
|
return assembleCodeBundle({
|
|
596
|
-
baseDir,
|
|
597
|
-
entries: normalized,
|
|
598
|
-
sources,
|
|
599
|
-
resolutions,
|
|
600
|
-
workspaceName: RUST_WORKSPACE_NAME,
|
|
601
|
-
workspaceVersion: RUST_WORKSPACE_VERSION,
|
|
602
|
-
format: RUST_FORMAT,
|
|
603
|
-
formats,
|
|
604
|
-
conditionKey: 'rust',
|
|
381
|
+
baseDir, entries: normalized, sources, resolutions, workspaceName: 'rust-bundle', format: 'rust', formats, conditionKey: 'rust',
|
|
605
382
|
classifyDep: makeRustClassifier(cargoCtx),
|
|
606
383
|
})
|
|
607
384
|
}
|
|
608
385
|
|
|
609
|
-
//
|
|
610
|
-
// require/include graph (and Composer's class-autoload graph when present); no PHP is
|
|
611
|
-
// executed. Autoload resolution is best-effort; explicit require/include of a literal path
|
|
612
|
-
// 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.
|
|
613
387
|
export async function buildPhpBundle({ cwd = process.cwd(), entries } = {}) {
|
|
614
|
-
|
|
615
|
-
throw new Error('buildPhpBundle: at least one entry .php file is required')
|
|
616
|
-
}
|
|
617
|
-
for (const e of entries) {
|
|
618
|
-
if (!e.endsWith('.php')) throw new Error(`buildPhpBundle: not a .php file: ${e}`)
|
|
619
|
-
}
|
|
620
|
-
|
|
388
|
+
assertEntries('buildPhpBundle', entries, '.php file', (e) => e.endsWith('.php'))
|
|
621
389
|
const baseDir = resolve(cwd)
|
|
622
390
|
const normalized = normalizeEntries(entries, cwd)
|
|
623
391
|
const autoload = loadComposerAutoload(baseDir)
|
|
624
|
-
// The Composer packages, of composer.lock (vendor/composer/installed.json held identical to it
|
|
625
|
-
// where both are), or of installed.json alone: read once, before anything is scanned.
|
|
626
392
|
const packages = loadComposerPackages(baseDir)
|
|
627
|
-
|
|
628
|
-
// Laravel auto-discovers service providers rather than referencing them statically; seed
|
|
629
|
-
// 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.
|
|
630
394
|
const providerRoots = loadLaravelProviderFiles(baseDir, autoload, packages)
|
|
631
|
-
|
|
632
395
|
const sources = await collectPhpFilesFromDisk(baseDir, [...normalized, ...providerRoots], { autoload })
|
|
633
396
|
const { resolutions, missing } = buildPhpTree(sources, { baseDir, autoload })
|
|
634
|
-
|
|
635
397
|
assertSelfContained('PHP', 'import', normalized, sources, missing)
|
|
636
|
-
|
|
637
|
-
return codeBundle({
|
|
638
|
-
baseDir,
|
|
639
|
-
entries: normalized,
|
|
640
|
-
sources,
|
|
641
|
-
// Group per Composer package (vendor/<pkg>), not the node_modules bucketizer.
|
|
642
|
-
modules: bucketizePhpSources(baseDir, sources, PHP_WORKSPACE_NAME, PHP_WORKSPACE_VERSION, packages),
|
|
643
|
-
resolutions,
|
|
644
|
-
format: PHP_FORMAT,
|
|
645
|
-
// PHP includes don't vary by Node condition; key edges under "php", not the JS wildcard "*".
|
|
646
|
-
conditionKey: 'php',
|
|
647
|
-
})
|
|
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' })
|
|
648
400
|
}
|
|
649
401
|
|
|
650
|
-
// Render a scanned file URL for diagnostics: project-relative when inside `baseDir`, else absolute.
|
|
651
|
-
// The scan issue checks run on the LIVE scan, whose files/imports are keyed by absolute file: URLs
|
|
652
|
-
// (the resolver returns absolute paths); relativizing here matches the project-relative paths the
|
|
653
|
-
// bundle itself stores (toRel), rather than leaking the machine's absolute paths into the message.
|
|
654
402
|
function displayPath(url, baseDir) {
|
|
655
403
|
const abs = fileURLToPath(url)
|
|
656
404
|
const rel = relative(baseDir, abs)
|
|
657
405
|
return rel && !relativeEscapes(rel) ? rel : abs
|
|
658
406
|
}
|
|
659
407
|
|
|
660
|
-
// One unresolved scan edge for diagnostics, naming the file --typescript would resolve it to when
|
|
661
|
-
// the scan found one (Scan#typescriptCandidate).
|
|
662
408
|
function describeUnresolved(u, show) {
|
|
663
409
|
const typescript = u.typescriptURL ? `; resolves to ${show(u.typescriptURL)} under --typescript` : ''
|
|
664
410
|
return `${u.kind} ${u.spec ?? '<dynamic>'} from ${show(u.parentURL)} (${u.reason}${typescript})`
|
|
665
411
|
}
|
|
666
412
|
|
|
667
|
-
//
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
function
|
|
671
|
-
const
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
while (walk.length > 0) {
|
|
678
|
-
for (const e of scanner.files.get(walk.shift())?.edges ?? []) {
|
|
679
|
-
if (staticKinds.has(e.kind) && e.child && !staticReachable.has(e.child)) {
|
|
680
|
-
staticReachable.add(e.child)
|
|
413
|
+
// Static ESM edges load before user code, so a failure there is uncatchable, unlike one past a require()/import().
|
|
414
|
+
const STATIC_KINDS = new Set(['import', 'export-from'])
|
|
415
|
+
|
|
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)) {
|
|
422
|
+
linked.add(e.child)
|
|
681
423
|
walk.push(e.child)
|
|
682
424
|
}
|
|
683
425
|
}
|
|
684
426
|
}
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
427
|
+
return linked
|
|
428
|
+
}
|
|
429
|
+
|
|
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.
|
|
434
|
+
function withinRoot(scanner, within) {
|
|
435
|
+
const outside = [...eagerlyLinked(scanner)].filter((url) => scanner.files.has(url) && !within(url))
|
|
436
|
+
if (outside.length > 0) return { scanner, outside, leftOut: [] }
|
|
437
|
+
const kept = linkedThrough(scanner, [...scanner.entries].filter((url) => scanner.files.has(url)), (e) => scanner.files.has(e.child) && within(e.child))
|
|
438
|
+
if (kept.size === scanner.files.size) return { scanner, outside, leftOut: [] }
|
|
439
|
+
const leftOut = []
|
|
440
|
+
const files = new Map()
|
|
441
|
+
for (const url of kept) {
|
|
442
|
+
const info = scanner.files.get(url)
|
|
443
|
+
const edges = info.edges.map((e) => {
|
|
444
|
+
if (!e.child || within(e.child)) return e
|
|
445
|
+
const { child, ...rest } = e
|
|
446
|
+
leftOut.push({ parentURL: url, kind: e.kind, spec: e.spec, outsideURL: child })
|
|
447
|
+
return { ...rest, error: 'outside the project root' }
|
|
448
|
+
})
|
|
449
|
+
files.set(url, { ...info, edges })
|
|
450
|
+
}
|
|
451
|
+
const imports = new Map()
|
|
452
|
+
for (const [key, byParent] of scanner.imports) {
|
|
453
|
+
const parents = new Map()
|
|
454
|
+
for (const [parentURL, specMap] of byParent) {
|
|
455
|
+
if (!files.has(parentURL)) continue
|
|
456
|
+
const specs = new Map([...specMap].filter(([, childURL]) => within(childURL)))
|
|
457
|
+
if (specs.size > 0) parents.set(parentURL, specs)
|
|
458
|
+
}
|
|
459
|
+
if (parents.size > 0) imports.set(key, parents)
|
|
460
|
+
}
|
|
461
|
+
const unresolved = scanner.unresolved.filter((u) => files.has(u.parentURL))
|
|
462
|
+
const parseErrors = scanner.parseErrors.filter((p) => files.has(p.url))
|
|
463
|
+
return { scanner: { entries: scanner.entries, files, imports, unresolved, parseErrors }, outside, leftOut }
|
|
464
|
+
}
|
|
465
|
+
|
|
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
|
|
469
|
+
}
|
|
470
|
+
|
|
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 }) {
|
|
474
|
+
const show = (url) => displayPath(url, baseDir)
|
|
475
|
+
const staticReachable = eagerlyLinked(scanner)
|
|
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.
|
|
488
|
+
for (const p of scanner.parseErrors) fatal.push(`parse error in ${show(p.url)}: ${p.message}`)
|
|
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()) {
|
|
697
491
|
for (const [parentURL, specMap] of byParent) {
|
|
698
492
|
for (const [spec, childURL] of specMap) {
|
|
699
|
-
if (!scanner.files.has(childURL)) {
|
|
700
|
-
fatal.push(`${spec} from ${show(parentURL)} resolves to ${show(childURL)}, which a source bundle can't carry`)
|
|
701
|
-
}
|
|
493
|
+
if (!scanner.files.has(childURL)) fatal.push(`${spec} from ${show(parentURL)} resolves to ${show(childURL)}, which a source bundle can't carry`)
|
|
702
494
|
}
|
|
703
495
|
}
|
|
704
496
|
}
|
|
705
|
-
|
|
706
|
-
const toleratedParse = scanner.parseErrors.filter((p) => !fatalParse(p))
|
|
707
|
-
return { fatal, tolerated, toleratedParse }
|
|
497
|
+
return { fatal, tolerated, typescript }
|
|
708
498
|
}
|
|
709
499
|
|
|
710
|
-
|
|
711
|
-
// relativizes the paths in the warnings (see displayPath), matching analyzeScanner's messages.
|
|
712
|
-
function reportScanIssues({ fatal, tolerated, toleratedParse }, { label = '', baseDir }) {
|
|
500
|
+
function reportScanIssues({ fatal, tolerated, typescript, leftOut }, { label = '', baseDir, root }) {
|
|
713
501
|
const where = label ? ` (${label})` : ''
|
|
714
502
|
const show = (url) => displayPath(url, baseDir)
|
|
715
|
-
// The first 10 of `items` as indented lines (`line` renders one), then how many more there are.
|
|
716
|
-
const listed = (items, line = (s) => s) => {
|
|
717
|
-
const more = items.length > 10 ? `\n ... and ${items.length - 10} more` : ''
|
|
718
|
-
return items.slice(0, 10).map((x) => ` ${line(x)}`).join('\n') + more
|
|
719
|
-
}
|
|
720
503
|
if (fatal.length > 0) {
|
|
721
|
-
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 })
|
|
722
505
|
}
|
|
723
506
|
if (tolerated.length > 0) {
|
|
724
507
|
const summary = listed(tolerated, (u) => describeUnresolved(u, show))
|
|
725
508
|
console.warn(`[stasis] Bundle has ${tolerated.length} unresolved import(s)${where}; they will fall through at load time:\n${summary}`)
|
|
726
509
|
}
|
|
727
|
-
if (
|
|
728
|
-
const summary = listed(
|
|
729
|
-
console.warn(`[stasis] Bundle
|
|
510
|
+
if (leftOut.length > 0) {
|
|
511
|
+
const summary = listed(leftOut, (u) => `${u.kind} ${u.spec} from ${show(u.parentURL)} (at ${relative(baseDir, fileURLToPath(u.outsideURL))})`)
|
|
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}`)
|
|
730
513
|
}
|
|
731
514
|
}
|
|
732
515
|
|
|
733
|
-
//
|
|
734
|
-
// token into the resolver.
|
|
516
|
+
// Trimmed, empties dropped, so a sloppy programmatic caller can't push a bogus token into the resolver.
|
|
735
517
|
const cleanConditions = (conditions) => conditions.map((c) => (typeof c === 'string' ? c.trim() : c)).filter(Boolean)
|
|
736
518
|
|
|
737
|
-
// --typescript
|
|
738
|
-
// exist), else those of the tsconfig.json beside each importing file's package.json
|
|
739
|
-
// (packageTsconfigPaths, the project read as the State reads it, to `env`'s PROJECT_CWD; null
|
|
740
|
-
// matcher = no aliases). Without --typescript they only hint a miss (Scan#typescriptCandidate), so a
|
|
741
|
-
// 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.
|
|
742
520
|
function typescriptPathsFor(typescript, baseDir, tsconfig, host, env) {
|
|
743
|
-
if (typescript && tsconfig !== undefined)
|
|
521
|
+
if (typescript && tsconfig !== undefined) {
|
|
522
|
+
const matcher = loadTsconfigPaths(discoverTsconfig(baseDir, tsconfig, host), host)
|
|
523
|
+
return { matchPaths: (spec) => matcher?.matchPaths(spec) ?? [], outputs: tsconfigOutputsOf({ host }) }
|
|
524
|
+
}
|
|
744
525
|
return packageTsconfigPaths(baseDir, { host, lenient: !typescript, projectCwd: env.PROJECT_CWD })
|
|
745
526
|
}
|
|
746
527
|
|
|
747
|
-
//
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
// extra `exports`/`imports` conditions; on their own they don't honour legacy mainFields or
|
|
751
|
-
// platform suffixes (see `--mainFields` / buildResolvedJsBundle). `typescript` maps a failed
|
|
752
|
-
// resolution to its on-disk TS source (tsc's rules; see resolve-typescript.js), honouring the
|
|
753
|
-
// `paths` aliases of `tsconfig` (an explicit config path, default each package's tsconfig.json); it
|
|
754
|
-
// is taken as given where the entries import TS sources by output name (importsTypescriptByOutputName).
|
|
755
|
-
// Files are read through `host` (@exodus/stasis-core/host), the disk by default; EXODUS_STASIS_*
|
|
756
|
-
// settings from `env`. With `innermostRoot`, the State is rooted at the innermost package at or
|
|
757
|
-
// above cwd that holds every file the scan reaches (innermostRootOf), rather than at the project's
|
|
758
|
-
// root above it, where that holds no stasis file of its own.
|
|
759
|
-
export async function buildJsBundle({ cwd = process.cwd(), env = process.env, entries, scope, conditions = [], jsx = false, flow = false, typescript = false, tsconfig, resources = [], packageJSON = false, host = diskHost, innermostRoot = false } = {}) {
|
|
760
|
-
if (!Array.isArray(entries) || entries.length === 0) {
|
|
761
|
-
throw new Error('buildJsBundle: at least one entry .js/.cjs/.mjs/.ts/.cts/.mts/.jsx/.tsx file is required')
|
|
762
|
-
}
|
|
763
|
-
for (const e of entries) {
|
|
764
|
-
if (!JS_EXTS.has(extname(e))) throw new Error(`buildJsBundle: not a JS/TS file: ${e}`)
|
|
765
|
-
}
|
|
766
|
-
|
|
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')
|
|
767
531
|
const baseDir = resolve(cwd)
|
|
768
532
|
const absEntries = entries.map((e) => resolve(baseDir, e))
|
|
769
|
-
|
|
770
533
|
const scanConditions = cleanConditions(conditions)
|
|
771
|
-
// --resources: extensions/filenames carried as opaque assets instead of failing "can't carry".
|
|
772
534
|
const resourceSet = parseResourcesOption('buildJsBundle', resources)
|
|
773
|
-
|
|
774
535
|
typescript ||= importsTypescriptByOutputName(absEntries, host)
|
|
775
536
|
const typescriptPaths = typescriptPathsFor(typescript, baseDir, tsconfig, host, env)
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
//
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
// Materialise via a non-preload State: addFile bucketizes + records sources/formats,
|
|
783
|
-
// addImport replays the edge map; serialize emits the runtime loader's v1 layout.
|
|
784
|
-
// bundle:'replace' skips reading any on-disk stasis.code.br (bundle:'add' would leak stale
|
|
785
|
-
// entries); lock:'ignore' tolerates a pre-existing lockfile without consuming it; and it is never
|
|
786
|
-
// written, so it claims no write target.
|
|
787
|
-
const stateOptions = { bundle: 'replace', lock: 'ignore', ...(scope ? { scope } : {}), host, claim: false }
|
|
537
|
+
const scanned = scan(absEntries, { conditions: scanConditions, jsx, flow, typescript, typescriptPaths, resources: resourceSet, define, host, memo: memo ? nestedMap(memo, 'js') : null, missTargets: generated !== undefined })
|
|
538
|
+
// A round of a lazy tree that asked for a package already is void: the rest is moot.
|
|
539
|
+
if (memo && host.touches() > 0) throw new Error('buildJsBundle: this round reads a package not unpacked yet')
|
|
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 }
|
|
788
543
|
let state = new State(baseDir, { ...stateOptions, env })
|
|
789
544
|
// A root holding a stasis file is the project's own choice, whose config the build takes.
|
|
790
545
|
if (innermostRoot && !STASIS_ROOT_FILES.some((file) => host.stat(join(state.root, file)) !== null)) {
|
|
791
|
-
const root = innermostRootOf(baseDir, [...
|
|
546
|
+
const root = innermostRootOf(baseDir, [...scanned.files.keys()].map((url) => host.realpath(fileURLToPath(url))), state.root, host)
|
|
792
547
|
// The State's walk up from cwd stops at PROJECT_CWD, as it does where yarn sets it.
|
|
793
548
|
if (root !== state.root) state = new State(baseDir, { ...stateOptions, env: { ...env, PROJECT_CWD: root } })
|
|
794
549
|
}
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
for (const url of
|
|
798
|
-
|
|
799
|
-
if (!relativeEscapes(relative(state.root, file))) continue
|
|
800
|
-
const markers = STASIS_ROOT_FILES.filter((name) => host.stat(join(state.root, name)) !== null)
|
|
801
|
-
const rootedBy = markers.length === 0 ? '' : ` (rooted there by its ${markers.join(' and ')}, which can be removed if stale)`
|
|
802
|
-
throw new Error(`buildJsBundle: ${relative(baseDir, file)} is outside the project root at ${relative(baseDir, state.root) || '.'}${rootedBy}`)
|
|
803
|
-
}
|
|
550
|
+
const { scanner, outside, leftOut } = withinRoot(scanned, (url) => isPathWithin(state.root, fileURLToPath(url)))
|
|
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 })
|
|
804
554
|
for (const [url, info] of scanner.files) {
|
|
805
|
-
|
|
806
|
-
// and stores it under `resources` (a resource can't be an entry, so no isEntry).
|
|
807
|
-
if (info.resource) {
|
|
808
|
-
state.addFile(url, { resource: true })
|
|
809
|
-
continue
|
|
810
|
-
}
|
|
811
|
-
const isEntry = scanner.entries.has(url)
|
|
812
|
-
state.addFile(url, { format: info.format, isEntry })
|
|
555
|
+
state.addFile(url, info.resource ? { resource: true, reason: 'bundle' } : { format: info.format, isEntry: scanner.entries.has(url), reason: 'bundle' })
|
|
813
556
|
}
|
|
814
|
-
//
|
|
815
|
-
//
|
|
816
|
-
|
|
817
|
-
// would serve one context the other's file; an unmatched set fails closed via resolveBundled.
|
|
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.
|
|
559
|
+
const missed = new Set([...scanner.unresolved, ...leftOut].filter((u) => u.spec !== undefined).map((u) => `${u.parentURL}\0${u.spec}`))
|
|
818
560
|
const byParent = new Map()
|
|
819
561
|
for (const [key, parents] of scanner.imports) {
|
|
820
562
|
for (const [parentURL, specs] of parents) {
|
|
821
|
-
|
|
822
|
-
const bySpec = byParent.get(parentURL)
|
|
823
|
-
for (const [spec, childURL] of specs) {
|
|
824
|
-
if (!bySpec.has(spec)) bySpec.set(spec, new Map())
|
|
825
|
-
bySpec.get(spec).set(key, childURL)
|
|
826
|
-
}
|
|
563
|
+
for (const [spec, childURL] of specs) nestedMap(nestedMap(byParent, parentURL), spec).set(key, childURL)
|
|
827
564
|
}
|
|
828
565
|
}
|
|
829
566
|
for (const [parentURL, bySpec] of byParent) {
|
|
830
567
|
for (const [spec, byKey] of bySpec) {
|
|
831
568
|
const targets = new Set(byKey.values())
|
|
832
|
-
if (targets.size === 1) {
|
|
569
|
+
if (targets.size === 1 && !missed.has(`${parentURL}\0${spec}`)) {
|
|
833
570
|
state.addImport(parentURL, spec, [...targets][0], { conditions: '*' })
|
|
834
571
|
} else {
|
|
835
|
-
for (const [key, childURL] of byKey) {
|
|
836
|
-
state.addImport(parentURL, spec, childURL, { conditions: key.split(', ') })
|
|
837
|
-
}
|
|
572
|
+
for (const [key, childURL] of byKey) state.addImport(parentURL, spec, childURL, { conditions: key.split(', ') })
|
|
838
573
|
}
|
|
839
574
|
}
|
|
840
575
|
}
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
if (packageJSON) state.includePackageJson()
|
|
576
|
+
if (extra) state.includeExtra({ outputs: outputsAt(state.root), resources: resources === undefined ? undefined : resourceSet })
|
|
577
|
+
if (packageJSON || state.config.packageJSON) state.includePackageJson()
|
|
844
578
|
return state
|
|
845
579
|
}
|
|
846
580
|
|
|
847
581
|
// The files that root a State in the directory holding one (state.js's discovery).
|
|
848
|
-
const STASIS_ROOT_FILES = [
|
|
582
|
+
const STASIS_ROOT_FILES = [CONFIG_FILE, LOCK_FILE, BUNDLE_FILE]
|
|
849
583
|
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
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.
|
|
854
590
|
function innermostRootOf(baseDir, files, root, host) {
|
|
855
591
|
for (let dir = baseDir; dir !== root && dirname(dir) !== dir; dir = dirname(dir)) {
|
|
856
592
|
if (files.every((file) => isPathWithin(dir, file)) && readPackageJson(dir, 'package.json', { host })?.name !== undefined) return dir
|
|
@@ -858,687 +594,1614 @@ function innermostRootOf(baseDir, files, root, host) {
|
|
|
858
594
|
return root
|
|
859
595
|
}
|
|
860
596
|
|
|
861
|
-
// Extensions probed when a resolved target names none, in Metro's default sourceExts order.
|
|
862
|
-
// Limited to what a source bundle can carry (scan's RESOLVABLE_EXTS) so the resolver never
|
|
863
|
-
// resolves a file the bundle would reject; an extensionless `import './Foo'` can land on
|
|
864
|
-
// Foo.jsx/Foo.tsx, as it does under Metro. Like Metro's default, this doesn't track a project's
|
|
865
|
-
// own sourceExts.
|
|
866
|
-
const SOURCE_EXTS = ['js', 'jsx', 'json', 'ts', 'tsx']
|
|
867
|
-
// React Native preset mainFields for `--metro` (which also sets the RN conditions + platform suffixes).
|
|
868
597
|
const METRO_MAIN_FIELDS = ['react-native', 'browser', 'main']
|
|
869
|
-
|
|
870
|
-
//
|
|
871
|
-
|
|
872
|
-
|
|
598
|
+
|
|
599
|
+
// What a React Native app's build loads from its root, though nothing the app imports reaches it.
|
|
600
|
+
const METRO_CONFIG_FILE = 'metro.config.js'
|
|
601
|
+
const METRO_CONFIG_FILES = [METRO_CONFIG_FILE, 'react-native.config.js']
|
|
602
|
+
const RN_CONFIG_FILE = 'rn.config.json'
|
|
603
|
+
const BABEL_CONFIG_FILE = 'babel.config.js'
|
|
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.
|
|
606
|
+
const METRO_VIA = ['react-native', '@react-native/community-cli-plugin', 'metro']
|
|
607
|
+
const METRO_TOOLCHAIN = [
|
|
608
|
+
{ name: 'react-native', file: 'cli.js', via: [] },
|
|
609
|
+
{ name: 'metro', via: METRO_VIA.slice(0, -1) },
|
|
610
|
+
// Loaded by a path: Metro's worker, its transform worker, jest-worker's child process and the Haste worker.
|
|
611
|
+
{ name: 'metro', file: 'src/DeltaBundler/Worker.js', via: METRO_VIA.slice(0, -1) },
|
|
612
|
+
{ name: 'metro-transform-worker', via: METRO_VIA },
|
|
613
|
+
{ name: 'jest-worker', file: 'build/workers/processChild.js', via: METRO_VIA },
|
|
614
|
+
{ name: 'metro-file-map', file: 'src/plugins/haste/worker.js', via: METRO_VIA },
|
|
615
|
+
// metro-config's default `minifierPath`, which the transform worker requires.
|
|
616
|
+
{ name: 'metro-minify-terser', via: [...METRO_VIA, 'metro-transform-worker'] },
|
|
617
|
+
]
|
|
618
|
+
// With --babel(-aliases), the `babelTransformerPath` React Native's Metro config names.
|
|
619
|
+
const BABEL_TOOLCHAIN = [{ name: '@react-native/metro-babel-transformer', via: ['@react-native/metro-config'] }]
|
|
620
|
+
// Beside stasis's Metro plugin (src/metro.js), run by Metro's workers under withStasis().
|
|
621
|
+
const STASIS_METRO_TRANSFORMERS = ['metro-transformer.js', 'metro-babel-transformer.js']
|
|
622
|
+
const NODE_REQUIRE_CONDITIONS = new Set(['require', 'node', 'node-addons', 'module-sync'])
|
|
623
|
+
const REQUIRE_KEY = [...NODE_REQUIRE_CONDITIONS].join(', ')
|
|
624
|
+
// What esbuild and webpack 5 both assert besides `import`/`require` and `default`, which Node and Metro never do.
|
|
873
625
|
const BUNDLER_CONDITIONS = ['module']
|
|
874
626
|
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
627
|
+
export function fieldResolverFor(platform, { mainFields, metro = false, assetExts, sourceExts = METRO_SOURCE_EXTS, assetResolutions, conditions = [], typescript = false, typescriptPaths = null, host = diskHost, kept }) {
|
|
628
|
+
const extras = metro ? ['react-native', ...(platform === 'web' ? ['browser'] : [])] : [...new Set([...BUNDLER_CONDITIONS, ...conditions])]
|
|
629
|
+
const fields = metro ? METRO_MAIN_FIELDS : mainFields
|
|
630
|
+
const resolver = createFieldResolver({
|
|
631
|
+
mainFields: fields, platform, preferNative: platform !== null && platform !== 'web', sourceExts, conditions: resolveConditions('commonjs', extras),
|
|
632
|
+
metro, assetExts, assetResolutions, typescript, typescriptPaths, host, kept,
|
|
633
|
+
})
|
|
634
|
+
return { extras, mainFields: fields, resolver }
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function parseJson(text, message) {
|
|
638
|
+
try {
|
|
639
|
+
return JSON.parse(text)
|
|
640
|
+
} catch (cause) {
|
|
641
|
+
throw new Error(message, { cause })
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
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)
|
|
666
|
+
|
|
667
|
+
export const configuredLockfiles = (name, cwd, host) => parseLockfilesOption(`${name}: ${CONFIG_FILE}`, readConfigFile(name, cwd, host)?.lockfiles)
|
|
668
|
+
|
|
669
|
+
// --babel-aliases: babel-plugin-module-resolver's `alias` option as data, from a JSON file in the project.
|
|
670
|
+
function readAliasesFile(baseDir, file, host) {
|
|
671
|
+
const rel = relative(baseDir, resolve(baseDir, file))
|
|
672
|
+
if (relativeEscapes(rel)) throw new Error(`stasis bundle: --babel-aliases must name a file in the project: ${file}`)
|
|
673
|
+
const text = readFileSyncMaybe(baseDir, toPosix(rel), 'utf8', host)
|
|
674
|
+
if (text === null) throw new Error(`stasis bundle: --babel-aliases file not found: ${file}`)
|
|
675
|
+
assertRealPathWithinBase(host.realpath(baseDir), baseDir, toPosix(rel), host)
|
|
676
|
+
const alias = parseJson(text, `stasis bundle: --babel-aliases file ${file} is not valid JSON`)
|
|
677
|
+
if (!isPlainObject(alias) || !Object.values(alias).every((value) => typeof value === 'string')) {
|
|
678
|
+
throw new Error(`stasis bundle: --babel-aliases file ${file} must be an object mapping each alias to a string`)
|
|
679
|
+
}
|
|
680
|
+
for (const key of Object.keys(alias)) {
|
|
681
|
+
if (!key.startsWith('^') && !key.endsWith('$')) continue
|
|
682
|
+
try {
|
|
683
|
+
RegExp(key) // compiled to check it, as createAliases compiles it
|
|
684
|
+
} catch (cause) {
|
|
685
|
+
throw new Error(`stasis bundle: --babel-aliases file ${file}: ${JSON.stringify(key)} is not a valid regular expression`, { cause })
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
return alias
|
|
689
|
+
}
|
|
690
|
+
|
|
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 }) {
|
|
693
|
+
const resourcesGiven = resources !== undefined
|
|
694
|
+
const config = scope === undefined || !resourcesGiven || !packageJSON ? readConfigFile(name, cwd, host) : null
|
|
695
|
+
if (scope !== undefined && scope !== 'full') {
|
|
696
|
+
throw new Error(`${name}: --scope=${scope} is not supported with --mainFields or --metro (they always build a full-scope bundle)`)
|
|
697
|
+
}
|
|
698
|
+
if (scope === undefined && config?.scope !== undefined && config.scope !== 'full') {
|
|
699
|
+
throw new Error(`${name}: ${CONFIG_FILE} sets scope ${JSON.stringify(config.scope)}, which --mainFields and --metro don't support (they always build a full-scope bundle); pass --scope=full to override it`)
|
|
700
|
+
}
|
|
701
|
+
if (config?.packageJSON !== undefined && typeof config.packageJSON !== 'boolean') {
|
|
702
|
+
throw new Error(`${name}: ${CONFIG_FILE} sets packageJSON ${JSON.stringify(config.packageJSON)}, which must be true or false`)
|
|
703
|
+
}
|
|
704
|
+
return {
|
|
705
|
+
resources: resourcesGiven ? parseResourcesOption(name, resources) : parseResourcesOption(CONFIG_FILE, config?.resources),
|
|
706
|
+
packageJSON: packageJSON || config?.packageJSON === true,
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// Metro's default resolver.emptyModulePath, which it resolves a disabled import to.
|
|
711
|
+
const METRO_EMPTY_MODULE = 'node_modules/metro-runtime/src/modules/empty-module.js'
|
|
712
|
+
const EMPTY_MODULE_SOURCE = /^(?:(["'])use strict\1;?)?$/u
|
|
713
|
+
|
|
714
|
+
function metroEmptyModule(baseDir, host) {
|
|
715
|
+
const abs = join(baseDir, METRO_EMPTY_MODULE)
|
|
716
|
+
if (!host.stat(abs)?.isFile()) return null
|
|
717
|
+
const real = host.realpath(abs)
|
|
718
|
+
if (!isPathWithin(host.realpath(baseDir), real)) return null
|
|
719
|
+
const buf = host.readFile(real)
|
|
720
|
+
return isUtf8(buf) && EMPTY_MODULE_SOURCE.test(buf.toString('utf8').trim()) ? real : null
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
// Through each of `via` first, for an install (pnpm) that links `name` only beside the one depending on it.
|
|
724
|
+
function packageDirFrom(dir, name, host, via = []) {
|
|
725
|
+
let from = dir
|
|
726
|
+
for (const step of [...via, name]) {
|
|
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)
|
|
731
|
+
}
|
|
732
|
+
from = host.realpath(join(d, 'node_modules', step))
|
|
733
|
+
}
|
|
734
|
+
return from
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
function requireFrom(dir, spec, host) {
|
|
880
738
|
try {
|
|
881
|
-
|
|
739
|
+
return host.resolve(join(dir, 'package.json'), spec, NODE_REQUIRE_CONDITIONS)
|
|
882
740
|
} catch {
|
|
883
|
-
return
|
|
884
|
-
}
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
741
|
+
return undefined
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
const parseModule = (file, host, source = host.readFile(file).toString('utf8')) => getParser().parseSync(file, source, { sourceType: 'unambiguous' }).program
|
|
746
|
+
|
|
747
|
+
function walkAst(node, fn) {
|
|
748
|
+
if (Array.isArray(node)) {
|
|
749
|
+
for (const child of node) walkAst(child, fn)
|
|
750
|
+
return
|
|
751
|
+
}
|
|
752
|
+
if (!node || typeof node !== 'object' || typeof node.type !== 'string') return
|
|
753
|
+
fn(node)
|
|
754
|
+
for (const key of Object.keys(node)) if (key !== 'type') walkAst(node[key], fn)
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
// The method `node` calls (`x.concat(...)` -> 'concat'), on the object named `object` where given.
|
|
758
|
+
function memberCall(node, object) {
|
|
759
|
+
if (node?.type !== 'CallExpression' || node.callee.type !== 'MemberExpression' || node.callee.computed) return undefined
|
|
760
|
+
if (object !== undefined && (node.callee.object.type !== 'Identifier' || node.callee.object.name !== object)) return undefined
|
|
761
|
+
return node.callee.property.name
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// What `file` names by `require.resolve('x')`, read off its source, never run. -> [specifier, path][]
|
|
765
|
+
function requireResolutions(file, host) {
|
|
766
|
+
const resolved = []
|
|
767
|
+
const source = host.readFile(file).toString('utf8')
|
|
768
|
+
// Parsed only where both words occur, however the call spaces them (`require\n .resolve(`).
|
|
769
|
+
if (!source.includes('require') || !source.includes('resolve')) return resolved
|
|
770
|
+
walkAst(parseModule(file, host, source), (node) => {
|
|
771
|
+
const spec = memberCall(node, 'require') === 'resolve' ? literalSpec(node.arguments[0]) : null
|
|
772
|
+
const path = spec === null ? null : babelPathCall(node, file, host)
|
|
773
|
+
if (path !== null) resolved.push([spec, path])
|
|
774
|
+
})
|
|
775
|
+
return resolved
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
// The name Babel requires a plugin or preset by, as @babel/core's standardizeName has it.
|
|
779
|
+
const BABEL_PREFIX = { plugin: /^(?!@|module:|[^/]+\/|babel-plugin-)/u, preset: /^(?!@|module:|[^/]+\/|babel-preset-)/u }
|
|
780
|
+
const BABEL_ORG = { plugin: /^(@babel\/)(?!plugin-|[^/]+\/)/u, preset: /^(@babel\/)(?!preset-|[^/]+\/)/u }
|
|
781
|
+
const OTHER_ORG = { plugin: /^(@(?!babel\/)[^/]+\/)(?![^/]*babel-plugin(?:-|\/|$)|[^/]+\/)/u, preset: /^(@(?!babel\/)[^/]+\/)(?![^/]*babel-preset(?:-|\/|$)|[^/]+\/)/u }
|
|
782
|
+
const OTHER_ORG_DEFAULT = /^(@(?!babel$)[^/]+)$/u
|
|
783
|
+
const standardizeBabelName = (type, name) => (isAbsolute(name) ? name : name
|
|
784
|
+
.replace(BABEL_PREFIX[type], `babel-${type}-`)
|
|
785
|
+
.replace(BABEL_ORG[type], `$1${type}-`)
|
|
786
|
+
.replace(OTHER_ORG[type], `$1babel-${type}-`)
|
|
787
|
+
.replace(OTHER_ORG_DEFAULT, `$1/babel-${type}`)
|
|
788
|
+
.replace(/^module:/u, ''))
|
|
789
|
+
|
|
790
|
+
// A string naming a Babel plugin or preset by its form, a package or a path.
|
|
791
|
+
const BABEL_MODULE_STRING = /^module:|babel[-/](?:plugin|preset)/u
|
|
792
|
+
// `babel` and `config` in its file's name (create-babel-config.js), or its package's (@org/babel-config).
|
|
793
|
+
const BABEL_CONFIG_NAME = /babel.*config|config.*babel/iu
|
|
794
|
+
const isBabelConfigModule = (rel) => BABEL_CONFIG_NAME.test(posix.basename(rel)) || BABEL_CONFIG_NAME.test(splitNodeModulesPath(rel)?.name ?? '')
|
|
795
|
+
|
|
796
|
+
// The name of a key, variable or object holding a list -> what an entry of it is taken as.
|
|
797
|
+
const BABEL_LISTS = new Map([['plugins', 'plugin'], ['presets', 'preset']])
|
|
798
|
+
const METRO_ASSET_LISTS = new Map([['assetExts', 'asset']])
|
|
799
|
+
// The array methods making a list of the one they're called on (`concat` with its arguments').
|
|
800
|
+
const LIST_METHODS = new Set(['concat', 'filter', 'flat', 'slice'])
|
|
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
|
|
809
|
+
function staticPath(node, file) {
|
|
810
|
+
if (node?.type === 'Identifier' && node.name === '__dirname') return dirname(file)
|
|
811
|
+
const string = literalSpec(node)
|
|
812
|
+
if (string !== null || node?.type !== 'CallExpression' || memberCall(node, 'require') !== undefined) return string
|
|
813
|
+
const name = node.callee.type === 'Identifier' ? node.callee.name : memberCall(node)
|
|
814
|
+
if (name !== 'resolve' && name !== 'join') return null
|
|
815
|
+
const parts = node.arguments.map((arg) => staticPath(arg, file))
|
|
816
|
+
if (parts.length === 0 || parts.includes(null)) return null
|
|
817
|
+
return name === 'resolve' && parts.some((part) => isAbsolute(part)) ? resolve(...parts) : join(...parts)
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
// A staticPath, as Babel takes a plugin named by path, or what `require.resolve('x')` resolves to. -> it, or null
|
|
821
|
+
function babelPathCall(node, file, host) {
|
|
822
|
+
if (memberCall(node, 'require') !== 'resolve') {
|
|
823
|
+
const path = node?.type === 'CallExpression' ? staticPath(node, file) : null
|
|
824
|
+
return path === null || isAbsolute(path) || path.startsWith('.') ? path : `./${path}`
|
|
825
|
+
}
|
|
826
|
+
const spec = literalSpec(node.arguments[0])
|
|
827
|
+
const option = node.arguments[1]
|
|
828
|
+
const paths = option?.type === 'ObjectExpression'
|
|
829
|
+
? option.properties.find((property) => property.type === 'Property' && !property.computed && (property.key.name ?? property.key.value) === 'paths')
|
|
830
|
+
: undefined
|
|
831
|
+
const dirs = paths === undefined ? [dirname(file)] : paths.value.type === 'ArrayExpression' ? paths.value.elements.map((element) => staticPath(element, file)) : [null]
|
|
832
|
+
// One only the run knows, or one relative to its cwd: no telling where Node looks.
|
|
833
|
+
if (spec === null || dirs.some((dir) => dir === null || !isAbsolute(dir))) return null
|
|
834
|
+
for (const dir of dirs) {
|
|
835
|
+
try {
|
|
836
|
+
return host.resolve(join(dir, 'index.js'), spec, NODE_REQUIRE_CONDITIONS)
|
|
837
|
+
} catch { /* not from here: the next */ }
|
|
838
|
+
}
|
|
839
|
+
return null
|
|
840
|
+
}
|
|
841
|
+
|
|
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.
|
|
844
|
+
function readLists(program, lists, entry) {
|
|
845
|
+
const declarators = []
|
|
846
|
+
walkAst(program, (node) => {
|
|
847
|
+
if (node.type === 'VariableDeclarator' && node.id.type === 'Identifier' && node.init) declarators.push(node)
|
|
848
|
+
})
|
|
849
|
+
const inits = Map.groupBy(declarators, (node) => node.id.name)
|
|
850
|
+
const seen = new Map()
|
|
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.
|
|
853
|
+
const read = (node, type, depth) => {
|
|
854
|
+
if (!node || depth > MAX_LIST_DEPTH) return
|
|
855
|
+
const key = `${type}${depth}`
|
|
856
|
+
const keys = upsert(seen, node, () => new Set())
|
|
857
|
+
if (keys.has(key)) return
|
|
858
|
+
keys.add(key)
|
|
859
|
+
if (node.type === 'ArrayExpression') {
|
|
860
|
+
if (depth === 0) return read(node.elements[0], type, 0)
|
|
861
|
+
for (const element of node.elements) {
|
|
862
|
+
if (element?.type === 'SpreadElement') read(element.argument, type, depth)
|
|
863
|
+
else read(element, type, depth - 1)
|
|
864
|
+
}
|
|
865
|
+
} else if (node.type === 'Identifier') {
|
|
866
|
+
for (const { init } of inits.get(node.name) ?? []) read(init, type, depth)
|
|
867
|
+
} else if (node.type === 'ConditionalExpression' || node.type === 'LogicalExpression') {
|
|
868
|
+
for (const side of node.type === 'LogicalExpression' ? [node.left, node.right] : [node.consequent, node.alternate]) read(side, type, depth)
|
|
869
|
+
} else if (depth > 0 && LIST_METHODS.has(memberCall(node))) {
|
|
870
|
+
const method = memberCall(node)
|
|
871
|
+
const levels = method === 'flat' ? flatDepth(node.arguments[0]) : 0
|
|
872
|
+
read(node.callee.object, type, Math.min(depth + levels, MAX_LIST_DEPTH))
|
|
873
|
+
if (method === 'concat') for (const arg of node.arguments) read(arg.argument ?? arg, type, arg.type === 'SpreadElement' ? depth + 1 : depth)
|
|
874
|
+
} else {
|
|
875
|
+
entry(type, node)
|
|
892
876
|
}
|
|
893
877
|
}
|
|
878
|
+
const listName = (node) => (node.type === 'Identifier' ? node.name : node.type === 'MemberExpression' && !node.computed ? node.property.name : undefined)
|
|
879
|
+
walkAst(program, (node) => {
|
|
880
|
+
// A list under a key, held in a variable or assigned to a property, so named.
|
|
881
|
+
const [name, value] = node.type === 'Property' && !node.computed ? [node.key.name ?? node.key.value, node.value]
|
|
882
|
+
: node.type === 'VariableDeclarator' ? [listName(node.id), node.init]
|
|
883
|
+
: node.type === 'AssignmentExpression' ? [listName(node.left), node.right]
|
|
884
|
+
: []
|
|
885
|
+
if (lists.has(name)) read(value, lists.get(name), 1)
|
|
886
|
+
// Or added to one: `push` and `unshift` each argument an entry, `concat` as it concatenates.
|
|
887
|
+
const method = memberCall(node)
|
|
888
|
+
const type = method && lists.get(listName(node.callee.object))
|
|
889
|
+
if (!type) return
|
|
890
|
+
if (method === 'concat') read(node, type, 1)
|
|
891
|
+
else if (method === 'push' || method === 'unshift') for (const arg of node.arguments) read(arg.argument ?? arg, type, arg.type === 'SpreadElement' ? 1 : 0)
|
|
892
|
+
})
|
|
894
893
|
}
|
|
895
894
|
|
|
896
|
-
// The
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
const manifests = []
|
|
906
|
-
walkNative(pkgAbs, manifests, host, isNativeManifest, isExcludedNativeDir)
|
|
907
|
-
const hasIos = host.stat(join(pkgAbs, 'ios')) !== null
|
|
908
|
-
const hasAndroid = host.stat(join(pkgAbs, 'android')) !== null
|
|
909
|
-
if (!hasIos && !hasAndroid && !manifests.some((f) => isPodspec(f))) return []
|
|
910
|
-
const out = [...manifests]
|
|
911
|
-
if (hasIos) walkNative(join(pkgAbs, 'ios'), out, host, isNativeSource)
|
|
912
|
-
if (hasAndroid) walkNative(join(pkgAbs, 'android'), out, host, isNativeSource)
|
|
913
|
-
return [...new Set(out)]
|
|
914
|
-
}
|
|
915
|
-
|
|
916
|
-
// The built-in field/suffix resolver (resolve-fields.js) the legacy-field build resolves with on
|
|
917
|
-
// `platform` (null for --mainFields), its `mainFields` (Metro's under --metro), and the conditions
|
|
918
|
-
// it adds to the bundler base (`import`/`require` + `default`, never Node's `node`; see
|
|
919
|
-
// resolveConditions): --metro asserts the RN conditions (+ browser on web), Metro's own set;
|
|
920
|
-
// --mainFields asserts esbuild's and webpack's `module` (BUNDLER_CONDITIONS) and the user's --conditions.
|
|
921
|
-
export function fieldResolverFor(platform, { mainFields, metro = false, conditions = [], typescript = false, typescriptPaths = null, host = diskHost }) {
|
|
922
|
-
const extras = metro ? ['react-native', ...(platform === 'web' ? ['browser'] : [])] : [...new Set([...BUNDLER_CONDITIONS, ...conditions])]
|
|
923
|
-
const fields = metro ? METRO_MAIN_FIELDS : mainFields
|
|
924
|
-
const resolver = createFieldResolver({
|
|
925
|
-
mainFields: fields,
|
|
926
|
-
platform,
|
|
927
|
-
preferNative: platform !== null && platform !== 'web',
|
|
928
|
-
sourceExts: SOURCE_EXTS,
|
|
929
|
-
conditions: resolveConditions('commonjs', extras),
|
|
930
|
-
// Opt into Metro's package-entry browser-field quirks only on the --metro path.
|
|
931
|
-
metro,
|
|
932
|
-
// --typescript: tsc's mapping, inside the field resolver (the scanner's own fallback
|
|
933
|
-
// only backs the built-in resolver). Unreachable under --metro-resolver
|
|
934
|
-
// (classifyEntries rejects the combination -- metro-resolver can't substitute).
|
|
935
|
-
typescript,
|
|
936
|
-
typescriptPaths,
|
|
937
|
-
host,
|
|
895
|
+
// The plugins and presets `file` lists, as Babel requires them; with `anyString`, any BABEL_MODULE_STRING too.
|
|
896
|
+
function babelNamedModules(file, host, { anyString = false } = {}) {
|
|
897
|
+
const program = parseModule(file, host)
|
|
898
|
+
const named = new Set()
|
|
899
|
+
readLists(program, BABEL_LISTS, (type, node) => {
|
|
900
|
+
const name = literalSpec(node)
|
|
901
|
+
const path = name ? null : babelPathCall(node, file, host)
|
|
902
|
+
if (name) named.add(standardizeBabelName(type, name))
|
|
903
|
+
else if (path) named.add(path)
|
|
938
904
|
})
|
|
939
|
-
|
|
905
|
+
if (anyString) {
|
|
906
|
+
walkAst(program, (node) => {
|
|
907
|
+
const string = literalSpec(node)
|
|
908
|
+
if (string && BABEL_MODULE_STRING.test(string)) named.add(string.replace(/^module:/u, ''))
|
|
909
|
+
})
|
|
910
|
+
}
|
|
911
|
+
return named
|
|
912
|
+
}
|
|
913
|
+
|
|
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) {
|
|
916
|
+
const exts = new Set(METRO_ASSET_EXTS)
|
|
917
|
+
const config = resolve(baseDir, file)
|
|
918
|
+
if (!host.stat(config)?.isFile()) return exts
|
|
919
|
+
readLists(parseModule(config, host), METRO_ASSET_LISTS, (_, node) => {
|
|
920
|
+
const ext = literalSpec(node)
|
|
921
|
+
if (ext) exts.add(ext)
|
|
922
|
+
})
|
|
923
|
+
return exts
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
// rn.config.json's options stasis reads, each [valid, what], as @preventive/rn's rn-config.js checks them.
|
|
927
|
+
const listOf = (valid) => (value) => Array.isArray(value) && value.every(valid)
|
|
928
|
+
const EXTENSIONS = [listOf((value) => typeof value === 'string' && /^[^./\\][^/\\]*$/u.test(value)), 'a list of extensions, each without its dot ("riv")']
|
|
929
|
+
const isScale = (value) => (typeof value === 'number' && Number.isFinite(value) && value > 0) || (typeof value === 'string' && /^\d+(?:\.\d+)?$/u.test(value) && Number(value) > 0)
|
|
930
|
+
const RN_CONFIG_LISTS = {
|
|
931
|
+
metroConfig: [(value) => typeof value === 'boolean' || (typeof value === 'string' && value !== ''), 'true, false or the path of a Metro config'],
|
|
932
|
+
assetExts: EXTENSIONS,
|
|
933
|
+
sourceExts: EXTENSIONS,
|
|
934
|
+
assetResolutions: [listOf(isScale), 'a list of scales ("0.75", or 0.75)'],
|
|
935
|
+
}
|
|
936
|
+
const PATH_GLOBS = [listOf((value) => typeof value === 'string' && isPathGlob(value)), 'a list of globs of paths from the project root, each a directory\'s files too ("src", "src/**/*.js", "node_modules")']
|
|
937
|
+
const isPackageGlob = (value) => typeof value === 'string' && /^(?:@[^/]+\/[^/]+|[^@/.][^/]*)$/u.test(value) && !value.includes('\\') && globRegExp(value) !== null
|
|
938
|
+
const PACKAGE_GLOBS = [listOf(isPackageGlob), 'a list of globs of packages\' names ("react-native", "@react-native/*")']
|
|
939
|
+
const RN_CONFIG_RESTRICTIONS = { allowedPaths: PATH_GLOBS, disallowedPaths: PATH_GLOBS, allowedModules: PACKAGE_GLOBS, disallowedModules: PACKAGE_GLOBS }
|
|
940
|
+
|
|
941
|
+
// Throws where it isn't a JSON object, as rn-bundle refuses to build.
|
|
942
|
+
function readRnConfig(baseDir, host) {
|
|
943
|
+
const text = readFileSyncMaybe(baseDir, RN_CONFIG_FILE, 'utf8', host)
|
|
944
|
+
if (text === null) return null
|
|
945
|
+
const config = parseJson(text, `${RN_CONFIG_FILE} is not valid JSON`)
|
|
946
|
+
if (!isPlainObject(config)) throw new Error(`${RN_CONFIG_FILE} is not an object of options`)
|
|
947
|
+
return config
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
function rnOptions(config, options, prefix = '') {
|
|
951
|
+
const taken = {}
|
|
952
|
+
for (const [key, [valid, what]] of Object.entries(options)) {
|
|
953
|
+
if (config[key] === undefined) continue
|
|
954
|
+
if (!valid(config[key])) throw new Error(`${RN_CONFIG_FILE}: ${prefix}${key} is ${what}, not ${JSON.stringify(config[key])}`)
|
|
955
|
+
taken[key] = config[key]
|
|
956
|
+
}
|
|
957
|
+
return taken
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
function rnRestrictions(config) {
|
|
961
|
+
const restrictions = rnOptions(config, RN_CONFIG_RESTRICTIONS)
|
|
962
|
+
for (const mode of ['development', 'production']) {
|
|
963
|
+
if (config[mode] === undefined) continue
|
|
964
|
+
if (!isPlainObject(config[mode])) throw new Error(`${RN_CONFIG_FILE}: ${mode} is an object of options, not ${JSON.stringify(config[mode])}`)
|
|
965
|
+
restrictions[mode] = rnOptions(config[mode], RN_CONFIG_RESTRICTIONS, `${mode}.`)
|
|
966
|
+
}
|
|
967
|
+
return restrictions
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
// --metro: Metro's resolver lists as rn-bundle takes them from rn.config.json and the Metro config it loads
|
|
971
|
+
// (unless rn.config.json has assetExts or metroConfig false), and the `configs` they came of, to carry.
|
|
972
|
+
function metroLists(baseDir, host, config) {
|
|
973
|
+
const rn = config === null ? {} : rnOptions(config, RN_CONFIG_LISTS)
|
|
974
|
+
const metroFile = rn.assetExts !== undefined || rn.metroConfig === false ? null : typeof rn.metroConfig === 'string' ? rn.metroConfig : METRO_CONFIG_FILE
|
|
975
|
+
const assetExts = metroFile === null ? new Set(addedTo(METRO_ASSET_EXTS, rn.assetExts ?? [])) : metroAssetExts(baseDir, host, metroFile)
|
|
976
|
+
const sourceExts = addedTo(METRO_SOURCE_EXTS, rn.sourceExts ?? [])
|
|
977
|
+
// Metro takes a file of an extension in both for an asset, and rn.config.json removes none.
|
|
978
|
+
const both = rn.sourceExts?.find((ext) => assetExts.has(ext)) ?? rn.assetExts?.find((ext) => sourceExts.includes(ext))
|
|
979
|
+
if (both !== undefined) throw new Error(`${RN_CONFIG_FILE}: ${both} is in both assetExts and sourceExts, which Metro takes a file of as an asset`)
|
|
980
|
+
return {
|
|
981
|
+
assetExts,
|
|
982
|
+
sourceExts: sourceExts.filter((ext) => isResolvableExt(`.${ext}`)),
|
|
983
|
+
assetResolutions: addedTo(METRO_ASSET_RESOLUTIONS, (rn.assetResolutions ?? []).map(String)),
|
|
984
|
+
configs: [join(baseDir, RN_CONFIG_FILE), ...(metroFile === null ? [] : [resolve(baseDir, metroFile)])],
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
function refuseRestricted(files, restrict, host) {
|
|
989
|
+
const packageOf = packagesOf({ isFile: (path) => host.stat(path)?.isFile() ?? false, readFile: (path) => host.readFile(path) })
|
|
990
|
+
const errors = [...new Set([...files].map((file) => host.realpath(file)).flatMap((file) => restrict(file, packageOf(file))))].toSorted()
|
|
991
|
+
if (errors.length > 0) throw new Error(`stasis bundle: the --metro build takes what ${RN_CONFIG_FILE} forbids:\n${listed(errors)}`)
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
const JSX_FILE_EXTS = new Set(['.jsx', '.tsx'])
|
|
995
|
+
// metro-config's default `asyncRequireModulePath`, which Metro adds to a build with an `import()`.
|
|
996
|
+
const ASYNC_REQUIRE = 'metro-runtime/src/modules/asyncRequire.js'
|
|
997
|
+
|
|
998
|
+
// A cache across a lazy tree's rounds: what `read()` gives, kept where it read no package not unpacked yet.
|
|
999
|
+
function keptAcross(label, host, memo) {
|
|
1000
|
+
const kept = memo ? nestedMap(memo, label) : null
|
|
1001
|
+
return (key, read) => {
|
|
1002
|
+
if (kept?.has(key)) return kept.get(key)
|
|
1003
|
+
const before = kept === null ? 0 : host.touches()
|
|
1004
|
+
const value = read()
|
|
1005
|
+
if (kept !== null && host.touches() === before) kept.set(key, value)
|
|
1006
|
+
return value
|
|
1007
|
+
}
|
|
940
1008
|
}
|
|
941
1009
|
|
|
942
|
-
//
|
|
943
|
-
//
|
|
944
|
-
|
|
945
|
-
|
|
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.
|
|
1012
|
+
const probing = (host, fn) => (host.probing === undefined ? fn() : host.probing(fn))
|
|
1013
|
+
|
|
1014
|
+
function metroInsertedFiles(files, { assets, baseDir, babelled, dynamicImports, jsx, resolver, platformResolver, conditions: conds, host, kept }) {
|
|
1015
|
+
const inserted = new Set()
|
|
1016
|
+
// A specifier resolves alike from every file of a directory.
|
|
1017
|
+
const onePerDir = (list) => [...new Map(list.map((file) => [dirname(file), file])).values()]
|
|
1018
|
+
const resolveFrom = (from, spec, by = resolver) => kept(`${by === resolver ? '' : 'platform\0'}${dirname(from)}\0${spec}`, () => {
|
|
1019
|
+
const url = by(from, spec, conds)?.url
|
|
1020
|
+
return url === undefined ? undefined : fileURLToPath(url)
|
|
1021
|
+
})
|
|
1022
|
+
if (babelled) {
|
|
1023
|
+
// Every helper of each @babel/runtime a file finds: which ones Babel adds hangs on the syntax.
|
|
1024
|
+
const helpersDirs = new Set()
|
|
1025
|
+
for (const from of onePerDir(files)) {
|
|
1026
|
+
const probe = resolveFrom(from, '@babel/runtime/helpers/interopRequireDefault')
|
|
1027
|
+
if (probe !== undefined) helpersDirs.add(dirname(probe))
|
|
1028
|
+
}
|
|
1029
|
+
for (const dir of helpersDirs) {
|
|
1030
|
+
for (const ent of host.readdir(dir)) if (ent.isFile() && ent.name.endsWith('.js')) inserted.add(join(dir, ent.name))
|
|
1031
|
+
}
|
|
1032
|
+
const metroRuntime = packageDirFrom(baseDir, 'metro-runtime', host) ?? packageDirFrom(baseDir, 'metro-runtime', host, ['metro'])
|
|
1033
|
+
const moduleSystem = metroRuntime === undefined ? undefined : join(metroRuntime, 'src/polyfills/require.js')
|
|
1034
|
+
if (moduleSystem !== undefined && host.stat(moduleSystem)?.isFile()) inserted.add(moduleSystem)
|
|
1035
|
+
const polyfills = packageDirFrom(baseDir, '@react-native/js-polyfills', host) ?? packageDirFrom(baseDir, '@react-native/js-polyfills', host, ['react-native'])
|
|
1036
|
+
if (polyfills !== undefined && host.stat(join(polyfills, 'index.js'))?.isFile()) {
|
|
1037
|
+
for (const [, file] of requireResolutions(join(polyfills, 'index.js'), host)) inserted.add(file)
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
// From the project, else through Metro, beside which a pnpm install links metro-runtime.
|
|
1041
|
+
if (dynamicImports) {
|
|
1042
|
+
const metroDir = packageDirFrom(baseDir, 'metro', host)
|
|
1043
|
+
const file = requireFrom(baseDir, ASYNC_REQUIRE, host) ?? (metroDir === undefined ? undefined : requireFrom(metroDir, ASYNC_REQUIRE, host))
|
|
1044
|
+
if (file !== undefined) inserted.add(file)
|
|
1045
|
+
}
|
|
1046
|
+
for (const from of onePerDir(jsx ? files : files.filter((file) => JSX_FILE_EXTS.has(extname(file))))) {
|
|
1047
|
+
const runtime = resolveFrom(from, 'react/jsx-runtime')
|
|
1048
|
+
if (runtime !== undefined) inserted.add(runtime)
|
|
1049
|
+
}
|
|
1050
|
+
// Metro's module of an asset requires it past Babel, so with no alias of its.
|
|
1051
|
+
for (const from of onePerDir(assets)) {
|
|
1052
|
+
const registry = resolveFrom(from, ASSET_REGISTRY, platformResolver)
|
|
1053
|
+
if (registry !== undefined) inserted.add(registry)
|
|
1054
|
+
}
|
|
1055
|
+
return inserted
|
|
1056
|
+
}
|
|
1057
|
+
|
|
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 }) {
|
|
946
1061
|
const baseDir = resolve(cwd)
|
|
947
1062
|
const absEntries = entries.map((e) => resolve(baseDir, e))
|
|
948
1063
|
const normalized = normalizeEntries(entries, cwd)
|
|
949
1064
|
|
|
950
|
-
// A resolved file arrives by its real path, an entry as spelled:
|
|
951
|
-
//
|
|
952
|
-
// is the root; a file below neither is outside it. (Realpathed only when needed, so a missing cwd
|
|
953
|
-
// 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.)
|
|
954
1067
|
let realRoot
|
|
955
|
-
const
|
|
1068
|
+
const fromRoot = memo ? nestedMap(memo, 'paths from the root') : new Map()
|
|
1069
|
+
const relWithin = (abs) => upsert(fromRoot, abs, () => {
|
|
956
1070
|
let rel = toPosix(relative(baseDir, abs))
|
|
957
1071
|
if (relativeEscapes(rel)) {
|
|
958
1072
|
realRoot ??= host.realpath(baseDir)
|
|
959
1073
|
rel = toPosix(relative(realRoot, abs))
|
|
960
1074
|
}
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
1075
|
+
return relativeEscapes(rel) ? null : rel
|
|
1076
|
+
})
|
|
1077
|
+
const pathsOf = memo ? nestedMap(memo, 'file paths') : new Map()
|
|
1078
|
+
const pathOf = (url) => upsert(pathsOf, url, () => fileURLToPath(url))
|
|
1079
|
+
const toRel = (abs) => {
|
|
1080
|
+
const rel = relWithin(abs)
|
|
1081
|
+
if (rel === null) throw new Error(`Bundle would reach a file outside the project root: ${abs}`)
|
|
964
1082
|
return rel
|
|
965
1083
|
}
|
|
966
1084
|
|
|
967
1085
|
const scanConditions = cleanConditions(conditions)
|
|
968
|
-
|
|
969
|
-
// Taken as given where the entries import TS sources by output name, but under --metro-resolver,
|
|
970
|
-
// which has no tsc mapping (classifyEntries rejects --typescript there).
|
|
1086
|
+
// Not under --metro-resolver, which has no tsc mapping.
|
|
971
1087
|
if (!metroResolver) typescript ||= importsTypescriptByOutputName(absEntries, host)
|
|
972
|
-
// --typescript's tsconfig `paths` matcher, shared by every per-platform resolver below.
|
|
973
1088
|
const typescriptPaths = typescriptPathsFor(typescript, baseDir, tsconfig, host, env)
|
|
974
|
-
|
|
975
|
-
const resourceSet = parseResourcesOption('buildResolvedJsBundle', resources)
|
|
1089
|
+
const { resources: resourceSet, packageJSON: withManifests } = fieldBuildOptions('buildResolvedJsBundle', { cwd, scope, resources, packageJSON, host })
|
|
976
1090
|
|
|
977
1091
|
const formatsByRel = new Map()
|
|
978
|
-
// Reached files the scanner tagged as resources (rel keys); read as bytes below, not UTF-8 source.
|
|
979
1092
|
const resourceRels = new Set()
|
|
980
1093
|
// parentRel -> specifier -> Map<platformKey, targetRel>; collapsed after all platforms scanned.
|
|
981
1094
|
const edges = new Map()
|
|
982
|
-
const reached = new Set()
|
|
1095
|
+
const reached = new Set()
|
|
983
1096
|
let usesEmpty = false
|
|
984
|
-
|
|
1097
|
+
// A disabled import resolves to Metro's own empty module where the project has it, else the reserved one.
|
|
1098
|
+
const metroEmpty = metro ? metroEmptyModule(baseDir, host) : null
|
|
1099
|
+
const emptyTarget = metroEmpty === null ? EMPTY_MODULE_PATH : toRel(metroEmpty)
|
|
1100
|
+
|
|
1101
|
+
const fileAliases = aliasesFile === undefined ? null : createAliases(readAliasesFile(baseDir, aliasesFile, host), baseDir)
|
|
1102
|
+
// Metro runs the build through Babel, which adds its helpers to each file.
|
|
1103
|
+
const babelled = babel || fileAliases !== null
|
|
1104
|
+
const leftOutEdges = []
|
|
1105
|
+
const rnConfig = metro ? readRnConfig(baseDir, host) : null
|
|
1106
|
+
const { assetExts, sourceExts, assetResolutions, configs: listConfigs } = metro ? metroLists(baseDir, host, rnConfig) : { assetExts: METRO_ASSET_EXTS, sourceExts: METRO_SOURCE_EXTS, assetResolutions: METRO_ASSET_RESOLUTIONS, configs: [] }
|
|
1107
|
+
// A --metro bundle builds as a release build does, so production's restrictions apply, not development's.
|
|
1108
|
+
const restrict = rnConfig === null ? null : restrictionsOf(rnRestrictions(rnConfig), host.realpath(baseDir), false)
|
|
1109
|
+
const metroTaken = new Set()
|
|
1110
|
+
// A resource that is no asset of Metro's reaches the build only as a transform's input, inlined by a Babel
|
|
1111
|
+
// plugin (`require('./messages.po')`): no edge to one, nor asset registry.
|
|
1112
|
+
const isTransformInput = (abs, info) => metro && info?.resource === true && !isAssetFile(abs, assetExts)
|
|
1113
|
+
// A round of a lazy tree that asked for a package already is void: it reads on, for all else it asks for,
|
|
1114
|
+
// but reports and refuses nothing, and stops before it reads the files in.
|
|
1115
|
+
const voided = () => Boolean(memo) && host.touches() > 0
|
|
1116
|
+
const scanWithin = (roots, options, label) => {
|
|
1117
|
+
const { scanner, outside, leftOut } = withinRoot(scan(roots, { ...options, memo: memo ? nestedMap(memo, label) : null }), (url) => relWithin(pathOf(url)) !== null)
|
|
1118
|
+
for (const url of outside) toRel(pathOf(url))
|
|
1119
|
+
if (!voided()) reportScanIssues({ ...analyzeScanner(scanner, { baseDir, generated }), leftOut }, { baseDir, label, root: ' at .' })
|
|
1120
|
+
return { scanner, leftOut }
|
|
1121
|
+
}
|
|
985
1122
|
for (const platform of platforms) {
|
|
986
|
-
const
|
|
1123
|
+
const keptFor = (label) => (memo ? nestedMap(memo, `${label} ${platform ?? '*'}`) : undefined)
|
|
1124
|
+
const field = fieldResolverFor(platform, { mainFields, metro, assetExts, sourceExts, assetResolutions, conditions: scanConditions, typescript, typescriptPaths, host, kept: keptFor('resolutions') })
|
|
987
1125
|
const { extras } = field
|
|
988
|
-
//
|
|
989
|
-
// fidelity; otherwise the built-in field/suffix resolver reproduces it. metro-resolver derives
|
|
990
|
-
// default/require|import/platform conditions itself, so it takes only the extra `react-native`
|
|
991
|
-
// 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.
|
|
992
1127
|
const resolver = metroResolver
|
|
993
|
-
? createMetroResolver({ projectDir: baseDir, platform, sourceExts
|
|
1128
|
+
? createMetroResolver({ projectDir: baseDir, platform, sourceExts, mainFields, conditionNames: ['react-native'], assetExts, assetResolutions, host })
|
|
994
1129
|
: field.resolver
|
|
995
|
-
// Without --typescript, the
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
const
|
|
1002
|
-
|
|
1003
|
-
const
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
const
|
|
1016
|
-
|
|
1017
|
-
|
|
1130
|
+
// Without --typescript, the file a miss would land on under it: a hint in the unresolved report, never an edge.
|
|
1131
|
+
const typescriptResolve = typescript || metroResolver ? null : fieldResolverFor(platform, { mainFields, metro, assetExts, sourceExts, assetResolutions, conditions: scanConditions, typescript: true, typescriptPaths, host, kept: keptFor('typescript resolutions') }).resolver
|
|
1132
|
+
// --babel: a specifier babel-plugin-module-resolver rewrites resolves as rewritten, as Metro resolves it
|
|
1133
|
+
// after Babel; its edge stays keyed by the specifier the source holds.
|
|
1134
|
+
const aliases = babel ? createBabelAliases({ projectDir: baseDir, platform }) : fileAliases
|
|
1135
|
+
const keepsTypeImports = babel ? createBabelTypeImportsKept({ projectDir: baseDir, platform }) : null
|
|
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
|
+
}
|
|
1141
|
+
// --metro: built through Babel, which erases an import listing types alone (Scan's `babel`).
|
|
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 ?? '*'
|
|
1145
|
+
const absorb = (roots) => {
|
|
1146
|
+
const { scanner, leftOut } = scanWithin(roots, scanOptions, platform ?? 'mainFields')
|
|
1147
|
+
leftOutEdges.push(...leftOut)
|
|
1148
|
+
for (const [url, info] of scanner.files) {
|
|
1149
|
+
const abs = pathOf(url)
|
|
1150
|
+
const rel = toRel(abs)
|
|
1151
|
+
reached.add(abs)
|
|
1152
|
+
if (restrict !== null && !isTransformInput(abs, info)) metroTaken.add(abs)
|
|
1153
|
+
if (info.resource) resourceRels.add(rel)
|
|
1154
|
+
// Under --metro a code file has only the format its name gives it, as the Metro plugin records it.
|
|
1155
|
+
formatsByRel.set(rel, metro && !info.resource ? nodeExtFormat(abs) : info.format)
|
|
1156
|
+
for (const e of info.edges) {
|
|
1157
|
+
if (e.builtin || e.dynamic) continue
|
|
1158
|
+
let target
|
|
1159
|
+
if (e.empty) { usesEmpty = true; target = emptyTarget }
|
|
1160
|
+
else if (e.child) target = toRel(pathOf(e.child))
|
|
1161
|
+
else continue // unresolved: reportScanIssues warned or threw
|
|
1162
|
+
// A transform's input is no dependency of Metro's: the import is gone before Metro sees it.
|
|
1163
|
+
if (e.child && isTransformInput(pathOf(e.child), scanner.files.get(e.child))) continue
|
|
1164
|
+
nestedMap(nestedMap(edges, rel), e.spec).set(platformKey, target)
|
|
1165
|
+
}
|
|
1018
1166
|
}
|
|
1167
|
+
return scanner
|
|
1168
|
+
}
|
|
1169
|
+
const scanner = staged('scan', () => absorb(absEntries))
|
|
1170
|
+
// --metro: and what Babel and Metro add, scanned in turn; one above the root passed over, as the Metro plugin does.
|
|
1171
|
+
if (metro) {
|
|
1172
|
+
const metroAt = performance.now()
|
|
1173
|
+
const code = [...scanner.files].filter(([, info]) => !info.resource).map(([url]) => pathOf(url))
|
|
1174
|
+
const assets = [...scanner.files].filter(([url, info]) => info.resource && !isTransformInput(pathOf(url), info)).map(([url]) => pathOf(url))
|
|
1175
|
+
// As a require(): Babel's CommonJS transform makes one of each it adds, as Metro writes its own.
|
|
1176
|
+
const required = new Set(['require', 'default', ...extras])
|
|
1177
|
+
// Each asset's other scales (`logo@2x.png`), which Metro reads to build its module: no import names one.
|
|
1178
|
+
const scales = assets.flatMap((file) => metroAssetFiles(file, platform, host)).filter((file) => relWithin(host.realpath(file)) !== null)
|
|
1179
|
+
const dynamicImports = [...scanner.files.values()].some((info) => !info.resource && info.edges.some((e) => e.kind === 'dynamic-import'))
|
|
1180
|
+
const kept = keptAcross(`Metro's additions ${platformKey}`, host, memo)
|
|
1181
|
+
const added = [...new Set([...metroInsertedFiles(code, { assets, baseDir, babelled, dynamicImports, jsx, resolver: aliased(resolver), platformResolver: resolver, conditions: required, host, kept }), ...scales])]
|
|
1182
|
+
.filter((file) => !scanner.files.has(pathToFileURL(file).toString()) && relWithin(file) !== null)
|
|
1183
|
+
stageTimes.metro += performance.now() - metroAt
|
|
1184
|
+
if (added.length > 0) staged('scan', () => absorb(added))
|
|
1019
1185
|
}
|
|
1020
1186
|
}
|
|
1021
1187
|
|
|
1022
|
-
//
|
|
1023
|
-
|
|
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.
|
|
1200
|
+
const keyedResolutions = new Map()
|
|
1201
|
+
const keyedFrom = (key, parent) => nestedMap(nestedMap(keyedResolutions, key), parent)
|
|
1202
|
+
// `${parentRel}\0${specifier}` -> the configs' miss.
|
|
1203
|
+
const configMissed = new Map()
|
|
1204
|
+
// What the configs reach, and of it what the app's build doesn't: attributed to `bundle`, not `bundle:metro`.
|
|
1205
|
+
const configReached = new Set()
|
|
1206
|
+
const configOnly = new Set()
|
|
1207
|
+
const configsAt = performance.now()
|
|
1208
|
+
if (metro) {
|
|
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())
|
|
1211
|
+
const toolchainFile = ({ name, file, via }) => {
|
|
1212
|
+
for (let i = 0; i <= via.length; i++) {
|
|
1213
|
+
const chain = via.slice(i)
|
|
1214
|
+
if (file !== undefined) {
|
|
1215
|
+
const dir = packageDirFrom(baseDir, name, host, chain)
|
|
1216
|
+
if (dir !== undefined && host.stat(join(dir, file))?.isFile()) return join(dir, file)
|
|
1217
|
+
continue
|
|
1218
|
+
}
|
|
1219
|
+
const from = chain.length === 0 ? baseDir : packageDirFrom(baseDir, chain.at(-1), host, chain.slice(0, -1))
|
|
1220
|
+
const found = from === undefined ? undefined : requireFrom(from, name, host)
|
|
1221
|
+
if (found !== undefined) return found
|
|
1222
|
+
}
|
|
1223
|
+
return undefined
|
|
1224
|
+
}
|
|
1225
|
+
for (const entry of [...METRO_TOOLCHAIN, ...(babelled ? BABEL_TOOLCHAIN : [])]) {
|
|
1226
|
+
const file = toolchainFile(entry)
|
|
1227
|
+
if (file !== undefined && relWithin(file) !== null) configs.push(file)
|
|
1228
|
+
}
|
|
1229
|
+
// And the React Native configs of the app's packages, which its CLI requires, by their real path.
|
|
1230
|
+
for (const dir of probing(host, () => linkedPackages(baseDir, host))) {
|
|
1231
|
+
const file = join(dir, 'react-native.config.js')
|
|
1232
|
+
if (!probing(host, () => host.stat(file)?.isFile())) continue
|
|
1233
|
+
const real = host.realpath(file)
|
|
1234
|
+
if (relWithin(real) !== null) configs.push(real)
|
|
1235
|
+
}
|
|
1236
|
+
const traceNode = (roots) => {
|
|
1237
|
+
const { scanner, leftOut } = scanWithin(roots, { resources: resourceSet, host }, 'configs and toolchain')
|
|
1238
|
+
for (const [url, info] of scanner.files) {
|
|
1239
|
+
const abs = pathOf(url)
|
|
1240
|
+
const rel = toRel(abs)
|
|
1241
|
+
configReached.add(rel)
|
|
1242
|
+
if (!reached.has(abs)) configOnly.add(rel)
|
|
1243
|
+
reached.add(abs)
|
|
1244
|
+
if (info.resource) resourceRels.add(rel)
|
|
1245
|
+
formatsByRel.set(rel, info.format)
|
|
1246
|
+
}
|
|
1247
|
+
for (const u of [...scanner.unresolved, ...leftOut]) if (u.spec !== undefined) configMissed.set(`${toRel(pathOf(u.parentURL))}\0${u.spec}`, u)
|
|
1248
|
+
for (const [key, byParent] of scanner.imports) {
|
|
1249
|
+
for (const [parentURL, specs] of byParent) {
|
|
1250
|
+
const keyed = keyedFrom(key, toRel(pathOf(parentURL)))
|
|
1251
|
+
for (const [spec, childURL] of specs) keyed.set(spec, toRel(pathOf(childURL)))
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
return scanner
|
|
1255
|
+
}
|
|
1256
|
+
const traced = configs.length > 0 ? traceNode(configs) : null
|
|
1257
|
+
// And the transformers beside stasis's Metro plugin, where a config requires it.
|
|
1258
|
+
const transformers = [...(traced?.files.keys() ?? [])].map((url) => pathOf(url))
|
|
1259
|
+
.filter((file) => basename(file) === 'metro.js' && basename(dirname(file)) === 'src' &&
|
|
1260
|
+
readJson(join(dirname(dirname(file)), 'package.json'), host)?.name === '@exodus/stasis-plugins')
|
|
1261
|
+
.flatMap((file) => STASIS_METRO_TRANSFORMERS.map((name) => join(dirname(file), name)))
|
|
1262
|
+
.filter((file) => host.stat(file)?.isFile() && relWithin(file) !== null && !traced.files.has(pathToFileURL(file).toString()))
|
|
1263
|
+
if (transformers.length > 0) traceNode(transformers)
|
|
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.
|
|
1266
|
+
const babelConfig = pathToFileURL(join(baseDir, BABEL_CONFIG_FILE)).toString()
|
|
1267
|
+
const resolutionsOf = keptAcross('require.resolve', host, memo)
|
|
1268
|
+
const namedOf = keptAcross('babel names', host, memo)
|
|
1269
|
+
if (traced?.files.has(babelConfig)) {
|
|
1270
|
+
const files = new Map(traced.files)
|
|
1271
|
+
const visited = new Set()
|
|
1272
|
+
const resolveNamed = (name, from) => {
|
|
1273
|
+
const pathLike = name.startsWith('.') || isAbsolute(name) || name.startsWith('node_modules/')
|
|
1274
|
+
for (const dir of [baseDir, dirname(from)]) {
|
|
1275
|
+
const file = requireFrom(dir, pathLike ? resolve(dir, name) : name, host)
|
|
1276
|
+
if (file !== undefined) return file
|
|
1277
|
+
}
|
|
1278
|
+
return undefined
|
|
1279
|
+
}
|
|
1280
|
+
for (let frontier = [babelConfig]; frontier.length > 0;) {
|
|
1281
|
+
const named = new Set()
|
|
1282
|
+
for (let i = 0; i < frontier.length; i++) {
|
|
1283
|
+
const url = frontier[i]
|
|
1284
|
+
const info = files.get(url)
|
|
1285
|
+
if (visited.has(url) || info === undefined) continue
|
|
1286
|
+
visited.add(url)
|
|
1287
|
+
for (const e of info.edges) if (e.child) frontier.push(e.child)
|
|
1288
|
+
const file = pathOf(url)
|
|
1289
|
+
const rel = toRel(file)
|
|
1290
|
+
if (info.resource || !JS_EXTENSIONS.has(extname(file))) continue
|
|
1291
|
+
for (const [spec, target] of resolutionsOf(file, () => requireResolutions(file, host))) {
|
|
1292
|
+
if (!JS_EXTENSIONS.has(extname(target)) || relWithin(target) === null) continue
|
|
1293
|
+
named.add(target)
|
|
1294
|
+
// A specifier keeps the first edge recorded, as a run records a resolution only where no edge is.
|
|
1295
|
+
const keyed = keyedFrom(REQUIRE_KEY, rel)
|
|
1296
|
+
if (!keyed.has(spec)) keyed.set(spec, toRel(target))
|
|
1297
|
+
}
|
|
1298
|
+
const configModule = isBabelConfigModule(rel)
|
|
1299
|
+
if (!configModule && splitNodeModulesPath(rel)) continue
|
|
1300
|
+
for (const name of namedOf(`${configModule}\0${file}`, () => babelNamedModules(file, host, { anyString: configModule }))) {
|
|
1301
|
+
const found = resolveNamed(name, file)
|
|
1302
|
+
if (found !== undefined && JS_EXTENSIONS.has(extname(found)) && relWithin(found) !== null) named.add(found)
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
const fresh = [...named].filter((file) => !files.has(pathToFileURL(file).toString()))
|
|
1306
|
+
if (fresh.length > 0) for (const [url, info] of traceNode(fresh).files) files.set(url, info)
|
|
1307
|
+
frontier = [...named].map((file) => pathToFileURL(file).toString())
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
// And the toolchain files the Metro plugin carries for every build, tagged as it does them.
|
|
1311
|
+
for (const specifier of AUTO_INCLUDES) {
|
|
1312
|
+
const file = requireFrom(baseDir, specifier, host)
|
|
1313
|
+
if (file === undefined || reached.has(file) || relWithin(file) === null) continue
|
|
1314
|
+
const rel = toRel(file)
|
|
1315
|
+
const format = classifyFormat(file, { content: host.readFile(file) })
|
|
1316
|
+
reached.add(file)
|
|
1317
|
+
configReached.add(rel)
|
|
1318
|
+
configOnly.add(rel)
|
|
1319
|
+
if (format === undefined) resourceRels.add(rel)
|
|
1320
|
+
else formatsByRel.set(rel, format ?? undefined)
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
stageTimes.configs += performance.now() - configsAt
|
|
1325
|
+
if (voided()) throw new Error('buildResolvedJsBundle: this round reads a package not unpacked yet')
|
|
1326
|
+
const carryingAt = performance.now()
|
|
1327
|
+
|
|
1328
|
+
// Each reached file carried as the bytes read, encoded without loss, for the lockfile to hash once wanted.
|
|
1024
1329
|
const sources = new Map()
|
|
1025
|
-
const
|
|
1026
|
-
// Carry `rel`: `content` in the bundle as `format`, the integrity of its bytes `buf` in the lockfile.
|
|
1027
|
-
const put = (rel, content, format, buf) => {
|
|
1330
|
+
const put = (rel, content, format) => {
|
|
1028
1331
|
sources.set(rel, content)
|
|
1029
1332
|
formatsByRel.set(rel, format)
|
|
1030
|
-
integrities.set(rel, sha512integrity(buf))
|
|
1031
1333
|
}
|
|
1032
|
-
|
|
1033
|
-
//
|
|
1034
|
-
const
|
|
1334
|
+
const putResource = (rel, buf) => (isUtf8(buf) ? put(rel, buf.toString('utf8'), 'resource') : put(rel, buf.toString('base64'), 'resource:base64'))
|
|
1335
|
+
// Code carries its source, UTF-8, which non-UTF-8 bytes would not survive: refused.
|
|
1336
|
+
const putCode = (rel, buf, format, what = 'JS bundle source') => {
|
|
1337
|
+
if (!isUtf8(buf)) throw new Error(`${what} is not valid UTF-8: ${rel}`)
|
|
1338
|
+
put(rel, buf.toString('utf8'), format)
|
|
1339
|
+
}
|
|
1035
1340
|
const realBase = host.realpath(baseDir)
|
|
1341
|
+
// Metro's empty module is carried as the file it is, a code file of Metro's (no format).
|
|
1342
|
+
if (usesEmpty && metroEmpty !== null) reached.add(metroEmpty)
|
|
1036
1343
|
for (const abs of reached) {
|
|
1037
1344
|
const rel = toRel(abs)
|
|
1038
|
-
// Security:
|
|
1039
|
-
//
|
|
1040
|
-
// in-tree-named symlink escaping the root would slip past toRel's textual check -- realpath and
|
|
1041
|
-
// 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.
|
|
1042
1347
|
assertRealPathWithinBase(realBase, baseDir, rel, host)
|
|
1043
1348
|
const buf = host.readFile(abs)
|
|
1044
|
-
if (resourceRels.has(rel))
|
|
1045
|
-
|
|
1046
|
-
} else {
|
|
1047
|
-
if (!isUtf8(buf)) throw new Error(`JS bundle source is not valid UTF-8: ${rel}`)
|
|
1048
|
-
// Its format is the scan's.
|
|
1049
|
-
put(rel, buf.toString('utf8'), formatsByRel.get(rel), buf)
|
|
1050
|
-
}
|
|
1349
|
+
if (resourceRels.has(rel)) putResource(rel, buf)
|
|
1350
|
+
else putCode(rel, buf, formatsByRel.get(rel))
|
|
1051
1351
|
}
|
|
1052
|
-
if (usesEmpty) {
|
|
1053
|
-
// Refuse to shadow a real reached file sitting at the reserved empty-module path.
|
|
1352
|
+
if (usesEmpty && metroEmpty === null) {
|
|
1054
1353
|
if (formatsByRel.has(EMPTY_MODULE_PATH)) {
|
|
1055
1354
|
throw new Error(`Bundle needs the reserved empty-module path ${EMPTY_MODULE_PATH}, but the project has a real file there`)
|
|
1056
1355
|
}
|
|
1057
|
-
put(EMPTY_MODULE_PATH, '', 'commonjs'
|
|
1356
|
+
put(EMPTY_MODULE_PATH, '', metro ? undefined : 'commonjs')
|
|
1058
1357
|
}
|
|
1059
1358
|
|
|
1060
|
-
// The package a bundled file is in (findPackageMetadata: its pkgDir and identity, null where no
|
|
1061
|
-
// manifest names one), memoized per directory so a package's many files don't each re-walk to it.
|
|
1062
1359
|
const packageOf = packageLookup(baseDir, { host })
|
|
1063
1360
|
|
|
1064
|
-
// --metro also carries
|
|
1065
|
-
// sources + podspecs), scoped to the packages in the bundle installed or linked into
|
|
1066
|
-
// node_modules. Native source is stored as code under a language tag; other assets as
|
|
1067
|
-
// 'resource'/'resource:base64'.
|
|
1361
|
+
// --metro also carries the native build inputs of each native package the app links, as the Metro plugin does.
|
|
1068
1362
|
if (metro) {
|
|
1069
|
-
const pkgDirs = new Map()
|
|
1070
|
-
//
|
|
1071
|
-
// not a linked native dependency, so it must not drag in its ios/android surface.
|
|
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.
|
|
1072
1365
|
const isCode = (rel) => !resourceRels.has(rel)
|
|
1073
1366
|
for (const abs of reached) {
|
|
1074
1367
|
const rel = toRel(abs)
|
|
1075
|
-
const nm = isCode(rel) && splitNodeModulesPath(rel)
|
|
1368
|
+
const nm = isCode(rel) && !configOnly.has(rel) && splitNodeModulesPath(rel)
|
|
1076
1369
|
if (nm) pkgDirs.set(nm.dir, nm.name)
|
|
1077
1370
|
}
|
|
1078
|
-
// A package linked into node_modules
|
|
1079
|
-
//
|
|
1080
|
-
|
|
1081
|
-
// name, into a node_modules its importer finds packages in (the directories the resolver looks
|
|
1082
|
-
// in). One linked nowhere there is the project's own source.
|
|
1083
|
-
const linksIn = new Map() // directory -> Set of the real package dirs linked into a node_modules it finds packages in
|
|
1084
|
-
const linkedFrom = (dir) => {
|
|
1085
|
-
let links = linksIn.get(dir)
|
|
1086
|
-
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) => {
|
|
1087
1374
|
const parent = dirname(dir)
|
|
1088
|
-
links = new Set(parent === dir ? [] : linkedFrom(parent))
|
|
1375
|
+
const links = new Set(parent === dir ? [] : linkedFrom(parent))
|
|
1089
1376
|
const link = (abs) => {
|
|
1090
1377
|
try {
|
|
1091
1378
|
const rel = toRel(host.realpath(abs))
|
|
1092
1379
|
if (!splitNodeModulesPath(rel)) links.add(rel)
|
|
1093
1380
|
} catch { /* a dangling link, or one out of the root, whose files the scan pass refused already */ }
|
|
1094
1381
|
}
|
|
1095
|
-
const listing = (abs) => {
|
|
1096
|
-
try {
|
|
1097
|
-
return host.readdir(abs)
|
|
1098
|
-
} catch {
|
|
1099
|
-
return []
|
|
1100
|
-
}
|
|
1101
|
-
}
|
|
1102
1382
|
// As locatePackage: a directory named node_modules holds no node_modules of its own to look in.
|
|
1103
1383
|
if (basename(dir) !== 'node_modules') {
|
|
1104
1384
|
const nodeModules = join(dir, 'node_modules')
|
|
1105
|
-
for (const ent of listing(nodeModules)) {
|
|
1385
|
+
for (const ent of listing(nodeModules, host)) {
|
|
1106
1386
|
const abs = join(nodeModules, ent.name)
|
|
1107
1387
|
if (ent.name.startsWith('@')) {
|
|
1108
|
-
// A scope
|
|
1109
|
-
|
|
1110
|
-
for (const scoped of listing(abs)) {
|
|
1111
|
-
if (ent.isSymbolicLink() || scoped.isSymbolicLink()) link(join(abs, scoped.name))
|
|
1112
|
-
}
|
|
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))
|
|
1113
1390
|
} else if (ent.isSymbolicLink()) {
|
|
1114
1391
|
link(abs)
|
|
1115
1392
|
}
|
|
1116
1393
|
}
|
|
1117
1394
|
}
|
|
1118
|
-
linksIn.set(dir, links)
|
|
1119
1395
|
return links
|
|
1120
|
-
}
|
|
1396
|
+
})
|
|
1121
1397
|
for (const [parent, bySpec] of edges) {
|
|
1122
1398
|
for (const byPlatform of bySpec.values()) {
|
|
1123
1399
|
for (const target of byPlatform.values()) {
|
|
1124
1400
|
if (!isCode(target) || splitNodeModulesPath(target)) continue
|
|
1125
1401
|
const links = linkedFrom(dirname(join(baseDir, parent)))
|
|
1126
|
-
// The linked root
|
|
1127
|
-
// 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).
|
|
1128
1403
|
let root = posix.dirname(target)
|
|
1129
1404
|
while (root !== '.' && !links.has(root)) root = posix.dirname(root)
|
|
1130
|
-
// The project's own source isn't a dependency, nor is an import within the package itself.
|
|
1131
1405
|
if (root === '.' || parent.startsWith(`${root}/`) || pkgDirs.has(root)) continue
|
|
1132
1406
|
// Named by its own manifest, whatever name it's linked under.
|
|
1133
1407
|
pkgDirs.set(root, readPackageJson(baseDir, `${root}/package.json`, { host })?.name)
|
|
1134
1408
|
}
|
|
1135
1409
|
}
|
|
1136
1410
|
}
|
|
1411
|
+
// And those React Native links whether JS imports them or not, but one above the root, as the plugin does.
|
|
1412
|
+
for (const dir of probing(host, () => linkedNativePackages(baseDir, host))) {
|
|
1413
|
+
const rel = relWithin(host.realpath(dir))
|
|
1414
|
+
if (rel !== null && !pkgDirs.has(rel)) pkgDirs.set(rel, readPackageJson(baseDir, `${rel}/package.json`, { host })?.name)
|
|
1415
|
+
}
|
|
1416
|
+
// A file read only once its real path is held to the root.
|
|
1417
|
+
const nativeFs = {
|
|
1418
|
+
readdir: (abs) => host.readdir(abs),
|
|
1419
|
+
stat: (abs) => host.stat(abs),
|
|
1420
|
+
readFile(abs) {
|
|
1421
|
+
assertRealPathWithinBase(realBase, baseDir, toRel(abs), host)
|
|
1422
|
+
return host.readFile(abs)
|
|
1423
|
+
},
|
|
1424
|
+
}
|
|
1137
1425
|
for (const [pkgDir, name] of [...pkgDirs].toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
|
|
1138
1426
|
const pkgAbs = join(baseDir, pkgDir)
|
|
1139
|
-
// react-native core isn't a Pod (config reports it via reactNativePath,
|
|
1140
|
-
// so walk its whole tree for native source (React/, ReactCommon/, ReactAndroid/, ...) the same
|
|
1141
|
-
// way a native dep's ios/android surface is walked; every other dep gets its ios/android + podspecs.
|
|
1427
|
+
// react-native core isn't a Pod (config reports it via reactNativePath), but its native source is walked too.
|
|
1142
1428
|
const isRnCore = name === 'react-native'
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
if (sources.has(rel)) continue
|
|
1148
|
-
assertRealPathWithinBase(realBase, baseDir, rel, host)
|
|
1149
|
-
// classifyNativeCapture (shared with the StasisMetro plugin) returns action skip/code/resource with a format tag.
|
|
1150
|
-
const byName = classifyNativeCapture(rel)
|
|
1151
|
-
if (byName.action === 'skip') continue
|
|
1152
|
-
const buf = host.readFile(abs)
|
|
1153
|
-
// Byte-level rules (prebuilt binaries, binary plists) -- see refineNativeCapture.
|
|
1154
|
-
const { action, format } = refineNativeCapture(byName, rel, buf, resourceSet)
|
|
1155
|
-
if (action === 'skip') continue
|
|
1156
|
-
if (action === 'code') {
|
|
1157
|
-
if (!isUtf8(buf)) throw new Error(`native source is not valid UTF-8: ${rel}`)
|
|
1158
|
-
put(rel, buf.toString('utf8'), format, buf)
|
|
1159
|
-
} else {
|
|
1160
|
-
putResource(rel, buf)
|
|
1161
|
-
}
|
|
1429
|
+
if (!isRnCore && !probing(host, () => looksNative(pkgAbs, nativeFs))) continue
|
|
1430
|
+
for (const { file, source, action, format } of nativeInputs(pkgAbs, nativeFs, { resources: resourceSet, skip: (abs) => sources.has(toRel(abs)) })) {
|
|
1431
|
+
if (action === 'code') putCode(toRel(file), source, format, 'native source')
|
|
1432
|
+
else putResource(toRel(file), source)
|
|
1162
1433
|
}
|
|
1163
|
-
// RN core's `.js` build scripts
|
|
1164
|
-
// 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.
|
|
1165
1435
|
if (isRnCore) {
|
|
1166
1436
|
for (const file of RN_CORE_INCLUDE_FILES) {
|
|
1167
1437
|
const abs = join(pkgAbs, file)
|
|
1168
|
-
if (host.stat(abs) === null) continue
|
|
1169
|
-
|
|
1170
|
-
if (sources.has(rel)) continue
|
|
1171
|
-
assertRealPathWithinBase(realBase, baseDir, rel, host)
|
|
1172
|
-
const buf = host.readFile(abs)
|
|
1173
|
-
if (!isUtf8(buf)) throw new Error(`native source is not valid UTF-8: ${rel}`)
|
|
1174
|
-
put(rel, buf.toString('utf8'), packageType(abs, host) === 'module' ? 'module' : 'commonjs', buf)
|
|
1438
|
+
if (host.stat(abs) === null || sources.has(toRel(abs))) continue
|
|
1439
|
+
putCode(toRel(abs), nativeFs.readFile(abs), packageType(abs, host) === 'module' ? 'module' : 'commonjs', 'native source')
|
|
1175
1440
|
}
|
|
1176
1441
|
}
|
|
1177
1442
|
}
|
|
1178
1443
|
}
|
|
1179
1444
|
|
|
1180
|
-
// --package-json:
|
|
1181
|
-
//
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
if (packageJSON) {
|
|
1186
|
-
const pkgDirs = new Set()
|
|
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.
|
|
1447
|
+
if (withManifests) {
|
|
1448
|
+
const exists = memoized((rel) => packageJSONStat(host, join(baseDir, rel))?.isFile() ?? false)
|
|
1449
|
+
const manifests = new Map()
|
|
1187
1450
|
for (const abs of reached) {
|
|
1188
1451
|
const rel = toRel(abs)
|
|
1189
|
-
const
|
|
1190
|
-
if (
|
|
1191
|
-
|
|
1452
|
+
const pkgDir = packageOf(rel)?.pkgDir ?? (splitNodeModulesPath(rel) ? undefined : '.')
|
|
1453
|
+
if (pkgDir === undefined) continue
|
|
1454
|
+
for (const manifest of [moduleFileKey(pkgDir, 'package.json'), scopeManifestOf(rel, pkgDir, exists)]) {
|
|
1455
|
+
if (manifest !== undefined) upsert(manifests, manifest, () => []).push(rel)
|
|
1456
|
+
}
|
|
1192
1457
|
}
|
|
1193
|
-
for (const
|
|
1194
|
-
const rel = moduleFileKey(pkgDir, 'package.json')
|
|
1458
|
+
for (const [rel, of] of manifests) {
|
|
1195
1459
|
if (sources.has(rel)) continue
|
|
1196
1460
|
const buf = readModuleManifest({ baseDir, realBase, rel, host })
|
|
1197
1461
|
if (!buf) continue
|
|
1198
|
-
put(rel, buf.toString('utf8'), 'json'
|
|
1462
|
+
put(rel, buf.toString('utf8'), 'json')
|
|
1463
|
+
// Attributed as the files it serves are, as State#includePackageJson attributes one.
|
|
1464
|
+
if (of.some((file) => configReached.has(file))) configReached.add(rel)
|
|
1465
|
+
if (of.every((file) => configOnly.has(file))) configOnly.add(rel)
|
|
1199
1466
|
}
|
|
1200
1467
|
}
|
|
1201
1468
|
|
|
1202
|
-
//
|
|
1203
|
-
const
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
}
|
|
1213
|
-
|
|
1214
|
-
|
|
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)))
|
|
1473
|
+
}
|
|
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.
|
|
1476
|
+
for (const [key, { parentURL, spec }] of configMissed) {
|
|
1477
|
+
if (!resolutions.get(key.slice(0, key.indexOf('\0')))?.has(spec)) continue
|
|
1478
|
+
throw new Error(
|
|
1479
|
+
`${spec} from ${displayPath(parentURL, baseDir)} resolves under Metro, for the app, and not under Node, for the ` +
|
|
1480
|
+
"project's configs: a bundle can't keep Node's miss without serving Node Metro's file in its place"
|
|
1481
|
+
)
|
|
1215
1482
|
}
|
|
1216
1483
|
|
|
1217
1484
|
const rootPkg = readJson(join(baseDir, 'package.json'), host) ?? {}
|
|
1218
|
-
const
|
|
1219
|
-
baseDir,
|
|
1220
|
-
|
|
1221
|
-
sources,
|
|
1222
|
-
resolutions,
|
|
1223
|
-
formats: formatsByRel,
|
|
1224
|
-
workspaceName: rootPkg.name ?? 'workspace',
|
|
1225
|
-
workspaceVersion: rootPkg.version ?? '0.0.0',
|
|
1226
|
-
conditionKey: '*',
|
|
1227
|
-
host,
|
|
1228
|
-
packageOf,
|
|
1485
|
+
const assembled = assembleCodeBundle({
|
|
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',
|
|
1488
|
+
consumerFiles: [...sources.keys()].filter((rel) => !configOnly.has(rel)),
|
|
1229
1489
|
})
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
const files = Object.create(null)
|
|
1235
|
-
for (const rel of Object.keys(m.files)) files[rel] = integrities.get(moduleFileKey(dir, rel))
|
|
1236
|
-
lockModules.set(dir, moduleInfo({ ...m, files }))
|
|
1237
|
-
}
|
|
1238
|
-
const lockfile = new Lockfile({
|
|
1239
|
-
config: bundle.config,
|
|
1240
|
-
entries: bundle.entries,
|
|
1241
|
-
modules: lockModules,
|
|
1242
|
-
imports: bundle.imports,
|
|
1243
|
-
formats: bundle.formats,
|
|
1244
|
-
// Same file set as the bundle, so the same executable list attests it.
|
|
1245
|
-
executable: bundle.executable,
|
|
1246
|
-
})
|
|
1247
|
-
|
|
1248
|
-
return { bundle, lockfile }
|
|
1490
|
+
// What the configs reach is the bundler's, not the app's: `bundle`, as a Node build's files are.
|
|
1491
|
+
const bundle = configReached.size === 0 ? assembled : assembled.withReason('bundle', [...configReached])
|
|
1492
|
+
stageTimes.carrying += performance.now() - carryingAt
|
|
1493
|
+
return { bundle, lockfile: deferredLockfile(bundle) }
|
|
1249
1494
|
}
|
|
1250
1495
|
|
|
1251
|
-
//
|
|
1252
|
-
//
|
|
1253
|
-
function classifyEntries(name, { cwd = process.cwd(), entries, mappingFile, manifests, scope, lockfile, conditions, mainFields, platforms, metro, metroResolver, jsx, flow, typescript, tsconfig, resources, packageJSON, cargo, cargoFeatures, cargoNoDefaultFeatures, cargoAllFeatures, cargoTarget, cargoManifests, host = diskHost, fetched }) {
|
|
1254
|
-
if (!
|
|
1255
|
-
throw new Error(`${name}: at least one entry file is required`)
|
|
1256
|
-
}
|
|
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.
|
|
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 }) {
|
|
1499
|
+
if (!nonEmpty(entries)) throw new Error(`${name}: at least one entry file is required`)
|
|
1257
1500
|
const dirError = directoryEntryError(entries, cwd, host, { fetched })
|
|
1258
1501
|
if (dirError !== null) throw new Error(`${name}: ${dirError}`)
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
}
|
|
1275
|
-
|
|
1276
|
-
//
|
|
1277
|
-
if (
|
|
1278
|
-
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 }
|
|
1279
|
-
for (const [flag, on] of Object.entries(given)) if (on) throw new Error(`${name}: --${flag} is only valid for Rust bundles`)
|
|
1280
|
-
}
|
|
1281
|
-
if (scope !== undefined && kind !== 'js') {
|
|
1282
|
-
throw new Error(`${name}: --scope is only valid for JS bundles`)
|
|
1283
|
-
}
|
|
1284
|
-
if (lockfile !== undefined && kind !== 'js') {
|
|
1285
|
-
throw new Error(`${name}: --lockfile is only valid for JS bundles`)
|
|
1286
|
-
}
|
|
1287
|
-
// Reject --conditions for non-JS: their resolvers don't consult exports/imports conditions, so it would silently do nothing.
|
|
1288
|
-
if (Array.isArray(conditions) && conditions.length > 0 && kind !== 'js') {
|
|
1289
|
-
throw new Error(`${name}: --conditions is only valid for JS bundles`)
|
|
1290
|
-
}
|
|
1291
|
-
// --flow strips Flow type syntax before the JS parser; the non-JS loaders never parse with oxc.
|
|
1292
|
-
if (flow && kind !== 'js') {
|
|
1293
|
-
throw new Error(`${name}: --flow is only valid for JS bundles`)
|
|
1294
|
-
}
|
|
1295
|
-
// --typescript maps JS-output specifiers to their on-disk TS sources; only the JS resolvers do it.
|
|
1296
|
-
if (typescript && kind !== 'js') {
|
|
1297
|
-
throw new Error(`${name}: --typescript is only valid for JS bundles`)
|
|
1298
|
-
}
|
|
1299
|
-
// --tsconfig names the config whose `paths` aliases --typescript honours; meaningless without it.
|
|
1300
|
-
if (tsconfig !== undefined && !typescript) {
|
|
1301
|
-
throw new Error(`${name}: --tsconfig is only valid with --typescript`)
|
|
1302
|
-
}
|
|
1303
|
-
// --resources carries allowlisted assets reached through the JS import graph; JS-only. The list
|
|
1304
|
-
// is validated (parseResourcesOption) in the builders, but reject a code extension here too so a
|
|
1305
|
-
// typo surfaces with the command name rather than deep in the scan.
|
|
1306
|
-
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)) {
|
|
1307
1521
|
if (kind !== 'js') throw new Error(`${name}: --resources is only valid for JS bundles`)
|
|
1308
1522
|
parseResourcesOption(name, resources)
|
|
1309
1523
|
}
|
|
1310
|
-
//
|
|
1311
|
-
|
|
1312
|
-
throw new Error(`${name}: --mainFields is only valid for JS bundles`)
|
|
1313
|
-
}
|
|
1314
|
-
// --jsx toggles the scanner's JSX parsing for the .js family (.jsx/.tsx need no flag); only JS
|
|
1315
|
-
// entries are scanned.
|
|
1316
|
-
if (jsx && kind !== 'js') {
|
|
1317
|
-
throw new Error(`${name}: --jsx is only valid for JS bundles`)
|
|
1318
|
-
}
|
|
1319
|
-
// --package-json folds each bundled module's npm package.json in; JS-only (the sol/php/bash/rust
|
|
1320
|
-
// bucketizers group by their own ecosystems' manifests, not an npm package.json).
|
|
1321
|
-
if (packageJSON && kind !== 'js') {
|
|
1322
|
-
throw new Error(`${name}: --package-json is only valid for JS bundles`)
|
|
1323
|
-
}
|
|
1324
|
-
if ((metro || (Array.isArray(platforms) && platforms.length > 0)) && kind !== 'js') {
|
|
1325
|
-
throw new Error(`${name}: --metro is only valid for JS bundles`)
|
|
1326
|
-
}
|
|
1327
|
-
// --metro presets conditions + mainFields + platform suffixes, so it can't combine with
|
|
1328
|
-
// 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) })
|
|
1329
1526
|
if (metro) {
|
|
1330
|
-
if (
|
|
1331
|
-
|
|
1332
|
-
}
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
}
|
|
1336
|
-
if (!Array.isArray(platforms) || platforms.length === 0) {
|
|
1337
|
-
throw new Error(`${name}: --metro requires --platforms (e.g. --platforms=ios,android)`)
|
|
1338
|
-
}
|
|
1339
|
-
// A platform name becomes an edge key: reject '/' (Bundle/Lockfile parse refuse it) and
|
|
1340
|
-
// the reserved '*' placeholder, so the writer never emits a map the readers reject.
|
|
1341
|
-
for (const p of platforms) {
|
|
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.
|
|
1531
|
+
for (const p of Array.isArray(platforms) ? platforms : []) {
|
|
1342
1532
|
if (typeof p !== 'string' || p.length === 0 || p === '*' || p.includes('/')) {
|
|
1343
1533
|
throw new Error(`${name}: invalid platform '${p}' (a platform name can't be empty, contain '/', or be '*')`)
|
|
1344
1534
|
}
|
|
1345
1535
|
}
|
|
1346
|
-
} else if (
|
|
1536
|
+
} else if (nonEmpty(platforms) && !partial) {
|
|
1347
1537
|
throw new Error(`${name}: --platforms is only valid with --metro`)
|
|
1348
1538
|
}
|
|
1349
|
-
|
|
1350
|
-
if (
|
|
1351
|
-
|
|
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`)
|
|
1541
|
+
if (babelAliases !== undefined) {
|
|
1542
|
+
if (typeof babelAliases !== 'string' || babelAliases === '') throw new TypeError(`${name}: babelAliases must be a path to a JSON file`)
|
|
1543
|
+
if (!metro && !partial) throw new Error(`${name}: --babel-aliases is only valid with --metro`)
|
|
1544
|
+
if (babel) throw new Error(`${name}: --babel-aliases can't be combined with --babel (both set the import aliases)`)
|
|
1352
1545
|
}
|
|
1353
|
-
// metro-resolver has no TS extension substitution, so --typescript would silently not apply -- reject it.
|
|
1354
1546
|
if (typescript && metroResolver) {
|
|
1355
1547
|
throw new Error(`${name}: --typescript is not supported with --metro-resolver (the project's metro-resolver doesn't substitute .js -> .ts)`)
|
|
1356
1548
|
}
|
|
1357
|
-
//
|
|
1358
|
-
if (
|
|
1359
|
-
throw new Error(`${name}: --scope is not supported with --mainFields or --metro`)
|
|
1360
|
-
}
|
|
1549
|
+
// Refused before anything is built (or fetched).
|
|
1550
|
+
if (mainFields !== undefined || metro) fieldBuildOptions(name, { cwd, scope, resources, host })
|
|
1361
1551
|
return kind
|
|
1362
1552
|
}
|
|
1363
1553
|
|
|
1364
|
-
//
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
// relative to: cwd for the resolver's, the State's root for the State's.
|
|
1368
|
-
// -> { bundle, lockfile: () => Lockfile, stateBuilt, root }
|
|
1369
|
-
async function buildJs({ mainFields, platforms, metro, metroResolver, innermostRoot, ...options }) {
|
|
1554
|
+
// By the legacy-field resolver with `mainFields` or `metro`, whose per-platform edges a State can't hold.
|
|
1555
|
+
async function buildJs({ mainFields, platforms, metro, metroResolver, babel, babelAliases, innermostRoot, extra, outputsAt, ...options }) {
|
|
1556
|
+
if (metro) options = { ...options, jsx: true, flow: true, define: { ...METRO_RELEASE_DEFINE, ...options.define } }
|
|
1370
1557
|
if (metro || mainFields !== undefined) {
|
|
1371
|
-
const built = await buildResolvedJsBundle({ ...options, mainFields: metro ? METRO_MAIN_FIELDS : mainFields, platforms: metro ? platforms : [null], metro: Boolean(metro), metroResolver: Boolean(metroResolver) })
|
|
1372
|
-
return { bundle: built.bundle, lockfile:
|
|
1373
|
-
}
|
|
1374
|
-
const state = await buildJsBundle({ ...options, innermostRoot })
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
}
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
//
|
|
1381
|
-
//
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
const built = await buildJs({ cwd, env, entries, scope, conditions, mainFields, platforms, metro, metroResolver, jsx, flow, typescript, tsconfig, resources, packageJSON })
|
|
1388
|
-
pinInstalledCommits(built.bundle, built.root)
|
|
1389
|
-
return built
|
|
1558
|
+
const built = await buildResolvedJsBundle({ ...options, mainFields: metro ? METRO_MAIN_FIELDS : mainFields, platforms: metro ? platforms : [null], metro: Boolean(metro), metroResolver: Boolean(metroResolver), babel: Boolean(babel), babelAliases })
|
|
1559
|
+
return { bundle: built.bundle, lockfile: built.lockfile, stateBuilt: false, root: resolve(options.cwd) }
|
|
1560
|
+
}
|
|
1561
|
+
const state = await buildJsBundle({ ...options, innermostRoot, extra, outputsAt })
|
|
1562
|
+
const bundle = state.sourceBundle
|
|
1563
|
+
// The `bundle` consumer, unless `extra` added files: then the State attributes each, to `bundle` or `extra`.
|
|
1564
|
+
return { bundle: bundle.reason === undefined ? bundle.withReason('bundle') : bundle, lockfile: () => state.lockfile, stateBuilt: true, root: state.root }
|
|
1565
|
+
}
|
|
1566
|
+
|
|
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))))
|
|
1390
1574
|
}
|
|
1391
1575
|
|
|
1392
|
-
//
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1576
|
+
// `built` (without a State) with stasis.config.json's `extra` files but `outputs`, packed as `stasis add` packs them.
|
|
1577
|
+
function withExtra(built, cwd, { outputs = [], resources, host = diskHost, name = 'bundle' }) {
|
|
1578
|
+
const config = readConfigFile(name, cwd, host)
|
|
1579
|
+
const patterns = parseExtraOption(CONFIG_FILE, config?.extra)
|
|
1580
|
+
if (patterns.length === 0) return built
|
|
1581
|
+
// The union of `builds` may be State-built in node_modules scope, which the full-scope files can't join.
|
|
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`)
|
|
1583
|
+
const baseDir = resolve(cwd)
|
|
1584
|
+
const written = new Set(outputs.map((file) => toPosix(relative(baseDir, file))))
|
|
1585
|
+
const allowlist = resources === undefined ? parseResourcesOption(CONFIG_FILE, config.resources) : parseResourcesOption('--resources', resources)
|
|
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)
|
|
1605
|
+
const { lockfile } = built
|
|
1606
|
+
return { ...built, bundle, ...(lockfile && extraLock && { lockfile: () => lockfile().merge(extraLock()) }) }
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
// From disk, with stasis.config.json's `extra` unless `extra` is false (one of several builds), not yet pinned.
|
|
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 }) {
|
|
1611
|
+
const outputs = outputsAt(resolve(cwd))
|
|
1612
|
+
const withExtraOf = (built, buildResources) => (extra ? withExtra(built, cwd, { outputs, resources: buildResources }) : built)
|
|
1613
|
+
if (kind === 'sol') return withExtraOf({ bundle: await buildSolidityBundle({ cwd, env, entries, mappingFile, manifests }) })
|
|
1614
|
+
if (kind === 'php') return withExtraOf({ bundle: await buildPhpBundle({ cwd, entries }) })
|
|
1615
|
+
if (kind === 'bash') return withExtraOf({ bundle: await buildBashBundle({ cwd, entries }) })
|
|
1616
|
+
if (kind === 'teal') return withExtraOf({ bundle: await buildTealBundle({ cwd, entries }) })
|
|
1617
|
+
if (kind === 'rust') return withExtraOf({ bundle: await buildRustBundle({ cwd, entries, cargo, cargoFeatures, cargoNoDefaultFeatures, cargoAllFeatures, cargoTarget, cargoManifests }) })
|
|
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) }) }
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1640
|
+
function unionOfBuilds(name, builds, builts, roots) {
|
|
1641
|
+
const other = roots.findIndex((root) => root !== roots[0])
|
|
1642
|
+
if (other !== -1) {
|
|
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]})`)
|
|
1644
|
+
}
|
|
1645
|
+
return builts.reduce((a, b) => ({ bundle: a.bundle.merge(b.bundle), lockfile: a.lockfile && b.lockfile && (() => a.lockfile().merge(b.lockfile())) }))
|
|
1646
|
+
}
|
|
1647
|
+
|
|
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
|
|
1396
1673
|
export function checkVfsOptions(name, options) {
|
|
1397
1674
|
const kind = classifyEntries(name, options)
|
|
1398
1675
|
if (options.metroResolver) throw new Error(`${name}: metroResolver is not supported`)
|
|
1399
|
-
|
|
1400
|
-
if (
|
|
1401
|
-
|
|
1676
|
+
// The project's Babel config is code, run from the disk.
|
|
1677
|
+
if (options.babel) throw new Error(`${name}: babel is not supported`)
|
|
1678
|
+
const { generate } = options
|
|
1679
|
+
if (generate !== undefined && (!Array.isArray(generate) || generate.some((step) => step !== 'prisma'))) throw new TypeError(`${name}: generate must be an array of 'prisma'`)
|
|
1680
|
+
if (generate?.length > 0 && kind !== 'js') throw new Error(`${name}: --generate is only valid for JS bundles`)
|
|
1402
1681
|
return kind
|
|
1403
1682
|
}
|
|
1404
1683
|
|
|
1405
|
-
//
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
//
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
const
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1684
|
+
// Nothing read from disk but tarballs and zips, nor from the environment (doc/github-bundle.md).
|
|
1685
|
+
export async function buildVfsBundle({ lock, ...options } = {}) {
|
|
1686
|
+
const { checkVfs, vfsHost } = await import('../vfs-bundle/tree.js')
|
|
1687
|
+
checkVfs('buildVfsBundle', options.vfs)
|
|
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')
|
|
1741
|
+
const { configuredBuilds } = await import('./bundle-options.js')
|
|
1742
|
+
const { checkGlob } = await import('../vfs-bundle/glob.js')
|
|
1743
|
+
checkTarget(name, { os, cpu, libc })
|
|
1744
|
+
checkCache(name, cache)
|
|
1745
|
+
if (lazy !== undefined && typeof lazy !== 'boolean') throw new TypeError(`${name}: lazy must be a boolean`)
|
|
1428
1746
|
// Checked as the Bundle checks it, before anything is fetched.
|
|
1429
1747
|
if (repo !== undefined) repo = new Bundle({ repo }).repo
|
|
1430
|
-
const project = vfsHost(vfs)
|
|
1431
1748
|
// A real path, as every file the scan reaches is.
|
|
1432
1749
|
cwd = project.realpath(posix.resolve('/', cwd))
|
|
1433
|
-
|
|
1434
|
-
const
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
//
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
const
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
const
|
|
1451
|
-
const
|
|
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.
|
|
1755
|
+
const configured = plan === undefined ? configuredBuilds(name, { cwd, host: project, entries: options.entries, options }) : null
|
|
1756
|
+
const { builds, bundleExtra } = plan ?? { builds: configured?.builds ?? [options], bundleExtra: configured !== null }
|
|
1757
|
+
if (builds.length === 0) throw new Error(`${name}: no entries given, and ${CONFIG_FILE}'s entries name none`)
|
|
1758
|
+
// That the Vfs can be globbed for `extra` is checked before anything is fetched.
|
|
1759
|
+
if (parseExtraOption(CONFIG_FILE, readConfigFile(name, cwd, project)?.extra).some((pattern) => !pattern.startsWith('!'))) checkGlob()
|
|
1760
|
+
const installDevDependencies = installsDevDependencies(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)
|
|
1765
|
+
if (generate?.length > 0 && !generatePrisma) throw new Error(`${name}: generate is given, and ${CONFIG_FILE}'s generatePrisma is false`)
|
|
1766
|
+
const vfsJs = vfs !== undefined && pms.some((pm) => pm.kind === 'js')
|
|
1767
|
+
const prismaGeneration = generate?.includes('prisma') ? 'asked' : !vfsJs ? null : generate === undefined && generatePrisma ? 'detected' : 'off'
|
|
1768
|
+
const kinds = builds.map((build) => {
|
|
1769
|
+
const kind = checkVfsOptions(name, { ...build, lockfile: options.lockfile, generate, cwd, host: project })
|
|
1770
|
+
checkKind(name, kind, packageManagers)
|
|
1771
|
+
return kind
|
|
1772
|
+
})
|
|
1773
|
+
// An entry out of what the trees install is in the project already: checked before anything is fetched. (A
|
|
1774
|
+
// Solidity entry that is no .sol file is a directory, skipped where it is missing.)
|
|
1775
|
+
const installs = new Set(pms.map((pm) => pm.installs))
|
|
1776
|
+
for (const [i, build] of builds.entries()) {
|
|
1777
|
+
for (const entry of build.entries) {
|
|
1778
|
+
const abs = posix.resolve(cwd, entry)
|
|
1779
|
+
if (kinds[i] === 'sol' && !abs.endsWith('.sol')) continue
|
|
1780
|
+
if (!posix.relative(cwd, abs).split('/').some((part) => installs.has(part)) && project.stat(abs) === null) throw new Error(`entry not found: ${abs}`)
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
// A Solidity build is built from cwd, but where a Soldeer project the trees lay out is below it: then each .sol
|
|
1784
|
+
// file is built from the innermost project holding it, with its foundry.toml and remappings.
|
|
1785
|
+
const solRoots = specs.filter((spec, i) => pms[i].kind === 'sol' && spec.cwd !== cwd).map((spec) => spec.cwd)
|
|
1786
|
+
const projectOf = (file) => solRoots.filter((dir) => isPathWithin(dir, posix.resolve(cwd, file))).reduce((inner, dir) => (isPathWithin(inner, dir) ? dir : inner), cwd)
|
|
1787
|
+
const solidityBuilt = async (build, args) => {
|
|
1788
|
+
if (solRoots.length === 0) return [{ bundle: await buildSolidityBundle({ ...build, ...args }), root: cwd }]
|
|
1789
|
+
const out = []
|
|
1790
|
+
for (const [from, entries] of Map.groupBy(expandSolidityEntries(cwd, normalizeEntries(build.entries, cwd), args.host), projectOf)) {
|
|
1791
|
+
// eslint-disable-next-line no-await-in-loop -- one project at a time, in the order its warnings read
|
|
1792
|
+
out.push({ bundle: await solidityFrom({ ...build, entries }, from, args), root: cwd })
|
|
1793
|
+
}
|
|
1794
|
+
return out
|
|
1795
|
+
}
|
|
1796
|
+
const laidOut = specs.length > 1 ? 'Laid out the trees' : specs[0].lazy ? 'Laid out the tree' : 'Installed the tree'
|
|
1797
|
+
const tree = await debugTimed(laidOut, () => layOutTrees({ project, specs, os, cpu, libc, client, cache, installDevDependencies, refuseInstalled }))
|
|
1798
|
+
const { inRounds, warningsHeld } = await import('../vfs-bundle/lazy.js')
|
|
1799
|
+
// The Prisma Clients generated once before any build, so that each of a lazy tree's rounds reads the same project.
|
|
1800
|
+
let outside = project
|
|
1801
|
+
let expected = []
|
|
1802
|
+
const prisma = prismaGeneration === null ? null : await import('../vfs-bundle/prisma.js')
|
|
1803
|
+
if (prisma !== null) {
|
|
1804
|
+
// Before any `prisma` is read. Nothing done, nor said, where no project has a schema.
|
|
1805
|
+
const withSchema = prisma.schemaProjects(project, tree.root, tree.projects)
|
|
1806
|
+
const strict = prismaGeneration === 'asked'
|
|
1807
|
+
// By its real path, as the scan reaches it, where the project holds it.
|
|
1808
|
+
const entryPath = (entry) => {
|
|
1809
|
+
const path = posix.resolve(cwd, entry)
|
|
1810
|
+
try {
|
|
1811
|
+
return project.realpath(path)
|
|
1812
|
+
} catch {
|
|
1813
|
+
return path
|
|
1814
|
+
}
|
|
1815
|
+
}
|
|
1816
|
+
const reached = () => prisma.reachedProjects(project, tree.root, tree.projects, builds.flatMap((build) => build.entries.map(entryPath)))
|
|
1817
|
+
const projects = strict || withSchema.size === 0 ? withSchema : withSchema.intersection(reached())
|
|
1818
|
+
if (projects.size > 0) {
|
|
1819
|
+
const off = prismaGeneration === 'off' ? `generation is off: ${generate === undefined ? `${CONFIG_FILE}'s generatePrisma is false` : 'generate is [] (--no-generate)'}` : undefined
|
|
1820
|
+
const clients = off === undefined ? await debugTimed('Generated the Prisma Clients', () => prismaOutputs(tree, project, { projects, strict })) : { outputs: [] }
|
|
1821
|
+
// What their generators would write, read from the project alone: a lazy tree fetches nothing for it.
|
|
1822
|
+
expected = prisma.expectedOutputs({ host: project, root: tree.root, projects, ...clients, off })
|
|
1823
|
+
if (clients.outputs.length > 0) outside = vfsHost(prisma.withPrismaClients(vfs, clients.outputs))
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
const generated = expected.length === 0 ? undefined : prisma.generatedMiss(expected)
|
|
1827
|
+
// For a lazy tree, in rounds, each build keeping what its scans read across them in a memo of its own.
|
|
1828
|
+
const memos = tree.unpack === undefined ? undefined : builds.map(() => new Map())
|
|
1829
|
+
const attempt = async (reads) => {
|
|
1830
|
+
const host = treeHost(tree, outside, reads)
|
|
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
|
+
}
|
|
1843
|
+
const builts = []
|
|
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.
|
|
1846
|
+
let voided
|
|
1847
|
+
const ofBuilds = []
|
|
1848
|
+
for (const [i, build] of builds.entries()) {
|
|
1849
|
+
let built
|
|
1850
|
+
try {
|
|
1851
|
+
// eslint-disable-next-line no-await-in-loop -- one build at a time, in the order its warnings read
|
|
1852
|
+
built = kinds[i] === 'sol' ? await solidityBuilt(build, args) : [await jsBuilt(build, memos?.[i])]
|
|
1853
|
+
} catch (error) {
|
|
1854
|
+
if (reads === undefined || reads.asked.size === 0) throw error
|
|
1855
|
+
voided ??= { error }
|
|
1856
|
+
continue
|
|
1857
|
+
}
|
|
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
|
+
}
|
|
1864
|
+
}
|
|
1865
|
+
if (voided !== undefined) throw voided.error
|
|
1866
|
+
const { root } = builts[0]
|
|
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 }
|
|
1870
|
+
}
|
|
1871
|
+
const since = counts()
|
|
1872
|
+
const built = tree.unpack === undefined
|
|
1873
|
+
? await debugTimed('Built', () => attempt(), () => builtSince(since))
|
|
1874
|
+
: await inRounds(...debugging() ? reportedRounds(attempt, tree.unpack) : [attempt, tree.unpack])
|
|
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)
|
|
1452
1880
|
if (repo !== undefined) bundle.repo = repo
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1881
|
+
const accounts = pinFromTree(bundle, root, tree)
|
|
1882
|
+
const unpacked = unpackedOf(tree)
|
|
1883
|
+
// Built when first read: `stasis github-bundle` and `stasis bundle --deptree` read it for --lockfile alone.
|
|
1884
|
+
let lock
|
|
1885
|
+
return { bundle, get lockfile() { return (lock ??= lockfile && withAttested(lockfile(), bundle)) }, stats: { ...tree.stats, unpacked }, packageManager: packageManagers.length === 1 ? packageManagers[0] : undefined, packageManagers, root, accounts, accountsOf: (modules) => pinFromTree({ modules }, root, tree) }
|
|
1457
1886
|
}
|
|
1458
1887
|
|
|
1459
|
-
//
|
|
1460
|
-
//
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
return (await buildOfKind(classifyEntries('buildBundle', options), options)).bundle
|
|
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
|
|
1465
1893
|
}
|
|
1466
1894
|
|
|
1467
|
-
|
|
1468
|
-
// (stasis.code.br by default, `-` for stdout; the summary goes to stderr so it never
|
|
1469
|
-
// interleaves with binary stdout). An optional JS `lockfile` attests every bundled file;
|
|
1470
|
-
// with `--conditions` it attests the conditions-selected graph, so a plain
|
|
1471
|
-
// `stasis run --lock=frozen` (which doesn't replay them) fails closed -- pair it with
|
|
1472
|
-
// `--bundle=load` or replay the conditions. `add` unions the fresh build into the bundle
|
|
1473
|
-
// already on disk (strict; a conflicting file throws) and can't target stdout.
|
|
1474
|
-
export async function bundleCommand({ cwd = process.cwd(), env = process.env, entries, mappingFile, manifests = false, output, scope, lockfile, conditions, mainFields, platforms, metro, metroResolver, jsx = false, flow = false, typescript = false, tsconfig, resources = [], packageJSON = false, cargo = false, cargoFeatures = [], cargoNoDefaultFeatures = false, cargoAllFeatures = false, cargoTarget = null, cargoManifests = false, brotliQuality, add = false } = {}) {
|
|
1475
|
-
const options = { cwd, env, entries, mappingFile, manifests, scope, conditions, mainFields, platforms, metro, metroResolver, jsx, flow, typescript, tsconfig, resources, packageJSON, cargo, cargoFeatures, cargoNoDefaultFeatures, cargoAllFeatures, cargoTarget, cargoManifests }
|
|
1476
|
-
const kind = classifyEntries('bundleCommand', { ...options, lockfile })
|
|
1895
|
+
export const debugging = () => process.env.EXODUS_STASIS_DEBUG === '1' || process.env.EXODUS_STASIS_DEBUG === 'true'
|
|
1477
1896
|
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1897
|
+
// The time a --metro build's stages took, all told, in milliseconds, for EXODUS_STASIS_DEBUG.
|
|
1898
|
+
const stageTimes = { scan: 0, metro: 0, configs: 0, carrying: 0 }
|
|
1899
|
+
const staged = (stage, fn) => {
|
|
1900
|
+
const at = performance.now()
|
|
1901
|
+
try {
|
|
1902
|
+
return fn()
|
|
1903
|
+
} finally {
|
|
1904
|
+
stageTimes[stage] += performance.now() - at
|
|
1481
1905
|
}
|
|
1906
|
+
}
|
|
1907
|
+
const seconds = (ms) => `${(ms / 1000).toFixed(2)} s`
|
|
1482
1908
|
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
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.
|
|
1911
|
+
export async function debugTimed(what, run, detail = () => '') {
|
|
1912
|
+
if (!debugging()) return run()
|
|
1913
|
+
const at = performance.now()
|
|
1914
|
+
try {
|
|
1915
|
+
return await run()
|
|
1916
|
+
} finally {
|
|
1917
|
+
process.stderr.write(`[stasis] ${what} in ${seconds(performance.now() - at)}${detail()}\n`)
|
|
1918
|
+
}
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
const counts = () => ({ files: { ...scanCounts }, stages: { ...stageTimes } })
|
|
1922
|
+
const STAGES = { scan: 'scan', metro: "Metro's additions", configs: 'configs and toolchain', carrying: 'carrying' }
|
|
1923
|
+
|
|
1924
|
+
function builtSince(since) {
|
|
1925
|
+
const files = Object.keys(scanCounts).map((key) => `${scanCounts[key] - since.files[key]} ${key}`).join(', ')
|
|
1926
|
+
const times = Object.keys(STAGES).filter((key) => stageTimes[key] > since.stages[key]).map((key) => `${STAGES[key]} ${seconds(stageTimes[key] - since.stages[key])}`)
|
|
1927
|
+
return ` (files ${files}${times.length > 0 ? `; ${times.join(', ')}` : ''})`
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1930
|
+
const packageName = (dir) => dir.slice(dir.lastIndexOf('/node_modules/') + '/node_modules/'.length)
|
|
1931
|
+
|
|
1932
|
+
function reportedRounds(attempt, unpack) {
|
|
1933
|
+
let round = 0
|
|
1934
|
+
return [
|
|
1935
|
+
(reads) => {
|
|
1936
|
+
round++
|
|
1937
|
+
const since = counts()
|
|
1938
|
+
const asked = () => (reads.asked.size === 0 ? ', the build' : `, asked for ${reads.asked.size} package${reads.asked.size === 1 ? '' : 's'}: ${[...reads.asked].slice(0, 5).map(packageName).join(', ')}${reads.asked.size > 5 ? ', …' : ''}`)
|
|
1939
|
+
return debugTimed(`Lazy round ${round}: built`, () => attempt(reads), () => `${builtSince(since)}${asked()}`)
|
|
1940
|
+
},
|
|
1941
|
+
(asked, read) => debugTimed(`Lazy round ${round}: unpacked`, () => unpack(asked, read)),
|
|
1942
|
+
]
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
// `stasis bundle`'s Bundle, not written, a JS one held to the project's stasis.lock.json as `stasis bundle` holds it.
|
|
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 } = {}) {
|
|
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 }
|
|
1948
|
+
const built = await buildOfKind(classifyEntries('buildBundle', options), options)
|
|
1487
1949
|
const root = built.root ?? resolve(cwd)
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
// Only a JS bundle has a lockfile (classifyEntries refuses one for the others).
|
|
1493
|
-
let lockData = lockfile ? built.lockfile().serialize() : undefined
|
|
1950
|
+
assertPolicies('buildBundle', { bundle: built.bundle, root })
|
|
1951
|
+
heldOf(pinnedOf(built, root), root, { cwd, env })?.loads(built.bundle)
|
|
1952
|
+
return built.bundle
|
|
1953
|
+
}
|
|
1494
1954
|
|
|
1495
|
-
|
|
1496
|
-
|
|
1955
|
+
// Run `stasis bundle` (doc/bundle.md).
|
|
1956
|
+
export async function bundleCommand({ cwd = process.cwd(), output, lockfile, lockfileOnly = false, brotliQuality, add = false, ...options } = {}) {
|
|
1957
|
+
if (add && output === '-') throw new Error('bundleCommand: --add cannot be combined with --output=- (nothing to merge into on stdout)')
|
|
1958
|
+
const lockfileOnlyRefused = lockfileOnly ? lockfileOnlyError({ lockfile, output, add, brotliQuality }) : null
|
|
1959
|
+
if (lockfileOnlyRefused !== null) throw new Error(`bundleCommand: ${lockfileOnlyRefused}`)
|
|
1960
|
+
const built = await buildOfCommand({ ...options, cwd, output, lockfile })
|
|
1961
|
+
const { root } = built
|
|
1962
|
+
// By default at the root, where `stasis run --bundle=load` discovers it: in cwd below the root it would root
|
|
1963
|
+
// the next run and `stasis bundle` there, out of the files above it.
|
|
1964
|
+
const target = output ?? relative(resolve(cwd), join(root, BUNDLE_FILE))
|
|
1965
|
+
|
|
1966
|
+
let { bundle } = built
|
|
1967
|
+
// Only a JS bundle has a lockfile (classifyEntries refuses one for the others).
|
|
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
|
|
1497
1972
|
|
|
1498
|
-
// --add: union the fresh build into the existing on-disk bundle; a conflicting file throws. Skipped when nothing is on disk.
|
|
1499
1973
|
const outAbs = target === '-' ? undefined : resolve(cwd, target)
|
|
1500
1974
|
const lockAbs = lockData ? resolve(cwd, lockfile) : undefined
|
|
1501
|
-
//
|
|
1975
|
+
// What the bundle on disk carried; undefined where nothing is merged.
|
|
1502
1976
|
let mergedFrom
|
|
1503
1977
|
if (add && existsSync(outAbs)) {
|
|
1504
1978
|
let existing
|
|
1505
1979
|
try {
|
|
1506
|
-
existing =
|
|
1980
|
+
existing = parseBundleBytes(readFileSync(outAbs))
|
|
1507
1981
|
} catch (cause) {
|
|
1508
1982
|
throw new Error(`bundleCommand: --add failed to read the existing bundle at ${target}`, { cause })
|
|
1509
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 })
|
|
1510
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)
|
|
1511
1998
|
bundle = existing.merge(bundle)
|
|
1512
|
-
//
|
|
1513
|
-
// With none on disk we can't attest the pre-existing bundle's files (their hashes live
|
|
1514
|
-
// 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.
|
|
1515
2000
|
if (lockData) {
|
|
1516
|
-
if (!existsSync(lockAbs)) {
|
|
1517
|
-
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.`)
|
|
1518
|
-
}
|
|
1519
2001
|
let existingLock
|
|
1520
|
-
|
|
1521
|
-
existingLock =
|
|
1522
|
-
}
|
|
1523
|
-
|
|
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)
|
|
1524
2019
|
}
|
|
1525
2020
|
lockData = existingLock.merge(Lockfile.parse(lockData)).serialize()
|
|
1526
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 })
|
|
1527
2029
|
}
|
|
1528
2030
|
|
|
2031
|
+
// Serialized under --lockfile-only too: never write a lockfile of a bundle that couldn't be written.
|
|
1529
2032
|
const serialized = bundle.serialize()
|
|
2033
|
+
built.held?.loads(bundle, { text: serialized, at: outAbs })
|
|
1530
2034
|
const files = [...bundle.sources.keys()]
|
|
1531
2035
|
const modules = bundle.modules
|
|
1532
2036
|
|
|
1533
|
-
const dest = writeBundle(cwd, target, serialized, brotliQuality)
|
|
2037
|
+
const dest = lockfileOnly ? undefined : writeBundle(cwd, target, serialized, brotliQuality)
|
|
1534
2038
|
if (lockData) writeFile(lockAbs, lockData)
|
|
1535
2039
|
// The bundle's paths are relative to its root; shown relative to cwd, as `dest` is.
|
|
1536
2040
|
const fromDir = posix.join(toPosix(relative(resolve(cwd), root)), outermostDir(files, root))
|
|
1537
|
-
|
|
1538
|
-
|
|
2041
|
+
if (lockfileOnly) {
|
|
2042
|
+
console.warn(lockedSummary(files.length, modules, fromDir, lockfile))
|
|
2043
|
+
} else if (mergedFrom !== undefined) {
|
|
1539
2044
|
const added = files.length - mergedFrom
|
|
1540
2045
|
console.warn(`[stasis] Added ${added} file${added === 1 ? '' : 's'} (${files.length} total in ${packagesLabel(modules)}) from ${fromDir} to ${dest}`)
|
|
1541
2046
|
} else {
|
|
1542
2047
|
console.warn(bundledSummary(files.length, modules, fromDir, dest))
|
|
1543
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
|
|
2114
|
+
}
|
|
2115
|
+
|
|
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.
|
|
2159
|
+
export async function buildCommandBundle(options = {}) {
|
|
2160
|
+
const { bundle } = await buildOfCommand(options)
|
|
2161
|
+
return Bundle.parse(bundle.serialize())
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
// bundleCommand's build, not written; `extra` never takes in `output` and `lockfile`, where it would write.
|
|
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 } = {}) {
|
|
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 }
|
|
2167
|
+
if (builds !== undefined) {
|
|
2168
|
+
if (!nonEmpty(builds)) throw new TypeError('bundleCommand: builds must be a non-empty array')
|
|
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))
|
|
2170
|
+
if (given) throw new TypeError(`bundleCommand: ${given[0]} can't be combined with builds (each build sets its own)`)
|
|
2171
|
+
}
|
|
2172
|
+
// Each build's options, the defaults above where it leaves one out.
|
|
2173
|
+
const plans = (builds ?? [{}]).map((build) => ({ ...options, ...build, cwd, env }))
|
|
2174
|
+
const kinds = plans.map((plan) => classifyEntries('bundleCommand', { ...plan, lockfile }))
|
|
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.
|
|
2177
|
+
const bundleExtra = builds !== undefined
|
|
2178
|
+
let union, root
|
|
2179
|
+
if (deptree) {
|
|
2180
|
+
// The disk read through the tree, nothing cached (`cache: false`, and no setCacheDir here).
|
|
2181
|
+
const { createClient } = await import('@preventive/upstream/github.js')
|
|
2182
|
+
const built = await withNpmToken(home, () => buildFromLockfile('bundleCommand', diskHost, {
|
|
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' }))
|
|
2185
|
+
console.warn(unpackedSummary(built.stats))
|
|
2186
|
+
root = built.root
|
|
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 }
|
|
2189
|
+
} else {
|
|
2190
|
+
const builts = []
|
|
2191
|
+
for (const [i, plan] of plans.entries()) {
|
|
2192
|
+
// eslint-disable-next-line no-await-in-loop -- one build at a time, in the order its warnings read
|
|
2193
|
+
builts.push(await buildOfKind(kinds[i], { ...plan, outputsAt, extra: !bundleExtra }))
|
|
2194
|
+
}
|
|
2195
|
+
// A State-built JS bundle's paths are relative to the project's root at or above cwd.
|
|
2196
|
+
const roots = builts.map((built) => built.root ?? resolve(cwd))
|
|
2197
|
+
root = roots[0]
|
|
2198
|
+
// State-built bundles keep the State root's repo (no cwd fallback: their paths are relative to that root).
|
|
2199
|
+
for (const built of builts) if (!built.stateBuilt) built.bundle.repo ??= detectRepo(cwd)
|
|
2200
|
+
union = unionOfBuilds('bundleCommand', plans, builts, roots)
|
|
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 })
|
|
2205
|
+
}
|
|
2206
|
+
return { ...union, root, held: heldOf(union, root, { cwd, env, lockfile }) }
|
|
1544
2207
|
}
|