@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.
Files changed (46) hide show
  1. package/README.md +5 -1
  2. package/bin/stasis.js +191 -149
  3. package/package.json +31 -7
  4. package/src/audit.js +224 -90
  5. package/src/babel.js +44 -37
  6. package/src/bundle-reader.js +24 -4
  7. package/src/cmd/build.js +218 -8
  8. package/src/cmd/bundle-options.js +31 -25
  9. package/src/cmd/bundle.js +1147 -311
  10. package/src/cmd/github-bundle.js +17 -9
  11. package/src/cmd/output.js +10 -3
  12. package/src/cmd/tokens.js +73 -0
  13. package/src/define.js +5 -0
  14. package/src/diff.js +2 -2
  15. package/src/flow.js +42 -0
  16. package/src/installed-commits.js +4 -2
  17. package/src/loaders/bash.js +2 -27
  18. package/src/loaders/cargo.js +6 -6
  19. package/src/loaders/foundry.js +4 -6
  20. package/src/loaders/paths.js +4 -15
  21. package/src/loaders/php.js +3 -5
  22. package/src/loaders/rust.js +19 -39
  23. package/src/loaders/solidity-ownership.js +4 -15
  24. package/src/loaders/solidity.js +4 -67
  25. package/src/metro-assets.js +65 -0
  26. package/src/metro-resolver.js +76 -123
  27. package/src/parse.js +7 -3
  28. package/src/resolve-fields.js +133 -366
  29. package/src/resolve-node.js +9 -7
  30. package/src/resolve-typescript.js +102 -30
  31. package/src/scan.js +154 -54
  32. package/src/sixel.js +333 -0
  33. package/src/table.js +116 -0
  34. package/src/validate-notes.d.ts +18 -0
  35. package/src/validate-notes.js +66 -0
  36. package/src/validate.d.ts +132 -0
  37. package/src/validate.js +1349 -0
  38. package/src/vfs-bundle/entries.js +5 -2
  39. package/src/vfs-bundle/github.js +68 -19
  40. package/src/vfs-bundle/glob.js +420 -0
  41. package/src/vfs-bundle/lazy.js +128 -0
  42. package/src/vfs-bundle/prisma.js +102 -29
  43. package/src/vfs-bundle/tree.js +238 -75
  44. package/src/vfs-bundle/yarnclean.js +100 -0
  45. package/src/vfs-bundle.js +16 -7
  46. package/src/why.js +9 -7
@@ -3,6 +3,7 @@ import { basename, dirname, isAbsolute, join, normalize, resolve } from 'node:pa
3
3
  import { fileURLToPath, pathToFileURL } from 'node:url'
4
4
 
5
5
  import { packageJSONStat, packageJSONText } from '@exodus/stasis-core/bundle-util'
6
+ import { untouchedMemo } from './vfs-bundle/lazy.js'
6
7
 
7
8
  // Node's CommonJS resolution (`createRequire(parent).resolve(spec, { conditions })`) over a
8
9
  // `host` (@exodus/stasis-core/host), mirroring lib/internal/modules/cjs/loader.js and
@@ -87,7 +88,11 @@ function parsePackageName(specifier, base) {
87
88
  return { packageName, packageSubpath: `.${separatorIndex === -1 ? '' : specifier.slice(separatorIndex)}`, isScoped }
88
89
  }
89
90
 
91
+ // `host.touches`, where it has one, counts the reads it makes of what a tree does not hold yet
92
+ // (vfs-bundle/tree.js's vfsHost of a lazy tree): what is read with none is cached alone, or with
93
+ // `host.retouch`, what is read with some too (untouchedMemo's `retouch`).
90
94
  export function createNodeResolver(host) {
95
+ const touches = host.touches ?? (() => 0)
91
96
  // package.json reads, memoized per path; a malformed manifest, or one there that can't be read,
92
97
  // throws ERR_INVALID_PACKAGE_CONFIG on every access.
93
98
  const parsePackage = (pjsonPath) => {
@@ -110,10 +115,9 @@ export function createNodeResolver(host) {
110
115
  const map = (value) => (typeof value === 'string' || (typeof value === 'object' && value !== null) ? value : undefined)
111
116
  return { exists: true, path: pjsonPath, name: string(data.name), main: string(data.main), exports: map(data.exports), imports: map(data.imports) }
112
117
  }
113
- const pkgCache = new Map()
118
+ const packages = untouchedMemo(touches, { retouch: host.retouch })
114
119
  const readPackage = (pjsonPath) => {
115
- let pkg = pkgCache.get(pjsonPath)
116
- if (pkg === undefined) pkgCache.set(pjsonPath, pkg = parsePackage(pjsonPath))
120
+ const pkg = packages(pjsonPath, () => parsePackage(pjsonPath))
117
121
  if (pkg instanceof Error) throw pkg
118
122
  return pkg
119
123
  }
@@ -401,7 +405,7 @@ export function createNodeResolver(host) {
401
405
  }
402
406
 
403
407
  // Node's Module._pathCache: the tree is immutable for the build.
404
- const memo = new Map()
408
+ const memo = untouchedMemo(touches, { retouch: host.retouch })
405
409
 
406
410
  return {
407
411
  // `conditions`: `require.resolve`'s own by default. A builtin returns its id, as it does.
@@ -411,9 +415,7 @@ export function createNodeResolver(host) {
411
415
  parentFile = resolve(parentFile)
412
416
  if (!(conditions instanceof Set)) conditions = new Set(conditions)
413
417
  const key = `${dirname(parentFile)}\0${request}\0${[...conditions].join(',')}${builtins ? '' : '\0nobuiltins'}`
414
- let hit = memo.get(key)
415
- if (hit === undefined) memo.set(key, hit = resolveFilename(parentFile, request, conditions, builtins))
416
- return hit
418
+ return memo(key, () => resolveFilename(parentFile, request, conditions, builtins))
417
419
  },
418
420
  }
419
421
  }
@@ -1,7 +1,8 @@
1
- import { basename, dirname, extname, isAbsolute, join, resolve as resolvePath } from 'node:path'
1
+ import { basename, dirname, extname, isAbsolute, join, relative, resolve as resolvePath } from 'node:path'
2
2
 
3
- import { isTypeDeclaration } from '@exodus/stasis-core/util'
3
+ import { isTypeDeclaration, relativeEscapes } from '@exodus/stasis-core/util'
4
4
  import { diskHost } from '@exodus/stasis-core/host'
5
+ import { untouchedMemo } from './vfs-bundle/lazy.js'
5
6
 
6
7
  // tsc-style module resolution (`--typescript`), shared by BOTH JS resolvers -- scan.js's built-in
7
8
  // Node resolver and resolve-fields.js's legacy-field resolver -- so the flag means one thing
@@ -15,6 +16,9 @@ import { diskHost } from '@exodus/stasis-core/host'
15
16
  // `./dir` -> `./dir/index.ts`), incl. through a directory's package.json `main`;
16
17
  // - the same substitution applied to manifest-declared targets: a package `main`, an `exports`
17
18
  // target, a `#name` `imports` target naming a compiled file whose only on-disk form is TS;
19
+ // - tsc's `outDir` -> `rootDir` inversion: a workspace package's unbuilt output (`./dist/x.js`)
20
+ // maps to the source tsc builds it from (`./src/x.ts`; see fromOutputSource), here and in the
21
+ // field resolvers' own probes;
18
22
  // - tsconfig `compilerOptions.paths` aliases (see loadTsconfigPaths), consulted for bare
19
23
  // specifiers nothing else resolved. Node never probed an alias target, so its completion also
20
24
  // tries the JS extensions after the TS ones (`@/x` -> x.ts, then x.js), as tsc does.
@@ -103,13 +107,13 @@ export function locatePackage(fromDir, spec, host = diskHost) {
103
107
  }
104
108
 
105
109
  // Nearest package.json at/above `file`'s dir — the package whose fields (browser/RN map, imports)
106
- // govern the file's own imports.
107
- export function nearestPackage(file, host = diskHost) {
110
+ // govern the file's own imports. `read` reads a manifest, as readJson does.
111
+ export function nearestPackage(file, host = diskHost, read = readJson) {
108
112
  let dir = dirname(file)
109
113
  while (true) {
110
114
  const pkgPath = join(dir, 'package.json')
111
115
  if (isFile(pkgPath, host)) {
112
- const pkg = readJson(pkgPath, host)
116
+ const pkg = read(pkgPath, host)
113
117
  if (pkg) return { pkgDir: dir, pkg }
114
118
  }
115
119
  const parent = dirname(dir)
@@ -145,8 +149,10 @@ function probeIndex(dir, exts, host) {
145
149
  // directory-only ('.', '..', a trailing '/'), so './' never probes the pathological '.ts' dotfile.
146
150
  // `completion`/`dir` are off for exports/imports targets: Node requires those to name exact files,
147
151
  // so only substitution applies (matching tsc's node16 rules). `js` completes JS extensions too, for
148
- // a base Node never probed (see completionExts).
149
- function probeTypescriptTarget(base, { js = false, dirOnly = false, completion = true, dir = true, host = diskHost } = {}) {
152
+ // a base Node never probed (see completionExts). `outputs` maps what still misses to an unbuilt
153
+ // output's source (fromOutputSource).
154
+ function probeTypescriptTarget(base, { js = false, dirOnly = false, completion = true, dir = true, outputs = null, host = diskHost } = {}) {
155
+ const rules = { js, dirOnly, completion, dir, host }
150
156
  const exts = completionExts(js)
151
157
  if (!dirOnly) {
152
158
  const literal = probe(base, host)
@@ -166,17 +172,83 @@ function probeTypescriptTarget(base, { js = false, dirOnly = false, completion =
166
172
  const pkg = readJson(join(base, 'package.json'), host)
167
173
  const main = typeof pkg?.main === 'string' && pkg.main.length > 0 ? pkg.main : null
168
174
  if (main) {
169
- // LOAD_AS_FILE(main) with substitution/completion, then LOAD_INDEX(main); a broken main
170
- // falls through to the package index, like Node.
175
+ // LOAD_AS_FILE(main) with substitution/completion, then LOAD_INDEX(main), each of its source
176
+ // too where it is unbuilt; a broken main falls through to the package index, like Node.
171
177
  const entry = resolvePath(base, main)
172
- const hit = probeTypescriptTarget(entry, { js, dir: false, host }) ?? probeIndex(entry, exts, host)
178
+ const fromMain = (path) => probeTypescriptTarget(path, { js, dir: false, host }) ?? probeIndex(path, exts, host)
179
+ const hit = fromMain(entry) ?? fromOutputSource(entry, fromMain, { outputs, host, pkgDir: base })
173
180
  if (hit) return hit
174
181
  }
175
- return probeIndex(base, exts, host)
182
+ const hit = probeIndex(base, exts, host)
183
+ if (hit) return hit
184
+ }
185
+ return fromOutputSource(base, (input) => probeTypescriptTarget(input, rules), { outputs, host })
186
+ }
187
+
188
+ // --- tsc's `outDir` -> `rootDir` inversion ---
189
+
190
+ // A package's tsc layout, by the tsconfig.json beside its package.json; null without `outDir`
191
+ // (outputs then sit beside their sources, which substitution maps) or in node_modules (installed
192
+ // packages are used as published). Without `rootDir` the root is the tsconfig's directory, as
193
+ // TypeScript 6 takes it; an older tsc infers it from its inputs, which this cannot.
194
+ export function tsconfigOutputs(pkgDir, host = diskHost) {
195
+ const dir = realpathOr(pkgDir, host)
196
+ if (IN_NODE_MODULES.test(dir)) return null
197
+ const file = join(dir, 'tsconfig.json')
198
+ if (!isFile(file, host)) return null
199
+ const { outDir, rootDir } = loadTsconfigCompilerOptions(file, host)
200
+ if (typeof outDir !== 'string') return null
201
+ return { dir, outDir, rootDir: typeof rootDir === 'string' ? rootDir : dir }
202
+ }
203
+
204
+ function leniently(lenient, read) {
205
+ try {
206
+ return read()
207
+ } catch (error) {
208
+ if (!lenient) throw error
209
+ return null
210
+ }
211
+ }
212
+
213
+ // tsconfigOutputs, cached as a lazy tree allows (untouchedMemo). `lenient` is for builds that only
214
+ // hint, where a tsconfig.json that does not load maps nothing rather than failing the build.
215
+ export function tsconfigOutputsOf({ host = diskHost, lenient = false } = {}) {
216
+ const layouts = untouchedMemo(host.touches, { retouch: host.retouch })
217
+ return (pkgDir) => {
218
+ const dir = realpathOr(pkgDir, host)
219
+ return layouts(dir, () => leniently(lenient, () => tsconfigOutputs(dir, host)))
220
+ }
221
+ }
222
+
223
+ // The package whose tsc build `path` would be an output of: the nearest package.json beside a
224
+ // tsconfig.json, named or not. One without (a `type` marker, a manifest copied into `dist/` to
225
+ // publish from) builds nothing. An installed package is none.
226
+ function packageHolding(path, host) {
227
+ for (let dir = dirname(path); basename(dir) !== 'node_modules'; dir = dirname(dir)) {
228
+ if (isFile(join(dir, 'package.json'), host)) {
229
+ if (inNodeModules(dir, host)) return null
230
+ if (isFile(join(dir, 'tsconfig.json'), host)) return dir
231
+ }
232
+ if (dirname(dir) === dir) return null
176
233
  }
177
234
  return null
178
235
  }
179
236
 
237
+ // tsc's mapping of an output back to its input (tryLoadInputFileForPath): where `path` lies under
238
+ // its package's `outDir`, `probeSource` of the same place under `rootDir` (`dist/x.js` ->
239
+ // `src/x.js`, whose TS siblings substitution finds), else null. Call it only once `path` itself
240
+ // missed: the tsconfig.json is read, and can throw, only then. `pkgDir` is the package that names
241
+ // `path` (its `main`, an `exports` target), where known: its `outDir` may lie outside it
242
+ // (`../../dist/dep`).
243
+ export function fromOutputSource(path, probeSource, { outputs, host = diskHost, pkgDir } = {}) {
244
+ if (outputs == null) return null
245
+ const owner = pkgDir ?? packageHolding(path, host)
246
+ const layout = owner === null ? null : outputs(owner)
247
+ if (layout === null) return null
248
+ const rel = relative(layout.outDir, join(layout.dir, relative(owner, path))) // by the package's real path
249
+ return relativeEscapes(rel) ? null : probeSource(join(layout.rootDir, rel))
250
+ }
251
+
180
252
  // --- package `exports`/`imports` map resolution (candidate generation) ---
181
253
 
182
254
  // Expand a target value (string | array | conditions object | null) to the target strings the
@@ -242,10 +314,13 @@ function manifestTargets(map, subpathKey, conditions) {
242
314
 
243
315
  // tsc's mapping of `key` ('.' or './sub') through the `exports` of the package in `pkgDir` (or of
244
316
  // a '#name' key through its `imports`): the targets `conditions` select, substitution only (Node
245
- // requires exports/imports targets to name exact files), the first on disk; or null.
246
- export function typescriptExportsTarget(pkgDir, exports, key, { conditions = new Set(), host = diskHost } = {}) {
317
+ // requires exports/imports targets to name exact files) or an unbuilt output's source (`outputs`);
318
+ // the first on disk, or null.
319
+ export function typescriptExportsTarget(pkgDir, exports, key, { conditions = new Set(), outputs = (dir) => tsconfigOutputs(dir, host), host = diskHost } = {}) {
320
+ const probeTarget = (path) => probeTypescriptTarget(path, { completion: false, dir: false, host })
247
321
  for (const target of manifestTargets(exports, key, conditions)) {
248
- const hit = probeTypescriptTarget(resolvePath(pkgDir, target), { completion: false, dir: false, host })
322
+ const base = resolvePath(pkgDir, target)
323
+ const hit = probeTarget(base) ?? fromOutputSource(base, probeTarget, { outputs, host, pkgDir })
249
324
  if (hit) return hit
250
325
  }
251
326
  return null
@@ -431,7 +506,8 @@ const declaresWorkspaces = (dir, host) => isFile(join(dir, 'pnpm-workspace.yaml'
431
506
  // the nearest named package.json at or above `cwd` (or the top-most package.json, where none is
432
507
  // named). Never in node_modules: no walk enters a node_modules directory, and resolveTypescriptFallback
433
508
  // matches no alias for a file under one. Each config loads once, when first matched; with `lenient`,
434
- // one that doesn't load matches nothing instead of throwing.
509
+ // one that doesn't load matches nothing instead of throwing. `outputs` is tsconfigOutputsOf's, as
510
+ // lenient.
435
511
  export function packageTsconfigPaths(cwd, { host = diskHost, lenient = false, projectCwd } = {}) {
436
512
  const stop = projectCwd ? realpathOr(resolvePath(projectCwd), host) : null
437
513
  const dirs = [] // cwd and above, while the State's walk would go on; real paths, as importers are read
@@ -460,15 +536,7 @@ export function packageTsconfigPaths(cwd, { host = diskHost, lenient = false, pr
460
536
  const matcherOf = (dir) => {
461
537
  if (!matchers.has(dir)) {
462
538
  const file = join(dir, 'tsconfig.json')
463
- let matcher = null
464
- if (isFile(file, host)) {
465
- try {
466
- matcher = loadTsconfigPaths(file, host)
467
- } catch (error) {
468
- if (!lenient) throw error
469
- }
470
- }
471
- matchers.set(dir, matcher)
539
+ matchers.set(dir, isFile(file, host) ? leniently(lenient, () => loadTsconfigPaths(file, host)) : null)
472
540
  }
473
541
  return matchers.get(dir)
474
542
  }
@@ -477,6 +545,7 @@ export function packageTsconfigPaths(cwd, { host = diskHost, lenient = false, pr
477
545
  const dir = packageOf(parentFile)
478
546
  return dir === null ? [] : (matcherOf(dir)?.matchPaths(spec) ?? [])
479
547
  },
548
+ outputs: tsconfigOutputsOf({ host, lenient }),
480
549
  }
481
550
  }
482
551
 
@@ -505,6 +574,8 @@ export const inNodeModules = (file, host = diskHost) => IN_NODE_MODULES.test(rea
505
574
  // completion too, as Node never probed the target), then
506
575
  // the named package: its `exports` targets (substitution only) when it has
507
576
  // them, else its `main`/index (bare root) or subpath (substitution/completion).
577
+ // Any path naming a package's unbuilt output maps to its source (fromOutputSource), by
578
+ // `paths.outputs` where `paths` has it.
508
579
  export function resolveTypescriptFallback(parentFile, spec, { conditions = new Set(), paths = null, host = diskHost } = {}) {
509
580
  if (inNodeModules(parentFile, host)) return null
510
581
  const hit = typescriptTarget(parentFile, spec, { conditions, paths, host })
@@ -512,17 +583,18 @@ export function resolveTypescriptFallback(parentFile, spec, { conditions = new S
512
583
  }
513
584
 
514
585
  function typescriptTarget(parentFile, spec, { conditions, paths, host }) {
586
+ const outputs = paths?.outputs ?? ((dir) => tsconfigOutputs(dir, host))
515
587
  if (spec.startsWith('#')) {
516
588
  const scope = nearestPackage(parentFile, host)
517
- return scope?.pkg.imports ? typescriptExportsTarget(scope.pkgDir, scope.pkg.imports, spec, { conditions, host }) : null
589
+ return scope?.pkg.imports ? typescriptExportsTarget(scope.pkgDir, scope.pkg.imports, spec, { conditions, outputs, host }) : null
518
590
  }
519
591
  if (spec.startsWith('./') || spec.startsWith('../') || spec === '.' || spec === '..' || isAbsolute(spec)) {
520
592
  const base = isAbsolute(spec) ? spec : resolvePath(dirname(parentFile), spec)
521
- return probeTypescriptTarget(base, { dirOnly: DIR_ONLY_SPEC.test(spec), host })
593
+ return probeTypescriptTarget(base, { dirOnly: DIR_ONLY_SPEC.test(spec), outputs, host })
522
594
  }
523
595
  if (paths) {
524
596
  for (const target of paths.matchPaths(spec, parentFile)) {
525
- const hit = probeTypescriptTarget(target, { js: true, dirOnly: target.endsWith('/'), host })
597
+ const hit = probeTypescriptTarget(target, { js: true, dirOnly: target.endsWith('/'), outputs, host })
526
598
  if (hit) return hit
527
599
  }
528
600
  }
@@ -531,11 +603,11 @@ function typescriptTarget(parentFile, spec, { conditions, paths, host }) {
531
603
  const pkg = readJson(join(loc.pkgDir, 'package.json'), host) ?? {}
532
604
  if (pkg.exports != null) {
533
605
  // `exports` fully governs a bare import (main is not a fallback); targets name exact files.
534
- return typescriptExportsTarget(loc.pkgDir, pkg.exports, loc.subpath === '' ? '.' : `./${loc.subpath}`, { conditions, host })
606
+ return typescriptExportsTarget(loc.pkgDir, pkg.exports, loc.subpath === '' ? '.' : `./${loc.subpath}`, { conditions, outputs, host })
535
607
  }
536
608
  if (loc.subpath === '') {
537
609
  // Bare package root: LOAD_AS_DIRECTORY only (never `node_modules/dep.ts`).
538
- return probeTypescriptTarget(loc.pkgDir, { dirOnly: true, host })
610
+ return probeTypescriptTarget(loc.pkgDir, { dirOnly: true, outputs, host })
539
611
  }
540
- return probeTypescriptTarget(join(loc.pkgDir, loc.subpath), { dirOnly: DIR_ONLY_SPEC.test(spec), host })
612
+ return probeTypescriptTarget(join(loc.pkgDir, loc.subpath), { dirOnly: DIR_ONLY_SPEC.test(spec), outputs, host })
541
613
  }