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