@exodus/stasis 1.0.0-beta.9 → 1.0.0-rc.1

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 (67) hide show
  1. package/README.md +6 -2
  2. package/bin/stasis.js +222 -334
  3. package/package.json +38 -14
  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 +1664 -1001
  13. package/src/cmd/diff.js +1 -4
  14. package/src/cmd/github-bundle.js +48 -21
  15. package/src/cmd/output.js +25 -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 +211 -492
  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 +152 -249
  44. package/src/resolve-node.js +29 -45
  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 +240 -282
  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 +50 -46
  57. package/src/vfs-bundle/github.js +114 -94
  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 +418 -277
  63. package/src/vfs-bundle/yarnclean.js +79 -0
  64. package/src/vfs-bundle.js +24 -55
  65. package/src/webpack.js +0 -4
  66. package/src/why.js +79 -231
  67. package/src/installed-commits.js +0 -182
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,21 +181,71 @@ 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
- // Node's resolver: must match the runtime resolver's condition set (omitting one resolves
241
- // `exports`-gated packages to a different file) AND its order: the recorded key must equal the
242
- // runtime hooks' string for exact lookups. Extras go where Node puts user conditions in each set.
243
- // A custom resolver (--mainFields/--metro) resolves for a bundler instead, so it gets what
244
- // esbuild, webpack and Metro all assert: `import`/`require`, `default` (which Node's resolver
245
- // matches anyway) and the extras, never `node`, `node-addons` or `module-sync`; the same set as
246
- // resolve-fields.js's resolveConditions.
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).
247
249
  #conditionSet(context) {
248
250
  if (this.customResolve) return new Set([context, 'default', ...this.extraConditions])
249
251
  return context === 'import'
@@ -251,26 +253,15 @@ export class Scan {
251
253
  : new Set(['require', 'node', 'node-addons', ...this.extraConditions, 'module-sync'])
252
254
  }
253
255
 
254
- // `recovered`: false = parser crashed, nothing salvaged; true = oxc returned but we
255
- // 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.
256
257
  #recordParseError(url, format, message, recovered) {
257
258
  this.files.set(url, { format, edges: [], parseError: message })
258
259
  this.parseErrors.push({ url, format, message, recovered })
259
260
  }
260
261
 
261
- // --typescript: resolve `spec` the way tsc maps TS sources, invoked only AFTER Node's own
262
- // resolution missed -- so an on-disk `.js` (or any Node-resolvable target) always wins, and the
263
- // mapping only completes a failed resolution, never rewrites a successful one. The rules
264
- // (extension substitution, extensionless/index completion, manifest-target and tsconfig-paths
265
- // mapping) live in the shared dispatcher (resolve-typescript.js), so this resolver and the
266
- // legacy-field one cannot drift. The hit is realpathed to match req.resolve's canonical output
267
- // (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.
268
263
  #typescriptResolve(parentFile, spec, conditions) {
269
- const hit = resolveTypescriptFallback(parentFile, spec, {
270
- conditions,
271
- paths: this.typescriptPaths,
272
- host: this.host,
273
- })
264
+ const hit = resolveTypescriptFallback(parentFile, spec, { conditions, paths: this.typescriptPaths, host: this.host })
274
265
  if (hit == null) return null
275
266
  try {
276
267
  return this.host.realpath(hit)
@@ -279,9 +270,7 @@ export class Scan {
279
270
  }
280
271
  }
281
272
 
282
- // The file: URL a miss would resolve to under --typescript, or null: a hint for the unresolved
283
- // report only, never an edge. A custom resolver asks its --typescript twin; the built-in one
284
- // 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.
285
274
  #typescriptCandidate(parentFile, spec, conditions) {
286
275
  if (this.typescript) return null
287
276
  if (this.customResolve) return this.typescriptResolve?.(parentFile, spec, conditions)?.url ?? null
@@ -289,43 +278,60 @@ export class Scan {
289
278
  return hit == null ? null : pathToFileURL(hit).toString()
290
279
  }
291
280
 
292
- // Strip Flow type syntax to plain JS via the optional flow-remove-types dep (resolved lazily; a
293
- // missing dep is an env error and propagates). Only invoked as a parse fallback (see #scanFile),
294
- // so plain-JS files never reach it. Returns the rewritten source, or null when the transform
295
- // itself fails on this file -- the caller then keeps oxc's original parse error.
296
- #stripFlow(src, file) {
297
- 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) {
298
308
  try {
299
- // `all: true` ignores the @flow/@noflow pragma (RN sources mix both); the transform blanks
300
- // types with whitespace, so byte offsets and every import specifier are preserved.
301
- return flowRemoveTypes(src, { all: true }).toString()
309
+ return stripFlowTypes(src)
302
310
  } catch (cause) {
303
- 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})`)
304
312
  return null
305
313
  }
306
314
  }
307
315
 
308
- // Parse `file` (a script extension) as Node loads it: under the format its name and package
309
- // declare, or for a typeless .js/.ts the one Node detects from its syntax. -> { format, parsed,
310
- // errors } (`errors`: what the parse still reports), or { declared, crash } where the parser
311
- // 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.
312
317
  parseFile(file) {
313
318
  const ext = extname(file)
314
- // declared === null: typeless package; Node decides .js/.ts by syntax, resolved after parse.
315
- const declared = formatForFile(file, this.host)
319
+ const declared = formatForFile(file, this.host, this.#types)
316
320
  const src = this.host.readFile(file).toString('utf8')
317
-
318
- // Resolve the parser outside the try: a missing oxc-parser is an env error, not this
319
- // 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.
320
322
  const parser = getParser()
321
- // oxc picks JS/TS + JSX from the filename; the .js family lands on plain JS (no JSX), so
322
- // force the JSX variant when opted in (`--jsx`) to parse React Native's JSX-in-.js source.
323
323
  const lang = this.jsx && JSX_EXTS.has(ext) ? 'jsx' : undefined
324
324
  const parseOptions = (sourceType) => (lang ? { sourceType, lang } : { sourceType })
325
- // `commonjs`, not `script`: Node runs CJS inside its module wrapper function, where a top-level
326
- // `return` or `new.target` is legal; oxc's `commonjs` accepts both and is otherwise `script`.
327
- const baseSourceType = declared === null ? 'unambiguous'
328
- : 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'
329
335
  let parsed
330
336
  try {
331
337
  parsed = parser.parseSync(file, src, parseOptions(baseSourceType))
@@ -333,89 +339,55 @@ export class Scan {
333
339
  return { declared, crash: cause.message }
334
340
  }
335
341
 
336
- // Match Node's syntax detection for typeless packages: ESM syntax picks the module variant
337
- // (mislabeling as commonjs makes `--bundle=load` feed the wrong translator). Only .ts takes the
338
- // `-typescript` suffix; .jsx/.tsx deliberately land on plain module/commonjs (like a JSX-in-.js
339
- // file) — they're bundler-transformed, keyed by extension in `stasis build`, not Node-strippable,
340
- // 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.
341
343
  const tsSuffix = ext === '.ts' ? '-typescript' : ''
342
344
  const detectedFormat = (p) => `${p.module.hasModuleSyntax ? 'module' : 'commonjs'}${tsSuffix}`
343
345
  let format = declared ?? detectedFormat(parsed)
344
-
345
- // oxc recovers from syntax errors (reports them in parsed.errors) rather than throwing;
346
- // Warning/Advice severities aren't real parse errors.
347
346
  let errors = syntaxErrors(parsed)
348
347
 
349
- // oxc's `unambiguous` mode counts top-level await as module syntax (since 0.109; before, a
350
- // TLA-only ESM file parsed here as a broken script). Keep mirroring Node for any script parse
351
- // 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.
352
350
  if (errors.length > 0 && declared === null && !format.startsWith('module')) {
353
- try {
354
- const asModule = parser.parseSync(file, src, parseOptions('module'))
355
- if (syntaxErrors(asModule).length === 0) {
356
- parsed = asModule
357
- format = `module${tsSuffix}`
358
- errors = []
359
- }
360
- } catch { /* keep the script parse and its recorded errors */ }
361
- }
362
-
363
- // A typeless file Node detects as CJS runs inside the module wrapper too, but `unambiguous`
364
- // parses it as a plain script, so a top-level `return` still fails there: retry as `commonjs`
365
- // and prefer a clean parse (the detected commonjs format stands).
366
- if (errors.length > 0 && declared === null && !format.startsWith('module')) {
367
- try {
368
- const asCommonjs = parser.parseSync(file, src, parseOptions('commonjs'))
369
- if (syntaxErrors(asCommonjs).length === 0) {
370
- parsed = asCommonjs
371
- errors = []
372
- }
373
- } 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
+ }
374
358
  }
375
359
 
376
- // --flow: oxc parses JS/TS but not Flow, so a Flow-typed .js/.cjs/.mjs file lands here with
377
- // parse errors. Only now -- after a raw oxc parse actually failed -- strip Flow types and
378
- // re-parse, so plain-JS files never pay the transform cost. Only the parse input is rewritten;
379
- // the bundle stores the pristine on-disk source (State/buildResolvedJsBundle re-read it), so
380
- // attestation still covers the real bytes. A clean re-parse replaces the errored one; otherwise
381
- // we keep the original result so a genuine (non-Flow) parse error still surfaces.
382
- if (errors.length > 0 && this.flow && FLOW_EXTS.has(ext)) {
383
- const flowSrc = this.#stripFlow(src, file)
384
- if (flowSrc !== null) {
385
- try {
386
- const asFlow = parser.parseSync(file, flowSrc, parseOptions(baseSourceType))
387
- if (syntaxErrors(asFlow).length === 0) {
388
- parsed = asFlow
389
- format = declared ?? detectedFormat(parsed)
390
- errors = []
391
- }
392
- } 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
393
371
  }
394
372
  }
395
-
396
- return { format, parsed, errors }
373
+ return { format, parsed, errors, source: src, strippedSource }
397
374
  }
398
375
 
399
376
  #scanFile(url, queue) {
400
377
  const file = fileURLToPath(url)
401
378
  const ext = extname(file)
402
- if (ext === '.json') {
403
- this.files.set(url, { format: 'json', edges: [] })
404
- return
405
- }
406
- if (this.#isResource(file)) {
407
- // Allowlisted asset: carry it as a resource with no parse and no edges. `resource: true` is
408
- // the authoritative signal (both bundle builders key off it); the concrete format
409
- // (resource vs resource:base64) is byte-derived by the builder, so leave it unset here.
410
- this.files.set(url, { format: null, edges: [], resource: true })
411
- return
412
- }
413
- if (!SCRIPT_EXTS.has(ext)) {
414
- 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)
415
386
  return
416
387
  }
417
388
 
418
- const result = this.parseFile(file)
389
+ const result = this.#reparse ?? this.parseFile(file)
390
+ this.#parse = result
419
391
  if (result.crash !== undefined) {
420
392
  this.#recordParseError(url, result.declared, result.crash, false)
421
393
  return
@@ -430,30 +402,34 @@ export class Scan {
430
402
  this.#recordParseError(url, format, parseError, true)
431
403
  return
432
404
  }
433
- // 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.
434
406
  this.parseErrors.push({ url, format, message: parseError, recovered: true })
435
407
  }
436
408
 
437
- 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
+ }
438
417
 
439
- // The edge KIND picks the resolution context, not the parent's format: createRequire()d
440
- // require() inside ESM resolves under REQUIRE conditions, literal import() inside CJS under
441
- // IMPORT conditions -- the parent-format set baked the wrong `exports` branch into the bundle.
442
- // 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.
443
420
  const formatContext = ['module', 'module-typescript'].includes(format) ? 'import' : 'require'
444
421
  const edges = []
445
422
  const specMaps = new Map() // condition key -> specifier -> child URL
446
423
  const addChild = (s, key, childURL, childPath) => {
447
424
  edges.push({ ...s, child: childURL })
448
- if (!specMaps.has(key)) specMaps.set(key, new Map())
449
- specMaps.get(key).set(s.spec, childURL)
425
+ nestedMap(specMaps, key).set(s.spec, childURL)
450
426
  if (RESOLVABLE_EXTS.has(extname(childPath)) || this.#isResource(childPath)) queue.push(childURL)
451
427
  }
452
- // Records the file --typescript would land the miss on too (typescriptURL), resolved as the miss was.
453
- const addUnresolved = (s, reason, conditions) => {
428
+ const addUnresolved = (s, reason, conditions, target) => {
454
429
  edges.push({ ...s, error: reason })
455
430
  const typescriptURL = this.#typescriptCandidate(file, s.spec, conditions)
456
- 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 } : {}) })
457
433
  }
458
434
 
459
435
  for (const s of specs) {
@@ -464,20 +440,14 @@ export class Scan {
464
440
  }
465
441
  const context = this.customResolve ? formatContext : (s.kind === 'require' ? 'require' : 'import')
466
442
  const conditions = this.#conditionSet(context)
467
- const key = condKey(conditions)
468
- // Custom (legacy-field) resolver owns builtin handling and the `empty` outcome (browser/RN
469
- // `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.
470
445
  if (this.customResolve) {
471
446
  const r = this.customResolve(file, s.spec, conditions)
472
- if (r?.builtin) {
473
- edges.push({ ...s, builtin: true })
474
- } else if (r?.empty) {
475
- edges.push({ ...s, empty: true })
476
- } else if (r?.url) {
477
- addChild(s, key, r.url, fileURLToPath(r.url))
478
- } else {
479
- addUnresolved(s, 'MODULE_NOT_FOUND', conditions)
480
- }
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)
481
451
  continue
482
452
  }
483
453
  if (isBuiltin(s.spec)) {
@@ -488,21 +458,17 @@ export class Scan {
488
458
  try {
489
459
  childPath = this.host.resolve(file, s.spec, conditions)
490
460
  } catch (cause) {
491
- // --typescript: complete the miss with tsc's mapping (see #typescriptResolve); the edge
492
- // 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.
493
462
  if (this.typescript) childPath = this.#typescriptResolve(file, s.spec, conditions)
494
463
  if (childPath == null) {
495
- addUnresolved(s, cause.code ?? cause.message, conditions)
464
+ addUnresolved(s, cause.code ?? cause.message, conditions, cause.target)
496
465
  continue
497
466
  }
498
467
  }
499
468
  addChild(s, key, pathToFileURL(childPath).toString(), childPath)
500
469
  }
501
470
 
502
- for (const [key, specMap] of specMaps) {
503
- if (!this.imports.has(key)) this.imports.set(key, new Map())
504
- this.imports.get(key).set(url, specMap)
505
- }
471
+ for (const [key, specMap] of specMaps) nestedMap(this.imports, key).set(url, specMap)
506
472
  this.files.set(url, parseError ? { format, edges, parseError } : { format, edges })
507
473
  }
508
474
 
@@ -514,23 +480,15 @@ export class Scan {
514
480
  return r
515
481
  }
516
482
  const relEdge = (e) => (e.child ? { ...e, child: rel(e.child) } : e)
483
+ const remap = (map, entry) => new Map(map.entries().map(entry))
517
484
  return {
518
485
  entries: new Set([...this.entries].map(rel)),
519
- files: new Map(
520
- [...this.files].map(([k, v]) => [rel(k), { ...v, edges: v.edges.map(relEdge) }])
521
- ),
522
- imports: new Map(
523
- [...this.imports].map(([cond, byParent]) => [
524
- cond,
525
- new Map(
526
- [...byParent].map(([p, specs]) => [
527
- rel(p),
528
- new Map([...specs].map(([spec, child]) => [spec, rel(child)])),
529
- ])
530
- ),
531
- ])
532
- ),
533
- 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) } : {}) })),
534
492
  parseErrors: this.parseErrors.map(({ url, ...rest }) => ({ file: rel(url), ...rest })),
535
493
  }
536
494
  }