@exodus/stasis 1.0.0-beta.13 → 1.0.0-beta.15
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 +5 -1
- package/bin/stasis.js +191 -149
- package/package.json +31 -7
- package/src/audit.js +224 -90
- package/src/babel.js +44 -37
- package/src/bundle-reader.js +24 -4
- package/src/cmd/build.js +218 -8
- package/src/cmd/bundle-options.js +31 -25
- package/src/cmd/bundle.js +1147 -311
- package/src/cmd/github-bundle.js +17 -9
- package/src/cmd/output.js +10 -3
- package/src/cmd/tokens.js +73 -0
- package/src/define.js +5 -0
- package/src/diff.js +2 -2
- package/src/flow.js +42 -0
- package/src/installed-commits.js +4 -2
- package/src/loaders/bash.js +2 -27
- package/src/loaders/cargo.js +6 -6
- package/src/loaders/foundry.js +4 -6
- package/src/loaders/paths.js +4 -15
- package/src/loaders/php.js +3 -5
- package/src/loaders/rust.js +19 -39
- package/src/loaders/solidity-ownership.js +4 -15
- package/src/loaders/solidity.js +4 -67
- package/src/metro-assets.js +65 -0
- package/src/metro-resolver.js +76 -123
- package/src/parse.js +7 -3
- package/src/resolve-fields.js +133 -366
- package/src/resolve-node.js +9 -7
- package/src/resolve-typescript.js +102 -30
- package/src/scan.js +154 -54
- package/src/sixel.js +333 -0
- package/src/table.js +116 -0
- package/src/validate-notes.d.ts +18 -0
- package/src/validate-notes.js +66 -0
- package/src/validate.d.ts +132 -0
- package/src/validate.js +1349 -0
- package/src/vfs-bundle/entries.js +5 -2
- package/src/vfs-bundle/github.js +68 -19
- package/src/vfs-bundle/glob.js +420 -0
- package/src/vfs-bundle/lazy.js +128 -0
- package/src/vfs-bundle/prisma.js +102 -29
- package/src/vfs-bundle/tree.js +238 -75
- package/src/vfs-bundle/yarnclean.js +100 -0
- package/src/vfs-bundle.js +16 -7
- package/src/why.js +9 -7
package/src/cmd/build.js
CHANGED
|
@@ -6,9 +6,16 @@ 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
10
|
import { StasisEsbuild } from '@exodus/stasis-plugins/esbuild'
|
|
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'
|
|
10
14
|
import { HERMES_UNSUPPORTED, createBabelConfigTransform, createHermesTransform } from '../babel.js'
|
|
11
15
|
import { bundleFromLockfile } from '../bundle-from-lockfile.js'
|
|
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'
|
|
12
19
|
import { parseFileWithKind } from '../parse.js'
|
|
13
20
|
import { writeFile } from './output.js'
|
|
14
21
|
|
|
@@ -178,6 +185,121 @@ function loadStateForBundle(root, bundle) {
|
|
|
178
185
|
return state
|
|
179
186
|
}
|
|
180
187
|
|
|
188
|
+
// A file's bytes as the State serves them: a Buffer for a base64 resource, else a string.
|
|
189
|
+
const asBytes = (source) => (Buffer.isBuffer(source) ? source : Buffer.from(source))
|
|
190
|
+
|
|
191
|
+
// The bytes the artifact carries for `file` (a path from the State's root, or an absolute one), verified
|
|
192
|
+
// by getFile.
|
|
193
|
+
const readCarried = (state, file) => asBytes(state.getFile(pathToFileURL(resolve(state.root, file)).toString()).source)
|
|
194
|
+
|
|
195
|
+
// --metro=PLATFORM's resource hook (StasisEsbuild's `resource`): each resource the build reaches -- in a
|
|
196
|
+
// --metro bundle, a Metro asset, as it records an import edge to no other -- served as the module
|
|
197
|
+
// Metro builds of it for `platform` (@preventive/rn's assetData and assetModule), from the files of the
|
|
198
|
+
// asset the artifact carries in its directory, at its path from the project root. The module's require
|
|
199
|
+
// of the asset registry resolves to the file the artifact carries for it (metro-assets.js), an edge
|
|
200
|
+
// added to the State's graph: the bundle records none, as Metro adds it past the source. `built`
|
|
201
|
+
// collects each asset's data for --assets-dest.
|
|
202
|
+
function metroAssetResource(state, platform, built) {
|
|
203
|
+
const listing = bundleDirHost(state)
|
|
204
|
+
const registryFor = assetRegistryResolver(platform, state.sources)
|
|
205
|
+
return (source, { path }) => {
|
|
206
|
+
const rel = toPosix(state.relative(path))
|
|
207
|
+
// The files the artifact carries, the asset's own bytes those the plugin serves it by.
|
|
208
|
+
const host = {
|
|
209
|
+
readdir: (dir) => listing.readdir(dir).map(({ name }) => name),
|
|
210
|
+
readFile: (file) => (file === path ? asBytes(source) : readCarried(state, file)),
|
|
211
|
+
}
|
|
212
|
+
const data = assetData(path, { urlPath: rel, platform, host })
|
|
213
|
+
state.addImport(pathToFileURL(path).href, ASSET_REGISTRY, pathToFileURL(resolve(state.root, registryFor(rel))).href)
|
|
214
|
+
built.set(rel, data)
|
|
215
|
+
return { contents: assetModule(data, ASSET_REGISTRY), loader: 'js' }
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// --flow's per-file transform (StasisEsbuild's `transform` hook shape), as `stasis bundle --flow` reads
|
|
220
|
+
// a file, and Metro's Babel builds it: a JS-family one gets its Flow types stripped (flow.js), an
|
|
221
|
+
// import listing types alone dropped whole, as Babel's Flow transform drops it (a --metro bundle
|
|
222
|
+
// records no edge for it) -- one marked `@flow` (hasFlowPragma) whether or not esbuild parses it as it
|
|
223
|
+
// is (where `f<T>(x)` is a comparison, not Flow's call), any other only where esbuild can't parse it.
|
|
224
|
+
// A file still unparseable fails the build in esbuild: Flow syntax that is code, which stripping
|
|
225
|
+
// leaves, or one flow-remove-types itself can't parse, served as it is.
|
|
226
|
+
function createFlowTransform(esbuild) {
|
|
227
|
+
return async (source, { path, loader }) => {
|
|
228
|
+
if (!FLOW_EXTS.has(extname(path))) return undefined
|
|
229
|
+
const text = String(source)
|
|
230
|
+
if (!hasFlowPragma(text)) {
|
|
231
|
+
try {
|
|
232
|
+
await esbuild.transform(text, { loader, sourcefile: path, logLevel: 'silent' })
|
|
233
|
+
return undefined
|
|
234
|
+
} catch { /* stripped below */ }
|
|
235
|
+
}
|
|
236
|
+
try {
|
|
237
|
+
return { contents: stripFlowTypes(text, { removeEmptyImports: true }), loader }
|
|
238
|
+
} catch {
|
|
239
|
+
return undefined
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Two transform hooks over a file in turn, the second over what the first made of it.
|
|
245
|
+
const chainTransforms = (first, second) => async (source, file) => {
|
|
246
|
+
const firstMade = await first(source, file)
|
|
247
|
+
return (await second(firstMade?.contents ?? source, { ...file, loader: firstMade?.loader ?? file.loader })) ?? firstMade
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const CONFIG_FILE = 'stasis.config.json'
|
|
251
|
+
|
|
252
|
+
// A list option as the command line takes one, as an array of strings or one string, each split at commas.
|
|
253
|
+
const listOf = (value) => [value].flat().flatMap((v) => v.split(',')).map((v) => v.trim()).filter(Boolean)
|
|
254
|
+
|
|
255
|
+
// The options of `entry` in the `entries` of the stasis.config.json the artifact carries at its root
|
|
256
|
+
// (`state`'s), read by `stasis build` itself, apart from how `stasis bundle` builds by them: of each
|
|
257
|
+
// entry's options, those that say how its code is built -- `jsx` (as --loader=.js:jsx), `flow`,
|
|
258
|
+
// `define` (its K=V strings, one or an array) and `metro` with its `platforms` (a list, as on the
|
|
259
|
+
// command line); the rest are the bundle's. An entry is named as `stasis bundle` names it, by its path
|
|
260
|
+
// from the config's directory. -> { jsx, flow, define, platforms } (`platforms` only under `metro`),
|
|
261
|
+
// or null where the artifact carries no config, or it names no such entry
|
|
262
|
+
function configEntryOptions(state, entry) {
|
|
263
|
+
if (!state.sources.has(CONFIG_FILE) && !state.resources.has(CONFIG_FILE)) return null
|
|
264
|
+
let config
|
|
265
|
+
try {
|
|
266
|
+
config = JSON.parse(readCarried(state, CONFIG_FILE).toString('utf8'))
|
|
267
|
+
} catch (cause) {
|
|
268
|
+
throw new Error(`stasis build: the artifact's ${CONFIG_FILE} is not valid JSON`, { cause })
|
|
269
|
+
}
|
|
270
|
+
const entries = config?.entries
|
|
271
|
+
if (entries === undefined) return null
|
|
272
|
+
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
273
|
+
if (!isObject(entries)) throw new Error(`stasis build: the artifact's ${CONFIG_FILE}: entries must be an object`)
|
|
274
|
+
const name = Object.keys(entries).find((key) => toPosix(relative(state.root, resolve(state.root, key))) === entry)
|
|
275
|
+
if (name === undefined) return null
|
|
276
|
+
const what = `the artifact's ${CONFIG_FILE}: entries ${JSON.stringify(name)}`
|
|
277
|
+
const options = entries[name]
|
|
278
|
+
if (!isObject(options)) throw new Error(`stasis build: ${what} must be an object of options`)
|
|
279
|
+
for (const flag of ['jsx', 'flow', 'metro']) {
|
|
280
|
+
if (options[flag] !== undefined && typeof options[flag] !== 'boolean') throw new Error(`stasis build: ${what}: ${flag} must be true or false`)
|
|
281
|
+
}
|
|
282
|
+
const strings = (flag) => {
|
|
283
|
+
const value = options[flag] ?? []
|
|
284
|
+
if (typeof value !== 'string' && !(Array.isArray(value) && value.every((v) => typeof v === 'string'))) {
|
|
285
|
+
throw new Error(`stasis build: ${what}: ${flag} must be a string or an array of strings`)
|
|
286
|
+
}
|
|
287
|
+
return [value].flat()
|
|
288
|
+
}
|
|
289
|
+
const define = {}
|
|
290
|
+
for (const pair of strings('define')) {
|
|
291
|
+
const eq = pair.indexOf('=')
|
|
292
|
+
if (eq <= 0) throw new Error(`stasis build: ${what}: define must be KEY=VALUE (got '${pair}')`)
|
|
293
|
+
define[pair.slice(0, eq)] = pair.slice(eq + 1)
|
|
294
|
+
}
|
|
295
|
+
let platforms
|
|
296
|
+
if (options.metro === true) {
|
|
297
|
+
platforms = [...new Set(listOf(strings('platforms')))]
|
|
298
|
+
if (platforms.length === 0) throw new Error(`stasis build: ${what} sets metro but names no platforms`)
|
|
299
|
+
}
|
|
300
|
+
return { name, jsx: options.jsx === true, flow: options.flow === true, define, platforms }
|
|
301
|
+
}
|
|
302
|
+
|
|
181
303
|
// Run `stasis build`: run esbuild over one recorded entry, following the artifact's
|
|
182
304
|
// recorded import graph EXACTLY (not esbuild's resolver). A specifier the artifact records
|
|
183
305
|
// is always followed from the artifact; `external` only steers the specifiers it doesn't.
|
|
@@ -189,15 +311,24 @@ export async function buildCommand({
|
|
|
189
311
|
format,
|
|
190
312
|
platform = 'node',
|
|
191
313
|
babel = false,
|
|
314
|
+
flow = false,
|
|
192
315
|
minify = false,
|
|
193
316
|
sourcemap = false,
|
|
194
317
|
define = {},
|
|
195
318
|
external = [],
|
|
196
319
|
loader = {},
|
|
320
|
+
metro,
|
|
321
|
+
assetsDest,
|
|
197
322
|
} = {}) {
|
|
198
323
|
if (!artifact) throw new Error('stasis build: an artifact (bundle or lockfile) is required')
|
|
199
324
|
if (!output) throw new Error('stasis build: --output is required (a .js/.cjs/.mjs file, or a directory)')
|
|
200
325
|
if (!Array.isArray(external)) throw new Error('stasis build: external must be an array of esbuild patterns')
|
|
326
|
+
if (metro !== undefined && (typeof metro !== 'string' || !/^[^/,]+$/u.test(metro))) {
|
|
327
|
+
throw new Error(`stasis build: --metro takes the platform to build for, e.g. --metro=ios (got '${metro}')`)
|
|
328
|
+
}
|
|
329
|
+
if (flow && babel) {
|
|
330
|
+
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')
|
|
331
|
+
}
|
|
201
332
|
// --platform=hermes is a stasis-level target, not an esbuild platform (esbuild gets
|
|
202
333
|
// 'neutral'): every code file is downleveled to Hermes' dialect before the final bundle,
|
|
203
334
|
// and the bundle itself must avoid ESM -- Hermes has no import/export.
|
|
@@ -221,14 +352,68 @@ export async function buildCommand({
|
|
|
221
352
|
// Make the entry absolute against the State's root so resolution is independent of process cwd.
|
|
222
353
|
const entryPoint = resolve(state.root, selectedEntry)
|
|
223
354
|
|
|
355
|
+
// The entry's options in the stasis.config.json the artifact carries, under the flags given: jsx as
|
|
356
|
+
// --loader=.js:jsx, flow as --flow (not under --babel, whose config compiles Flow itself), its define
|
|
357
|
+
// under --define's, and metro as --metro for its one platform; of several, --metro picks one, which
|
|
358
|
+
// must be one of them.
|
|
359
|
+
const configured = configEntryOptions(state, selectedEntry)
|
|
360
|
+
if (configured !== null) {
|
|
361
|
+
const taken = []
|
|
362
|
+
if (configured.jsx && loader['.js'] === undefined) {
|
|
363
|
+
loader = { ...loader, '.js': 'jsx' }
|
|
364
|
+
taken.push('--loader=.js:jsx')
|
|
365
|
+
}
|
|
366
|
+
if (configured.flow && !flow && !babel) {
|
|
367
|
+
flow = true
|
|
368
|
+
taken.push('--flow')
|
|
369
|
+
}
|
|
370
|
+
for (const [key, value] of Object.entries(configured.define)) {
|
|
371
|
+
if (Object.hasOwn(define, key)) continue
|
|
372
|
+
define = { ...define, [key]: value }
|
|
373
|
+
taken.push(`--define=${key}=${value}`)
|
|
374
|
+
}
|
|
375
|
+
const { platforms } = configured
|
|
376
|
+
if (platforms !== undefined) {
|
|
377
|
+
const choices = platforms.map((name) => `--metro=${name}`).join(' or ')
|
|
378
|
+
const what = `${CONFIG_FILE} bundles ${selectedEntry} for ${platforms.join(', ')}`
|
|
379
|
+
if (metro === undefined && platforms.length > 1) throw new Error(`stasis build: ${what}: pass ${choices}`)
|
|
380
|
+
if (metro !== undefined && !platforms.includes(metro)) throw new Error(`stasis build: --metro=${metro}, but ${what} alone: pass ${choices}`)
|
|
381
|
+
if (metro === undefined) {
|
|
382
|
+
metro = platforms[0]
|
|
383
|
+
taken.push(`--metro=${metro}`)
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if (taken.length > 0) console.warn(`[stasis] ${selectedEntry}: ${taken.join(' ')}, as ${CONFIG_FILE}'s entries ${JSON.stringify(configured.name)} sets`)
|
|
387
|
+
}
|
|
388
|
+
// --metro, given or the config's, builds as `stasis bundle --metro` reads the files: JSX in .js
|
|
389
|
+
// (--loader=.js:jsx, where no --loader names .js), Flow stripped (--flow, not under --babel, whose
|
|
390
|
+
// config compiles it), and each of METRO_RELEASE_DEFINE's names defined as Metro's release build
|
|
391
|
+
// defines it, where neither --define nor the config gives it a value, as esbuild takes one (JS):
|
|
392
|
+
// outside Metro's prelude, `__DEV__` is no global at all.
|
|
393
|
+
if (metro !== undefined) {
|
|
394
|
+
if (loader['.js'] === undefined) loader = { ...loader, '.js': 'jsx' }
|
|
395
|
+
if (!babel) flow = true
|
|
396
|
+
for (const [key, value] of Object.entries(METRO_RELEASE_DEFINE)) {
|
|
397
|
+
if (!Object.hasOwn(define, key)) define = { ...define, [key]: JSON.stringify(value) }
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
if (assetsDest !== undefined && metro === undefined) {
|
|
401
|
+
throw new Error('stasis build: --assets-dest copies the assets --metro builds; pass --metro=PLATFORM with it')
|
|
402
|
+
}
|
|
403
|
+
|
|
224
404
|
const esbuild = await importEsbuild()
|
|
225
405
|
// Per-file code rewrites the plugin applies before esbuild parses a served file:
|
|
226
406
|
// --babel runs the project's own babel.config.* over the original source (no esbuild
|
|
227
407
|
// pre-transform), and otherwise --platform=hermes uses the built-in downlevel pipeline.
|
|
228
|
-
// Both resolve Babel from the project (cwd), never from stasis's own dependencies.
|
|
408
|
+
// Both resolve Babel from the project (cwd), never from stasis's own dependencies. --flow
|
|
409
|
+
// strips Flow types first, from what esbuild can't parse.
|
|
229
410
|
let transform
|
|
230
411
|
if (babel) transform = createBabelConfigTransform({ projectDir: cwd })
|
|
231
412
|
else if (hermes) transform = createHermesTransform({ projectDir: cwd, esbuild })
|
|
413
|
+
if (flow) transform = transform === undefined ? createFlowTransform(esbuild) : chainTransforms(createFlowTransform(esbuild), transform)
|
|
414
|
+
// --metro=PLATFORM: PLATFORM's target of each per-platform edge, and each asset as Metro builds it.
|
|
415
|
+
const builtAssets = new Map()
|
|
416
|
+
const resource = metro === undefined ? undefined : metroAssetResource(state, metro, builtAssets)
|
|
232
417
|
|
|
233
418
|
const outAbs = resolve(cwd, output)
|
|
234
419
|
const asFile = /\.[cm]?js$/u.test(output)
|
|
@@ -255,24 +440,44 @@ export async function buildCommand({
|
|
|
255
440
|
...(asFile ? { outfile: outAbs } : { outdir: outAbs }),
|
|
256
441
|
// Loader overrides go through the plugin: it serves every in-bundle file, so esbuild's
|
|
257
442
|
// own `loader` option never sees them. e.g. { '.js': 'jsx' } to parse JSX in .js files.
|
|
258
|
-
plugins: [new StasisEsbuild(state, { loaders: new Map(Object.entries(loader)), transform })],
|
|
443
|
+
plugins: [new StasisEsbuild(state, { loaders: new Map(Object.entries(loader)), transform, resource, platform: metro })],
|
|
259
444
|
})
|
|
260
445
|
} catch (err) {
|
|
261
446
|
// Under logLevel:'silent' esbuild puts the real errors on err.errors; surface them, then fail.
|
|
262
447
|
if (Array.isArray(err?.errors) && err.errors.length > 0) {
|
|
263
448
|
const formatted = await esbuild.formatMessages(err.errors, { kind: 'error', color: false })
|
|
264
449
|
for (const line of formatted) process.stderr.write(line)
|
|
450
|
+
// A resource the build reaches with no loader for its extension: what builds one.
|
|
451
|
+
const unloaded = err.errors.map((e) => /^No loader is configured for "([^"]+)" files/u.exec(e.text)?.[1]).find(Boolean)
|
|
452
|
+
if (unloaded !== undefined) {
|
|
453
|
+
process.stderr.write(
|
|
454
|
+
`[stasis] Build a "${unloaded}" resource with --loader=${unloaded}:NAME (an esbuild loader: dataurl, file, text, ...), ` +
|
|
455
|
+
`or with --metro=PLATFORM as the asset module Metro builds of it\n`
|
|
456
|
+
)
|
|
457
|
+
}
|
|
458
|
+
if (metro === undefined && err.errors.some((e) => e.detail?.code === 'ERR_STASIS_PLATFORM_SPECIFIC')) {
|
|
459
|
+
process.stderr.write('[stasis] A --metro multi-platform bundle builds for one platform at a time: pass --metro=PLATFORM (e.g. --metro=ios)\n')
|
|
460
|
+
}
|
|
265
461
|
throw new Error(`stasis build failed: esbuild reported ${err.errors.length} error(s)`, { cause: err })
|
|
266
462
|
}
|
|
267
463
|
throw err
|
|
268
464
|
}
|
|
269
465
|
|
|
270
|
-
//
|
|
271
|
-
|
|
272
|
-
|
|
466
|
+
// --assets-dest's copies of the assets built, as `react-native bundle --assets-dest` lays them out for
|
|
467
|
+
// the platform (@preventive/rn's assetsDestFiles), from the bytes the artifact carries, in a stable
|
|
468
|
+
// order (esbuild loads in parallel).
|
|
469
|
+
const assetFiles = assetsDest === undefined
|
|
470
|
+
? []
|
|
471
|
+
: assetsDestFiles([...builtAssets.keys()].toSorted().map((rel) => builtAssets.get(rel)), metro)
|
|
472
|
+
.map(({ dest, src, contents }) => ({ path: resolve(cwd, assetsDest, dest), contents: contents ?? readCarried(state, src) }))
|
|
473
|
+
const writes = [...result.outputFiles, ...assetFiles]
|
|
474
|
+
|
|
475
|
+
// Refuse to clobber an attested source, checked against the actual output paths before any write.
|
|
476
|
+
for (const { path } of writes) {
|
|
477
|
+
if (attested.has(path)) {
|
|
273
478
|
throw new Error(
|
|
274
|
-
`stasis build: refusing to write '${relative(cwd,
|
|
275
|
-
`choose an --output path outside the artifact's own files`
|
|
479
|
+
`stasis build: refusing to write '${relative(cwd, path)}' -- it is a source the artifact records; ` +
|
|
480
|
+
`choose an --output${assetsDest === undefined ? '' : '/--assets-dest'} path outside the artifact's own files`
|
|
276
481
|
)
|
|
277
482
|
}
|
|
278
483
|
}
|
|
@@ -282,8 +487,13 @@ export async function buildCommand({
|
|
|
282
487
|
for (const line of formatted) process.stderr.write(line)
|
|
283
488
|
}
|
|
284
489
|
|
|
285
|
-
for (const
|
|
490
|
+
for (const { path, contents } of writes) writeFile(path, contents)
|
|
286
491
|
|
|
287
492
|
console.warn(`[stasis] Built ${selectedEntry} from ${kind} ${artifact} to ${output}`)
|
|
493
|
+
if (assetsDest !== undefined) {
|
|
494
|
+
console.warn(`[stasis] Copied ${builtAssets.size} asset${builtAssets.size === 1 ? '' : 's'} (${assetFiles.length} file${assetFiles.length === 1 ? '' : 's'}) for ${metro} to ${assetsDest}`)
|
|
495
|
+
} else if (builtAssets.size > 0) {
|
|
496
|
+
console.warn(`[stasis] Built ${builtAssets.size} asset${builtAssets.size === 1 ? '' : 's'} for ${metro}; pass --assets-dest to copy their files out`)
|
|
497
|
+
}
|
|
288
498
|
return { entry: selectedEntry, dir: asFile ? undefined : outAbs, outfile: asFile ? outAbs : undefined }
|
|
289
499
|
}
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { isAbsolute, relative, resolve } from 'node:path'
|
|
2
2
|
|
|
3
3
|
import { diskHost } from '@exodus/stasis-core/host'
|
|
4
|
-
import { readFileSyncMaybe } from '@exodus/stasis-core/state-util'
|
|
5
4
|
import { isPlainObject, parseBrotliQuality, parseResourcesOption, relativeEscapes, toPosix } from '@exodus/stasis-core/util'
|
|
6
5
|
import { parseFeatureList } from '../loaders/cargo.js'
|
|
7
6
|
import { parseDefineFlags } from '../define.js'
|
|
8
|
-
import { directoryEntryError, isSolidityEntry } from './bundle.js'
|
|
7
|
+
import { directoryEntryError, isSolidityEntry, readConfigFile } from './bundle.js'
|
|
9
8
|
|
|
10
9
|
const CONFIG_FILE = 'stasis.config.json'
|
|
11
10
|
|
|
12
|
-
// `stasis bundle`'s flags, as parseArgs takes them
|
|
11
|
+
// `stasis bundle`'s flags, as parseArgs takes them.
|
|
13
12
|
export const BUNDLE_FLAGS = {
|
|
14
13
|
mapping: { type: 'string' },
|
|
15
14
|
manifests: { type: 'boolean' },
|
|
@@ -38,12 +37,13 @@ export const BUNDLE_FLAGS = {
|
|
|
38
37
|
'cargo-manifests': { type: 'boolean' },
|
|
39
38
|
'brotli-quality': { type: 'string' },
|
|
40
39
|
add: { type: 'boolean' },
|
|
40
|
+
deptree: { type: 'boolean' },
|
|
41
41
|
}
|
|
42
|
-
export const BUNDLE_VALUE_FLAGS = ['--mapping', '--output', '--scope', '--lockfile', '--conditions', '--mainFields', '--babel-aliases', '--platforms', '--resources', '--tsconfig', '--define', '--cargo-features', '--cargo-target', '--brotli-quality', '-o']
|
|
43
42
|
|
|
44
|
-
// The flags
|
|
45
|
-
// built, which stasis.config.json's `entries`
|
|
46
|
-
|
|
43
|
+
// The flags of the bundle as a whole: where and how it is written, and where its dependencies are
|
|
44
|
+
// read from. Every other one says how its entries are built, which stasis.config.json's `entries`
|
|
45
|
+
// sets per entry instead.
|
|
46
|
+
const OUTPUT_FLAGS = new Set(['output', 'lockfile', 'brotli-quality', 'add', 'deptree'])
|
|
47
47
|
// The entry flags taking a list (comma-separated, or repeated), which an entry's options may give as an array.
|
|
48
48
|
const LIST_FLAGS = new Set(['conditions', 'mainFields', 'platforms', 'resources', 'cargo-features'])
|
|
49
49
|
// The entry flags given once per value, which a comma doesn't separate (a define's JSON value may
|
|
@@ -134,9 +134,15 @@ function buildOptions(values, { allSol, allJs, allRust }) {
|
|
|
134
134
|
if (values.conditions !== undefined && conditions.length === 0) {
|
|
135
135
|
throw new Error('--conditions must list at least one condition name (e.g. --conditions=react-native,browser)')
|
|
136
136
|
}
|
|
137
|
-
// --
|
|
137
|
+
// --metro: resolve like Metro/RN (its own conditions + mainFields, platform suffixes per
|
|
138
|
+
// --platforms target). --platforms is repeatable and/or comma-separated; the values union.
|
|
139
|
+
// It turns on --jsx and --flow, as Metro's Babel parses JSX and Flow in every file (and defines what
|
|
140
|
+
// Metro's release build does, see buildJs).
|
|
141
|
+
const metro = Boolean(values.metro)
|
|
142
|
+
// --flow: strip Flow type syntax (via stasis's own flow-remove-types) from a file marked `@flow`
|
|
143
|
+
// before parsing; JS-only.
|
|
138
144
|
if (values.flow && !allJs) throw new Error('--flow is only valid for JS bundles')
|
|
139
|
-
const flow = Boolean(values.flow)
|
|
145
|
+
const flow = Boolean(values.flow) || metro
|
|
140
146
|
// --typescript: resolve like tsc -- a specifier naming a missing JS output (./x.js, .mjs, .cjs,
|
|
141
147
|
// extensionless) lands on its on-disk TS source (./x.ts, .mts, .cts); an existing .js always wins.
|
|
142
148
|
if (values.typescript && !allJs) throw new Error('--typescript is only valid for JS bundles')
|
|
@@ -152,9 +158,6 @@ function buildOptions(values, { allSol, allJs, allRust }) {
|
|
|
152
158
|
if (values.mainFields !== undefined && mainFields.length === 0) {
|
|
153
159
|
throw new Error('--mainFields must list at least one field (e.g. --mainFields=react-native,browser,main)')
|
|
154
160
|
}
|
|
155
|
-
// --metro: resolve like Metro/RN (its own conditions + mainFields, platform suffixes per
|
|
156
|
-
// --platforms target). --platforms is repeatable and/or comma-separated; the values union.
|
|
157
|
-
const metro = Boolean(values.metro)
|
|
158
161
|
// --metro-resolver: resolve through the project's own metro-resolver instead of the built-in
|
|
159
162
|
// approximation. Only meaningful alongside --metro (enforced below and in classifyEntries).
|
|
160
163
|
const metroResolver = Boolean(values['metro-resolver'])
|
|
@@ -166,8 +169,8 @@ function buildOptions(values, { allSol, allJs, allRust }) {
|
|
|
166
169
|
if ((metro || platforms.length > 0) && !allJs) throw new Error('--metro is only valid for JS bundles')
|
|
167
170
|
// --jsx: parse React Native's JSX-in-.js source in the static scanner (off by default; oxc,
|
|
168
171
|
// like tsc, auto-enables JSX for .jsx/.tsx only). Orthogonal to the resolver, so it pairs with
|
|
169
|
-
// plain/--mainFields/--metro alike -- JS-only, since no other entry language is scanned.
|
|
170
|
-
const jsx = Boolean(values.jsx)
|
|
172
|
+
// plain/--mainFields/--metro alike -- JS-only, since no other entry language is scanned. On under --metro.
|
|
173
|
+
const jsx = Boolean(values.jsx) || metro
|
|
171
174
|
if (jsx && !allJs) throw new Error('--jsx is only valid for JS bundles')
|
|
172
175
|
// --package-json: fold each bundled module's package.json into the bundle. JS-only (only JS
|
|
173
176
|
// modules carry an npm package.json; the sol/php/bash/rust bucketizers have no such manifest).
|
|
@@ -209,15 +212,26 @@ function buildOptions(values, { allSol, allJs, allRust }) {
|
|
|
209
212
|
return { mappingFile: values.mapping, manifests, scope: values.scope, conditions, mainFields, metro, metroResolver, babel, babelAliases: values['babel-aliases'], platforms, jsx, flow, typescript, tsconfig: values.tsconfig, resources, define, packageJSON, cargo, cargoFeatures, cargoNoDefaultFeatures, cargoAllFeatures, cargoTarget, cargoManifests }
|
|
210
213
|
}
|
|
211
214
|
|
|
215
|
+
// The CONSENT_FLAGS that run a program of the project's from disk, which needs what it installs on
|
|
216
|
+
// disk: none that --deptree lays out in memory.
|
|
217
|
+
const DISK_RUN_FLAGS = ['babel', 'metro-resolver']
|
|
218
|
+
|
|
212
219
|
// bundleCommand's options for writing the bundle of entries in `lang` from the parsed output flags in `values`.
|
|
213
|
-
function outputOptions(values, { allJs }) {
|
|
220
|
+
function outputOptions(values, { allJs, allSol }) {
|
|
214
221
|
if (values.lockfile && !allJs) throw new Error('--lockfile is only valid for JS bundles')
|
|
222
|
+
// --deptree: the dependencies laid out in memory from the lockfile (pnpm, yarn 1, npm or Soldeer's),
|
|
223
|
+
// never read from disk, which holds none.
|
|
224
|
+
const deptree = Boolean(values.deptree)
|
|
225
|
+
if (deptree && !allJs && !allSol) throw new Error('--deptree is only valid for JS and .sol bundles')
|
|
226
|
+
for (const flag of deptree ? DISK_RUN_FLAGS : []) {
|
|
227
|
+
if (values[flag]) throw new Error(`--${flag} can't be combined with --deptree (it runs ${CONSENT_FLAGS.get(flag)} from disk, where --deptree installs nothing)`)
|
|
228
|
+
}
|
|
215
229
|
// --brotli-quality: valid for every entry language (no allJs gate).
|
|
216
230
|
const brotliQuality = values['brotli-quality'] === undefined ? undefined : parseBrotliQuality('--brotli-quality', values['brotli-quality'])
|
|
217
231
|
// --add merges into the existing bundle at --output, so it can't target write-only stdout.
|
|
218
232
|
const add = Boolean(values.add)
|
|
219
233
|
if (add && values.output === '-') throw new Error('--add cannot be combined with --output=-')
|
|
220
|
-
return { output: values.output, lockfile: values.lockfile, brotliQuality, add }
|
|
234
|
+
return { output: values.output, lockfile: values.lockfile, brotliQuality, add, deptree }
|
|
221
235
|
}
|
|
222
236
|
|
|
223
237
|
// An entry's options in stasis.config.json's `entries` (`what` names it) as the flags they alias, in
|
|
@@ -253,15 +267,7 @@ function entryFlags(what, options) {
|
|
|
253
267
|
// -> { entry: the name as written, values: its options as flags (entryFlags') }. null where there is
|
|
254
268
|
// none.
|
|
255
269
|
export function readConfigEntries(cwd = process.cwd(), host = diskHost) {
|
|
256
|
-
const
|
|
257
|
-
if (text === null) return null
|
|
258
|
-
let config
|
|
259
|
-
try {
|
|
260
|
-
config = JSON.parse(text)
|
|
261
|
-
} catch (cause) {
|
|
262
|
-
throw new Error(`${CONFIG_FILE} is not valid JSON`, { cause })
|
|
263
|
-
}
|
|
264
|
-
const entries = config?.entries
|
|
270
|
+
const entries = readConfigFile(undefined, cwd, host)?.entries
|
|
265
271
|
if (entries === undefined) return null
|
|
266
272
|
if (!isPlainObject(entries)) throw new Error(`${CONFIG_FILE}: entries must be an object of each entry's stasis bundle options`)
|
|
267
273
|
const named = new Map()
|