@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.
Files changed (66) hide show
  1. package/README.md +7 -2
  2. package/bin/stasis.js +263 -446
  3. package/package.json +38 -13
  4. package/src/audit-corrections.js +11 -42
  5. package/src/audit.js +256 -212
  6. package/src/babel.js +119 -82
  7. package/src/bundle-from-lockfile.js +9 -21
  8. package/src/bundle-reader.js +24 -8
  9. package/src/bundle.js +0 -3
  10. package/src/cmd/build.js +211 -117
  11. package/src/cmd/bundle-options.js +347 -0
  12. package/src/cmd/bundle.js +1718 -908
  13. package/src/cmd/diff.js +1 -4
  14. package/src/cmd/github-bundle.js +48 -21
  15. package/src/cmd/output.js +24 -12
  16. package/src/cmd/sbom.js +6 -9
  17. package/src/cmd/tokens.js +60 -0
  18. package/src/define.js +218 -0
  19. package/src/diff.js +73 -129
  20. package/src/esbuild.js +0 -4
  21. package/src/flow.js +27 -0
  22. package/src/installed.js +265 -0
  23. package/src/json-stream.js +4 -9
  24. package/src/loader-mock.js +3 -8
  25. package/src/loader.js +1 -5
  26. package/src/loaders/bash.js +43 -120
  27. package/src/loaders/cargo.js +301 -808
  28. package/src/loaders/foundry.js +112 -290
  29. package/src/loaders/paths.js +15 -26
  30. package/src/loaders/php.js +221 -475
  31. package/src/loaders/rust.js +518 -1461
  32. package/src/loaders/solidity-ownership.js +51 -142
  33. package/src/loaders/solidity.js +42 -188
  34. package/src/loaders/toml.js +7 -17
  35. package/src/lockfile.js +0 -3
  36. package/src/metro-assets.js +41 -0
  37. package/src/metro-resolver.js +58 -133
  38. package/src/metro-transformer.js +0 -6
  39. package/src/metro.js +0 -4
  40. package/src/mock.js +32 -80
  41. package/src/parse.js +8 -7
  42. package/src/prune.js +0 -3
  43. package/src/resolve-fields.js +170 -235
  44. package/src/resolve-node.js +33 -46
  45. package/src/resolve-typescript.js +166 -298
  46. package/src/rollup.js +0 -4
  47. package/src/run-installed.js +10 -0
  48. package/src/sbom.js +70 -97
  49. package/src/scan.js +241 -278
  50. package/src/sixel.js +310 -0
  51. package/src/table.js +95 -0
  52. package/src/validate-notes.d.ts +19 -0
  53. package/src/validate-notes.js +72 -0
  54. package/src/validate.d.ts +159 -0
  55. package/src/validate.js +1315 -0
  56. package/src/vfs-bundle/entries.js +54 -46
  57. package/src/vfs-bundle/github.js +115 -92
  58. package/src/vfs-bundle/glob.js +379 -0
  59. package/src/vfs-bundle/lazy.js +99 -0
  60. package/src/vfs-bundle/prisma.js +235 -105
  61. package/src/vfs-bundle/soldeer.js +28 -49
  62. package/src/vfs-bundle/tree.js +434 -259
  63. package/src/vfs-bundle/yarnclean.js +79 -0
  64. package/src/vfs-bundle.js +25 -51
  65. package/src/webpack.js +0 -4
  66. package/src/why.js +79 -231
package/src/scan.js CHANGED
@@ -1,80 +1,62 @@
1
- import { dirname, extname, resolve as resolvePath, relative } from 'node:path'
1
+ import { basename, dirname, extname, isAbsolute, join, resolve as resolvePath, relative } from 'node:path'
2
2
  import { fileURLToPath, pathToFileURL } from 'node:url'
3
3
  import { createRequire, isBuiltin } from 'node:module'
4
4
  import assert from 'node:assert/strict'
5
5
  import { packageType } from '@exodus/stasis-core/bundle-util'
6
- import { classifyExtension, classifyFormat, isTypeDeclaration, relativeEscapes } from '@exodus/stasis-core/util'
7
- import { inNodeModules, resolveTypescriptFallback, typescriptSiblings } from './resolve-typescript.js'
6
+ import { classifyExtension, classifyFormat, JS_EXTENSIONS, nestedMap, relativeEscapes } from '@exodus/stasis-core/util'
7
+ import { inNodeModules, isSourceFile, resolveTypescriptFallback, typescriptSiblings } from './resolve-typescript.js'
8
+ import { RELATIVE_REQUEST } from './resolve-node.js'
9
+ import { branchFolder, parseDefine } from './define.js'
8
10
  import { diskHost } from '@exodus/stasis-core/host'
11
+ import { hostMemo } from './vfs-bundle/lazy.js'
12
+ import { FLOW_EXTS, hasFlowPragma, stripFlowTypes } from './flow.js'
9
13
 
10
14
  // Static require/import graph walker: parses source, never loads or executes user code.
11
- // Dynamic specifiers (`require(name)`, `import('./'+x)`) are recorded as unresolved.
12
-
13
- // .jsx/.tsx need no flag: oxc parses them as JSX/TSX purely from the filename, so a reached one
14
- // -- e.g. a package whose React Native entry is src/index.tsx -- is parsed and carried like any .ts.
15
- const SCRIPT_EXTS = new Set(['.js', '.cjs', '.mjs', '.ts', '.cts', '.mts', '.jsx', '.tsx'])
16
- const RESOLVABLE_EXTS = new Set([...SCRIPT_EXTS, '.json'])
17
- // The extensions whose name can't tell whether they hold JSX, parsed with JSX enabled only under
18
- // `jsx` (React Native's JSX-in-.js convention). Excludes the .ts family: TypeScript's `<T>`
19
- // generics collide with JSX, so tsc/oxc reserve JSX for .tsx — JSX-in-TS belongs in a .tsx file.
15
+
16
+ const RESOLVABLE_EXTS = new Set([...JS_EXTENSIONS, '.json'])
17
+ export const isResolvableExt = (ext) => RESOLVABLE_EXTS.has(ext)
18
+ // Parsed with JSX only under `jsx` (React Native's JSX-in-.js). Not the .ts family: TypeScript's `<T>` generics
19
+ // collide with JSX, so tsc and oxc reserve JSX for .tsx.
20
20
  const JSX_EXTS = new Set(['.js', '.cjs', '.mjs'])
21
21
 
22
- // Flow type syntax lives in the JS (non-TS) family; oxc parses TS natively, and running
23
- // flow-remove-types over a .ts/.tsx file would corrupt TS-only constructs, so --flow only strips
24
- // these. .jsx is included (it's JS + JSX, and can carry Flow types like .js); .tsx is not (it's
25
- // TypeScript). flow-remove-types preserves JSX, so a stripped .jsx re-parses under lang:jsx.
26
- const FLOW_EXTS = new Set(['.js', '.cjs', '.mjs', '.jsx'])
27
22
  const TS_EXTS = new Set(['.ts', '.cts', '.mts', '.tsx'])
28
23
 
29
- // Required lazily so non-JS bundlers don't load the native oxc parser.
24
+ // Required lazily so non-JS bundlers don't load the native oxc parser. Raw transfer, where the platform takes it,
25
+ // hands each AST over from the parser's own buffer rather than as JSON, which takes longer to parse than the source.
30
26
  let _parser
31
27
  export function getParser() {
32
- _parser ??= createRequire(import.meta.url)('oxc-parser')
33
- return _parser
34
- }
35
-
36
- // flow-remove-types is an optional dependency, loaded lazily and only under --flow: it strips
37
- // Flow type syntax down to plain JS before oxc (which parses JS/TS, not Flow) sees it. A missing
38
- // dep is an env error, so surface it with an actionable install hint rather than a bare resolve throw.
39
- let _flowRemoveTypes
40
- function getFlowRemoveTypes() {
41
- if (_flowRemoveTypes) return _flowRemoveTypes
42
- try {
43
- _flowRemoveTypes = createRequire(import.meta.url)('flow-remove-types')
44
- } catch (cause) {
45
- throw new Error(
46
- "--flow needs the optional 'flow-remove-types' dependency; install it (e.g. `npm i flow-remove-types`)",
47
- { cause }
48
- )
28
+ if (_parser === undefined) {
29
+ const oxc = createRequire(import.meta.url)('oxc-parser')
30
+ _parser = oxc.rawTransferSupported() ? { ...oxc, parseSync: (file, source, options) => oxc.parseSync(file, source, { experimentalRawTransfer: true, ...options }) } : oxc
49
31
  }
50
- return _flowRemoveTypes
32
+ return _parser
51
33
  }
52
34
 
53
- // classifyFormat is the shared name->format authority (static and runtime bundles must agree); it
54
- // returns null for the .js/.ts family whose module system comes from package.json `type` (null =
55
- // detect from syntax, signaled up as null).
56
- function formatForFile(file, host) {
35
+ // null for a typeless .js/.ts: Node detects its format from its syntax.
36
+ function formatForFile(file, host, kept) {
57
37
  const format = classifyFormat(file)
58
38
  if (format != null) return format
59
- const type = packageType(file, host)
60
- if (type === null) return null
61
- return `${type}${extname(file) === '.ts' ? '-typescript' : ''}`
39
+ const type = packageType(file, host, kept)
40
+ return type === null ? null : `${type}${extname(file) === '.ts' ? '-typescript' : ''}`
62
41
  }
63
42
 
64
- // The errors oxc reports for a parse, but its warnings and advice.
65
43
  export const syntaxErrors = (parsed) => (parsed.errors ?? []).filter((e) => e.severity !== 'Warning' && e.severity !== 'Advice')
66
44
 
67
45
  export function literalSpec(node) {
68
46
  if (!node) return null
69
47
  if (node.type === 'Literal' && typeof node.value === 'string') return node.value
70
- if (node.type === 'TemplateLiteral' && node.expressions.length === 0 && node.quasis.length === 1) {
71
- return node.quasis[0].value.cooked
72
- }
48
+ if (node.type === 'TemplateLiteral' && node.expressions.length === 0 && node.quasis.length === 1) return node.quasis[0].value.cooked
73
49
  return null
74
50
  }
75
51
 
76
- // Walks require()/import() calls only; ESM static imports/re-exports come from oxc module records.
77
- function findCallSpecifiers(ast, push) {
52
+ const NO_CHILD_KEYS = new Set(['type', 'loc', 'range', 'start', 'end'])
53
+
54
+ // `fold`: define.js's branchFolder, or null. The side of a branch that can't run is never walked.
55
+ function findCallSpecifiers(ast, push, fold = null) {
56
+ const pushSpec = (kind, node) => {
57
+ const spec = literalSpec(node)
58
+ push(spec == null ? { kind, dynamic: true } : { kind, spec })
59
+ }
78
60
  const visit = (node) => {
79
61
  if (!node || typeof node !== 'object') return
80
62
  if (Array.isArray(node)) {
@@ -82,67 +64,46 @@ function findCallSpecifiers(ast, push) {
82
64
  return
83
65
  }
84
66
  if (typeof node.type !== 'string') return
67
+ const live = fold?.(node)
68
+ if (live !== undefined) {
69
+ for (const c of live) visit(c)
70
+ return
71
+ }
85
72
 
86
73
  if (node.type === 'CallExpression') {
87
74
  const { callee, arguments: args } = node
88
- if (callee.type === 'Identifier' && callee.name === 'require' && args.length >= 1) {
89
- const spec = literalSpec(args[0])
90
- if (spec != null) push({ kind: 'require', spec })
91
- else push({ kind: 'require', dynamic: true })
92
- }
75
+ if (callee.type === 'Identifier' && callee.name === 'require' && args.length >= 1) pushSpec('require', args[0])
93
76
  } else if (node.type === 'ImportExpression') {
94
- const spec = literalSpec(node.source)
95
- if (spec != null) push({ kind: 'dynamic-import', spec })
96
- else push({ kind: 'dynamic-import', dynamic: true })
77
+ pushSpec('dynamic-import', node.source)
97
78
  } else if (node.type === 'TSImportEqualsDeclaration') {
98
- // `import lib = require('./dep.cts')`: TS-only CJS import; record the real edge.
99
- // Qualified-name form (`import A = B.C`) carries no file edge.
100
- if (node.moduleReference?.type === 'TSExternalModuleReference') {
101
- const spec = literalSpec(node.moduleReference.expression)
102
- if (spec != null) push({ kind: 'require', spec })
103
- else push({ kind: 'require', dynamic: true })
104
- }
79
+ // `import lib = require('./dep.cts')`; the qualified-name form (`import A = B.C`) carries no file edge.
80
+ if (node.moduleReference?.type === 'TSExternalModuleReference') pushSpec('require', node.moduleReference.expression)
105
81
  }
106
82
 
107
- for (const key of Object.keys(node)) {
108
- if (key === 'type' || key === 'loc' || key === 'range' || key === 'start' || key === 'end') continue
109
- visit(node[key])
110
- }
83
+ for (const key of Object.keys(node)) if (!NO_CHILD_KEYS.has(key)) visit(node[key])
111
84
  }
112
85
  visit(ast)
113
86
  }
114
87
 
115
- // The import specifiers `program` holds, as { kind, spec } ({ kind, dynamic: true } for a computed
116
- // one). STATEMENT-level type-ness decides an edge, matching what survives type erasure at runtime
117
- // (verbatimModuleSyntax / Node's own type stripping): an `import type` / `export type` statement is
118
- // erased whole, so no edge; a statement that merely lists inline `type` specifiers
119
- // (`import { type A }`, `export { type B } from`) -- or none at all (`import {}`, `export {} from`)
120
- // -- still loads its module, so its edge is real. oxc's module records can't draw that line
121
- // (`import type { A }` and `import { type A }` yield identical all-isType entries, and
122
- // `export {} from` yields no entry at all), so the statements are read off the AST, where
123
- // importKind/exportKind carry it.
124
- function collectSpecifiers(program) {
88
+ // Statement-level type-ness decides an edge, as type erasure does (`import { type A }` still loads its module). oxc's
89
+ // module records can't tell them apart, so the statements are read off the AST.
90
+ function collectSpecifiers(program, defines = new Map(), typesOnly = () => false) {
125
91
  const specs = []
126
92
  for (const node of program.body) {
127
93
  if (node.type === 'ImportDeclaration') {
128
- if (node.importKind !== 'type') specs.push({ kind: 'import', spec: node.source.value })
94
+ if (node.importKind !== 'type' && !typesOnly(node)) specs.push({ kind: 'import', spec: node.source.value })
129
95
  } else if (node.type === 'ExportNamedDeclaration') {
130
- if (node.source != null && node.exportKind !== 'type') specs.push({ kind: 'export-from', spec: node.source.value })
96
+ if (node.source != null && node.exportKind !== 'type' && !typesOnly(node)) specs.push({ kind: 'export-from', spec: node.source.value })
131
97
  } else if (node.type === 'ExportAllDeclaration') {
132
98
  if (node.exportKind !== 'type') specs.push({ kind: 'export-from', spec: node.source.value })
133
99
  }
134
100
  }
135
- findCallSpecifiers(program, (s) => specs.push(s))
101
+ findCallSpecifiers(program, (s) => specs.push(s), branchFolder(defines, program))
136
102
  return specs
137
103
  }
138
104
 
139
- // Whether `entries` (absolute paths) import TS sources by their OUTPUT names -- tsc's convention,
140
- // which only --typescript resolves -- so the build can take --typescript as given: every entry is
141
- // TypeScript, they hold at least one relative import naming a JS output (`./a.js`, or .jsx/.mjs/
142
- // .cjs), and not one of those is on disk while the TS source it maps to is (`./a.ts`; see
143
- // typescriptSiblings), outside node_modules as the resolver requires. Any part missing is no tell, and neither is an entry that doesn't parse as
144
- // the scan parses it (Scan#parseFile; the scan reports it), nor one in node_modules, where
145
- // --typescript maps nothing. Read off the entries alone, before the scan.
105
+ // Whether `entries` import TS sources by their output names (`./a.js` absent, its TS source there), tsc's convention,
106
+ // so the build can take --typescript as given.
146
107
  export function importsTypescriptByOutputName(entries, host = diskHost) {
147
108
  if (!entries.every((entry) => TS_EXTS.has(extname(entry)) && !inNodeModules(entry, host))) return false
148
109
  // Outside the try, as in the scan: a missing oxc-parser is an env error, not an entry's.
@@ -165,18 +126,26 @@ export function importsTypescriptByOutputName(entries, host = diskHost) {
165
126
  }
166
127
  }
167
128
  // tsc's pick, the first TS source on disk, as the resolver maps it: none where it lies in node_modules.
168
- const isSource = (file) => !isTypeDeclaration(file) && (host.stat(file)?.isFile() ?? false)
169
129
  const mapsTo = (target) => {
170
- const pick = typescriptSiblings(target).find(isSource)
130
+ const pick = typescriptSiblings(target).find((file) => isSourceFile(file, host))
171
131
  return pick !== undefined && !inNodeModules(pick, host)
172
132
  }
173
133
  return targets.length > 0 && targets.every((target) => host.stat(target) === null && mapsTo(target))
174
134
  }
175
135
 
176
- function condKey(set) {
177
- return [...set].join(', ')
136
+ const TYPE_KINDS = new Set(['type', 'typeof'])
137
+
138
+ // Babel's Flow and TypeScript transforms erase such a statement whole (Node keeps it, as `import {} from`). --flow blanks
139
+ // the types in place in `strippedSource`. The Metro plugin's transformer draws the same line.
140
+ function listsTypesOnly(node, { source, strippedSource }) {
141
+ const specifiers = node.specifiers ?? []
142
+ if (specifiers.length > 0) return specifiers.every((specifier) => TYPE_KINDS.has(specifier.importKind ?? specifier.exportKind))
143
+ return strippedSource !== null && source.slice(node.start, node.end) !== strippedSource.slice(node.start, node.end)
178
144
  }
179
145
 
146
+ // What a build is reported by (EXODUS_STASIS_DEBUG); see #scanKept.
147
+ export const scanCounts = { parsed: 0, resolved: 0, replayed: 0 }
148
+
180
149
  export class Scan {
181
150
  files = new Map()
182
151
  imports = new Map()
@@ -184,38 +153,21 @@ export class Scan {
184
153
  parseErrors = []
185
154
  entries = new Set()
186
155
 
187
- // `conditions` is additive on top of the edge-context base set. `resolve` optionally
188
- // replaces Node's resolver (resolve-fields.js): returns `{ url } | { empty } | { builtin } | null`
189
- // (`empty` = a browser/react-native `false` redirect) and owns builtin handling when set.
190
- // `jsx` opts the .js/.cjs/.mjs family into JSX syntax (React Native's JSX-in-.js convention);
191
- // off by default because oxc, like tsc, only auto-enables JSX for .jsx/.tsx by extension.
192
- // `flow`: strip Flow type syntax from JS-family sources before parsing (see #scanFile).
193
- // `typescript`: retry a failed resolution with tsc's mapping (see #typescriptResolve), with
194
- // `typescriptPaths` (a tsconfig paths matcher; see resolveTypescriptFallback) adding tsconfig alias support; only consulted
195
- // on the built-in (Node) resolver -- a custom `resolve` owns its own TS handling. Without it, the
196
- // mapping only names the file a miss would resolve to under --typescript (see #typescriptCandidate).
197
- // `typescriptResolve`: the custom `resolve`'s --typescript twin (same shape), consulted only for
198
- // that hint; null when there is no custom resolver or `typescript` is on.
199
- // `resources` (a `parseResourcesOption` Set of extensions/filenames): reached files matching it
200
- // are carried as opaque resources (bytes only) rather than rejected as un-carryable -- for graphs
201
- // that aren't fully loadable in JS (e.g. Metro consuming .png/.svg assets).
202
- // `host`: the filesystem the walk reads and resolves through (@exodus/stasis-core/host).
203
- constructor({ conditions = [], resolve = null, jsx = false, flow = false, typescript = false, typescriptPaths = null, typescriptResolve = null, resources = new Set(), host = diskHost } = {}) {
156
+ // `resolve`: a replacement for Node's resolver (resolve-fields.js), owning builtins. Without `typescript`, tsc's
157
+ // mapping only hints where a miss would land (#typescriptCandidate). `babel`: built by Babel (Metro), whose transforms
158
+ // erase an import listing types alone, but in a TypeScript file `keepsTypeImports(file)` holds.
159
+ constructor({ conditions = [], resolve = null, jsx = false, flow = false, typescript = false, typescriptPaths = null, typescriptResolve = null, resources = new Set(), define, babel = false, keepsTypeImports = null, host = diskHost, memo = null, missTargets = false } = {}) {
204
160
  this.extraConditions = [...conditions]
205
161
  this.customResolve = resolve
206
- this.typescriptResolve = typescriptResolve
207
- this.jsx = jsx
208
- this.flow = flow
209
- this.typescript = typescript
210
- this.typescriptPaths = typescriptPaths
211
- this.resources = resources
212
- this.host = host
162
+ this.defines = parseDefine('scan', define)
163
+ Object.assign(this, { typescriptResolve, jsx, flow, typescript, typescriptPaths, missTargets, resources, babel, keepsTypeImports, host, memo })
164
+ this.#types = hostMemo(host)
213
165
  }
214
166
 
215
- // A reached file whose extension/filename is in the --resources allowlist: carried as an opaque
216
- // resource (bytes only, no parse, no edges), not rejected as "a source bundle can't carry".
217
- // classifyExtension returns 'resource' only for non-code names in the allowlist, so this never
218
- // shadows a script/json extension (parseResourcesOption rejects code extensions).
167
+ // The `type` of each package.json the files scanned are under (formatForFile's).
168
+ #types
169
+
170
+ // classifyExtension gives 'resource' only for a non-code name.
219
171
  #isResource(file) {
220
172
  return this.resources.size > 0 && classifyExtension(file, this.resources) === 'resource'
221
173
  }
@@ -229,43 +181,87 @@ export class Scan {
229
181
  this.entries.add(url)
230
182
  queue.push(url)
231
183
  }
232
- while (queue.length > 0) {
233
- const url = queue.shift()
184
+ // FIFO by an index, not shift(): the queue holds every edge's target, and shift() copies a large one.
185
+ for (let head = 0; head < queue.length; head++) {
186
+ const url = queue[head]
234
187
  if (this.files.has(url)) continue
235
- this.#scanFile(url, queue)
188
+ if (this.memo === null) {
189
+ scanCounts.parsed++
190
+ this.#scanFile(url, queue)
191
+ } else {
192
+ this.#scanKept(url, queue)
193
+ }
236
194
  }
237
195
  return this
238
196
  }
239
197
 
240
- // Must match the runtime resolver's condition set (omitting one resolves `exports`-gated
241
- // packages to a different file) AND its order: the recorded key must equal the runtime
242
- // hooks' string for exact lookups. Extras go where Node puts user conditions in each set.
198
+ // The parse of the file being scanned a scan before kept, and the parse of it made here.
199
+ #reparse
200
+ #parse
201
+
202
+ // `memo`: url -> what a scan read of it, kept across a lazy tree's rounds. A file is kept whole where reading it
203
+ // touched no package not unpacked yet (`host.touches()`); else its parse alone, so the next scan parses it no more.
204
+ #scanKept(url, queue) {
205
+ const kept = this.memo.get(url)
206
+ scanCounts[kept?.queued !== undefined ? 'replayed' : kept?.parse === undefined ? 'parsed' : 'resolved'] += 1
207
+ if (kept?.queued !== undefined) {
208
+ this.files.set(url, kept.file)
209
+ for (const [key, specMap] of kept.imports) nestedMap(this.imports, key).set(url, specMap)
210
+ this.unresolved.push(...kept.unresolved)
211
+ this.parseErrors.push(...kept.parseErrors)
212
+ queue.push(...kept.queued)
213
+ for (const args of kept.warnings) console.warn(...args)
214
+ return
215
+ }
216
+ const touches = this.host.touches()
217
+ const at = { unresolved: this.unresolved.length, parseErrors: this.parseErrors.length, queue: queue.length }
218
+ // The scan of a file is synchronous: what console.warn is given meanwhile is the file's.
219
+ const warnings = []
220
+ const warn = console.warn
221
+ console.warn = (...args) => {
222
+ warnings.push(args)
223
+ warn.apply(console, args)
224
+ }
225
+ this.#reparse = kept?.parse
226
+ this.#parse = undefined
227
+ try {
228
+ this.#scanFile(url, queue)
229
+ } finally {
230
+ console.warn = warn
231
+ this.#reparse = undefined
232
+ }
233
+ if (this.host.touches() !== touches) {
234
+ if (this.#parse !== undefined) this.memo.set(url, { parse: this.#parse })
235
+ return
236
+ }
237
+ this.memo.set(url, {
238
+ file: this.files.get(url),
239
+ imports: [...this.imports].filter(([, byParent]) => byParent.has(url)).map(([key, byParent]) => [key, byParent.get(url)]),
240
+ unresolved: this.unresolved.slice(at.unresolved),
241
+ parseErrors: this.parseErrors.slice(at.parseErrors),
242
+ queued: queue.slice(at.queue),
243
+ warnings,
244
+ })
245
+ }
246
+
247
+ // Node's: the runtime resolver's conditions in its order (the recorded key must equal the runtime hooks' string).
248
+ // A custom resolver's: what esbuild, webpack and Metro assert (resolve-fields.js's resolveConditions).
243
249
  #conditionSet(context) {
250
+ if (this.customResolve) return new Set([context, 'default', ...this.extraConditions])
244
251
  return context === 'import'
245
252
  ? new Set(['node', 'import', 'module-sync', 'node-addons', ...this.extraConditions])
246
253
  : new Set(['require', 'node', 'node-addons', ...this.extraConditions, 'module-sync'])
247
254
  }
248
255
 
249
- // `recovered`: false = parser crashed, nothing salvaged; true = oxc returned but we
250
- // discarded its partial module records (module-family only; see #scanFile).
256
+ // `recovered`: false where the parser crashed; true where oxc's partial module records were discarded.
251
257
  #recordParseError(url, format, message, recovered) {
252
258
  this.files.set(url, { format, edges: [], parseError: message })
253
259
  this.parseErrors.push({ url, format, message, recovered })
254
260
  }
255
261
 
256
- // --typescript: resolve `spec` the way tsc maps TS sources, invoked only AFTER Node's own
257
- // resolution missed -- so an on-disk `.js` (or any Node-resolvable target) always wins, and the
258
- // mapping only completes a failed resolution, never rewrites a successful one. The rules
259
- // (extension substitution, extensionless/index completion, manifest-target and tsconfig-paths
260
- // mapping) live in the shared dispatcher (resolve-typescript.js), so this resolver and the
261
- // legacy-field one cannot drift. The hit is realpathed to match req.resolve's canonical output
262
- // (every other recorded edge is a realpath).
262
+ // Only once Node's resolution missed, so it never rewrites a successful one; realpathed, as every edge is.
263
263
  #typescriptResolve(parentFile, spec, conditions) {
264
- const hit = resolveTypescriptFallback(parentFile, spec, {
265
- conditions,
266
- paths: this.typescriptPaths,
267
- host: this.host,
268
- })
264
+ const hit = resolveTypescriptFallback(parentFile, spec, { conditions, paths: this.typescriptPaths, host: this.host })
269
265
  if (hit == null) return null
270
266
  try {
271
267
  return this.host.realpath(hit)
@@ -274,9 +270,7 @@ export class Scan {
274
270
  }
275
271
  }
276
272
 
277
- // The file: URL a miss would resolve to under --typescript, or null: a hint for the unresolved
278
- // report only, never an edge. A custom resolver asks its --typescript twin; the built-in one
279
- // tsc's mapping, as #typescriptResolve gives it under --typescript.
273
+ // The file: URL a miss would resolve to under --typescript: a hint for the unresolved report only, never an edge.
280
274
  #typescriptCandidate(parentFile, spec, conditions) {
281
275
  if (this.typescript) return null
282
276
  if (this.customResolve) return this.typescriptResolve?.(parentFile, spec, conditions)?.url ?? null
@@ -284,43 +278,60 @@ export class Scan {
284
278
  return hit == null ? null : pathToFileURL(hit).toString()
285
279
  }
286
280
 
287
- // Strip Flow type syntax to plain JS via the optional flow-remove-types dep (resolved lazily; a
288
- // missing dep is an env error and propagates). Only invoked as a parse fallback (see #scanFile),
289
- // so plain-JS files never reach it. Returns the rewritten source, or null when the transform
290
- // itself fails on this file -- the caller then keeps oxc's original parse error.
291
- #stripFlow(src, file) {
292
- const flowRemoveTypes = getFlowRemoveTypes()
281
+ // Where a miss would have landed, for vfs-bundle/prisma.js to match to generator outputs: a path's file, the
282
+ // resolver's `target` (a custom one's missTargets), and under --typescript where a tsconfig alias points.
283
+ #missTargets(parentFile, spec, target) {
284
+ const path = RELATIVE_REQUEST.test(spec) || isAbsolute(spec)
285
+ const aliased = !path && !spec.startsWith('#') && this.typescript && this.typescriptPaths !== null && !inNodeModules(parentFile, this.host)
286
+ return [
287
+ ...(path ? [resolvePath(dirname(parentFile), spec)] : []),
288
+ ...(typeof target === 'string' ? [target] : Array.isArray(target) ? target : []),
289
+ ...(aliased ? this.typescriptPaths.matchPaths(spec, parentFile) : []),
290
+ ].map((at) => this.#realTarget(at))
291
+ }
292
+
293
+ // A missing file by its deepest ancestor that is there, realpathed (a package reached through node_modules is a link).
294
+ #realTarget(path) {
295
+ const rest = []
296
+ for (let at = path; ; at = dirname(at)) {
297
+ try {
298
+ return join(this.host.realpath(at), ...rest)
299
+ } catch {
300
+ if (dirname(at) === at) return path
301
+ rest.unshift(basename(at))
302
+ }
303
+ }
304
+ }
305
+
306
+ // null where stripping fails: the caller keeps oxc's parse, saying so where that failed (`failed`).
307
+ #stripFlow(src, file, failed) {
293
308
  try {
294
- // `all: true` ignores the @flow/@noflow pragma (RN sources mix both); the transform blanks
295
- // types with whitespace, so byte offsets and every import specifier are preserved.
296
- return flowRemoveTypes(src, { all: true }).toString()
309
+ return stripFlowTypes(src)
297
310
  } catch (cause) {
298
- console.warn(`[stasis] --flow: could not strip Flow types from ${file}; leaving it to oxc (${cause.message})`)
311
+ if (failed) console.warn(`[stasis] --flow: could not strip Flow types from ${file}; leaving it to oxc (${cause.message})`)
299
312
  return null
300
313
  }
301
314
  }
302
315
 
303
- // Parse `file` (a script extension) as Node loads it: under the format its name and package
304
- // declare, or for a typeless .js/.ts the one Node detects from its syntax. -> { format, parsed,
305
- // errors } (`errors`: what the parse still reports), or { declared, crash } where the parser
306
- // itself threw on the file, salvaging nothing.
316
+ // As Node loads it: under the format its name and package declare, or for a typeless .js/.ts the one Node detects.
307
317
  parseFile(file) {
308
318
  const ext = extname(file)
309
- // declared === null: typeless package; Node decides .js/.ts by syntax, resolved after parse.
310
- const declared = formatForFile(file, this.host)
319
+ const declared = formatForFile(file, this.host, this.#types)
311
320
  const src = this.host.readFile(file).toString('utf8')
312
-
313
- // Resolve the parser outside the try: a missing oxc-parser is an env error, not this
314
- // file's — it must not be swallowed into a silent zero-edge leaf.
321
+ // Outside the try: a missing oxc-parser is an env error, not this file's, never a silent zero-edge leaf.
315
322
  const parser = getParser()
316
- // oxc picks JS/TS + JSX from the filename; the .js family lands on plain JS (no JSX), so
317
- // force the JSX variant when opted in (`--jsx`) to parse React Native's JSX-in-.js source.
318
323
  const lang = this.jsx && JSX_EXTS.has(ext) ? 'jsx' : undefined
319
324
  const parseOptions = (sourceType) => (lang ? { sourceType, lang } : { sourceType })
320
- // `commonjs`, not `script`: Node runs CJS inside its module wrapper function, where a top-level
321
- // `return` or `new.target` is legal; oxc's `commonjs` accepts both and is otherwise `script`.
322
- const baseSourceType = declared === null ? 'unambiguous'
323
- : declared.startsWith('module') ? 'module' : 'commonjs'
325
+ const cleanParse = (source, sourceType) => {
326
+ try {
327
+ const retried = parser.parseSync(file, source, parseOptions(sourceType))
328
+ return syntaxErrors(retried).length === 0 ? retried : null
329
+ } catch {
330
+ return null
331
+ }
332
+ }
333
+ // `commonjs`, not `script`: Node runs CJS in its module wrapper, where a top-level `return` or `new.target` is legal.
334
+ const baseSourceType = declared === null ? 'unambiguous' : declared.startsWith('module') ? 'module' : 'commonjs'
324
335
  let parsed
325
336
  try {
326
337
  parsed = parser.parseSync(file, src, parseOptions(baseSourceType))
@@ -328,89 +339,55 @@ export class Scan {
328
339
  return { declared, crash: cause.message }
329
340
  }
330
341
 
331
- // Match Node's syntax detection for typeless packages: ESM syntax picks the module variant
332
- // (mislabeling as commonjs makes `--bundle=load` feed the wrong translator). Only .ts takes the
333
- // `-typescript` suffix; .jsx/.tsx deliberately land on plain module/commonjs (like a JSX-in-.js
334
- // file) — they're bundler-transformed, keyed by extension in `stasis build`, not Node-strippable,
335
- // so the "needs a JSX/TS transform" signal rides the .jsx/.tsx extension, not the format tag.
342
+ // Only .ts takes `-typescript`: .jsx/.tsx are bundler-transformed, not Node-strippable.
336
343
  const tsSuffix = ext === '.ts' ? '-typescript' : ''
337
344
  const detectedFormat = (p) => `${p.module.hasModuleSyntax ? 'module' : 'commonjs'}${tsSuffix}`
338
345
  let format = declared ?? detectedFormat(parsed)
339
-
340
- // oxc recovers from syntax errors (reports them in parsed.errors) rather than throwing;
341
- // Warning/Advice severities aren't real parse errors.
342
346
  let errors = syntaxErrors(parsed)
343
347
 
344
- // oxc's `unambiguous` mode counts top-level await as module syntax (since 0.109; before, a
345
- // TLA-only ESM file parsed here as a broken script). Keep mirroring Node for any script parse
346
- // that still fails: retry as module and prefer a clean module parse.
348
+ // Mirror Node for a typeless script that fails: a clean module parse makes it ESM, else a clean `commonjs` parse
349
+ // (`unambiguous` refuses the CJS wrapper's top-level `return`) keeps it CJS.
347
350
  if (errors.length > 0 && declared === null && !format.startsWith('module')) {
348
- try {
349
- const asModule = parser.parseSync(file, src, parseOptions('module'))
350
- if (syntaxErrors(asModule).length === 0) {
351
- parsed = asModule
352
- format = `module${tsSuffix}`
353
- errors = []
354
- }
355
- } catch { /* keep the script parse and its recorded errors */ }
356
- }
357
-
358
- // A typeless file Node detects as CJS runs inside the module wrapper too, but `unambiguous`
359
- // parses it as a plain script, so a top-level `return` still fails there: retry as `commonjs`
360
- // and prefer a clean parse (the detected commonjs format stands).
361
- if (errors.length > 0 && declared === null && !format.startsWith('module')) {
362
- try {
363
- const asCommonjs = parser.parseSync(file, src, parseOptions('commonjs'))
364
- if (syntaxErrors(asCommonjs).length === 0) {
365
- parsed = asCommonjs
366
- errors = []
367
- }
368
- } catch { /* keep the script parse and its recorded errors */ }
351
+ const asModule = cleanParse(src, 'module')
352
+ const clean = asModule ?? cleanParse(src, 'commonjs')
353
+ if (clean !== null) {
354
+ parsed = clean
355
+ if (asModule !== null) format = `module${tsSuffix}`
356
+ errors = []
357
+ }
369
358
  }
370
359
 
371
- // --flow: oxc parses JS/TS but not Flow, so a Flow-typed .js/.cjs/.mjs file lands here with
372
- // parse errors. Only now -- after a raw oxc parse actually failed -- strip Flow types and
373
- // re-parse, so plain-JS files never pay the transform cost. Only the parse input is rewritten;
374
- // the bundle stores the pristine on-disk source (State/buildResolvedJsBundle re-read it), so
375
- // attestation still covers the real bytes. A clean re-parse replaces the errored one; otherwise
376
- // we keep the original result so a genuine (non-Flow) parse error still surfaces.
377
- if (errors.length > 0 && this.flow && FLOW_EXTS.has(ext)) {
378
- const flowSrc = this.#stripFlow(src, file)
379
- if (flowSrc !== null) {
380
- try {
381
- const asFlow = parser.parseSync(file, flowSrc, parseOptions(baseSourceType))
382
- if (syntaxErrors(asFlow).length === 0) {
383
- parsed = asFlow
384
- format = declared ?? detectedFormat(parsed)
385
- errors = []
386
- }
387
- } catch { /* keep the original parse + its errors */ }
360
+ // --flow, as Metro's Babel reads the file: one marked `@flow` even where oxc parses it (`f<T>(x)` a comparison
361
+ // there), any other only where oxc's parse failed. A re-parse that still fails keeps the original's errors.
362
+ let strippedSource = null
363
+ if (this.flow && FLOW_EXTS.has(ext) && (errors.length > 0 || hasFlowPragma(src))) {
364
+ const flowSrc = this.#stripFlow(src, file, errors.length > 0)
365
+ const asFlow = flowSrc === null ? null : cleanParse(flowSrc, baseSourceType)
366
+ if (asFlow !== null) {
367
+ parsed = asFlow
368
+ format = declared ?? detectedFormat(parsed)
369
+ errors = []
370
+ strippedSource = flowSrc
388
371
  }
389
372
  }
390
-
391
- return { format, parsed, errors }
373
+ return { format, parsed, errors, source: src, strippedSource }
392
374
  }
393
375
 
394
376
  #scanFile(url, queue) {
395
377
  const file = fileURLToPath(url)
396
378
  const ext = extname(file)
397
- if (ext === '.json') {
398
- this.files.set(url, { format: 'json', edges: [] })
399
- return
400
- }
401
- if (this.#isResource(file)) {
402
- // Allowlisted asset: carry it as a resource with no parse and no edges. `resource: true` is
403
- // the authoritative signal (both bundle builders key off it); the concrete format
404
- // (resource vs resource:base64) is byte-derived by the builder, so leave it unset here.
405
- this.files.set(url, { format: null, edges: [], resource: true })
406
- return
407
- }
408
- if (!SCRIPT_EXTS.has(ext)) {
409
- this.files.set(url, { format: null, edges: [] })
379
+ // `resource: true` is what both bundle builders key off; they derive its format from its bytes.
380
+ const leaf = ext === '.json' ? { format: 'json', edges: [] }
381
+ : this.#isResource(file) ? { format: null, edges: [], resource: true }
382
+ : JS_EXTENSIONS.has(ext) ? null
383
+ : { format: null, edges: [] }
384
+ if (leaf !== null) {
385
+ this.files.set(url, leaf)
410
386
  return
411
387
  }
412
388
 
413
- const result = this.parseFile(file)
389
+ const result = this.#reparse ?? this.parseFile(file)
390
+ this.#parse = result
414
391
  if (result.crash !== undefined) {
415
392
  this.#recordParseError(url, result.declared, result.crash, false)
416
393
  return
@@ -425,30 +402,34 @@ export class Scan {
425
402
  this.#recordParseError(url, format, parseError, true)
426
403
  return
427
404
  }
428
- // Script (CJS) files: oxc's recovered AST still yields the require()/import() edges — salvage them.
405
+ // A script walks on through oxc's recovered AST, so a scan reports the rest of the graph's errors too.
429
406
  this.parseErrors.push({ url, format, message: parseError, recovered: true })
430
407
  }
431
408
 
432
- const specs = collectSpecifiers(parsed.program)
409
+ const typesErased = () => result.strippedSource !== null || !this.keepsTypeImports?.(file)
410
+ let { specs } = result
411
+ if (specs === undefined) {
412
+ const touches = this.memo === null ? 0 : this.host.touches()
413
+ specs = collectSpecifiers(parsed.program, this.defines, this.babel ? (node) => listsTypesOnly(node, result) && typesErased() : undefined)
414
+ // Where finding the specifiers touched no package not unpacked yet, they are kept in place of the parse.
415
+ if (this.memo !== null && this.host.touches() === touches) this.#parse = { format, errors, specs }
416
+ }
433
417
 
434
- // The edge KIND picks the resolution context, not the parent's format: createRequire()d
435
- // require() inside ESM resolves under REQUIRE conditions, literal import() inside CJS under
436
- // IMPORT conditions -- the parent-format set baked the wrong `exports` branch into the bundle.
437
- // The custom (legacy-field) resolver keeps the format set: bundlers don't split per edge.
418
+ // The edge kind picks Node's resolution context, not the parent's format: a createRequire()d require() in ESM
419
+ // resolves under require conditions. A custom resolver keeps the format's: bundlers don't split per edge.
438
420
  const formatContext = ['module', 'module-typescript'].includes(format) ? 'import' : 'require'
439
421
  const edges = []
440
422
  const specMaps = new Map() // condition key -> specifier -> child URL
441
423
  const addChild = (s, key, childURL, childPath) => {
442
424
  edges.push({ ...s, child: childURL })
443
- if (!specMaps.has(key)) specMaps.set(key, new Map())
444
- specMaps.get(key).set(s.spec, childURL)
425
+ nestedMap(specMaps, key).set(s.spec, childURL)
445
426
  if (RESOLVABLE_EXTS.has(extname(childPath)) || this.#isResource(childPath)) queue.push(childURL)
446
427
  }
447
- // Records the file --typescript would land the miss on too (typescriptURL), resolved as the miss was.
448
- const addUnresolved = (s, reason, conditions) => {
428
+ const addUnresolved = (s, reason, conditions, target) => {
449
429
  edges.push({ ...s, error: reason })
450
430
  const typescriptURL = this.#typescriptCandidate(file, s.spec, conditions)
451
- this.unresolved.push({ parentURL: url, kind: s.kind, spec: s.spec, reason, ...(typescriptURL ? { typescriptURL } : {}) })
431
+ const targets = this.missTargets ? this.#missTargets(file, s.spec, target) : []
432
+ this.unresolved.push({ parentURL: url, kind: s.kind, spec: s.spec, reason, ...(typescriptURL ? { typescriptURL } : {}), ...(targets.length > 0 ? { targets } : {}) })
452
433
  }
453
434
 
454
435
  for (const s of specs) {
@@ -459,20 +440,14 @@ export class Scan {
459
440
  }
460
441
  const context = this.customResolve ? formatContext : (s.kind === 'require' ? 'require' : 'import')
461
442
  const conditions = this.#conditionSet(context)
462
- const key = condKey(conditions)
463
- // Custom (legacy-field) resolver owns builtin handling and the `empty` outcome (browser/RN
464
- // `false` redirect): an empty edge is recorded but neither queued nor counted as unresolved.
443
+ const key = [...conditions].join(', ')
444
+ // An `empty` edge (a browser/RN `false` redirect) is recorded but neither queued nor counted as unresolved.
465
445
  if (this.customResolve) {
466
446
  const r = this.customResolve(file, s.spec, conditions)
467
- if (r?.builtin) {
468
- edges.push({ ...s, builtin: true })
469
- } else if (r?.empty) {
470
- edges.push({ ...s, empty: true })
471
- } else if (r?.url) {
472
- addChild(s, key, r.url, fileURLToPath(r.url))
473
- } else {
474
- addUnresolved(s, 'MODULE_NOT_FOUND', conditions)
475
- }
447
+ if (r?.builtin) edges.push({ ...s, builtin: true })
448
+ else if (r?.empty) edges.push({ ...s, empty: true })
449
+ else if (r?.url) addChild(s, key, r.url, fileURLToPath(r.url))
450
+ else addUnresolved(s, 'MODULE_NOT_FOUND', conditions, this.missTargets ? this.customResolve.missTargets?.(file, s.spec, conditions) : undefined)
476
451
  continue
477
452
  }
478
453
  if (isBuiltin(s.spec)) {
@@ -483,21 +458,17 @@ export class Scan {
483
458
  try {
484
459
  childPath = this.host.resolve(file, s.spec, conditions)
485
460
  } catch (cause) {
486
- // --typescript: complete the miss with tsc's mapping (see #typescriptResolve); the edge
487
- // stays keyed by the ORIGINAL specifier -- only the target is the mapped file.
461
+ // The edge stays keyed by the original specifier; only the target is the mapped file.
488
462
  if (this.typescript) childPath = this.#typescriptResolve(file, s.spec, conditions)
489
463
  if (childPath == null) {
490
- addUnresolved(s, cause.code ?? cause.message, conditions)
464
+ addUnresolved(s, cause.code ?? cause.message, conditions, cause.target)
491
465
  continue
492
466
  }
493
467
  }
494
468
  addChild(s, key, pathToFileURL(childPath).toString(), childPath)
495
469
  }
496
470
 
497
- for (const [key, specMap] of specMaps) {
498
- if (!this.imports.has(key)) this.imports.set(key, new Map())
499
- this.imports.get(key).set(url, specMap)
500
- }
471
+ for (const [key, specMap] of specMaps) nestedMap(this.imports, key).set(url, specMap)
501
472
  this.files.set(url, parseError ? { format, edges, parseError } : { format, edges })
502
473
  }
503
474
 
@@ -509,23 +480,15 @@ export class Scan {
509
480
  return r
510
481
  }
511
482
  const relEdge = (e) => (e.child ? { ...e, child: rel(e.child) } : e)
483
+ const remap = (map, entry) => new Map(map.entries().map(entry))
512
484
  return {
513
485
  entries: new Set([...this.entries].map(rel)),
514
- files: new Map(
515
- [...this.files].map(([k, v]) => [rel(k), { ...v, edges: v.edges.map(relEdge) }])
516
- ),
517
- imports: new Map(
518
- [...this.imports].map(([cond, byParent]) => [
519
- cond,
520
- new Map(
521
- [...byParent].map(([p, specs]) => [
522
- rel(p),
523
- new Map([...specs].map(([spec, child]) => [spec, rel(child)])),
524
- ])
525
- ),
526
- ])
527
- ),
528
- unresolved: this.unresolved.map(({ parentURL, typescriptURL, ...rest }) => ({ parent: rel(parentURL), ...rest, ...(typescriptURL ? { typescript: rel(typescriptURL) } : {}) })),
486
+ files: remap(this.files, ([k, v]) => [rel(k), { ...v, edges: v.edges.map(relEdge) }]),
487
+ imports: remap(this.imports, ([cond, byParent]) => [
488
+ cond,
489
+ remap(byParent, ([p, specs]) => [rel(p), remap(specs, ([spec, child]) => [spec, rel(child)])]),
490
+ ]),
491
+ unresolved: this.unresolved.map(({ parentURL, typescriptURL, targets: _, ...rest }) => ({ parent: rel(parentURL), ...rest, ...(typescriptURL ? { typescript: rel(typescriptURL) } : {}) })),
529
492
  parseErrors: this.parseErrors.map(({ url, ...rest }) => ({ file: rel(url), ...rest })),
530
493
  }
531
494
  }