@exodus/stasis 1.0.0-beta.14 → 1.0.0-beta.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/bin/stasis.js +99 -16
- package/package.json +22 -9
- package/src/audit-corrections.js +11 -42
- package/src/audit.js +241 -190
- package/src/babel.js +39 -122
- package/src/bundle-from-lockfile.js +9 -21
- package/src/bundle-reader.js +5 -9
- package/src/bundle.js +0 -3
- package/src/cmd/build.js +82 -174
- package/src/cmd/bundle-options.js +113 -167
- package/src/cmd/bundle.js +942 -1294
- package/src/cmd/diff.js +1 -4
- package/src/cmd/github-bundle.js +21 -26
- package/src/cmd/output.js +15 -12
- package/src/cmd/sbom.js +3 -8
- package/src/cmd/tokens.js +16 -23
- package/src/define.js +36 -94
- package/src/diff.js +72 -128
- package/src/esbuild.js +0 -4
- package/src/flow.js +18 -13
- 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 +41 -93
- package/src/loaders/cargo.js +298 -805
- package/src/loaders/foundry.js +109 -285
- package/src/loaders/paths.js +13 -13
- package/src/loaders/php.js +210 -489
- package/src/loaders/rust.js +507 -1430
- package/src/loaders/solidity-ownership.js +48 -128
- package/src/loaders/solidity.js +39 -122
- package/src/loaders/toml.js +7 -17
- package/src/lockfile.js +0 -3
- package/src/metro-assets.js +10 -384
- package/src/metro-resolver.js +58 -134
- package/src/metro-transformer.js +0 -5
- package/src/metro.js +0 -4
- package/src/mock.js +32 -80
- package/src/parse.js +4 -4
- package/src/prune.js +0 -3
- package/src/resolve-fields.js +137 -532
- package/src/resolve-node.js +27 -45
- package/src/resolve-typescript.js +166 -298
- package/src/rollup.js +0 -4
- package/src/run-installed.js +10 -0
- package/src/sbom.js +52 -128
- package/src/scan.js +142 -284
- package/src/sixel.js +310 -0
- package/src/table.js +63 -31
- 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 +596 -522
- package/src/vfs-bundle/entries.js +40 -57
- package/src/vfs-bundle/github.js +80 -122
- package/src/vfs-bundle/glob.js +4 -45
- package/src/vfs-bundle/lazy.js +27 -56
- package/src/vfs-bundle/prisma.js +235 -105
- package/src/vfs-bundle/soldeer.js +28 -49
- package/src/vfs-bundle/tree.js +295 -318
- package/src/vfs-bundle/yarnclean.js +10 -31
- package/src/vfs-bundle.js +23 -64
- package/src/webpack.js +0 -4
- package/src/why.js +78 -232
- package/src/installed-commits.js +0 -184
package/src/cmd/build.js
CHANGED
|
@@ -6,38 +6,34 @@ import { spawnSync } from 'node:child_process'
|
|
|
6
6
|
|
|
7
7
|
import { Bundle } from '@exodus/stasis-core/bundle'
|
|
8
8
|
import { State } from '@exodus/stasis-core/state'
|
|
9
|
-
import { toPosix } from '@exodus/stasis-core/util'
|
|
9
|
+
import { CONFIG_FILE, isPlainObject, JS_EXTENSIONS, toPosix } from '@exodus/stasis-core/util'
|
|
10
10
|
import { StasisEsbuild } from '@exodus/stasis-plugins/esbuild'
|
|
11
|
-
import { bundleDirHost
|
|
11
|
+
import { bundleDirHost } from '@exodus/stasis-plugins/metro-native'
|
|
12
|
+
import { assetsDestFiles } from '@preventive/rn/assets-dest.js'
|
|
13
|
+
import { assetData, assetModule } from '@preventive/rn/metro-assets.js'
|
|
12
14
|
import { HERMES_UNSUPPORTED, createBabelConfigTransform, createHermesTransform } from '../babel.js'
|
|
13
15
|
import { bundleFromLockfile } from '../bundle-from-lockfile.js'
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
+
import { METRO_RELEASE_DEFINE } from '../define.js'
|
|
17
|
+
import { FLOW_EXTS, hasFlowPragma, stripFlowTypes } from '../flow.js'
|
|
18
|
+
import { ASSET_REGISTRY, assetRegistryResolver } from '../metro-assets.js'
|
|
16
19
|
import { parseFileWithKind } from '../parse.js'
|
|
17
20
|
import { writeFile } from './output.js'
|
|
18
21
|
|
|
19
22
|
export { bundleFromLockfile }
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
// Allowlist (not blocklist) of per-file formats `stasis build` accepts, so a bundle
|
|
24
|
-
// format added later fails closed here rather than in a cryptic esbuild error.
|
|
24
|
+
// An allowlist, so that a bundle format added later fails closed here rather than in a cryptic esbuild error.
|
|
25
25
|
const BUILDABLE_FORMATS = new Set([
|
|
26
26
|
'module', 'commonjs', 'json', 'module-typescript', 'commonjs-typescript',
|
|
27
27
|
'resource', 'resource:base64', 'directory', 'stat:file', 'stat:directory',
|
|
28
28
|
])
|
|
29
29
|
|
|
30
|
-
// esbuild is an optional peer
|
|
31
|
-
// to a global one.
|
|
30
|
+
// esbuild is an optional peer dependency: the local install's, else a global one.
|
|
32
31
|
async function importEsbuild() {
|
|
33
32
|
let mod
|
|
34
33
|
try {
|
|
35
34
|
mod = await import('esbuild')
|
|
36
35
|
} catch (cause) {
|
|
37
|
-
|
|
38
|
-
if (cause?.code !== 'ERR_MODULE_NOT_FOUND' && cause?.code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED') {
|
|
39
|
-
throw cause
|
|
40
|
-
}
|
|
36
|
+
if (cause?.code !== 'ERR_MODULE_NOT_FOUND' && cause?.code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED') throw cause
|
|
41
37
|
const globalMain = resolveGlobalEsbuild()
|
|
42
38
|
if (!globalMain) {
|
|
43
39
|
throw new Error(
|
|
@@ -49,41 +45,31 @@ async function importEsbuild() {
|
|
|
49
45
|
}
|
|
50
46
|
mod = await import(pathToFileURL(globalMain).href)
|
|
51
47
|
}
|
|
52
|
-
// A globally-imported CJS copy may land its exports on `default
|
|
48
|
+
// A globally-imported CJS copy may land its exports on `default`.
|
|
53
49
|
return typeof mod.build === 'function' ? mod : mod.default
|
|
54
50
|
}
|
|
55
51
|
|
|
56
52
|
function resolveGlobalEsbuild() {
|
|
57
|
-
const
|
|
58
|
-
['npm', ['root', '-g']],
|
|
59
|
-
['pnpm', ['root', '-g']],
|
|
60
|
-
['yarn', ['global', 'dir']],
|
|
61
|
-
]
|
|
62
|
-
for (const [cmd, args] of probes) {
|
|
53
|
+
for (const [cmd, args] of [['npm', ['root', '-g']], ['pnpm', ['root', '-g']], ['yarn', ['global', 'dir']]]) {
|
|
63
54
|
let out
|
|
64
55
|
try {
|
|
65
56
|
out = spawnSync(cmd, args, { encoding: 'utf8' })
|
|
66
57
|
} catch {
|
|
67
58
|
continue
|
|
68
59
|
}
|
|
69
|
-
|
|
70
|
-
const base = out.stdout.trim()
|
|
60
|
+
const base = out?.status === 0 ? out.stdout?.trim() : undefined
|
|
71
61
|
if (!base) continue
|
|
72
|
-
// `npm/pnpm root -g` print the global node_modules
|
|
73
|
-
// dir whose node_modules holds the packages. Try both shapes.
|
|
62
|
+
// `npm/pnpm root -g` print the global node_modules; `yarn global dir` a dir whose node_modules holds them.
|
|
74
63
|
for (const nm of [base, join(base, 'node_modules')]) {
|
|
75
64
|
if (!existsSync(join(nm, 'esbuild', 'package.json'))) continue
|
|
76
65
|
try {
|
|
77
66
|
return createRequire(join(nm, 'noop.js')).resolve('esbuild')
|
|
78
|
-
} catch {
|
|
79
|
-
}
|
|
67
|
+
} catch { /* try the next */ }
|
|
80
68
|
}
|
|
81
69
|
}
|
|
82
70
|
return undefined
|
|
83
71
|
}
|
|
84
72
|
|
|
85
|
-
// Preconditions independent of which entry we build. Per-entry language checks are deferred
|
|
86
|
-
// to assertBuildableEntry, once selectEntry has picked one.
|
|
87
73
|
function assertBuildableArtifact(artifact, label) {
|
|
88
74
|
if (artifact.config.scope !== 'full') {
|
|
89
75
|
throw new Error(
|
|
@@ -91,163 +77,112 @@ function assertBuildableArtifact(artifact, label) {
|
|
|
91
77
|
`node_modules-scope artifacts omit the entry and workspace code, so there is nothing to build.`
|
|
92
78
|
)
|
|
93
79
|
}
|
|
94
|
-
if (artifact.entries.size === 0) {
|
|
95
|
-
throw new Error(`stasis build: ${label} declares no entries to build`)
|
|
96
|
-
}
|
|
80
|
+
if (artifact.entries.size === 0) throw new Error(`stasis build: ${label} declares no entries to build`)
|
|
97
81
|
}
|
|
98
82
|
|
|
99
|
-
//
|
|
100
|
-
// (all conditions buckets; a --metro edge's per-platform targets included).
|
|
83
|
+
// As esbuild loads them: every conditions bucket's edges, every platform's target (a Set visits what is added to it).
|
|
101
84
|
function reachableFiles(entry, imports) {
|
|
102
85
|
const seen = new Set([entry])
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const specifiers = byParent.get(parent)
|
|
108
|
-
if (specifiers === undefined) continue
|
|
109
|
-
for (const [, target] of specifiers) {
|
|
110
|
-
// A plain edge is a file string; a --metro edge is a {platform: file} Map.
|
|
111
|
-
const targets = typeof target === 'string' ? [target] : [...target.values()]
|
|
112
|
-
for (const file of targets) {
|
|
113
|
-
if (!seen.has(file)) {
|
|
114
|
-
seen.add(file)
|
|
115
|
-
stack.push(file)
|
|
116
|
-
}
|
|
117
|
-
}
|
|
86
|
+
for (const parent of seen) {
|
|
87
|
+
for (const byParent of imports.values()) {
|
|
88
|
+
for (const target of byParent.get(parent)?.values() ?? []) {
|
|
89
|
+
for (const file of typeof target === 'string' ? [target] : target.values()) seen.add(file)
|
|
118
90
|
}
|
|
119
91
|
}
|
|
120
92
|
}
|
|
121
93
|
return seen
|
|
122
94
|
}
|
|
123
95
|
|
|
124
|
-
//
|
|
125
|
-
// it must be buildable. A non-JS file elsewhere (another entry, or a `stasis add` attachment)
|
|
126
|
-
// isn't reached, so it doesn't block the build. imports/formats are null on a legacy lockfile.
|
|
96
|
+
// Only what the entry reaches must be buildable: another entry or a `stasis add` attachment doesn't block it.
|
|
127
97
|
function assertBuildableEntry(artifact, entry, label) {
|
|
128
|
-
if (!
|
|
98
|
+
if (!JS_EXTENSIONS.has(extname(entry))) {
|
|
129
99
|
throw new Error(`stasis build only builds JavaScript/TypeScript entries (esbuild); got non-JS entry '${entry}' in ${label}`)
|
|
130
100
|
}
|
|
131
101
|
const reachable = reachableFiles(entry, artifact.imports ?? new Map())
|
|
132
|
-
// A reachable resource (plugin captures DO record asset edges) stays allowed via BUILDABLE_FORMATS;
|
|
133
|
-
// an entry that imports an unbuildable asset fails later at esbuild, not here.
|
|
134
102
|
for (const [file, format] of artifact.formats ?? new Map()) {
|
|
135
|
-
if (
|
|
136
|
-
if (!BUILDABLE_FORMATS.has(format)) {
|
|
103
|
+
if (reachable.has(file) && !BUILDABLE_FORMATS.has(format)) {
|
|
137
104
|
throw new Error(`stasis build only supports JavaScript/TypeScript bundles (esbuild); ${label} carries a '${format}' file (${file})`)
|
|
138
105
|
}
|
|
139
106
|
}
|
|
140
107
|
}
|
|
141
108
|
|
|
142
|
-
// Pick which recorded entry to build; required only when several are recorded. Matched
|
|
143
|
-
// against the recorded project-relative entries (verbatim, `./`-stripped, or cwd-relative).
|
|
144
109
|
function selectEntry(entryArg, entries, { cwd, root, label }) {
|
|
145
110
|
const recorded = [...entries]
|
|
146
111
|
const list = recorded.map((e) => ` ${e}`).join('\n')
|
|
147
112
|
if (entryArg === undefined) {
|
|
148
113
|
if (recorded.length === 1) return recorded[0]
|
|
149
|
-
// length === 0 is impossible here (assertBuildableArtifact required >= 1 entry).
|
|
150
114
|
throw new Error(`stasis build: ${label} has ${recorded.length} entry points; pass one as the second argument:\n${list}`)
|
|
151
115
|
}
|
|
152
|
-
const candidates = new Set([
|
|
153
|
-
entryArg,
|
|
154
|
-
entryArg.replace(/^\.\//u, ''),
|
|
155
|
-
relative(root, resolve(cwd, entryArg)).split(sep).join('/'),
|
|
156
|
-
])
|
|
116
|
+
const candidates = new Set([entryArg, entryArg.replace(/^\.\//u, ''), relative(root, resolve(cwd, entryArg)).split(sep).join('/')])
|
|
157
117
|
const match = recorded.find((e) => candidates.has(e))
|
|
158
|
-
if (match === undefined) {
|
|
159
|
-
throw new Error(`stasis build: '${entryArg}' is not an entry point of ${label}; available:\n${list}`)
|
|
160
|
-
}
|
|
118
|
+
if (match === undefined) throw new Error(`stasis build: '${entryArg}' is not an entry point of ${label}; available:\n${list}`)
|
|
161
119
|
return match
|
|
162
120
|
}
|
|
163
121
|
|
|
164
|
-
//
|
|
165
|
-
// filesystem discovery (no package.json walk, no on-disk config, no stray stasis.code.br).
|
|
122
|
+
// A bundle-load State serving `bundle`, with no discovery on disk (no package.json, config or stray stasis.code.br).
|
|
166
123
|
function loadStateForBundle(root, bundle) {
|
|
167
124
|
const state = new State(root, { bundle: 'load', lock: 'ignore', scope: bundle.config.scope, skipDiscovery: true })
|
|
168
|
-
|
|
169
|
-
|
|
125
|
+
state.sources = new Map()
|
|
126
|
+
state.resources = new Map()
|
|
170
127
|
for (const [file, content] of bundle.sources) {
|
|
171
|
-
if (Bundle.isResourceFormat(bundle.formats.get(file))) resources.set(file, content)
|
|
172
|
-
else sources.set(file, content)
|
|
128
|
+
if (Bundle.isResourceFormat(bundle.formats.get(file))) state.resources.set(file, content)
|
|
129
|
+
else state.sources.set(file, content)
|
|
173
130
|
}
|
|
174
|
-
state.sources = sources
|
|
175
|
-
state.resources = resources
|
|
176
131
|
state.formats = bundle.formats
|
|
177
132
|
state.imports = bundle.imports
|
|
178
133
|
state.entries = new Set(bundle.entries)
|
|
179
|
-
// Not state.
|
|
180
|
-
// consumer narrows against state.modules -- which is empty here -- so copying it would be inert.
|
|
181
|
-
// Not state.modules: the load path never reads it (only the capture paths write it).
|
|
134
|
+
// Not state.modules or state.executable: the load path reads neither.
|
|
182
135
|
return state
|
|
183
136
|
}
|
|
184
137
|
|
|
185
|
-
// A file's bytes as the State serves them: a Buffer for a base64 resource, else a string.
|
|
186
138
|
const asBytes = (source) => (Buffer.isBuffer(source) ? source : Buffer.from(source))
|
|
187
139
|
|
|
188
|
-
// The bytes the artifact carries for `file
|
|
140
|
+
// The bytes the artifact carries for `file`, verified by getFile.
|
|
189
141
|
const readCarried = (state, file) => asBytes(state.getFile(pathToFileURL(resolve(state.root, file)).toString()).source)
|
|
190
142
|
|
|
191
|
-
//
|
|
192
|
-
// --metro bundle, a Metro asset, as it records an import edge to no other -- served as the module
|
|
193
|
-
// Metro builds of it for `platform` (metro-assets.js), from the files of the asset the artifact carries
|
|
194
|
-
// in its directory. The module's require of the asset registry resolves to the file the artifact
|
|
195
|
-
// carries for it, an edge added to the State's graph: the bundle records none, as Metro adds it past
|
|
196
|
-
// the source. `built` collects each asset's { descriptor, files } for --assets-dest.
|
|
143
|
+
// Each Metro asset served as the module Metro builds of it, its require of the registry added as Metro adds it.
|
|
197
144
|
function metroAssetResource(state, platform, built) {
|
|
198
|
-
const
|
|
145
|
+
const listing = bundleDirHost(state)
|
|
199
146
|
const registryFor = assetRegistryResolver(platform, state.sources)
|
|
200
147
|
return (source, { path }) => {
|
|
201
148
|
const rel = toPosix(state.relative(path))
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
|
|
149
|
+
const host = {
|
|
150
|
+
readdir: (dir) => listing.readdir(dir).map(({ name }) => name),
|
|
151
|
+
readFile: (file) => (file === path ? asBytes(source) : readCarried(state, file)),
|
|
152
|
+
}
|
|
153
|
+
const data = assetData(path, { urlPath: rel, platform, host })
|
|
205
154
|
state.addImport(pathToFileURL(path).href, ASSET_REGISTRY, pathToFileURL(resolve(state.root, registryFor(rel))).href)
|
|
206
|
-
built.set(rel,
|
|
207
|
-
return { contents:
|
|
155
|
+
built.set(rel, data)
|
|
156
|
+
return { contents: assetModule(data, ASSET_REGISTRY), loader: 'js' }
|
|
208
157
|
}
|
|
209
158
|
}
|
|
210
159
|
|
|
211
|
-
// --flow's
|
|
212
|
-
//
|
|
213
|
-
// types alone dropped whole, as Babel's Flow transform drops it (a --metro bundle records no edge for
|
|
214
|
-
// it). One that parses is never touched, as stripping plain JS as Flow can misread it (`a < b > (c)`
|
|
215
|
-
// as a call). A file still unparseable fails the build in esbuild: Flow syntax that is code, which
|
|
216
|
-
// stripping leaves, or one flow-remove-types itself can't parse, served as it is.
|
|
160
|
+
// --flow, as Metro's Babel builds a file: Flow types stripped from a file marked `@flow`, else only where
|
|
161
|
+
// esbuild can't parse it; what still doesn't parse fails the build in esbuild.
|
|
217
162
|
function createFlowTransform(esbuild) {
|
|
218
163
|
return async (source, { path, loader }) => {
|
|
219
164
|
if (!FLOW_EXTS.has(extname(path))) return undefined
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
return undefined
|
|
223
|
-
} catch {
|
|
165
|
+
const text = String(source)
|
|
166
|
+
if (!hasFlowPragma(text)) {
|
|
224
167
|
try {
|
|
225
|
-
|
|
226
|
-
} catch {
|
|
168
|
+
await esbuild.transform(text, { loader, sourcefile: path, logLevel: 'silent' })
|
|
227
169
|
return undefined
|
|
228
|
-
}
|
|
170
|
+
} catch { /* stripped below */ }
|
|
171
|
+
}
|
|
172
|
+
try {
|
|
173
|
+
return { contents: stripFlowTypes(text, { removeEmptyImports: true }), loader }
|
|
174
|
+
} catch {
|
|
175
|
+
return undefined
|
|
229
176
|
}
|
|
230
177
|
}
|
|
231
178
|
}
|
|
232
179
|
|
|
233
|
-
// Two transform hooks over a file in turn, the second over what the first made of it.
|
|
234
180
|
const chainTransforms = (first, second) => async (source, file) => {
|
|
235
181
|
const firstMade = await first(source, file)
|
|
236
182
|
return (await second(firstMade?.contents ?? source, { ...file, loader: firstMade?.loader ?? file.loader })) ?? firstMade
|
|
237
183
|
}
|
|
238
184
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
// A list option as the command line takes one, as an array of strings or one string, each split at commas.
|
|
242
|
-
const listOf = (value) => [value].flat().flatMap((v) => v.split(',')).map((v) => v.trim()).filter(Boolean)
|
|
243
|
-
|
|
244
|
-
// The options of `entry` in the `entries` of the stasis.config.json the artifact carries at its root
|
|
245
|
-
// (`state`'s), read by `stasis build` itself, apart from how `stasis bundle` builds by them: of each
|
|
246
|
-
// entry's options, those that say how its code is built -- `jsx` (as --loader=.js:jsx), `flow`,
|
|
247
|
-
// `define` (its K=V strings, one or an array) and `metro` with its `platforms` (a list, as on the
|
|
248
|
-
// command line); the rest are the bundle's. An entry is named as `stasis bundle` names it, by its path
|
|
249
|
-
// from the config's directory. -> { jsx, flow, define, platforms } (`platforms` only under `metro`),
|
|
250
|
-
// or null where the artifact carries no config, or it names no such entry
|
|
185
|
+
// `entry`'s options in the stasis.config.json the artifact carries that say how its code is built, or null.
|
|
251
186
|
function configEntryOptions(state, entry) {
|
|
252
187
|
if (!state.sources.has(CONFIG_FILE) && !state.resources.has(CONFIG_FILE)) return null
|
|
253
188
|
let config
|
|
@@ -258,13 +193,12 @@ function configEntryOptions(state, entry) {
|
|
|
258
193
|
}
|
|
259
194
|
const entries = config?.entries
|
|
260
195
|
if (entries === undefined) return null
|
|
261
|
-
|
|
262
|
-
if (!isObject(entries)) throw new Error(`stasis build: the artifact's ${CONFIG_FILE}: entries must be an object`)
|
|
196
|
+
if (!isPlainObject(entries)) throw new Error(`stasis build: the artifact's ${CONFIG_FILE}: entries must be an object`)
|
|
263
197
|
const name = Object.keys(entries).find((key) => toPosix(relative(state.root, resolve(state.root, key))) === entry)
|
|
264
198
|
if (name === undefined) return null
|
|
265
199
|
const what = `the artifact's ${CONFIG_FILE}: entries ${JSON.stringify(name)}`
|
|
266
200
|
const options = entries[name]
|
|
267
|
-
if (!
|
|
201
|
+
if (!isPlainObject(options)) throw new Error(`stasis build: ${what} must be an object of options`)
|
|
268
202
|
for (const flag of ['jsx', 'flow', 'metro']) {
|
|
269
203
|
if (options[flag] !== undefined && typeof options[flag] !== 'boolean') throw new Error(`stasis build: ${what}: ${flag} must be true or false`)
|
|
270
204
|
}
|
|
@@ -283,31 +217,17 @@ function configEntryOptions(state, entry) {
|
|
|
283
217
|
}
|
|
284
218
|
let platforms
|
|
285
219
|
if (options.metro === true) {
|
|
286
|
-
platforms = [...new Set(
|
|
220
|
+
platforms = [...new Set(strings('platforms').flatMap((v) => v.split(',')).map((v) => v.trim()).filter(Boolean))]
|
|
287
221
|
if (platforms.length === 0) throw new Error(`stasis build: ${what} sets metro but names no platforms`)
|
|
288
222
|
}
|
|
289
223
|
return { name, jsx: options.jsx === true, flow: options.flow === true, define, platforms }
|
|
290
224
|
}
|
|
291
225
|
|
|
292
|
-
// Run `stasis build`:
|
|
293
|
-
//
|
|
294
|
-
// is always followed from the artifact; `external` only steers the specifiers it doesn't.
|
|
226
|
+
// Run `stasis build`: esbuild over one recorded entry, following the artifact's recorded import graph exactly,
|
|
227
|
+
// not esbuild's resolver: `external` only steers the specifiers it doesn't record.
|
|
295
228
|
export async function buildCommand({
|
|
296
|
-
cwd = process.cwd(),
|
|
297
|
-
|
|
298
|
-
entry,
|
|
299
|
-
output,
|
|
300
|
-
format,
|
|
301
|
-
platform = 'node',
|
|
302
|
-
babel = false,
|
|
303
|
-
flow = false,
|
|
304
|
-
minify = false,
|
|
305
|
-
sourcemap = false,
|
|
306
|
-
define = {},
|
|
307
|
-
external = [],
|
|
308
|
-
loader = {},
|
|
309
|
-
metro,
|
|
310
|
-
assetsDest,
|
|
229
|
+
cwd = process.cwd(), artifact, entry, output, format, platform = 'node', babel = false, flow = false, minify = false,
|
|
230
|
+
sourcemap = false, define = {}, external = [], loader = {}, metro, assetsDest,
|
|
311
231
|
} = {}) {
|
|
312
232
|
if (!artifact) throw new Error('stasis build: an artifact (bundle or lockfile) is required')
|
|
313
233
|
if (!output) throw new Error('stasis build: --output is required (a .js/.cjs/.mjs file, or a directory)')
|
|
@@ -318,9 +238,7 @@ export async function buildCommand({
|
|
|
318
238
|
if (flow && babel) {
|
|
319
239
|
throw new Error('stasis build: --babel hands the project\'s Babel config each file as it is, Flow included, for it to compile; --flow strips Flow before esbuild without it -- pass one')
|
|
320
240
|
}
|
|
321
|
-
// --platform=hermes is
|
|
322
|
-
// 'neutral'): every code file is downleveled to Hermes' dialect before the final bundle,
|
|
323
|
-
// and the bundle itself must avoid ESM -- Hermes has no import/export.
|
|
241
|
+
// --platform=hermes is stasis's target, esbuild's 'neutral': code downleveled to Hermes' dialect, and no ESM.
|
|
324
242
|
const hermes = platform === 'hermes'
|
|
325
243
|
format ??= hermes ? 'iife' : 'esm'
|
|
326
244
|
if (hermes && format === 'esm') {
|
|
@@ -330,21 +248,16 @@ export async function buildCommand({
|
|
|
330
248
|
const { kind, artifact: parsed } = parseFileWithKind(artifactAbs)
|
|
331
249
|
assertBuildableArtifact(parsed, artifact)
|
|
332
250
|
|
|
333
|
-
//
|
|
334
|
-
// are read + verified from there.
|
|
251
|
+
// Paths are the artifact's directory's, where a lockfile's sources are read and verified.
|
|
335
252
|
const root = dirname(artifactAbs)
|
|
336
253
|
const selectedEntry = selectEntry(entry, parsed.entries, { cwd, root, label: artifact })
|
|
337
254
|
assertBuildableEntry(parsed, selectedEntry, artifact)
|
|
338
255
|
|
|
339
256
|
const bundle = kind === 'lockfile' ? bundleFromLockfile(parsed, { root }) : parsed
|
|
340
257
|
const state = loadStateForBundle(root, bundle)
|
|
341
|
-
// Make the entry absolute against the State's root so resolution is independent of process cwd.
|
|
342
258
|
const entryPoint = resolve(state.root, selectedEntry)
|
|
343
259
|
|
|
344
|
-
// The
|
|
345
|
-
// --loader=.js:jsx, flow as --flow (not under --babel, whose config compiles Flow itself), its define
|
|
346
|
-
// under --define's, and metro as --metro for its one platform; of several, --metro picks one, which
|
|
347
|
-
// must be one of them.
|
|
260
|
+
// The config's options for the entry, under the flags given (flow not under --babel, whose config compiles it).
|
|
348
261
|
const configured = configEntryOptions(state, selectedEntry)
|
|
349
262
|
if (configured !== null) {
|
|
350
263
|
const taken = []
|
|
@@ -374,29 +287,31 @@ export async function buildCommand({
|
|
|
374
287
|
}
|
|
375
288
|
if (taken.length > 0) console.warn(`[stasis] ${selectedEntry}: ${taken.join(' ')}, as ${CONFIG_FILE}'s entries ${JSON.stringify(configured.name)} sets`)
|
|
376
289
|
}
|
|
290
|
+
// As `stasis bundle --metro` reads the files; METRO_RELEASE_DEFINE's names too, since outside Metro's
|
|
291
|
+
// prelude `__DEV__` is no global at all.
|
|
292
|
+
if (metro !== undefined) {
|
|
293
|
+
if (loader['.js'] === undefined) loader = { ...loader, '.js': 'jsx' }
|
|
294
|
+
if (!babel) flow = true
|
|
295
|
+
for (const [key, value] of Object.entries(METRO_RELEASE_DEFINE)) {
|
|
296
|
+
if (!Object.hasOwn(define, key)) define = { ...define, [key]: JSON.stringify(value) }
|
|
297
|
+
}
|
|
298
|
+
}
|
|
377
299
|
if (assetsDest !== undefined && metro === undefined) {
|
|
378
300
|
throw new Error('stasis build: --assets-dest copies the assets --metro builds; pass --metro=PLATFORM with it')
|
|
379
301
|
}
|
|
380
302
|
|
|
381
303
|
const esbuild = await importEsbuild()
|
|
382
|
-
// Per-file code rewrites the plugin applies before esbuild parses a served file:
|
|
383
|
-
// --babel runs the project's own babel.config.* over the original source (no esbuild
|
|
384
|
-
// pre-transform), and otherwise --platform=hermes uses the built-in downlevel pipeline.
|
|
385
|
-
// Both resolve Babel from the project (cwd), never from stasis's own dependencies. --flow
|
|
386
|
-
// strips Flow types first, from what esbuild can't parse.
|
|
387
304
|
let transform
|
|
388
305
|
if (babel) transform = createBabelConfigTransform({ projectDir: cwd })
|
|
389
306
|
else if (hermes) transform = createHermesTransform({ projectDir: cwd, esbuild })
|
|
390
307
|
if (flow) transform = transform === undefined ? createFlowTransform(esbuild) : chainTransforms(createFlowTransform(esbuild), transform)
|
|
391
|
-
// --metro=PLATFORM: PLATFORM's target of each per-platform edge, and each asset as Metro builds it.
|
|
392
308
|
const builtAssets = new Map()
|
|
393
309
|
const resource = metro === undefined ? undefined : metroAssetResource(state, metro, builtAssets)
|
|
394
310
|
|
|
395
311
|
const outAbs = resolve(cwd, output)
|
|
396
312
|
const asFile = /\.[cm]?js$/u.test(output)
|
|
397
|
-
// Never overwrite a file the artifact attests: esbuild's own overwrite check fires only
|
|
398
|
-
//
|
|
399
|
-
// clobber any recorded file before touching disk.
|
|
313
|
+
// Never overwrite a file the artifact attests: esbuild's own overwrite check fires only after writing
|
|
314
|
+
// (corrupting a lockfile's hash), so it builds write:false and each output is checked before any is written.
|
|
400
315
|
const attested = new Set([...state.sources.keys(), ...state.resources.keys()].map((f) => resolve(state.root, f)))
|
|
401
316
|
|
|
402
317
|
let result
|
|
@@ -415,16 +330,13 @@ export async function buildCommand({
|
|
|
415
330
|
...(external.length > 0 ? { external } : {}),
|
|
416
331
|
...(Object.keys(define).length > 0 ? { define } : {}),
|
|
417
332
|
...(asFile ? { outfile: outAbs } : { outdir: outAbs }),
|
|
418
|
-
//
|
|
419
|
-
// own `loader` option never sees them. e.g. { '.js': 'jsx' } to parse JSX in .js files.
|
|
333
|
+
// Loaders go through the plugin: it serves every file, so esbuild's own `loader` option never sees them.
|
|
420
334
|
plugins: [new StasisEsbuild(state, { loaders: new Map(Object.entries(loader)), transform, resource, platform: metro })],
|
|
421
335
|
})
|
|
422
336
|
} catch (err) {
|
|
423
|
-
// Under logLevel:'silent' esbuild puts the real errors on err.errors
|
|
337
|
+
// Under logLevel:'silent' esbuild puts the real errors on err.errors.
|
|
424
338
|
if (Array.isArray(err?.errors) && err.errors.length > 0) {
|
|
425
|
-
const
|
|
426
|
-
for (const line of formatted) process.stderr.write(line)
|
|
427
|
-
// A resource the build reaches with no loader for its extension: what builds one.
|
|
339
|
+
for (const line of await esbuild.formatMessages(err.errors, { kind: 'error', color: false })) process.stderr.write(line)
|
|
428
340
|
const unloaded = err.errors.map((e) => /^No loader is configured for "([^"]+)" files/u.exec(e.text)?.[1]).find(Boolean)
|
|
429
341
|
if (unloaded !== undefined) {
|
|
430
342
|
process.stderr.write(
|
|
@@ -440,15 +352,12 @@ export async function buildCommand({
|
|
|
440
352
|
throw err
|
|
441
353
|
}
|
|
442
354
|
|
|
443
|
-
//
|
|
444
|
-
// the platform, from the bytes the artifact carries, in a stable order (esbuild loads in parallel).
|
|
355
|
+
// As `react-native bundle --assets-dest` lays them out, in a stable order (esbuild loads in parallel).
|
|
445
356
|
const assetFiles = assetsDest === undefined
|
|
446
357
|
? []
|
|
447
|
-
:
|
|
448
|
-
.map(({ dest,
|
|
358
|
+
: assetsDestFiles([...builtAssets.keys()].toSorted().map((rel) => builtAssets.get(rel)), metro)
|
|
359
|
+
.map(({ dest, src, contents }) => ({ path: resolve(cwd, assetsDest, dest), contents: contents ?? readCarried(state, src) }))
|
|
449
360
|
const writes = [...result.outputFiles, ...assetFiles]
|
|
450
|
-
|
|
451
|
-
// Refuse to clobber an attested source, checked against the actual output paths before any write.
|
|
452
361
|
for (const { path } of writes) {
|
|
453
362
|
if (attested.has(path)) {
|
|
454
363
|
throw new Error(
|
|
@@ -459,8 +368,7 @@ export async function buildCommand({
|
|
|
459
368
|
}
|
|
460
369
|
|
|
461
370
|
if (result.warnings.length > 0) {
|
|
462
|
-
const
|
|
463
|
-
for (const line of formatted) process.stderr.write(line)
|
|
371
|
+
for (const line of await esbuild.formatMessages(result.warnings, { kind: 'warning', color: false })) process.stderr.write(line)
|
|
464
372
|
}
|
|
465
373
|
|
|
466
374
|
for (const { path, contents } of writes) writeFile(path, contents)
|