@exodus/stasis 1.0.0-beta.8 → 1.0.0-rc.0
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 +7 -2
- package/bin/stasis.js +263 -446
- package/package.json +38 -13
- 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 +1718 -908
- package/src/cmd/diff.js +1 -4
- package/src/cmd/github-bundle.js +48 -21
- package/src/cmd/output.js +24 -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 +221 -475
- 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 +170 -235
- package/src/resolve-node.js +33 -46
- 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 +241 -278
- 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 +54 -46
- package/src/vfs-bundle/github.js +115 -92
- 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 +434 -259
- package/src/vfs-bundle/yarnclean.js +79 -0
- package/src/vfs-bundle.js +25 -51
- package/src/webpack.js +0 -4
- package/src/why.js +79 -231
package/src/cmd/bundle.js
CHANGED
|
@@ -1,20 +1,33 @@
|
|
|
1
1
|
import { isUtf8 } from 'node:buffer'
|
|
2
2
|
import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs'
|
|
3
|
-
import { 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
|
-
import { Bundle } from '@exodus/stasis-core/bundle'
|
|
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'
|
|
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'
|
|
13
20
|
import { State } from '@exodus/stasis-core/state'
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
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'
|
|
17
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'
|
|
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'
|
|
18
31
|
import {
|
|
19
32
|
SOLIDITY_PACKAGE_MANIFESTS,
|
|
20
33
|
SOLIDITY_ROOT_MANIFESTS,
|
|
@@ -23,10 +36,11 @@ import {
|
|
|
23
36
|
discoverSolidityConfig,
|
|
24
37
|
expandSolidityEntries,
|
|
25
38
|
} from '../loaders/solidity.js'
|
|
26
|
-
import { decodeUtf8 } from '../loaders/solidity-ownership.js'
|
|
39
|
+
import { decodeUtf8, solidityOwnership } from '../loaders/solidity-ownership.js'
|
|
27
40
|
import { buildBashTree, collectBashFilesFromDisk } from '../loaders/bash.js'
|
|
28
41
|
import { boundaryOf, collectRustBundle, withinRealDir } from '../loaders/rust.js'
|
|
29
42
|
import { createCargoContext } from '../loaders/cargo.js'
|
|
43
|
+
import { ancestors } from '../loaders/paths.js'
|
|
30
44
|
import {
|
|
31
45
|
bucketizePhpSources,
|
|
32
46
|
buildPhpTree,
|
|
@@ -35,125 +49,100 @@ import {
|
|
|
35
49
|
loadComposerPackages,
|
|
36
50
|
loadLaravelProviderFiles,
|
|
37
51
|
} from '../loaders/php.js'
|
|
38
|
-
import {
|
|
52
|
+
import { bundledSummary, lockedSummary, lockfileOnlyError, packagesLabel, unpackedSummary, writeBundle, writeFile } from './output.js'
|
|
53
|
+
import { withNpmToken } from './tokens.js'
|
|
39
54
|
|
|
40
|
-
const JS_EXTS = new Set(['.js', '.cjs', '.mjs', '.ts', '.cts', '.mts', '.jsx', '.tsx'])
|
|
41
55
|
const BASH_EXTS = new Set(['.sh', '.bash'])
|
|
42
56
|
const RUST_EXTS = new Set(['.rs'])
|
|
57
|
+
const nonEmpty = (list) => Array.isArray(list) && list.length > 0
|
|
43
58
|
|
|
44
|
-
//
|
|
45
|
-
const SOLIDITY_WORKSPACE_NAME = 'solidity-bundle'
|
|
46
|
-
const SOLIDITY_WORKSPACE_VERSION = '0.0.0'
|
|
47
|
-
const SOLIDITY_FORMAT = 'solidity'
|
|
48
|
-
const BASH_WORKSPACE_NAME = 'bash-bundle'
|
|
49
|
-
const BASH_WORKSPACE_VERSION = '0.0.0'
|
|
50
|
-
const SHELL_FORMAT = 'shell'
|
|
51
|
-
|
|
52
|
-
const RUST_WORKSPACE_NAME = 'rust-bundle'
|
|
53
|
-
const RUST_WORKSPACE_VERSION = '0.0.0'
|
|
54
|
-
const RUST_FORMAT = 'rust'
|
|
55
|
-
|
|
56
|
-
const PHP_WORKSPACE_NAME = 'php-bundle'
|
|
57
|
-
const PHP_WORKSPACE_VERSION = '0.0.0'
|
|
58
|
-
const PHP_FORMAT = 'php'
|
|
59
|
-
|
|
60
|
-
// Deepest common parent dir of `paths`, relative to `cwd`; starts with `..` when entries
|
|
61
|
-
// 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).
|
|
62
60
|
export function outermostDir(paths, cwd) {
|
|
63
61
|
if (paths.length === 0) return '.'
|
|
64
62
|
const cwdAbs = posix.resolve(toPosix(cwd))
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const c = partsList[0][i]
|
|
70
|
-
if (!partsList.every((parts) => parts[i] === c)) break
|
|
71
|
-
common.push(c)
|
|
72
|
-
}
|
|
73
|
-
const absCommon = common.join('/') || '/'
|
|
74
|
-
const rel = posix.relative(cwdAbs, absCommon)
|
|
75
|
-
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('/') || '/') || '.'
|
|
76
67
|
}
|
|
77
68
|
|
|
78
|
-
//
|
|
79
|
-
// 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.
|
|
80
70
|
function parseSoldeerDir(seg) {
|
|
81
71
|
const m = /^(.+)-(\d+\.\d+\.\d+(?:[-+][\w.-]+)?)$/u.exec(seg)
|
|
82
72
|
return m ? { name: m[1], version: m[2] } : { name: seg, version: '0.0.0' }
|
|
83
73
|
}
|
|
84
74
|
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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.
|
|
77
|
+
const GIT_URL = /^(?:[^/]*:(?!\/\/)|(?:https?|git|ssh|git\+ssh|ssh\+git):\/\/(?:[^/@]*@)?[^/:@]*(?::\d*)?\/)/u
|
|
78
|
+
|
|
79
|
+
// Not npm's `github:owner/name` (the host `github` to git), nor one with a `#`, which git keeps in the path.
|
|
80
|
+
const submoduleGithub = (url) => (GIT_URL.test(url) && !/^github:/iu.test(url) && !url.includes('#') ? parseGithubRepository(url.replace(/^ssh\+git:/u, 'ssh:')) : null)
|
|
90
81
|
|
|
91
|
-
// The github.com ones of `submodules` (readGitmodules), as Map<submodulePath, { name, branch }>.
|
|
92
82
|
function githubSubmodules(submodules) {
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
83
|
+
const named = submodules.map(({ path, url, branch }) => [path, { name: url && submoduleGithub(url), branch }])
|
|
84
|
+
return new Map(named.filter(([, { name }]) => name))
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const repoRootAt = (github, commit) => (isValidRepoField('github', github) && isValidRepoField('commit', commit) ? { github, directory: '', commit } : undefined)
|
|
88
|
+
|
|
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')
|
|
92
|
+
let lock = null
|
|
93
|
+
try {
|
|
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()
|
|
98
|
+
for (const dep of Object.values(lock?.dependencies ?? {})) {
|
|
99
|
+
locks.set(`${dep.name}-${dep.version}`, dep.type === 'git' ? { repo: repoRootAt(parseGithubRepository(dep.git), dep.rev) } : { integrity: archiveIntegrity('soldeer', dep.checksum) })
|
|
100
|
+
}
|
|
101
|
+
return locks
|
|
99
102
|
}
|
|
100
103
|
|
|
101
|
-
//
|
|
102
|
-
// github submodule (`lib/`, via the `.gitmodules` `ownership` read), else null to defer to the
|
|
103
|
-
// node_modules/workspace logic. `ownership.assert` vets a package.json path before it is read
|
|
104
|
-
// through `host`.
|
|
104
|
+
// A bucket's `repo` is its checkout's HEAD; a Soldeer git dependency's only at its soldeer.lock `rev`.
|
|
105
105
|
function makeSolidityClassifier(baseDir, ownership, host) {
|
|
106
106
|
const submodules = githubSubmodules(ownership.submodules)
|
|
107
|
-
const
|
|
108
|
-
const
|
|
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
|
|
109
110
|
return (path) => {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
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 }) }
|
|
116
119
|
}
|
|
117
120
|
for (const [sub, { name, branch }] of submodules) {
|
|
118
121
|
if (path === sub || path.startsWith(`${sub}/`)) {
|
|
119
|
-
|
|
120
|
-
|
|
122
|
+
const version = versionOf(sub)
|
|
123
|
+
const repo = repoRootAt(name, commitOf(sub))
|
|
124
|
+
return { bucketDir: sub, name, version: version ?? branch ?? '0.0.0', ecosystem: 'github', ...(repo && { repo }) }
|
|
121
125
|
}
|
|
122
126
|
}
|
|
123
127
|
return null
|
|
124
128
|
}
|
|
125
129
|
}
|
|
126
130
|
|
|
127
|
-
//
|
|
128
|
-
//
|
|
129
|
-
|
|
130
|
-
// Neither is ever sent to a public advisory database: a private git dependency's name has no place
|
|
131
|
-
// there, and a crates.io crate of the same name isn't it.
|
|
132
|
-
const vendoredEcosystem = (from) => {
|
|
133
|
-
if (from === 'registry') return 'cargo'
|
|
134
|
-
return from === 'git' ? 'cargo-git' : 'cargo-unknown'
|
|
135
|
-
}
|
|
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')
|
|
136
134
|
|
|
137
|
-
// Classify a Rust file by the nearest Cargo.toml `[package]`: a `cargo vendor`ed crate under the
|
|
138
|
-
// vendor dir (`vendor/<dir>/`, or where .cargo/config.toml points) is a dependency (tagged
|
|
139
|
-
// `cargo`, `cargo-git` or `cargo-unknown`: see vendoredEcosystem); any other package (the crate
|
|
140
|
-
// itself, a workspace member reached through a `path` dependency) is first-party, so no
|
|
141
|
-
// ecosystem. Null (no manifest above the file) defers to the package.json/placeholder logic.
|
|
142
135
|
function makeRustClassifier(cargo) {
|
|
143
136
|
return (path) => {
|
|
144
137
|
const pkg = cargo.packageInfo(path)
|
|
145
138
|
if (!pkg) return null
|
|
146
|
-
|
|
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) }) }
|
|
147
141
|
}
|
|
148
142
|
}
|
|
149
143
|
|
|
150
|
-
//
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
// A file of the bundle root (`realBase` its real path), its bytes, by project-relative path, or
|
|
154
|
-
// null when it isn't a regular file there (a manifest the context saw may have gone since; a FIFO
|
|
155
|
-
// would hang a read) or, with `within` a directory, doesn't really lie in it (a vendored crate's
|
|
156
|
-
// 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).
|
|
157
146
|
function readFileWithinBase(baseDir, realBase, rel, within) {
|
|
158
147
|
try {
|
|
159
148
|
assertRealPathWithinBase(realBase, baseDir, rel)
|
|
@@ -166,48 +155,26 @@ function readFileWithinBase(baseDir, realBase, rel, within) {
|
|
|
166
155
|
}
|
|
167
156
|
}
|
|
168
157
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
function executableSources(baseDir, sources, host) {
|
|
173
|
-
const executable = new Set()
|
|
174
|
-
for (const path of sources.keys()) {
|
|
175
|
-
if (isExecutableFile(join(baseDir, path), host)) executable.add(path)
|
|
176
|
-
}
|
|
177
|
-
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}`)
|
|
178
161
|
}
|
|
179
162
|
|
|
180
|
-
// Bundles must be self-contained: throw on a missing entry or an unresolved `noun` (import, script,
|
|
181
|
-
// module) of a `lang` bundle -- `missing`'s { spec, from }, with the `reason` it was refused if given.
|
|
182
163
|
function assertSelfContained(lang, noun, entries, sources, missing) {
|
|
183
|
-
const issues = []
|
|
184
|
-
for (const
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
for (const { spec, from, reason } of missing) {
|
|
188
|
-
issues.push(`Unresolved ${noun}: ${spec} from ${from}${reason ? ` (refused: ${reason})` : ''}`)
|
|
189
|
-
}
|
|
190
|
-
if (issues.length > 0) {
|
|
191
|
-
throw new Error(`${lang} bundle has unresolved ${noun}s:\n${issues.map((s) => ` ${s}`).join('\n')}`)
|
|
192
|
-
}
|
|
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')}`)
|
|
193
167
|
}
|
|
194
168
|
|
|
195
|
-
//
|
|
196
|
-
// nearest package.json (node_modules -> `npm`-tagged bucket, workspace -> its dir, none ->
|
|
197
|
-
// "." with the placeholder identity); a node_modules file whose nearest package.json is the
|
|
198
|
-
// workspace root is rejected, not mislabeled; `packageOf(path)` finds that package.json
|
|
199
|
-
// (packageLookup by default, which walks past a malformed one). `classifyDep(path)` optionally
|
|
200
|
-
// places a file directly (non-node_modules ecosystems like Soldeer/github); null defers. `format` tags
|
|
201
|
-
// every file; `formats` (Map<path,format>) overrides it per file. `resolutions` values are
|
|
202
|
-
// 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.
|
|
203
170
|
function assembleCodeBundle({
|
|
204
|
-
baseDir, entries, sources, resolutions, workspaceName, workspaceVersion, format, formats, conditionKey, classifyDep, host,
|
|
205
|
-
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,
|
|
206
173
|
}) {
|
|
207
174
|
const modules = new Map()
|
|
208
|
-
// `identity`'s name, version, ecosystem and repo (moduleInfo takes those alone).
|
|
209
175
|
const ensureBucket = (dir, identity) => {
|
|
210
|
-
|
|
176
|
+
const bucket = modules.get(dir)
|
|
177
|
+
modules.set(dir, bucket === undefined ? moduleInfo({ ...identity, files: Object.create(null) }) : withMetadataOf(bucket, identity))
|
|
211
178
|
return modules.get(dir)
|
|
212
179
|
}
|
|
213
180
|
|
|
@@ -218,80 +185,48 @@ function assembleCodeBundle({
|
|
|
218
185
|
continue
|
|
219
186
|
}
|
|
220
187
|
const meta = packageOf(path)
|
|
221
|
-
|
|
222
|
-
if (meta) {
|
|
223
|
-
|
|
224
|
-
throw new Error(`No package.json with name+version found for ${path}`)
|
|
225
|
-
}
|
|
226
|
-
ensureBucket(meta.pkgDir, meta).files[fileInBucket(meta.pkgDir, path)] = content
|
|
227
|
-
} else {
|
|
228
|
-
if (inNodeModules) throw new Error(`No package.json with name+version found for ${path}`)
|
|
229
|
-
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}`)
|
|
230
191
|
}
|
|
192
|
+
const [dir, identity] = meta ? [meta.pkgDir, meta] : ['.', { name: workspaceName, version: workspaceVersion }]
|
|
193
|
+
ensureBucket(dir, identity).files[fileInBucket(dir, path)] = content
|
|
231
194
|
}
|
|
232
195
|
|
|
233
|
-
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 })
|
|
234
197
|
}
|
|
235
198
|
|
|
236
|
-
|
|
237
|
-
// (Map<path,format>) overrides it per file, and the `resolutions` edges are keyed under `conditionKey`.
|
|
238
|
-
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 }) {
|
|
239
200
|
const formatsMap = new Map()
|
|
240
|
-
for (const path of sources.keys())
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
const imports = new Map([[conditionKey, importsForKey]])
|
|
245
|
-
|
|
246
|
-
// Executable bits, straight off disk (a synthetic source with no file there is simply not
|
|
247
|
-
// executable). Shell bundles lean on this most: `stasis extract` puts the +x back on the scripts.
|
|
248
|
-
const executable = executableSources(baseDir, sources, host)
|
|
249
|
-
|
|
250
|
-
// 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
|
+
}
|
|
251
205
|
return new Bundle({
|
|
252
206
|
config: { scope: 'full' },
|
|
253
207
|
entries: new Set(entries),
|
|
254
208
|
modules,
|
|
255
209
|
formats: formatsMap,
|
|
256
|
-
imports,
|
|
257
|
-
executable
|
|
258
|
-
|
|
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)
|
|
259
214
|
}
|
|
260
215
|
|
|
261
|
-
//
|
|
262
|
-
// (both however they're cased).
|
|
216
|
+
// Never carried, whatever reads them, however cased: `.env` files, and Hardhat's config, which is code.
|
|
263
217
|
const neverCarried = (rel) => isDotEnvFile(rel) || posix.basename(rel).toLowerCase().startsWith('hardhat.config.')
|
|
264
218
|
|
|
265
|
-
//
|
|
266
|
-
function packageLookup(baseDir, options) {
|
|
267
|
-
const byDir = new Map()
|
|
268
|
-
return (path) => {
|
|
269
|
-
const dir = posix.dirname(path)
|
|
270
|
-
if (!byDir.has(dir)) byDir.set(dir, findPackageMetadata(baseDir, path, options))
|
|
271
|
-
return byDir.get(dir)
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// The build-description files of a Solidity bundle (--manifests), as Map<path, text>, sorted:
|
|
276
|
-
// `configFiles` (what discoverSolidityConfig read, whatever they're called), each of which must be
|
|
277
|
-
// carried -- one outside the root, neverCarried, refused or gone is an error, as the bundle
|
|
278
|
-
// couldn't reproduce the resolution without it -- plus the SOLIDITY_*_MANIFESTS that exist, for the
|
|
279
|
-
// root and for each package dir `classifyDep`/`packageOf` places a bundled source in, but none
|
|
280
|
-
// whose path `ownership` refuses (see solidityOwnership). Carried as written: whatever they hold
|
|
281
|
-
// (an RPC URL with its API key, an Etherscan key, a URL's credentials) is in the bundle too, as
|
|
282
|
-
// with --package-json. Read through `host`.
|
|
219
|
+
// --manifests: carried as written, secrets too.
|
|
283
220
|
function solidityManifests(baseDir, sources, configFiles, { classifyDep, packageOf, ownership, host }) {
|
|
284
221
|
const realBase = host.realpath(baseDir)
|
|
285
|
-
// `{ text }`, or `{ why }` it can't be carried (null: nothing is there).
|
|
286
|
-
// `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).
|
|
287
223
|
const carry = (rel) => {
|
|
288
224
|
const { reason, real, outside } = ownership.of(rel)
|
|
289
225
|
if (reason) return { why: reason }
|
|
290
226
|
if (real === null) return { why: null }
|
|
291
227
|
if (outside) throw new Error(`Refusing to follow symlink escaping bundle root: ${rel} -> ${resolve(realBase, real)}`)
|
|
292
228
|
const buf = readRegularFileOrNull(join(realBase, real), rel, host)
|
|
293
|
-
|
|
294
|
-
return { text: decodeUtf8(buf, rel) }
|
|
229
|
+
return buf === null ? { why: null } : { text: decodeUtf8(buf, rel) }
|
|
295
230
|
}
|
|
296
231
|
const unreproducible = (rel, why) => new Error(`--manifests can't carry ${rel}, which the Solidity resolution read: ${why}`)
|
|
297
232
|
const out = new Map()
|
|
@@ -319,18 +254,12 @@ function solidityManifests(baseDir, sources, configFiles, { classifyDep, package
|
|
|
319
254
|
return new Map([...out.keys()].toSorted().map((rel) => [rel, out.get(rel)]))
|
|
320
255
|
}
|
|
321
256
|
|
|
322
|
-
//
|
|
323
|
-
|
|
324
|
-
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)
|
|
325
259
|
|
|
326
|
-
// Whether `entry` (resolved against `cwd`) is a Solidity bundle's: a .sol file or a directory entry.
|
|
327
260
|
export const isSolidityEntry = (entry, cwd = process.cwd(), host = diskHost) => entry.endsWith('.sol') || isDirEntry(resolve(cwd, entry), host)
|
|
328
261
|
|
|
329
|
-
//
|
|
330
|
-
// entry stands for the .sol files under it, so it goes with Solidity entries only; and entries
|
|
331
|
-
// that are all missing extensionless paths are a mistyped file, not a project without those dirs.
|
|
332
|
-
// `fetched` false: `host` is an empty tree standing in for one not fetched yet, which can't say
|
|
333
|
-
// 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.
|
|
334
263
|
export function directoryEntryError(entries, cwd = process.cwd(), host = diskHost, { fetched = true } = {}) {
|
|
335
264
|
const dirs = entries.filter((e) => isDirEntry(resolve(cwd, e), host))
|
|
336
265
|
const absent = fetched ? dirs.filter((e) => host.stat(resolve(cwd, e)) === null) : []
|
|
@@ -341,35 +270,19 @@ export function directoryEntryError(entries, cwd = process.cwd(), host = diskHos
|
|
|
341
270
|
: `a directory entry is only supported for Solidity bundles (it stands for the .sol files under it): ${dirs[0]}`
|
|
342
271
|
}
|
|
343
272
|
|
|
344
|
-
//
|
|
345
|
-
// files under it, as forge's src/test/script dirs and Hardhat's contracts dir do; a missing or
|
|
346
|
-
// empty one is skipped). Imports resolve through the remappings the project's build uses
|
|
347
|
-
// (discoverSolidityConfig: forge's discovery for a Foundry project); `mappingFile`
|
|
348
|
-
// (foundry.toml/remappings.txt) pins them to exactly what it lists. An import is refused when it
|
|
349
|
-
// reaches a non-.sol file or leaves the root, or when a dependency's reaches the project's own
|
|
350
|
-
// files. Config files are read, and bundled only with `manifests` (see solidityManifests). `env`
|
|
351
|
-
// supplies FOUNDRY_PROFILE / FOUNDRY_REMAPPINGS, reported when they apply. The project is read
|
|
352
|
-
// through `host`.
|
|
273
|
+
// A missing directory entry is skipped; `env` supplies FOUNDRY_PROFILE / FOUNDRY_REMAPPINGS.
|
|
353
274
|
export async function buildSolidityBundle({ cwd = process.cwd(), entries, mappingFile, manifests = false, env = process.env, host = diskHost } = {}) {
|
|
354
|
-
if (!
|
|
355
|
-
throw new Error('buildSolidityBundle: at least one entry .sol file or directory is required')
|
|
356
|
-
}
|
|
357
|
-
|
|
275
|
+
if (!nonEmpty(entries)) throw new Error('buildSolidityBundle: at least one entry .sol file or directory is required')
|
|
358
276
|
const baseDir = resolve(cwd)
|
|
359
277
|
const normalized = normalizeEntries(entries, cwd)
|
|
360
|
-
for (const e of normalized) {
|
|
361
|
-
if (!isSolidityEntry(e, baseDir, host)) throw new Error(`buildSolidityBundle: not a .sol file or directory: ${e}`)
|
|
362
|
-
}
|
|
278
|
+
for (const e of normalized) if (!isSolidityEntry(e, baseDir, host)) throw new Error(`buildSolidityBundle: not a .sol file or directory: ${e}`)
|
|
363
279
|
const expanded = expandSolidityEntries(baseDir, normalized, host)
|
|
364
|
-
|
|
365
280
|
const { remappings, libs, ownership, files: configFiles, envUsed } = discoverSolidityConfig(baseDir, { mappingFile, env, host })
|
|
366
281
|
// The bundle doesn't record the environment, so say when it shaped the resolution.
|
|
367
282
|
if (envUsed.length > 0) console.warn(`[stasis] Solidity imports resolved with ${envUsed.join(', ')} from the environment`)
|
|
368
283
|
const sources = collectSolidityFilesFromDisk(baseDir, expanded, remappings, { libs, ownership, host })
|
|
369
284
|
const { resolutions, missing } = buildSolidityTree(sources, { remappings, baseDir, libs, ownership, host })
|
|
370
|
-
|
|
371
285
|
assertSelfContained('Solidity', 'import', expanded, sources, missing)
|
|
372
|
-
|
|
373
286
|
const classifyDep = makeSolidityClassifier(baseDir, ownership, host)
|
|
374
287
|
const packageOf = packageLookup(baseDir, { strict: true, check: ownership.assert, host })
|
|
375
288
|
const bundled = new Map(sources)
|
|
@@ -380,98 +293,49 @@ export async function buildSolidityBundle({ cwd = process.cwd(), entries, mappin
|
|
|
380
293
|
formats.set(path, posix.basename(path) === 'package.json' ? 'json' : 'resource')
|
|
381
294
|
}
|
|
382
295
|
}
|
|
383
|
-
|
|
384
296
|
return assembleCodeBundle({
|
|
385
|
-
baseDir,
|
|
386
|
-
|
|
387
|
-
sources: bundled,
|
|
388
|
-
resolutions,
|
|
389
|
-
workspaceName: SOLIDITY_WORKSPACE_NAME,
|
|
390
|
-
workspaceVersion: SOLIDITY_WORKSPACE_VERSION,
|
|
391
|
-
format: SOLIDITY_FORMAT,
|
|
392
|
-
formats,
|
|
393
|
-
conditionKey: 'solidity',
|
|
394
|
-
classifyDep,
|
|
395
|
-
packageOf,
|
|
396
|
-
host,
|
|
297
|
+
baseDir, entries: expanded, sources: bundled, resolutions, workspaceName: 'solidity-bundle', format: 'solidity', formats,
|
|
298
|
+
conditionKey: 'solidity', classifyDep, packageOf, host,
|
|
397
299
|
})
|
|
398
300
|
}
|
|
399
301
|
|
|
400
|
-
//
|
|
401
|
-
// A local `source` inside the bundle root that resolves to no bundled script is fatal;
|
|
402
|
-
// 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.
|
|
403
303
|
export async function buildBashBundle({ cwd = process.cwd(), entries } = {}) {
|
|
404
|
-
|
|
405
|
-
throw new Error('buildBashBundle: at least one entry .sh/.bash file is required')
|
|
406
|
-
}
|
|
407
|
-
for (const e of entries) {
|
|
408
|
-
if (!BASH_EXTS.has(extname(e))) throw new Error(`buildBashBundle: not a .sh/.bash file: ${e}`)
|
|
409
|
-
}
|
|
410
|
-
|
|
304
|
+
assertEntries('buildBashBundle', entries, '.sh/.bash file', (e) => BASH_EXTS.has(extname(e)))
|
|
411
305
|
const baseDir = resolve(cwd)
|
|
412
306
|
const normalized = normalizeEntries(entries, cwd)
|
|
413
|
-
|
|
414
307
|
const sources = await collectBashFilesFromDisk(baseDir, normalized)
|
|
415
308
|
const { resolutions, missing } = buildBashTree(sources)
|
|
416
|
-
|
|
417
|
-
// Self-contained (external refs never reach `missing`).
|
|
418
309
|
assertSelfContained('Bash', 'script', normalized, sources, missing)
|
|
419
|
-
|
|
420
|
-
return assembleCodeBundle({
|
|
421
|
-
baseDir,
|
|
422
|
-
entries: normalized,
|
|
423
|
-
sources,
|
|
424
|
-
resolutions,
|
|
425
|
-
workspaceName: BASH_WORKSPACE_NAME,
|
|
426
|
-
workspaceVersion: BASH_WORKSPACE_VERSION,
|
|
427
|
-
format: SHELL_FORMAT,
|
|
428
|
-
conditionKey: SHELL_FORMAT,
|
|
429
|
-
})
|
|
310
|
+
return assembleCodeBundle({ baseDir, entries: normalized, sources, resolutions, workspaceName: 'bash-bundle', format: 'shell', conditionKey: 'shell' })
|
|
430
311
|
}
|
|
431
312
|
|
|
432
|
-
//
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
// `cargoTarget` (a triple, or `host`) names the build's target: its cfgs, asked of rustc, decide
|
|
443
|
-
// `#[cfg(unix)]`-style code and target-specific dependency tables, which are otherwise all kept.
|
|
444
|
-
// `cargoManifests` also carries what describes each bundled package's build: its Cargo.toml (and
|
|
445
|
-
// the workspace's), its build script -- walked like a crate root, so its modules and the in-tree
|
|
446
|
-
// build-dependencies it reaches come along -- plus the root's Cargo.lock and .cargo/config.toml.
|
|
447
|
-
export async function buildRustBundle({ cwd = process.cwd(), entries, cargo = false, cargoFeatures = [], cargoNoDefaultFeatures = false, cargoAllFeatures = false, cargoTarget = null, cargoManifests = false } = {}) {
|
|
448
|
-
if (!Array.isArray(entries) || entries.length === 0) {
|
|
449
|
-
throw new Error('buildRustBundle: at least one entry .rs file is required')
|
|
450
|
-
}
|
|
451
|
-
for (const e of entries) {
|
|
452
|
-
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))
|
|
453
323
|
}
|
|
324
|
+
assertSelfContained('TEAL', 'program', normalized, sources, [])
|
|
325
|
+
return assembleCodeBundle({ baseDir, entries: normalized, sources, workspaceName: 'teal-bundle', format: 'teal', conditionKey: 'teal' })
|
|
326
|
+
}
|
|
454
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)))
|
|
455
330
|
const baseDir = resolve(cwd)
|
|
456
331
|
const normalized = normalizeEntries(entries, cwd)
|
|
457
|
-
|
|
458
|
-
// One Cargo context for the walk, the edge pass and the bucketing: manifests are read once, and
|
|
459
|
-
// 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.
|
|
460
333
|
const cargoCtx = createCargoContext(baseDir, {
|
|
461
|
-
entries: normalized,
|
|
462
|
-
cargo,
|
|
463
|
-
features: cargoFeatures,
|
|
464
|
-
noDefaultFeatures: cargoNoDefaultFeatures,
|
|
465
|
-
allFeatures: cargoAllFeatures,
|
|
466
|
-
target: cargoTarget,
|
|
334
|
+
entries: normalized, cargo, features: cargoFeatures, noDefaultFeatures: cargoNoDefaultFeatures, allFeatures: cargoAllFeatures, target: cargoTarget,
|
|
467
335
|
})
|
|
468
|
-
// `include_str!` / `include_bytes!` assets ride along as resources: the walk records their
|
|
469
|
-
// format. With `cargoManifests`, each package's build script is walked too (collectRustBundle).
|
|
470
336
|
const { sources, formats, tree } = await collectRustBundle(baseDir, normalized, { cargo: cargoCtx, buildScripts: cargoManifests })
|
|
471
337
|
if (cargoManifests) {
|
|
472
|
-
//
|
|
473
|
-
// written: whatever they hold (a registry token, a `git` URL's credentials) is in the bundle
|
|
474
|
-
// 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.
|
|
475
339
|
const files = new Set()
|
|
476
340
|
for (const path of sources.keys()) for (const f of cargoCtx.buildFilesFor(path)) files.add(f.path)
|
|
477
341
|
const realBase = realpathSync(baseDir)
|
|
@@ -486,19 +350,12 @@ export async function buildRustBundle({ cwd = process.cwd(), entries, cargo = fa
|
|
|
486
350
|
}
|
|
487
351
|
}
|
|
488
352
|
const { resolutions, missing, unresolvedCrates } = tree
|
|
489
|
-
|
|
490
353
|
assertSelfContained('Rust', 'module', normalized, sources, missing)
|
|
491
|
-
|
|
492
|
-
// Where the features the cfg decisions rest on come from: cargo's resolver, or -- something it
|
|
493
|
-
// takes missing -- a replay of the manifests, said so (a project without Cargo has neither).
|
|
494
354
|
const resolution = cargoCtx.resolution()
|
|
495
355
|
if (resolution.mode === 'replay' && normalized.some((e) => cargoCtx.packageInfo(e) !== null)) {
|
|
496
356
|
console.warn(`[stasis] Rust features from a replay of the manifests, not cargo's resolver: ${resolution.why}`)
|
|
497
357
|
}
|
|
498
|
-
|
|
499
|
-
// context, so the replay, cargo's resolver and `cargo metadata` (which unifies dev/build deps
|
|
500
|
-
// like resolver 1) can be compared.
|
|
501
|
-
if (process.env.EXODUS_STASIS_DEBUG === '1' || process.env.EXODUS_STASIS_DEBUG === 'true') {
|
|
358
|
+
if (debugging()) {
|
|
502
359
|
const mode = { cargo: "cargo's resolver", metadata: 'cargo metadata', replay: 'manifest replay' }[resolution.mode]
|
|
503
360
|
for (const context of ['target', 'host']) {
|
|
504
361
|
const resolved = [...cargoCtx.resolvedFeatures(context)].toSorted(([a], [b]) => (a < b ? -1 : 1))
|
|
@@ -509,12 +366,6 @@ export async function buildRustBundle({ cwd = process.cwd(), entries, cargo = fa
|
|
|
509
366
|
}
|
|
510
367
|
}
|
|
511
368
|
}
|
|
512
|
-
|
|
513
|
-
// A crate the code names that the bundle doesn't hold: a registry dependency that isn't vendored
|
|
514
|
-
// (or not in a version its requirement allows), a path or patch outside the bundle root (the
|
|
515
|
-
// loader warned of those), or a crate root the walk refused -- and any other dependency a bundled
|
|
516
|
-
// package's build links that nothing in-tree answers, however the code names it, if at all.
|
|
517
|
-
// Reported whatever the vendor dir holds; with none, the fix is one command.
|
|
518
369
|
const notLoaded = tree.wantedRoots.filter((r) => !sources.has(r)).map((r) => `${cargoCtx.packageInfo(r)?.name ?? r} (${r})`)
|
|
519
370
|
const bundledPackages = new Set([...sources.keys()].map((p) => cargoCtx.packageInfo(p)?.dir).filter((d) => d !== undefined))
|
|
520
371
|
const lacking = cargoCtx.lackingDependencies(bundledPackages, { buildScripts: cargoManifests })
|
|
@@ -526,266 +377,216 @@ export async function buildRustBundle({ cwd = process.cwd(), entries, cargo = fa
|
|
|
526
377
|
console.warn(`[stasis] ${names.length} crate${names.length === 1 ? '' : 's'} referenced but not in the bundle: ${shown}`)
|
|
527
378
|
if (!existsSync(join(baseDir, cargoCtx.vendorDir))) console.warn('[stasis] Registry dependencies are bundled only when vendored in-tree: run `cargo vendor` first.')
|
|
528
379
|
}
|
|
529
|
-
|
|
530
380
|
return assembleCodeBundle({
|
|
531
|
-
baseDir,
|
|
532
|
-
entries: normalized,
|
|
533
|
-
sources,
|
|
534
|
-
resolutions,
|
|
535
|
-
workspaceName: RUST_WORKSPACE_NAME,
|
|
536
|
-
workspaceVersion: RUST_WORKSPACE_VERSION,
|
|
537
|
-
format: RUST_FORMAT,
|
|
538
|
-
formats,
|
|
539
|
-
conditionKey: 'rust',
|
|
381
|
+
baseDir, entries: normalized, sources, resolutions, workspaceName: 'rust-bundle', format: 'rust', formats, conditionKey: 'rust',
|
|
540
382
|
classifyDep: makeRustClassifier(cargoCtx),
|
|
541
383
|
})
|
|
542
384
|
}
|
|
543
385
|
|
|
544
|
-
//
|
|
545
|
-
// require/include graph (and Composer's class-autoload graph when present); no PHP is
|
|
546
|
-
// executed. Autoload resolution is best-effort; explicit require/include of a literal path
|
|
547
|
-
// 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.
|
|
548
387
|
export async function buildPhpBundle({ cwd = process.cwd(), entries } = {}) {
|
|
549
|
-
|
|
550
|
-
throw new Error('buildPhpBundle: at least one entry .php file is required')
|
|
551
|
-
}
|
|
552
|
-
for (const e of entries) {
|
|
553
|
-
if (!e.endsWith('.php')) throw new Error(`buildPhpBundle: not a .php file: ${e}`)
|
|
554
|
-
}
|
|
555
|
-
|
|
388
|
+
assertEntries('buildPhpBundle', entries, '.php file', (e) => e.endsWith('.php'))
|
|
556
389
|
const baseDir = resolve(cwd)
|
|
557
390
|
const normalized = normalizeEntries(entries, cwd)
|
|
558
391
|
const autoload = loadComposerAutoload(baseDir)
|
|
559
|
-
// The Composer packages, of composer.lock (vendor/composer/installed.json held identical to it
|
|
560
|
-
// where both are), or of installed.json alone: read once, before anything is scanned.
|
|
561
392
|
const packages = loadComposerPackages(baseDir)
|
|
562
|
-
|
|
563
|
-
// Laravel auto-discovers service providers rather than referencing them statically; seed
|
|
564
|
-
// 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.
|
|
565
394
|
const providerRoots = loadLaravelProviderFiles(baseDir, autoload, packages)
|
|
566
|
-
|
|
567
395
|
const sources = await collectPhpFilesFromDisk(baseDir, [...normalized, ...providerRoots], { autoload })
|
|
568
396
|
const { resolutions, missing } = buildPhpTree(sources, { baseDir, autoload })
|
|
569
|
-
|
|
570
397
|
assertSelfContained('PHP', 'import', normalized, sources, missing)
|
|
571
|
-
|
|
572
|
-
return codeBundle({
|
|
573
|
-
baseDir,
|
|
574
|
-
entries: normalized,
|
|
575
|
-
sources,
|
|
576
|
-
// Group per Composer package (vendor/<pkg>), not the node_modules bucketizer.
|
|
577
|
-
modules: bucketizePhpSources(baseDir, sources, PHP_WORKSPACE_NAME, PHP_WORKSPACE_VERSION, packages),
|
|
578
|
-
resolutions,
|
|
579
|
-
format: PHP_FORMAT,
|
|
580
|
-
// PHP includes don't vary by Node condition; key edges under "php", not the JS wildcard "*".
|
|
581
|
-
conditionKey: 'php',
|
|
582
|
-
})
|
|
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' })
|
|
583
400
|
}
|
|
584
401
|
|
|
585
|
-
// Render a scanned file URL for diagnostics: project-relative when inside `baseDir`, else absolute.
|
|
586
|
-
// The scan issue checks run on the LIVE scan, whose files/imports are keyed by absolute file: URLs
|
|
587
|
-
// (the resolver returns absolute paths); relativizing here matches the project-relative paths the
|
|
588
|
-
// bundle itself stores (toRel), rather than leaking the machine's absolute paths into the message.
|
|
589
402
|
function displayPath(url, baseDir) {
|
|
590
403
|
const abs = fileURLToPath(url)
|
|
591
404
|
const rel = relative(baseDir, abs)
|
|
592
405
|
return rel && !relativeEscapes(rel) ? rel : abs
|
|
593
406
|
}
|
|
594
407
|
|
|
595
|
-
// One unresolved scan edge for diagnostics, naming the file --typescript would resolve it to when
|
|
596
|
-
// the scan found one (Scan#typescriptCandidate).
|
|
597
408
|
function describeUnresolved(u, show) {
|
|
598
409
|
const typescript = u.typescriptURL ? `; resolves to ${show(u.typescriptURL)} under --typescript` : ''
|
|
599
410
|
return `${u.kind} ${u.spec ?? '<dynamic>'} from ${show(u.parentURL)} (${u.reason}${typescript})`
|
|
600
411
|
}
|
|
601
412
|
|
|
602
|
-
//
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
function
|
|
606
|
-
const
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
while (walk.length > 0) {
|
|
613
|
-
for (const e of scanner.files.get(walk.shift())?.edges ?? []) {
|
|
614
|
-
if (staticKinds.has(e.kind) && e.child && !staticReachable.has(e.child)) {
|
|
615
|
-
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)
|
|
616
423
|
walk.push(e.child)
|
|
617
424
|
}
|
|
618
425
|
}
|
|
619
426
|
}
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
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()) {
|
|
632
491
|
for (const [parentURL, specMap] of byParent) {
|
|
633
492
|
for (const [spec, childURL] of specMap) {
|
|
634
|
-
if (!scanner.files.has(childURL)) {
|
|
635
|
-
fatal.push(`${spec} from ${show(parentURL)} resolves to ${show(childURL)}, which a source bundle can't carry`)
|
|
636
|
-
}
|
|
493
|
+
if (!scanner.files.has(childURL)) fatal.push(`${spec} from ${show(parentURL)} resolves to ${show(childURL)}, which a source bundle can't carry`)
|
|
637
494
|
}
|
|
638
495
|
}
|
|
639
496
|
}
|
|
640
|
-
|
|
641
|
-
const toleratedParse = scanner.parseErrors.filter((p) => !fatalParse(p))
|
|
642
|
-
return { fatal, tolerated, toleratedParse }
|
|
497
|
+
return { fatal, tolerated, typescript }
|
|
643
498
|
}
|
|
644
499
|
|
|
645
|
-
|
|
646
|
-
// relativizes the paths in the warnings (see displayPath), matching analyzeScanner's messages.
|
|
647
|
-
function reportScanIssues({ fatal, tolerated, toleratedParse }, { label = '', baseDir }) {
|
|
500
|
+
function reportScanIssues({ fatal, tolerated, typescript, leftOut }, { label = '', baseDir, root }) {
|
|
648
501
|
const where = label ? ` (${label})` : ''
|
|
649
502
|
const show = (url) => displayPath(url, baseDir)
|
|
650
|
-
// The first 10 of `items` as indented lines (`line` renders one), then how many more there are.
|
|
651
|
-
const listed = (items, line = (s) => s) => {
|
|
652
|
-
const more = items.length > 10 ? `\n ... and ${items.length - 10} more` : ''
|
|
653
|
-
return items.slice(0, 10).map((x) => ` ${line(x)}`).join('\n') + more
|
|
654
|
-
}
|
|
655
503
|
if (fatal.length > 0) {
|
|
656
|
-
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 })
|
|
657
505
|
}
|
|
658
506
|
if (tolerated.length > 0) {
|
|
659
507
|
const summary = listed(tolerated, (u) => describeUnresolved(u, show))
|
|
660
508
|
console.warn(`[stasis] Bundle has ${tolerated.length} unresolved import(s)${where}; they will fall through at load time:\n${summary}`)
|
|
661
509
|
}
|
|
662
|
-
if (
|
|
663
|
-
const summary = listed(
|
|
664
|
-
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}`)
|
|
665
513
|
}
|
|
666
514
|
}
|
|
667
515
|
|
|
668
|
-
//
|
|
669
|
-
// token into the resolver.
|
|
516
|
+
// Trimmed, empties dropped, so a sloppy programmatic caller can't push a bogus token into the resolver.
|
|
670
517
|
const cleanConditions = (conditions) => conditions.map((c) => (typeof c === 'string' ? c.trim() : c)).filter(Boolean)
|
|
671
518
|
|
|
672
|
-
// --typescript
|
|
673
|
-
// exist), else those of the tsconfig.json beside each importing file's package.json
|
|
674
|
-
// (packageTsconfigPaths, the project read as the State reads it, to `env`'s PROJECT_CWD; null
|
|
675
|
-
// matcher = no aliases). Without --typescript they only hint a miss (Scan#typescriptCandidate), so a
|
|
676
|
-
// 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.
|
|
677
520
|
function typescriptPathsFor(typescript, baseDir, tsconfig, host, env) {
|
|
678
|
-
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
|
+
}
|
|
679
525
|
return packageTsconfigPaths(baseDir, { host, lenient: !typescript, projectCwd: env.PROJECT_CWD })
|
|
680
526
|
}
|
|
681
527
|
|
|
682
|
-
//
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
// extra `exports`/`imports` conditions; on their own they don't honour legacy mainFields or
|
|
686
|
-
// platform suffixes (see `--mainFields` / buildResolvedJsBundle). `typescript` maps a failed
|
|
687
|
-
// resolution to its on-disk TS source (tsc's rules; see resolve-typescript.js), honouring the
|
|
688
|
-
// `paths` aliases of `tsconfig` (an explicit config path, default each package's tsconfig.json); it
|
|
689
|
-
// is taken as given where the entries import TS sources by output name (importsTypescriptByOutputName).
|
|
690
|
-
// Files are read through `host` (@exodus/stasis-core/host), the disk by default; EXODUS_STASIS_*
|
|
691
|
-
// settings from `env`. With `innermostRoot`, the State is rooted at the innermost package at or
|
|
692
|
-
// above cwd that holds every file the scan reaches (innermostRootOf), rather than at the project's
|
|
693
|
-
// root above it, where that holds no stasis file of its own.
|
|
694
|
-
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 } = {}) {
|
|
695
|
-
if (!Array.isArray(entries) || entries.length === 0) {
|
|
696
|
-
throw new Error('buildJsBundle: at least one entry .js/.cjs/.mjs/.ts/.cts/.mts/.jsx/.tsx file is required')
|
|
697
|
-
}
|
|
698
|
-
for (const e of entries) {
|
|
699
|
-
if (!JS_EXTS.has(extname(e))) throw new Error(`buildJsBundle: not a JS/TS file: ${e}`)
|
|
700
|
-
}
|
|
701
|
-
|
|
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')
|
|
702
531
|
const baseDir = resolve(cwd)
|
|
703
532
|
const absEntries = entries.map((e) => resolve(baseDir, e))
|
|
704
|
-
|
|
705
533
|
const scanConditions = cleanConditions(conditions)
|
|
706
|
-
// --resources: extensions/filenames carried as opaque assets instead of failing "can't carry".
|
|
707
534
|
const resourceSet = parseResourcesOption('buildJsBundle', resources)
|
|
708
|
-
|
|
709
535
|
typescript ||= importsTypescriptByOutputName(absEntries, host)
|
|
710
536
|
const typescriptPaths = typescriptPathsFor(typescript, baseDir, tsconfig, host, env)
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
//
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
// Materialise via a non-preload State: addFile bucketizes + records sources/formats,
|
|
718
|
-
// addImport replays the edge map; serialize emits the runtime loader's v1 layout.
|
|
719
|
-
// bundle:'replace' skips reading any on-disk stasis.code.br (bundle:'add' would leak stale
|
|
720
|
-
// entries); lock:'ignore' tolerates a pre-existing lockfile without consuming it; and it is never
|
|
721
|
-
// written, so it claims no write target.
|
|
722
|
-
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 }
|
|
723
543
|
let state = new State(baseDir, { ...stateOptions, env })
|
|
724
544
|
// A root holding a stasis file is the project's own choice, whose config the build takes.
|
|
725
545
|
if (innermostRoot && !STASIS_ROOT_FILES.some((file) => host.stat(join(state.root, file)) !== null)) {
|
|
726
|
-
const root = innermostRootOf(baseDir, [...
|
|
546
|
+
const root = innermostRootOf(baseDir, [...scanned.files.keys()].map((url) => host.realpath(fileURLToPath(url))), state.root, host)
|
|
727
547
|
// The State's walk up from cwd stops at PROJECT_CWD, as it does where yarn sets it.
|
|
728
548
|
if (root !== state.root) state = new State(baseDir, { ...stateOptions, env: { ...env, PROJECT_CWD: root } })
|
|
729
549
|
}
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
for (const url of
|
|
733
|
-
|
|
734
|
-
if (!relativeEscapes(relative(state.root, file))) continue
|
|
735
|
-
const markers = STASIS_ROOT_FILES.filter((name) => host.stat(join(state.root, name)) !== null)
|
|
736
|
-
const rootedBy = markers.length === 0 ? '' : ` (rooted there by its ${markers.join(' and ')}, which can be removed if stale)`
|
|
737
|
-
throw new Error(`buildJsBundle: ${relative(baseDir, file)} is outside the project root at ${relative(baseDir, state.root) || '.'}${rootedBy}`)
|
|
738
|
-
}
|
|
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 })
|
|
739
554
|
for (const [url, info] of scanner.files) {
|
|
740
|
-
|
|
741
|
-
// and stores it under `resources` (a resource can't be an entry, so no isEntry).
|
|
742
|
-
if (info.resource) {
|
|
743
|
-
state.addFile(url, { resource: true })
|
|
744
|
-
continue
|
|
745
|
-
}
|
|
746
|
-
const isEntry = scanner.entries.has(url)
|
|
747
|
-
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' })
|
|
748
556
|
}
|
|
749
|
-
//
|
|
750
|
-
//
|
|
751
|
-
|
|
752
|
-
// 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}`))
|
|
753
560
|
const byParent = new Map()
|
|
754
561
|
for (const [key, parents] of scanner.imports) {
|
|
755
562
|
for (const [parentURL, specs] of parents) {
|
|
756
|
-
|
|
757
|
-
const bySpec = byParent.get(parentURL)
|
|
758
|
-
for (const [spec, childURL] of specs) {
|
|
759
|
-
if (!bySpec.has(spec)) bySpec.set(spec, new Map())
|
|
760
|
-
bySpec.get(spec).set(key, childURL)
|
|
761
|
-
}
|
|
563
|
+
for (const [spec, childURL] of specs) nestedMap(nestedMap(byParent, parentURL), spec).set(key, childURL)
|
|
762
564
|
}
|
|
763
565
|
}
|
|
764
566
|
for (const [parentURL, bySpec] of byParent) {
|
|
765
567
|
for (const [spec, byKey] of bySpec) {
|
|
766
568
|
const targets = new Set(byKey.values())
|
|
767
|
-
if (targets.size === 1) {
|
|
569
|
+
if (targets.size === 1 && !missed.has(`${parentURL}\0${spec}`)) {
|
|
768
570
|
state.addImport(parentURL, spec, [...targets][0], { conditions: '*' })
|
|
769
571
|
} else {
|
|
770
|
-
for (const [key, childURL] of byKey) {
|
|
771
|
-
state.addImport(parentURL, spec, childURL, { conditions: key.split(', ') })
|
|
772
|
-
}
|
|
572
|
+
for (const [key, childURL] of byKey) state.addImport(parentURL, spec, childURL, { conditions: key.split(', ') })
|
|
773
573
|
}
|
|
774
574
|
}
|
|
775
575
|
}
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
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()
|
|
779
578
|
return state
|
|
780
579
|
}
|
|
781
580
|
|
|
782
581
|
// The files that root a State in the directory holding one (state.js's discovery).
|
|
783
|
-
const STASIS_ROOT_FILES = [
|
|
582
|
+
const STASIS_ROOT_FILES = [CONFIG_FILE, LOCK_FILE, BUNDLE_FILE]
|
|
583
|
+
|
|
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
|
+
}
|
|
784
588
|
|
|
785
|
-
//
|
|
786
|
-
// paths) and a package.json with a name, which a State can be rooted at; `root` where none below it
|
|
787
|
-
// does. One without a name, such as a `{"type":"module"}` marker, is no root: the State takes such a
|
|
788
|
-
// file's package to be the named one above it, which it then must hold.
|
|
589
|
+
// A package.json with no name (a `{"type":"module"}` marker) roots no State.
|
|
789
590
|
function innermostRootOf(baseDir, files, root, host) {
|
|
790
591
|
for (let dir = baseDir; dir !== root && dirname(dir) !== dir; dir = dirname(dir)) {
|
|
791
592
|
if (files.every((file) => isPathWithin(dir, file)) && readPackageJson(dir, 'package.json', { host })?.name !== undefined) return dir
|
|
@@ -793,605 +594,1614 @@ function innermostRootOf(baseDir, files, root, host) {
|
|
|
793
594
|
return root
|
|
794
595
|
}
|
|
795
596
|
|
|
796
|
-
// Extensions probed when a resolved target names none, in Metro's default sourceExts order.
|
|
797
|
-
// Limited to what a source bundle can carry (scan's RESOLVABLE_EXTS) so the resolver never
|
|
798
|
-
// resolves a file the bundle would reject; an extensionless `import './Foo'` can land on
|
|
799
|
-
// Foo.jsx/Foo.tsx, as it does under Metro. Like Metro's default, this doesn't track a project's
|
|
800
|
-
// own sourceExts.
|
|
801
|
-
const SOURCE_EXTS = ['js', 'jsx', 'json', 'ts', 'tsx']
|
|
802
|
-
// React Native preset mainFields for `--metro` (which also sets the RN conditions + platform suffixes).
|
|
803
597
|
const METRO_MAIN_FIELDS = ['react-native', 'browser', 'main']
|
|
804
|
-
|
|
805
|
-
//
|
|
806
|
-
const
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
//
|
|
811
|
-
|
|
812
|
-
|
|
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.
|
|
625
|
+
const BUNDLER_CONDITIONS = ['module']
|
|
626
|
+
|
|
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) {
|
|
813
738
|
try {
|
|
814
|
-
|
|
739
|
+
return host.resolve(join(dir, 'package.json'), spec, NODE_REQUIRE_CONDITIONS)
|
|
815
740
|
} catch {
|
|
816
|
-
return
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
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)
|
|
825
876
|
}
|
|
826
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
|
+
})
|
|
827
893
|
}
|
|
828
894
|
|
|
829
|
-
// The
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
const manifests = []
|
|
839
|
-
walkNative(pkgAbs, manifests, host, isNativeManifest, isExcludedNativeDir)
|
|
840
|
-
const hasIos = host.stat(join(pkgAbs, 'ios')) !== null
|
|
841
|
-
const hasAndroid = host.stat(join(pkgAbs, 'android')) !== null
|
|
842
|
-
if (!hasIos && !hasAndroid && !manifests.some((f) => isPodspec(f))) return []
|
|
843
|
-
const out = [...manifests]
|
|
844
|
-
if (hasIos) walkNative(join(pkgAbs, 'ios'), out, host, isNativeSource)
|
|
845
|
-
if (hasAndroid) walkNative(join(pkgAbs, 'android'), out, host, isNativeSource)
|
|
846
|
-
return [...new Set(out)]
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
// The built-in field/suffix resolver (resolve-fields.js) the legacy-field build resolves with on
|
|
850
|
-
// `platform` (null for --mainFields), its `mainFields` (Metro's under --metro), and the conditions
|
|
851
|
-
// it adds to Node's: --metro asserts the RN conditions (+ browser on web); --mainFields carries the
|
|
852
|
-
// user's --conditions.
|
|
853
|
-
export function fieldResolverFor(platform, { mainFields, metro = false, conditions = [], typescript = false, typescriptPaths = null, host = diskHost }) {
|
|
854
|
-
const extras = metro ? ['react-native', ...(platform === 'web' ? ['browser'] : [])] : conditions
|
|
855
|
-
const fields = metro ? METRO_MAIN_FIELDS : mainFields
|
|
856
|
-
const resolver = createFieldResolver({
|
|
857
|
-
mainFields: fields,
|
|
858
|
-
platform,
|
|
859
|
-
preferNative: platform !== null && platform !== 'web',
|
|
860
|
-
sourceExts: SOURCE_EXTS,
|
|
861
|
-
conditions: resolveConditions('commonjs', extras),
|
|
862
|
-
// Opt into Metro's package-entry browser-field quirks only on the --metro path.
|
|
863
|
-
metro,
|
|
864
|
-
// --typescript: tsc's mapping, inside the field resolver (the scanner's own fallback
|
|
865
|
-
// only backs the built-in resolver). Unreachable under --metro-resolver
|
|
866
|
-
// (classifyEntries rejects the combination -- metro-resolver can't substitute).
|
|
867
|
-
typescript,
|
|
868
|
-
typescriptPaths,
|
|
869
|
-
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)
|
|
870
904
|
})
|
|
871
|
-
|
|
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
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
|
|
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
|
|
872
1056
|
}
|
|
873
1057
|
|
|
874
|
-
//
|
|
875
|
-
//
|
|
876
|
-
|
|
877
|
-
async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, entries, mainFields, platforms, conditions = [], metro = false, metroResolver = false, jsx = false, flow = false, typescript = false, tsconfig, resources = [], packageJSON = false, host = diskHost }) {
|
|
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 }) {
|
|
878
1061
|
const baseDir = resolve(cwd)
|
|
879
1062
|
const absEntries = entries.map((e) => resolve(baseDir, e))
|
|
880
1063
|
const normalized = normalizeEntries(entries, cwd)
|
|
881
1064
|
|
|
882
|
-
|
|
883
|
-
|
|
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.)
|
|
1067
|
+
let realRoot
|
|
1068
|
+
const fromRoot = memo ? nestedMap(memo, 'paths from the root') : new Map()
|
|
1069
|
+
const relWithin = (abs) => upsert(fromRoot, abs, () => {
|
|
1070
|
+
let rel = toPosix(relative(baseDir, abs))
|
|
884
1071
|
if (relativeEscapes(rel)) {
|
|
885
|
-
|
|
1072
|
+
realRoot ??= host.realpath(baseDir)
|
|
1073
|
+
rel = toPosix(relative(realRoot, abs))
|
|
886
1074
|
}
|
|
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}`)
|
|
887
1082
|
return rel
|
|
888
1083
|
}
|
|
889
1084
|
|
|
890
1085
|
const scanConditions = cleanConditions(conditions)
|
|
891
|
-
|
|
892
|
-
// Taken as given where the entries import TS sources by output name, but under --metro-resolver,
|
|
893
|
-
// which has no tsc mapping (classifyEntries rejects --typescript there).
|
|
1086
|
+
// Not under --metro-resolver, which has no tsc mapping.
|
|
894
1087
|
if (!metroResolver) typescript ||= importsTypescriptByOutputName(absEntries, host)
|
|
895
|
-
// --typescript's tsconfig `paths` matcher, shared by every per-platform resolver below.
|
|
896
1088
|
const typescriptPaths = typescriptPathsFor(typescript, baseDir, tsconfig, host, env)
|
|
897
|
-
|
|
898
|
-
const resourceSet = parseResourcesOption('buildResolvedJsBundle', resources)
|
|
1089
|
+
const { resources: resourceSet, packageJSON: withManifests } = fieldBuildOptions('buildResolvedJsBundle', { cwd, scope, resources, packageJSON, host })
|
|
899
1090
|
|
|
900
1091
|
const formatsByRel = new Map()
|
|
901
|
-
// Reached files the scanner tagged as resources (rel keys); read as bytes below, not UTF-8 source.
|
|
902
1092
|
const resourceRels = new Set()
|
|
903
1093
|
// parentRel -> specifier -> Map<platformKey, targetRel>; collapsed after all platforms scanned.
|
|
904
1094
|
const edges = new Map()
|
|
905
|
-
const reached = new Set()
|
|
1095
|
+
const reached = new Set()
|
|
906
1096
|
let usesEmpty = false
|
|
907
|
-
|
|
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
|
+
}
|
|
908
1122
|
for (const platform of platforms) {
|
|
909
|
-
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') })
|
|
910
1125
|
const { extras } = field
|
|
911
|
-
//
|
|
912
|
-
// fidelity; otherwise the built-in field/suffix resolver reproduces it. metro-resolver derives
|
|
913
|
-
// default/require|import/platform conditions itself, so it takes only the extra `react-native`
|
|
914
|
-
// 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.
|
|
915
1127
|
const resolver = metroResolver
|
|
916
|
-
? createMetroResolver({ projectDir: baseDir, platform, sourceExts
|
|
1128
|
+
? createMetroResolver({ projectDir: baseDir, platform, sourceExts, mainFields, conditionNames: ['react-native'], assetExts, assetResolutions, host })
|
|
917
1129
|
: field.resolver
|
|
918
|
-
// Without --typescript, the
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
const
|
|
925
|
-
|
|
926
|
-
const
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
const
|
|
939
|
-
|
|
940
|
-
|
|
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
|
+
}
|
|
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))
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
|
|
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())
|
|
941
1308
|
}
|
|
942
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
|
+
}
|
|
943
1322
|
}
|
|
944
1323
|
|
|
945
|
-
|
|
946
|
-
|
|
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.
|
|
947
1329
|
const sources = new Map()
|
|
948
|
-
const
|
|
949
|
-
// Carry `rel`: `content` in the bundle as `format`, the integrity of its bytes `buf` in the lockfile.
|
|
950
|
-
const put = (rel, content, format, buf) => {
|
|
1330
|
+
const put = (rel, content, format) => {
|
|
951
1331
|
sources.set(rel, content)
|
|
952
1332
|
formatsByRel.set(rel, format)
|
|
953
|
-
integrities.set(rel, sha512integrity(buf))
|
|
954
1333
|
}
|
|
955
|
-
|
|
956
|
-
//
|
|
957
|
-
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
|
+
}
|
|
958
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)
|
|
959
1343
|
for (const abs of reached) {
|
|
960
1344
|
const rel = toRel(abs)
|
|
961
|
-
// Security:
|
|
962
|
-
// escaping the root would slip past toRel's textual check
|
|
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.
|
|
963
1347
|
assertRealPathWithinBase(realBase, baseDir, rel, host)
|
|
964
1348
|
const buf = host.readFile(abs)
|
|
965
|
-
if (resourceRels.has(rel))
|
|
966
|
-
|
|
967
|
-
} else {
|
|
968
|
-
if (!isUtf8(buf)) throw new Error(`JS bundle source is not valid UTF-8: ${rel}`)
|
|
969
|
-
// Its format is the scan's.
|
|
970
|
-
put(rel, buf.toString('utf8'), formatsByRel.get(rel), buf)
|
|
971
|
-
}
|
|
1349
|
+
if (resourceRels.has(rel)) putResource(rel, buf)
|
|
1350
|
+
else putCode(rel, buf, formatsByRel.get(rel))
|
|
972
1351
|
}
|
|
973
|
-
if (usesEmpty) {
|
|
974
|
-
// Refuse to shadow a real reached file sitting at the reserved empty-module path.
|
|
1352
|
+
if (usesEmpty && metroEmpty === null) {
|
|
975
1353
|
if (formatsByRel.has(EMPTY_MODULE_PATH)) {
|
|
976
1354
|
throw new Error(`Bundle needs the reserved empty-module path ${EMPTY_MODULE_PATH}, but the project has a real file there`)
|
|
977
1355
|
}
|
|
978
|
-
put(EMPTY_MODULE_PATH, '', 'commonjs'
|
|
1356
|
+
put(EMPTY_MODULE_PATH, '', metro ? undefined : 'commonjs')
|
|
979
1357
|
}
|
|
980
1358
|
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
//
|
|
1359
|
+
const packageOf = packageLookup(baseDir, { host })
|
|
1360
|
+
|
|
1361
|
+
// --metro also carries the native build inputs of each native package the app links, as the Metro plugin does.
|
|
984
1362
|
if (metro) {
|
|
985
|
-
const pkgDirs = new
|
|
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.
|
|
1365
|
+
const isCode = (rel) => !resourceRels.has(rel)
|
|
986
1366
|
for (const abs of reached) {
|
|
987
|
-
// Follow the CODE/module graph only: a package reached solely for an asset (--resources) is
|
|
988
|
-
// not a linked native dependency, so it must not drag in its ios/android surface.
|
|
989
1367
|
const rel = toRel(abs)
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
if (nm) pkgDirs.add(nm.dir)
|
|
1368
|
+
const nm = isCode(rel) && !configOnly.has(rel) && splitNodeModulesPath(rel)
|
|
1369
|
+
if (nm) pkgDirs.set(nm.dir, nm.name)
|
|
993
1370
|
}
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
const
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
const
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
}
|
|
1017
|
-
|
|
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) => {
|
|
1374
|
+
const parent = dirname(dir)
|
|
1375
|
+
const links = new Set(parent === dir ? [] : linkedFrom(parent))
|
|
1376
|
+
const link = (abs) => {
|
|
1377
|
+
try {
|
|
1378
|
+
const rel = toRel(host.realpath(abs))
|
|
1379
|
+
if (!splitNodeModulesPath(rel)) links.add(rel)
|
|
1380
|
+
} catch { /* a dangling link, or one out of the root, whose files the scan pass refused already */ }
|
|
1381
|
+
}
|
|
1382
|
+
// As locatePackage: a directory named node_modules holds no node_modules of its own to look in.
|
|
1383
|
+
if (basename(dir) !== 'node_modules') {
|
|
1384
|
+
const nodeModules = join(dir, 'node_modules')
|
|
1385
|
+
for (const ent of listing(nodeModules, host)) {
|
|
1386
|
+
const abs = join(nodeModules, ent.name)
|
|
1387
|
+
if (ent.name.startsWith('@')) {
|
|
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))
|
|
1390
|
+
} else if (ent.isSymbolicLink()) {
|
|
1391
|
+
link(abs)
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
return links
|
|
1396
|
+
})
|
|
1397
|
+
for (const [parent, bySpec] of edges) {
|
|
1398
|
+
for (const byPlatform of bySpec.values()) {
|
|
1399
|
+
for (const target of byPlatform.values()) {
|
|
1400
|
+
if (!isCode(target) || splitNodeModulesPath(target)) continue
|
|
1401
|
+
const links = linkedFrom(dirname(join(baseDir, parent)))
|
|
1402
|
+
// The nearest linked root: the target may lie below a manifest of its own (a named dist/package.json).
|
|
1403
|
+
let root = posix.dirname(target)
|
|
1404
|
+
while (root !== '.' && !links.has(root)) root = posix.dirname(root)
|
|
1405
|
+
if (root === '.' || parent.startsWith(`${root}/`) || pkgDirs.has(root)) continue
|
|
1406
|
+
// Named by its own manifest, whatever name it's linked under.
|
|
1407
|
+
pkgDirs.set(root, readPackageJson(baseDir, `${root}/package.json`, { host })?.name)
|
|
1018
1408
|
}
|
|
1019
1409
|
}
|
|
1020
|
-
|
|
1021
|
-
|
|
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
|
+
}
|
|
1425
|
+
for (const [pkgDir, name] of [...pkgDirs].toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
|
|
1426
|
+
const pkgAbs = join(baseDir, pkgDir)
|
|
1427
|
+
// react-native core isn't a Pod (config reports it via reactNativePath), but its native source is walked too.
|
|
1428
|
+
const isRnCore = name === 'react-native'
|
|
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)
|
|
1433
|
+
}
|
|
1434
|
+
// RN core's `.js` build scripts, never in the graph, which the podspec runs at pod-install.
|
|
1022
1435
|
if (isRnCore) {
|
|
1023
1436
|
for (const file of RN_CORE_INCLUDE_FILES) {
|
|
1024
1437
|
const abs = join(pkgAbs, file)
|
|
1025
|
-
if (host.stat(abs) === null) continue
|
|
1026
|
-
|
|
1027
|
-
if (sources.has(rel)) continue
|
|
1028
|
-
assertRealPathWithinBase(realBase, baseDir, rel, host)
|
|
1029
|
-
const buf = host.readFile(abs)
|
|
1030
|
-
if (!isUtf8(buf)) throw new Error(`native source is not valid UTF-8: ${rel}`)
|
|
1031
|
-
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')
|
|
1032
1440
|
}
|
|
1033
1441
|
}
|
|
1034
1442
|
}
|
|
1035
1443
|
}
|
|
1036
1444
|
|
|
1037
|
-
// --package-json:
|
|
1038
|
-
//
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
// with the State path (containment, UTF-8-aborts); no identity check here -- these buckets are
|
|
1043
|
-
// all fresh from disk.
|
|
1044
|
-
const packageOf = packageLookup(baseDir, { host })
|
|
1045
|
-
if (packageJSON) {
|
|
1046
|
-
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()
|
|
1047
1450
|
for (const abs of reached) {
|
|
1048
1451
|
const rel = toRel(abs)
|
|
1049
|
-
const
|
|
1050
|
-
if (
|
|
1051
|
-
|
|
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
|
+
}
|
|
1052
1457
|
}
|
|
1053
|
-
for (const
|
|
1054
|
-
const rel = moduleFileKey(pkgDir, 'package.json')
|
|
1458
|
+
for (const [rel, of] of manifests) {
|
|
1055
1459
|
if (sources.has(rel)) continue
|
|
1056
1460
|
const buf = readModuleManifest({ baseDir, realBase, rel, host })
|
|
1057
1461
|
if (!buf) continue
|
|
1058
|
-
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)
|
|
1059
1466
|
}
|
|
1060
1467
|
}
|
|
1061
1468
|
|
|
1062
|
-
//
|
|
1063
|
-
const
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
}
|
|
1073
|
-
|
|
1074
|
-
|
|
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
|
+
)
|
|
1075
1482
|
}
|
|
1076
1483
|
|
|
1077
1484
|
const rootPkg = readJson(join(baseDir, 'package.json'), host) ?? {}
|
|
1078
|
-
const
|
|
1079
|
-
baseDir,
|
|
1080
|
-
|
|
1081
|
-
sources,
|
|
1082
|
-
resolutions,
|
|
1083
|
-
formats: formatsByRel,
|
|
1084
|
-
workspaceName: rootPkg.name ?? 'workspace',
|
|
1085
|
-
workspaceVersion: rootPkg.version ?? '0.0.0',
|
|
1086
|
-
conditionKey: '*',
|
|
1087
|
-
host,
|
|
1088
|
-
packageOf,
|
|
1089
|
-
})
|
|
1090
|
-
|
|
1091
|
-
// The companion lockfile mirrors the bundle, swapping file content for its integrity.
|
|
1092
|
-
const lockModules = new Map()
|
|
1093
|
-
for (const [dir, m] of bundle.modules) {
|
|
1094
|
-
const files = Object.create(null)
|
|
1095
|
-
for (const rel of Object.keys(m.files)) files[rel] = integrities.get(moduleFileKey(dir, rel))
|
|
1096
|
-
lockModules.set(dir, moduleInfo({ ...m, files }))
|
|
1097
|
-
}
|
|
1098
|
-
const lockfile = new Lockfile({
|
|
1099
|
-
config: bundle.config,
|
|
1100
|
-
entries: bundle.entries,
|
|
1101
|
-
modules: lockModules,
|
|
1102
|
-
imports: bundle.imports,
|
|
1103
|
-
formats: bundle.formats,
|
|
1104
|
-
// Same file set as the bundle, so the same executable list attests it.
|
|
1105
|
-
executable: bundle.executable,
|
|
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)),
|
|
1106
1489
|
})
|
|
1107
|
-
|
|
1108
|
-
|
|
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) }
|
|
1109
1494
|
}
|
|
1110
1495
|
|
|
1111
|
-
//
|
|
1112
|
-
//
|
|
1113
|
-
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 }) {
|
|
1114
|
-
if (!
|
|
1115
|
-
throw new Error(`${name}: at least one entry file is required`)
|
|
1116
|
-
}
|
|
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`)
|
|
1117
1500
|
const dirError = directoryEntryError(entries, cwd, host, { fetched })
|
|
1118
1501
|
if (dirError !== null) throw new Error(`${name}: ${dirError}`)
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
}
|
|
1135
|
-
|
|
1136
|
-
//
|
|
1137
|
-
if (
|
|
1138
|
-
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 }
|
|
1139
|
-
for (const [flag, on] of Object.entries(given)) if (on) throw new Error(`${name}: --${flag} is only valid for Rust bundles`)
|
|
1140
|
-
}
|
|
1141
|
-
if (scope !== undefined && kind !== 'js') {
|
|
1142
|
-
throw new Error(`${name}: --scope is only valid for JS bundles`)
|
|
1143
|
-
}
|
|
1144
|
-
if (lockfile !== undefined && kind !== 'js') {
|
|
1145
|
-
throw new Error(`${name}: --lockfile is only valid for JS bundles`)
|
|
1146
|
-
}
|
|
1147
|
-
// Reject --conditions for non-JS: their resolvers don't consult exports/imports conditions, so it would silently do nothing.
|
|
1148
|
-
if (Array.isArray(conditions) && conditions.length > 0 && kind !== 'js') {
|
|
1149
|
-
throw new Error(`${name}: --conditions is only valid for JS bundles`)
|
|
1150
|
-
}
|
|
1151
|
-
// --flow strips Flow type syntax before the JS parser; the non-JS loaders never parse with oxc.
|
|
1152
|
-
if (flow && kind !== 'js') {
|
|
1153
|
-
throw new Error(`${name}: --flow is only valid for JS bundles`)
|
|
1154
|
-
}
|
|
1155
|
-
// --typescript maps JS-output specifiers to their on-disk TS sources; only the JS resolvers do it.
|
|
1156
|
-
if (typescript && kind !== 'js') {
|
|
1157
|
-
throw new Error(`${name}: --typescript is only valid for JS bundles`)
|
|
1158
|
-
}
|
|
1159
|
-
// --tsconfig names the config whose `paths` aliases --typescript honours; meaningless without it.
|
|
1160
|
-
if (tsconfig !== undefined && !typescript) {
|
|
1161
|
-
throw new Error(`${name}: --tsconfig is only valid with --typescript`)
|
|
1162
|
-
}
|
|
1163
|
-
// --resources carries allowlisted assets reached through the JS import graph; JS-only. The list
|
|
1164
|
-
// is validated (parseResourcesOption) in the builders, but reject a code extension here too so a
|
|
1165
|
-
// typo surfaces with the command name rather than deep in the scan.
|
|
1166
|
-
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)) {
|
|
1167
1521
|
if (kind !== 'js') throw new Error(`${name}: --resources is only valid for JS bundles`)
|
|
1168
1522
|
parseResourcesOption(name, resources)
|
|
1169
1523
|
}
|
|
1170
|
-
//
|
|
1171
|
-
|
|
1172
|
-
throw new Error(`${name}: --mainFields is only valid for JS bundles`)
|
|
1173
|
-
}
|
|
1174
|
-
// --jsx toggles the scanner's JSX parsing for the .js family (.jsx/.tsx need no flag); only JS
|
|
1175
|
-
// entries are scanned.
|
|
1176
|
-
if (jsx && kind !== 'js') {
|
|
1177
|
-
throw new Error(`${name}: --jsx is only valid for JS bundles`)
|
|
1178
|
-
}
|
|
1179
|
-
// --package-json folds each bundled module's npm package.json in; JS-only (the sol/php/bash/rust
|
|
1180
|
-
// bucketizers group by their own ecosystems' manifests, not an npm package.json).
|
|
1181
|
-
if (packageJSON && kind !== 'js') {
|
|
1182
|
-
throw new Error(`${name}: --package-json is only valid for JS bundles`)
|
|
1183
|
-
}
|
|
1184
|
-
if ((metro || (Array.isArray(platforms) && platforms.length > 0)) && kind !== 'js') {
|
|
1185
|
-
throw new Error(`${name}: --metro is only valid for JS bundles`)
|
|
1186
|
-
}
|
|
1187
|
-
// --metro presets conditions + mainFields + platform suffixes, so it can't combine with
|
|
1188
|
-
// 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) })
|
|
1189
1526
|
if (metro) {
|
|
1190
|
-
if (
|
|
1191
|
-
|
|
1192
|
-
}
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
}
|
|
1196
|
-
if (!Array.isArray(platforms) || platforms.length === 0) {
|
|
1197
|
-
throw new Error(`${name}: --metro requires --platforms (e.g. --platforms=ios,android)`)
|
|
1198
|
-
}
|
|
1199
|
-
// A platform name becomes an edge key: reject '/' (Bundle/Lockfile parse refuse it) and
|
|
1200
|
-
// the reserved '*' placeholder, so the writer never emits a map the readers reject.
|
|
1201
|
-
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 : []) {
|
|
1202
1532
|
if (typeof p !== 'string' || p.length === 0 || p === '*' || p.includes('/')) {
|
|
1203
1533
|
throw new Error(`${name}: invalid platform '${p}' (a platform name can't be empty, contain '/', or be '*')`)
|
|
1204
1534
|
}
|
|
1205
1535
|
}
|
|
1206
|
-
} else if (
|
|
1536
|
+
} else if (nonEmpty(platforms) && !partial) {
|
|
1207
1537
|
throw new Error(`${name}: --platforms is only valid with --metro`)
|
|
1208
1538
|
}
|
|
1209
|
-
|
|
1210
|
-
if (
|
|
1211
|
-
|
|
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)`)
|
|
1212
1545
|
}
|
|
1213
|
-
// metro-resolver has no TS extension substitution, so --typescript would silently not apply -- reject it.
|
|
1214
1546
|
if (typescript && metroResolver) {
|
|
1215
1547
|
throw new Error(`${name}: --typescript is not supported with --metro-resolver (the project's metro-resolver doesn't substitute .js -> .ts)`)
|
|
1216
1548
|
}
|
|
1217
|
-
//
|
|
1218
|
-
if (
|
|
1219
|
-
throw new Error(`${name}: --scope is not supported with --mainFields or --metro`)
|
|
1220
|
-
}
|
|
1549
|
+
// Refused before anything is built (or fetched).
|
|
1550
|
+
if (mainFields !== undefined || metro) fieldBuildOptions(name, { cwd, scope, resources, host })
|
|
1221
1551
|
return kind
|
|
1222
1552
|
}
|
|
1223
1553
|
|
|
1224
|
-
//
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
// relative to: cwd for the resolver's, the State's root for the State's.
|
|
1228
|
-
// -> { bundle, lockfile: () => Lockfile, stateBuilt, root }
|
|
1229
|
-
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 } }
|
|
1230
1557
|
if (metro || mainFields !== undefined) {
|
|
1231
|
-
const built = await buildResolvedJsBundle({ ...options, mainFields: metro ? METRO_MAIN_FIELDS : mainFields, platforms: metro ? platforms : [null], metro: Boolean(metro), metroResolver: Boolean(metroResolver) })
|
|
1232
|
-
return { bundle: built.bundle, lockfile:
|
|
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))))
|
|
1574
|
+
}
|
|
1575
|
+
|
|
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) },
|
|
1233
1629
|
}
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
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
|
+
})
|
|
1237
1661
|
}
|
|
1238
1662
|
|
|
1239
|
-
//
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
return buildJs({ cwd, env, entries, scope, conditions, mainFields, platforms, metro, metroResolver, jsx, flow, typescript, tsconfig, resources, packageJSON })
|
|
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))
|
|
1247
1670
|
}
|
|
1248
1671
|
|
|
1249
|
-
//
|
|
1250
|
-
// project's, or an empty tree's with `fetched: false`): no metro-resolver, which reads the disk;
|
|
1251
|
-
// `generate` only for JS.
|
|
1252
|
-
// -> the kind of bundle its entries make
|
|
1672
|
+
// Before anything is fetched (`host` an empty tree's, with `fetched: false`). -> the kind of bundle
|
|
1253
1673
|
export function checkVfsOptions(name, options) {
|
|
1254
1674
|
const kind = classifyEntries(name, options)
|
|
1255
1675
|
if (options.metroResolver) throw new Error(`${name}: metroResolver is not supported`)
|
|
1256
|
-
|
|
1257
|
-
if (
|
|
1258
|
-
|
|
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`)
|
|
1259
1681
|
return kind
|
|
1260
1682
|
}
|
|
1261
1683
|
|
|
1262
|
-
//
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
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`)
|
|
1282
1746
|
// Checked as the Bundle checks it, before anything is fetched.
|
|
1283
1747
|
if (repo !== undefined) repo = new Bundle({ repo }).repo
|
|
1284
|
-
const project = vfsHost(vfs)
|
|
1285
1748
|
// A real path, as every file the scan reaches is.
|
|
1286
1749
|
cwd = project.realpath(posix.resolve('/', cwd))
|
|
1287
|
-
|
|
1288
|
-
const
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
//
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
const
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
const
|
|
1305
|
-
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)
|
|
1306
1880
|
if (repo !== undefined) bundle.repo = repo
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
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) }
|
|
1310
1886
|
}
|
|
1311
1887
|
|
|
1312
|
-
//
|
|
1313
|
-
//
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
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
|
|
1318
1893
|
}
|
|
1319
1894
|
|
|
1320
|
-
|
|
1321
|
-
// (stasis.code.br by default, `-` for stdout; the summary goes to stderr so it never
|
|
1322
|
-
// interleaves with binary stdout). An optional JS `lockfile` attests every bundled file;
|
|
1323
|
-
// with `--conditions` it attests the conditions-selected graph, so a plain
|
|
1324
|
-
// `stasis run --lock=frozen` (which doesn't replay them) fails closed -- pair it with
|
|
1325
|
-
// `--bundle=load` or replay the conditions. `add` unions the fresh build into the bundle
|
|
1326
|
-
// already on disk (strict; a conflicting file throws) and can't target stdout.
|
|
1327
|
-
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 } = {}) {
|
|
1328
|
-
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 }
|
|
1329
|
-
const kind = classifyEntries('bundleCommand', { ...options, lockfile })
|
|
1895
|
+
export const debugging = () => process.env.EXODUS_STASIS_DEBUG === '1' || process.env.EXODUS_STASIS_DEBUG === 'true'
|
|
1330
1896
|
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
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
|
|
1334
1905
|
}
|
|
1906
|
+
}
|
|
1907
|
+
const seconds = (ms) => `${(ms / 1000).toFixed(2)} s`
|
|
1335
1908
|
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
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)
|
|
1340
1949
|
const root = built.root ?? resolve(cwd)
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
// Only a JS bundle has a lockfile (classifyEntries refuses one for the others).
|
|
1346
|
-
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
|
+
}
|
|
1347
1954
|
|
|
1348
|
-
|
|
1349
|
-
|
|
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
|
|
1350
1972
|
|
|
1351
|
-
// --add: union the fresh build into the existing on-disk bundle; a conflicting file throws. Skipped when nothing is on disk.
|
|
1352
1973
|
const outAbs = target === '-' ? undefined : resolve(cwd, target)
|
|
1353
1974
|
const lockAbs = lockData ? resolve(cwd, lockfile) : undefined
|
|
1354
|
-
//
|
|
1975
|
+
// What the bundle on disk carried; undefined where nothing is merged.
|
|
1355
1976
|
let mergedFrom
|
|
1356
1977
|
if (add && existsSync(outAbs)) {
|
|
1357
1978
|
let existing
|
|
1358
1979
|
try {
|
|
1359
|
-
existing =
|
|
1980
|
+
existing = parseBundleBytes(readFileSync(outAbs))
|
|
1360
1981
|
} catch (cause) {
|
|
1361
1982
|
throw new Error(`bundleCommand: --add failed to read the existing bundle at ${target}`, { cause })
|
|
1362
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 })
|
|
1363
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)
|
|
1364
1998
|
bundle = existing.merge(bundle)
|
|
1365
|
-
//
|
|
1366
|
-
// With none on disk we can't attest the pre-existing bundle's files (their hashes live
|
|
1367
|
-
// 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.
|
|
1368
2000
|
if (lockData) {
|
|
1369
|
-
if (!existsSync(lockAbs)) {
|
|
1370
|
-
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.`)
|
|
1371
|
-
}
|
|
1372
2001
|
let existingLock
|
|
1373
|
-
|
|
1374
|
-
existingLock =
|
|
1375
|
-
}
|
|
1376
|
-
|
|
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)
|
|
1377
2019
|
}
|
|
1378
2020
|
lockData = existingLock.merge(Lockfile.parse(lockData)).serialize()
|
|
1379
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 })
|
|
1380
2029
|
}
|
|
1381
2030
|
|
|
2031
|
+
// Serialized under --lockfile-only too: never write a lockfile of a bundle that couldn't be written.
|
|
1382
2032
|
const serialized = bundle.serialize()
|
|
2033
|
+
built.held?.loads(bundle, { text: serialized, at: outAbs })
|
|
1383
2034
|
const files = [...bundle.sources.keys()]
|
|
1384
2035
|
const modules = bundle.modules
|
|
1385
2036
|
|
|
1386
|
-
const dest = writeBundle(cwd, target, serialized, brotliQuality)
|
|
2037
|
+
const dest = lockfileOnly ? undefined : writeBundle(cwd, target, serialized, brotliQuality)
|
|
1387
2038
|
if (lockData) writeFile(lockAbs, lockData)
|
|
1388
2039
|
// The bundle's paths are relative to its root; shown relative to cwd, as `dest` is.
|
|
1389
2040
|
const fromDir = posix.join(toPosix(relative(resolve(cwd), root)), outermostDir(files, root))
|
|
1390
|
-
|
|
1391
|
-
|
|
2041
|
+
if (lockfileOnly) {
|
|
2042
|
+
console.warn(lockedSummary(files.length, modules, fromDir, lockfile))
|
|
2043
|
+
} else if (mergedFrom !== undefined) {
|
|
1392
2044
|
const added = files.length - mergedFrom
|
|
1393
2045
|
console.warn(`[stasis] Added ${added} file${added === 1 ? '' : 's'} (${files.length} total in ${packagesLabel(modules)}) from ${fromDir} to ${dest}`)
|
|
1394
2046
|
} else {
|
|
1395
2047
|
console.warn(bundledSummary(files.length, modules, fromDir, dest))
|
|
1396
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 }) }
|
|
1397
2207
|
}
|