@exodus/stasis 1.0.0-beta.14 → 1.0.0-beta.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +2 -1
  2. package/bin/stasis.js +99 -16
  3. package/package.json +22 -9
  4. package/src/audit-corrections.js +11 -42
  5. package/src/audit.js +241 -190
  6. package/src/babel.js +39 -122
  7. package/src/bundle-from-lockfile.js +9 -21
  8. package/src/bundle-reader.js +5 -9
  9. package/src/bundle.js +0 -3
  10. package/src/cmd/build.js +82 -174
  11. package/src/cmd/bundle-options.js +113 -167
  12. package/src/cmd/bundle.js +942 -1294
  13. package/src/cmd/diff.js +1 -4
  14. package/src/cmd/github-bundle.js +21 -26
  15. package/src/cmd/output.js +15 -12
  16. package/src/cmd/sbom.js +3 -8
  17. package/src/cmd/tokens.js +16 -23
  18. package/src/define.js +36 -94
  19. package/src/diff.js +72 -128
  20. package/src/esbuild.js +0 -4
  21. package/src/flow.js +18 -13
  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 +41 -93
  27. package/src/loaders/cargo.js +298 -805
  28. package/src/loaders/foundry.js +109 -285
  29. package/src/loaders/paths.js +13 -13
  30. package/src/loaders/php.js +210 -489
  31. package/src/loaders/rust.js +507 -1430
  32. package/src/loaders/solidity-ownership.js +48 -128
  33. package/src/loaders/solidity.js +39 -122
  34. package/src/loaders/toml.js +7 -17
  35. package/src/lockfile.js +0 -3
  36. package/src/metro-assets.js +10 -384
  37. package/src/metro-resolver.js +58 -134
  38. package/src/metro-transformer.js +0 -5
  39. package/src/metro.js +0 -4
  40. package/src/mock.js +32 -80
  41. package/src/parse.js +4 -4
  42. package/src/prune.js +0 -3
  43. package/src/resolve-fields.js +137 -532
  44. package/src/resolve-node.js +27 -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 +52 -128
  49. package/src/scan.js +142 -284
  50. package/src/sixel.js +310 -0
  51. package/src/table.js +63 -31
  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 +596 -522
  56. package/src/vfs-bundle/entries.js +40 -57
  57. package/src/vfs-bundle/github.js +80 -122
  58. package/src/vfs-bundle/glob.js +4 -45
  59. package/src/vfs-bundle/lazy.js +27 -56
  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 +295 -318
  63. package/src/vfs-bundle/yarnclean.js +10 -31
  64. package/src/vfs-bundle.js +23 -64
  65. package/src/webpack.js +0 -4
  66. package/src/why.js +78 -232
  67. package/src/installed-commits.js +0 -184
package/src/scan.js CHANGED
@@ -1,33 +1,28 @@
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, nestedMap, 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'
8
9
  import { branchFolder, parseDefine } from './define.js'
9
10
  import { diskHost } from '@exodus/stasis-core/host'
10
- import { untouchedMemo } from './vfs-bundle/lazy.js'
11
- import { FLOW_EXTS, stripFlowTypes } from './flow.js'
11
+ import { hostMemo } from './vfs-bundle/lazy.js'
12
+ import { FLOW_EXTS, hasFlowPragma, stripFlowTypes } from './flow.js'
12
13
 
13
14
  // Static require/import graph walker: parses source, never loads or executes user code.
14
- // Dynamic specifiers (`require(name)`, `import('./'+x)`) are recorded as unresolved. With
15
- // `define`, a branch the defined values keep from running is not walked (see define.js).
16
-
17
- // .jsx/.tsx need no flag: oxc parses them as JSX/TSX purely from the filename, so a reached one
18
- // -- e.g. a package whose React Native entry is src/index.tsx -- is parsed and carried like any .ts.
19
- const SCRIPT_EXTS = new Set(['.js', '.cjs', '.mjs', '.ts', '.cts', '.mts', '.jsx', '.tsx'])
20
- const RESOLVABLE_EXTS = new Set([...SCRIPT_EXTS, '.json'])
21
- // The extensions whose name can't tell whether they hold JSX, parsed with JSX enabled only under
22
- // `jsx` (React Native's JSX-in-.js convention). Excludes the .ts family: TypeScript's `<T>`
23
- // 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.
24
20
  const JSX_EXTS = new Set(['.js', '.cjs', '.mjs'])
25
21
 
26
22
  const TS_EXTS = new Set(['.ts', '.cts', '.mts', '.tsx'])
27
23
 
28
- // Required lazily so non-JS bundlers don't load the native oxc parser. Its parseSync hands each AST
29
- // over by raw transfer where the platform takes it (oxc's rawTransferSupported), read from the
30
- // parser's own buffer, rather than as JSON, which takes longer to parse than the source did.
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.
31
26
  let _parser
32
27
  export function getParser() {
33
28
  if (_parser === undefined) {
@@ -37,34 +32,31 @@ export function getParser() {
37
32
  return _parser
38
33
  }
39
34
 
40
-
41
- // classifyFormat is the shared name->format authority (static and runtime bundles must agree); it
42
- // returns null for the .js/.ts family whose module system comes from package.json `type` (null =
43
- // detect from syntax, signaled up as null).
35
+ // null for a typeless .js/.ts: Node detects its format from its syntax.
44
36
  function formatForFile(file, host, kept) {
45
37
  const format = classifyFormat(file)
46
38
  if (format != null) return format
47
39
  const type = packageType(file, host, kept)
48
- if (type === null) return null
49
- return `${type}${extname(file) === '.ts' ? '-typescript' : ''}`
40
+ return type === null ? null : `${type}${extname(file) === '.ts' ? '-typescript' : ''}`
50
41
  }
51
42
 
52
- // The errors oxc reports for a parse, but its warnings and advice.
53
43
  export const syntaxErrors = (parsed) => (parsed.errors ?? []).filter((e) => e.severity !== 'Warning' && e.severity !== 'Advice')
54
44
 
55
45
  export function literalSpec(node) {
56
46
  if (!node) return null
57
47
  if (node.type === 'Literal' && typeof node.value === 'string') return node.value
58
- if (node.type === 'TemplateLiteral' && node.expressions.length === 0 && node.quasis.length === 1) {
59
- return node.quasis[0].value.cooked
60
- }
48
+ if (node.type === 'TemplateLiteral' && node.expressions.length === 0 && node.quasis.length === 1) return node.quasis[0].value.cooked
61
49
  return null
62
50
  }
63
51
 
64
- // Walks require()/import() calls only; ESM static imports/re-exports come from oxc module records.
65
- // `fold` (define.js's branchFolder, or null) names the children of a branch whose test folds: the
66
- // side that can't run is never walked.
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.
67
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
+ }
68
60
  const visit = (node) => {
69
61
  if (!node || typeof node !== 'object') return
70
62
  if (Array.isArray(node)) {
@@ -80,44 +72,21 @@ function findCallSpecifiers(ast, push, fold = null) {
80
72
 
81
73
  if (node.type === 'CallExpression') {
82
74
  const { callee, arguments: args } = node
83
- if (callee.type === 'Identifier' && callee.name === 'require' && args.length >= 1) {
84
- const spec = literalSpec(args[0])
85
- if (spec != null) push({ kind: 'require', spec })
86
- else push({ kind: 'require', dynamic: true })
87
- }
75
+ if (callee.type === 'Identifier' && callee.name === 'require' && args.length >= 1) pushSpec('require', args[0])
88
76
  } else if (node.type === 'ImportExpression') {
89
- const spec = literalSpec(node.source)
90
- if (spec != null) push({ kind: 'dynamic-import', spec })
91
- else push({ kind: 'dynamic-import', dynamic: true })
77
+ pushSpec('dynamic-import', node.source)
92
78
  } else if (node.type === 'TSImportEqualsDeclaration') {
93
- // `import lib = require('./dep.cts')`: TS-only CJS import; record the real edge.
94
- // Qualified-name form (`import A = B.C`) carries no file edge.
95
- if (node.moduleReference?.type === 'TSExternalModuleReference') {
96
- const spec = literalSpec(node.moduleReference.expression)
97
- if (spec != null) push({ kind: 'require', spec })
98
- else push({ kind: 'require', dynamic: true })
99
- }
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)
100
81
  }
101
82
 
102
- for (const key of Object.keys(node)) {
103
- if (key === 'type' || key === 'loc' || key === 'range' || key === 'start' || key === 'end') continue
104
- visit(node[key])
105
- }
83
+ for (const key of Object.keys(node)) if (!NO_CHILD_KEYS.has(key)) visit(node[key])
106
84
  }
107
85
  visit(ast)
108
86
  }
109
87
 
110
- // The import specifiers `program` holds, as { kind, spec } ({ kind, dynamic: true } for a computed
111
- // one). STATEMENT-level type-ness decides an edge, matching what survives type erasure at runtime
112
- // (verbatimModuleSyntax / Node's own type stripping): an `import type` / `export type` statement is
113
- // erased whole, so no edge; a statement that merely lists inline `type` specifiers
114
- // (`import { type A }`, `export { type B } from`) -- or none at all (`import {}`, `export {} from`)
115
- // -- still loads its module, so its edge is real. oxc's module records can't draw that line
116
- // (`import type { A }` and `import { type A }` yield identical all-isType entries, and
117
- // `export {} from` yields no entry at all), so the statements are read off the AST, where
118
- // importKind/exportKind carry it. `defines` (parseDefine's) leave out the calls in branches they
119
- // keep from running; static import statements are never in one. `typesOnly(node)`, where given,
120
- // leaves out a statement it holds lists types alone (see listsTypesOnly).
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.
121
90
  function collectSpecifiers(program, defines = new Map(), typesOnly = () => false) {
122
91
  const specs = []
123
92
  for (const node of program.body) {
@@ -133,13 +102,8 @@ function collectSpecifiers(program, defines = new Map(), typesOnly = () => false
133
102
  return specs
134
103
  }
135
104
 
136
- // Whether `entries` (absolute paths) import TS sources by their OUTPUT names -- tsc's convention,
137
- // which only --typescript resolves -- so the build can take --typescript as given: every entry is
138
- // TypeScript, they hold at least one relative import naming a JS output (`./a.js`, or .jsx/.mjs/
139
- // .cjs), and not one of those is on disk while the TS source it maps to is (`./a.ts`; see
140
- // typescriptSiblings), outside node_modules as the resolver requires. Any part missing is no tell, and neither is an entry that doesn't parse as
141
- // the scan parses it (Scan#parseFile; the scan reports it), nor one in node_modules, where
142
- // --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.
143
107
  export function importsTypescriptByOutputName(entries, host = diskHost) {
144
108
  if (!entries.every((entry) => TS_EXTS.has(extname(entry)) && !inNodeModules(entry, host))) return false
145
109
  // Outside the try, as in the scan: a missing oxc-parser is an env error, not an entry's.
@@ -162,9 +126,8 @@ export function importsTypescriptByOutputName(entries, host = diskHost) {
162
126
  }
163
127
  }
164
128
  // tsc's pick, the first TS source on disk, as the resolver maps it: none where it lies in node_modules.
165
- const isSource = (file) => !isTypeDeclaration(file) && (host.stat(file)?.isFile() ?? false)
166
129
  const mapsTo = (target) => {
167
- const pick = typescriptSiblings(target).find(isSource)
130
+ const pick = typescriptSiblings(target).find((file) => isSourceFile(file, host))
168
131
  return pick !== undefined && !inNodeModules(pick, host)
169
132
  }
170
133
  return targets.length > 0 && targets.every((target) => host.stat(target) === null && mapsTo(target))
@@ -172,25 +135,15 @@ export function importsTypescriptByOutputName(entries, host = diskHost) {
172
135
 
173
136
  const TYPE_KINDS = new Set(['type', 'typeof'])
174
137
 
175
- // Whether the import or re-export statement `node` lists specifiers, every one an inline type
176
- // (`import { type A, typeof B } from`, `export { type C } from`), which Babel's Flow and TypeScript
177
- // transforms erase whole, as Metro builds (Node keeps one, as `import {} from`). A file --flow
178
- // stripped has lost them, blanked to whitespace in place in its `strippedSource`, so there the
179
- // statement lists types alone where it reads otherwise in its `source` (a comment in it stays);
180
- // a statement written with none (`import {} from`) lists no types. The Metro plugin's transformer
181
- // draws the same line (typesOnly).
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.
182
140
  function listsTypesOnly(node, { source, strippedSource }) {
183
141
  const specifiers = node.specifiers ?? []
184
142
  if (specifiers.length > 0) return specifiers.every((specifier) => TYPE_KINDS.has(specifier.importKind ?? specifier.exportKind))
185
143
  return strippedSource !== null && source.slice(node.start, node.end) !== strippedSource.slice(node.start, node.end)
186
144
  }
187
145
 
188
- function condKey(set) {
189
- return [...set].join(', ')
190
- }
191
-
192
- // How many files the scans parsed, and with a `memo` (#scanKept) resolved again from a parse kept,
193
- // and took as kept, all told: what a build is reported by (EXODUS_STASIS_DEBUG).
146
+ // What a build is reported by (EXODUS_STASIS_DEBUG); see #scanKept.
194
147
  export const scanCounts = { parsed: 0, resolved: 0, replayed: 0 }
195
148
 
196
149
  export class Scan {
@@ -200,55 +153,21 @@ export class Scan {
200
153
  parseErrors = []
201
154
  entries = new Set()
202
155
 
203
- // `conditions` is additive on top of the edge-context base set. `resolve` optionally
204
- // replaces Node's resolver (resolve-fields.js): returns `{ url } | { empty } | { builtin } | null`
205
- // (`empty` = a browser/react-native `false` redirect) and owns builtin handling when set.
206
- // `jsx` opts the .js/.cjs/.mjs family into JSX syntax (React Native's JSX-in-.js convention);
207
- // off by default because oxc, like tsc, only auto-enables JSX for .jsx/.tsx by extension.
208
- // `flow`: strip Flow type syntax from JS-family sources before parsing (see #scanFile).
209
- // `typescript`: retry a failed resolution with tsc's mapping (see #typescriptResolve), with
210
- // `typescriptPaths` (a tsconfig paths matcher; see resolveTypescriptFallback) adding tsconfig alias support; only consulted
211
- // on the built-in (Node) resolver -- a custom `resolve` owns its own TS handling. Without it, the
212
- // mapping only names the file a miss would resolve to under --typescript (see #typescriptCandidate).
213
- // `typescriptResolve`: the custom `resolve`'s --typescript twin (same shape), consulted only for
214
- // that hint; null when there is no custom resolver or `typescript` is on.
215
- // `resources` (a `parseResourcesOption` Set of extensions/filenames): reached files matching it
216
- // are carried as opaque resources (bytes only) rather than rejected as un-carryable -- for graphs
217
- // that aren't fully loadable in JS (e.g. Metro consuming .png/.svg assets).
218
- // `host`: the filesystem the walk reads and resolves through (@exodus/stasis-core/host).
219
- // `define` (each global name -> its value, see define.js): a require()/import() in a branch they
220
- // keep from running (`if (__DEV__)` under `{ __DEV__: false }`) is no edge.
221
- // `babel`: the files are built through Babel (Metro), whose Flow and TypeScript transforms erase an
222
- // import or re-export listing types alone (`import { type A } from`), so it's no edge either, but
223
- // in a TypeScript file `keepsTypeImports(file)` holds Babel keeps them for (its transform's
224
- // `onlyRemoveTypeImports`, as --babel reads the project's config); Flow's erases them always.
225
- // `memo`: what scans before this one, with these options, read of each file, and where to keep
226
- // what this one reads (see #scanKept).
227
- 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 } = {}) {
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 } = {}) {
228
160
  this.extraConditions = [...conditions]
229
161
  this.customResolve = resolve
230
- this.typescriptResolve = typescriptResolve
231
- this.jsx = jsx
232
- this.flow = flow
233
- this.typescript = typescript
234
- this.typescriptPaths = typescriptPaths
235
- this.resources = resources
236
162
  this.defines = parseDefine('scan', define)
237
- this.babel = babel
238
- this.keepsTypeImports = keepsTypeImports
239
- this.host = host
240
- this.memo = memo
241
- this.#types = untouchedMemo(host.touches, { retouch: host.retouch })
163
+ Object.assign(this, { typescriptResolve, jsx, flow, typescript, typescriptPaths, missTargets, resources, babel, keepsTypeImports, host, memo })
164
+ this.#types = hostMemo(host)
242
165
  }
243
166
 
244
- // The `type` of each package.json the files scanned are under, read once (formatForFile's), as
245
- // a host's own reads are kept (vfs-bundle/lazy.js's untouchedMemo).
167
+ // The `type` of each package.json the files scanned are under (formatForFile's).
246
168
  #types
247
169
 
248
- // A reached file whose extension/filename is in the --resources allowlist: carried as an opaque
249
- // resource (bytes only, no parse, no edges), not rejected as "a source bundle can't carry".
250
- // classifyExtension returns 'resource' only for non-code names in the allowlist, so this never
251
- // shadows a script/json extension (parseResourcesOption rejects code extensions).
170
+ // classifyExtension gives 'resource' only for a non-code name.
252
171
  #isResource(file) {
253
172
  return this.resources.size > 0 && classifyExtension(file, this.resources) === 'resource'
254
173
  }
@@ -276,18 +195,12 @@ export class Scan {
276
195
  return this
277
196
  }
278
197
 
279
- // The parse of the file being scanned a scan before kept (#scanKept), and the parse of it made here.
198
+ // The parse of the file being scanned a scan before kept, and the parse of it made here.
280
199
  #reparse
281
200
  #parse
282
201
 
283
- // With a `memo` (url -> what a scan read of it, kept across the rounds a lazy tree is read in,
284
- // vfs-bundle/lazy.js): a file a scan before kept is taken as it read it, what it found and queued
285
- // and the warnings it gave alike. One scanned here is kept where reading it touched no package not
286
- // unpacked yet (`host.touches()` counts each such read, through a host that caches nothing such a
287
- // read reads), so read what the full tree holds: the next scan reads only the files that did, and
288
- // those it reaches. Of one that did, its parse is kept alone, which is the file's whatever the tree
289
- // holds, or of that, what it imports (#scanFile): the next scan resolves its imports again, but
290
- // parses it no more.
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.
291
204
  #scanKept(url, queue) {
292
205
  const kept = this.memo.get(url)
293
206
  scanCounts[kept?.queued !== undefined ? 'replayed' : kept?.parse === undefined ? 'parsed' : 'resolved'] += 1
@@ -331,13 +244,8 @@ export class Scan {
331
244
  })
332
245
  }
333
246
 
334
- // Node's resolver: must match the runtime resolver's condition set (omitting one resolves
335
- // `exports`-gated packages to a different file) AND its order: the recorded key must equal the
336
- // runtime hooks' string for exact lookups. Extras go where Node puts user conditions in each set.
337
- // A custom resolver (--mainFields/--metro) resolves for a bundler instead, so it gets what
338
- // esbuild, webpack and Metro all assert: `import`/`require`, `default` (which Node's resolver
339
- // matches anyway) and the extras, never `node`, `node-addons` or `module-sync`; the same set as
340
- // resolve-fields.js's resolveConditions.
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).
341
249
  #conditionSet(context) {
342
250
  if (this.customResolve) return new Set([context, 'default', ...this.extraConditions])
343
251
  return context === 'import'
@@ -345,26 +253,15 @@ export class Scan {
345
253
  : new Set(['require', 'node', 'node-addons', ...this.extraConditions, 'module-sync'])
346
254
  }
347
255
 
348
- // `recovered`: false = parser crashed, nothing salvaged; true = oxc returned but we
349
- // 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.
350
257
  #recordParseError(url, format, message, recovered) {
351
258
  this.files.set(url, { format, edges: [], parseError: message })
352
259
  this.parseErrors.push({ url, format, message, recovered })
353
260
  }
354
261
 
355
- // --typescript: resolve `spec` the way tsc maps TS sources, invoked only AFTER Node's own
356
- // resolution missed -- so an on-disk `.js` (or any Node-resolvable target) always wins, and the
357
- // mapping only completes a failed resolution, never rewrites a successful one. The rules
358
- // (extension substitution, extensionless/index completion, manifest-target and tsconfig-paths
359
- // mapping) live in the shared dispatcher (resolve-typescript.js), so this resolver and the
360
- // legacy-field one cannot drift. The hit is realpathed to match req.resolve's canonical output
361
- // (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.
362
263
  #typescriptResolve(parentFile, spec, conditions) {
363
- const hit = resolveTypescriptFallback(parentFile, spec, {
364
- conditions,
365
- paths: this.typescriptPaths,
366
- host: this.host,
367
- })
264
+ const hit = resolveTypescriptFallback(parentFile, spec, { conditions, paths: this.typescriptPaths, host: this.host })
368
265
  if (hit == null) return null
369
266
  try {
370
267
  return this.host.realpath(hit)
@@ -373,9 +270,7 @@ export class Scan {
373
270
  }
374
271
  }
375
272
 
376
- // The file: URL a miss would resolve to under --typescript, or null: a hint for the unresolved
377
- // report only, never an edge. A custom resolver asks its --typescript twin; the built-in one
378
- // 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.
379
274
  #typescriptCandidate(parentFile, spec, conditions) {
380
275
  if (this.typescript) return null
381
276
  if (this.customResolve) return this.typescriptResolve?.(parentFile, spec, conditions)?.url ?? null
@@ -383,40 +278,60 @@ export class Scan {
383
278
  return hit == null ? null : pathToFileURL(hit).toString()
384
279
  }
385
280
 
386
- // Strip Flow type syntax to plain JS (flow.js's stripFlowTypes). Only invoked
387
- // as a parse fallback (see #scanFile), so plain-JS files never reach it. Returns the rewritten
388
- // source, or null when the transform itself fails on this file -- the caller then keeps oxc's
389
- // original parse error.
390
- #stripFlow(src, file) {
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) {
391
308
  try {
392
309
  return stripFlowTypes(src)
393
310
  } catch (cause) {
394
- 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})`)
395
312
  return null
396
313
  }
397
314
  }
398
315
 
399
- // Parse `file` (a script extension) as Node loads it: under the format its name and package
400
- // declare, or for a typeless .js/.ts the one Node detects from its syntax. -> { format, parsed,
401
- // errors } (`errors`: what the parse still reports), or { declared, crash } where the parser
402
- // 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.
403
317
  parseFile(file) {
404
318
  const ext = extname(file)
405
- // declared === null: typeless package; Node decides .js/.ts by syntax, resolved after parse.
406
319
  const declared = formatForFile(file, this.host, this.#types)
407
320
  const src = this.host.readFile(file).toString('utf8')
408
-
409
- // Resolve the parser outside the try: a missing oxc-parser is an env error, not this
410
- // 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.
411
322
  const parser = getParser()
412
- // oxc picks JS/TS + JSX from the filename; the .js family lands on plain JS (no JSX), so
413
- // force the JSX variant when opted in (`--jsx`) to parse React Native's JSX-in-.js source.
414
323
  const lang = this.jsx && JSX_EXTS.has(ext) ? 'jsx' : undefined
415
324
  const parseOptions = (sourceType) => (lang ? { sourceType, lang } : { sourceType })
416
- // `commonjs`, not `script`: Node runs CJS inside its module wrapper function, where a top-level
417
- // `return` or `new.target` is legal; oxc's `commonjs` accepts both and is otherwise `script`.
418
- const baseSourceType = declared === null ? 'unambiguous'
419
- : 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'
420
335
  let parsed
421
336
  try {
422
337
  parsed = parser.parseSync(file, src, parseOptions(baseSourceType))
@@ -424,88 +339,50 @@ export class Scan {
424
339
  return { declared, crash: cause.message }
425
340
  }
426
341
 
427
- // Match Node's syntax detection for typeless packages: ESM syntax picks the module variant
428
- // (mislabeling as commonjs makes `--bundle=load` feed the wrong translator). Only .ts takes the
429
- // `-typescript` suffix; .jsx/.tsx deliberately land on plain module/commonjs (like a JSX-in-.js
430
- // file) — they're bundler-transformed, keyed by extension in `stasis build`, not Node-strippable,
431
- // 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.
432
343
  const tsSuffix = ext === '.ts' ? '-typescript' : ''
433
344
  const detectedFormat = (p) => `${p.module.hasModuleSyntax ? 'module' : 'commonjs'}${tsSuffix}`
434
345
  let format = declared ?? detectedFormat(parsed)
435
-
436
- // oxc recovers from syntax errors (reports them in parsed.errors) rather than throwing;
437
- // Warning/Advice severities aren't real parse errors.
438
346
  let errors = syntaxErrors(parsed)
439
347
 
440
- // oxc's `unambiguous` mode counts top-level await as module syntax (since 0.109; before, a
441
- // TLA-only ESM file parsed here as a broken script). Keep mirroring Node for any script parse
442
- // that still fails: retry as module and prefer a clean module parse.
443
- if (errors.length > 0 && declared === null && !format.startsWith('module')) {
444
- try {
445
- const asModule = parser.parseSync(file, src, parseOptions('module'))
446
- if (syntaxErrors(asModule).length === 0) {
447
- parsed = asModule
448
- format = `module${tsSuffix}`
449
- errors = []
450
- }
451
- } catch { /* keep the script parse and its recorded errors */ }
452
- }
453
-
454
- // A typeless file Node detects as CJS runs inside the module wrapper too, but `unambiguous`
455
- // parses it as a plain script, so a top-level `return` still fails there: retry as `commonjs`
456
- // and prefer a clean parse (the detected commonjs format stands).
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.
457
350
  if (errors.length > 0 && declared === null && !format.startsWith('module')) {
458
- try {
459
- const asCommonjs = parser.parseSync(file, src, parseOptions('commonjs'))
460
- if (syntaxErrors(asCommonjs).length === 0) {
461
- parsed = asCommonjs
462
- errors = []
463
- }
464
- } 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
+ }
465
358
  }
466
359
 
467
- // --flow: oxc parses JS/TS but not Flow, so a Flow-typed .js/.cjs/.mjs file lands here with
468
- // parse errors. Only now -- after a raw oxc parse actually failed -- strip Flow types and
469
- // re-parse, so plain-JS files never pay the transform cost. Only the parse input is rewritten;
470
- // the bundle stores the pristine on-disk source (State/buildResolvedJsBundle re-read it), so
471
- // attestation still covers the real bytes. A clean re-parse replaces the errored one; otherwise
472
- // we keep the original result so a genuine (non-Flow) parse error still surfaces.
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.
473
362
  let strippedSource = null
474
- if (errors.length > 0 && this.flow && FLOW_EXTS.has(ext)) {
475
- const flowSrc = this.#stripFlow(src, file)
476
- if (flowSrc !== null) {
477
- try {
478
- const asFlow = parser.parseSync(file, flowSrc, parseOptions(baseSourceType))
479
- if (syntaxErrors(asFlow).length === 0) {
480
- parsed = asFlow
481
- format = declared ?? detectedFormat(parsed)
482
- errors = []
483
- strippedSource = flowSrc
484
- }
485
- } catch { /* keep the original parse + its errors */ }
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
486
371
  }
487
372
  }
488
-
489
- // `source`, and `strippedSource` where `parsed` is of its Flow-stripped form, for listsTypesOnly.
490
373
  return { format, parsed, errors, source: src, strippedSource }
491
374
  }
492
375
 
493
376
  #scanFile(url, queue) {
494
377
  const file = fileURLToPath(url)
495
378
  const ext = extname(file)
496
- if (ext === '.json') {
497
- this.files.set(url, { format: 'json', edges: [] })
498
- return
499
- }
500
- if (this.#isResource(file)) {
501
- // Allowlisted asset: carry it as a resource with no parse and no edges. `resource: true` is
502
- // the authoritative signal (both bundle builders key off it); the concrete format
503
- // (resource vs resource:base64) is byte-derived by the builder, so leave it unset here.
504
- this.files.set(url, { format: null, edges: [], resource: true })
505
- return
506
- }
507
- if (!SCRIPT_EXTS.has(ext)) {
508
- 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)
509
386
  return
510
387
  }
511
388
 
@@ -525,8 +402,7 @@ export class Scan {
525
402
  this.#recordParseError(url, format, parseError, true)
526
403
  return
527
404
  }
528
- // Script (CJS) files: walk on through oxc's recovered AST, so a scan reports the rest of the
529
- // graph's errors too; the parse error itself fails a bundle (analyzeScanner).
405
+ // A script walks on through oxc's recovered AST, so a scan reports the rest of the graph's errors too.
530
406
  this.parseErrors.push({ url, format, message: parseError, recovered: true })
531
407
  }
532
408
 
@@ -535,15 +411,12 @@ export class Scan {
535
411
  if (specs === undefined) {
536
412
  const touches = this.memo === null ? 0 : this.host.touches()
537
413
  specs = collectSpecifiers(parsed.program, this.defines, this.babel ? (node) => listsTypesOnly(node, result) && typesErased() : undefined)
538
- // What a scan keeps of a file whose reading touched a package not unpacked yet (#scanKept): of
539
- // its parse, what it imports, where finding that touched none, the parse itself let go.
414
+ // Where finding the specifiers touched no package not unpacked yet, they are kept in place of the parse.
540
415
  if (this.memo !== null && this.host.touches() === touches) this.#parse = { format, errors, specs }
541
416
  }
542
417
 
543
- // The edge KIND picks the resolution context, not the parent's format: createRequire()d
544
- // require() inside ESM resolves under REQUIRE conditions, literal import() inside CJS under
545
- // IMPORT conditions -- the parent-format set baked the wrong `exports` branch into the bundle.
546
- // 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.
547
420
  const formatContext = ['module', 'module-typescript'].includes(format) ? 'import' : 'require'
548
421
  const edges = []
549
422
  const specMaps = new Map() // condition key -> specifier -> child URL
@@ -552,11 +425,11 @@ export class Scan {
552
425
  nestedMap(specMaps, key).set(s.spec, childURL)
553
426
  if (RESOLVABLE_EXTS.has(extname(childPath)) || this.#isResource(childPath)) queue.push(childURL)
554
427
  }
555
- // Records the file --typescript would land the miss on too (typescriptURL), resolved as the miss was.
556
- const addUnresolved = (s, reason, conditions) => {
428
+ const addUnresolved = (s, reason, conditions, target) => {
557
429
  edges.push({ ...s, error: reason })
558
430
  const typescriptURL = this.#typescriptCandidate(file, s.spec, conditions)
559
- 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 } : {}) })
560
433
  }
561
434
 
562
435
  for (const s of specs) {
@@ -567,20 +440,14 @@ export class Scan {
567
440
  }
568
441
  const context = this.customResolve ? formatContext : (s.kind === 'require' ? 'require' : 'import')
569
442
  const conditions = this.#conditionSet(context)
570
- const key = condKey(conditions)
571
- // Custom (legacy-field) resolver owns builtin handling and the `empty` outcome (browser/RN
572
- // `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.
573
445
  if (this.customResolve) {
574
446
  const r = this.customResolve(file, s.spec, conditions)
575
- if (r?.builtin) {
576
- edges.push({ ...s, builtin: true })
577
- } else if (r?.empty) {
578
- edges.push({ ...s, empty: true })
579
- } else if (r?.url) {
580
- addChild(s, key, r.url, fileURLToPath(r.url))
581
- } else {
582
- addUnresolved(s, 'MODULE_NOT_FOUND', conditions)
583
- }
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)
584
451
  continue
585
452
  }
586
453
  if (isBuiltin(s.spec)) {
@@ -591,11 +458,10 @@ export class Scan {
591
458
  try {
592
459
  childPath = this.host.resolve(file, s.spec, conditions)
593
460
  } catch (cause) {
594
- // --typescript: complete the miss with tsc's mapping (see #typescriptResolve); the edge
595
- // 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.
596
462
  if (this.typescript) childPath = this.#typescriptResolve(file, s.spec, conditions)
597
463
  if (childPath == null) {
598
- addUnresolved(s, cause.code ?? cause.message, conditions)
464
+ addUnresolved(s, cause.code ?? cause.message, conditions, cause.target)
599
465
  continue
600
466
  }
601
467
  }
@@ -614,23 +480,15 @@ export class Scan {
614
480
  return r
615
481
  }
616
482
  const relEdge = (e) => (e.child ? { ...e, child: rel(e.child) } : e)
483
+ const remap = (map, entry) => new Map(map.entries().map(entry))
617
484
  return {
618
485
  entries: new Set([...this.entries].map(rel)),
619
- files: new Map(
620
- [...this.files].map(([k, v]) => [rel(k), { ...v, edges: v.edges.map(relEdge) }])
621
- ),
622
- imports: new Map(
623
- [...this.imports].map(([cond, byParent]) => [
624
- cond,
625
- new Map(
626
- [...byParent].map(([p, specs]) => [
627
- rel(p),
628
- new Map([...specs].map(([spec, child]) => [spec, rel(child)])),
629
- ])
630
- ),
631
- ])
632
- ),
633
- 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) } : {}) })),
634
492
  parseErrors: this.parseErrors.map(({ url, ...rest }) => ({ file: rel(url), ...rest })),
635
493
  }
636
494
  }