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

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.
@@ -15,6 +15,7 @@ import {
15
15
  } from './resolve-typescript.js'
16
16
  import { diskHost } from '@exodus/stasis-core/host'
17
17
  import { createNodeResolver } from './resolve-node.js'
18
+ import { untouchedMemo } from './vfs-bundle/lazy.js'
18
19
 
19
20
  // Static module resolver for legacy package fields (`react-native`/`browser`/`main` + browser-spec
20
21
  // redirect maps) and platform suffixes (`.ios`/`.android`/`.native`), reproducing Metro/React-Native
@@ -141,15 +142,24 @@ function matchRedirect(map, relPath) {
141
142
  return undefined
142
143
  }
143
144
 
145
+ // What readJson gives for each manifest a resolver reads, read once, as for a tree that holds still:
146
+ // but one whose reading read what a lazy tree does not hold yet (`host.touches`) read again, or with
147
+ // `host.retouch` taken as read again (untouchedMemo's). Each resolver reads its own, so a build on the
148
+ // disk reads each manifest as it is then.
149
+ function manifestReader(host) {
150
+ const manifests = untouchedMemo(host.touches, { retouch: host.retouch })
151
+ return (path) => manifests(path, () => readJson(path, host))
152
+ }
153
+
144
154
  // The package.json closest to `path` as Metro finds a module's package (getPackageForModule, through
145
155
  // metro-file-map's hierarchicalLookup): at the path itself where it is a directory, else above it,
146
156
  // none past a `node_modules` segment. A nested package.json (`dist/esm/package.json`, or a subpath
147
- // directory's own) is one. -> { pkgDir, pkg } or null
148
- function metroPackageScope(path, host) {
157
+ // directory's own) is one, read by `read`. -> { pkgDir, pkg } or null
158
+ function metroPackageScope(path, host, read = readJson) {
149
159
  for (let dir = isDir(path, host) ? path : dirname(path); basename(dir) !== 'node_modules'; dir = dirname(dir)) {
150
160
  const pkgPath = join(dir, 'package.json')
151
161
  if (isFile(pkgPath, host)) {
152
- const pkg = readJson(pkgPath, host)
162
+ const pkg = read(pkgPath, host)
153
163
  if (pkg) return { pkgDir: dir, pkg }
154
164
  }
155
165
  if (dirname(dir) === dir) break
@@ -162,7 +172,7 @@ function metroPackageScope(path, host) {
162
172
  // taken as package-relative, as Metro takes it, even where it names another package.
163
173
  // -> the path, redirected where the map says so, or false (an empty module)
164
174
  function metroRedirect(path, opts) {
165
- const scope = metroPackageScope(path, opts.host)
175
+ const scope = metroPackageScope(path, opts.host, opts.read)
166
176
  if (!scope) return path
167
177
  const rel = path === scope.pkgDir ? '' : toPosix(relative(scope.pkgDir, path))
168
178
  const r = matchRedirect(mergeRedirectMap(scope.pkg, opts.mainFields), `./${rel}`)
@@ -179,15 +189,18 @@ function parseBare(spec) {
179
189
  return { name: parts.slice(0, n).join('/'), firstPart: parts[0], subpath: parts.length > n ? `./${parts.slice(n).join('/')}` : '.' }
180
190
  }
181
191
 
182
- // Metro's default assetExts and assetResolutions (metro-config's defaults).
192
+ // Metro's default sourceExts, in the order its resolver probes them, and its default assetExts and
193
+ // assetResolutions (metro-config's defaults).
194
+ export const METRO_SOURCE_EXTS = ['js', 'jsx', 'json', 'ts', 'tsx']
183
195
  export const METRO_ASSET_EXTS = new Set(['bmp', 'gif', 'jpg', 'jpeg', 'png', 'psd', 'svg', 'webp', 'xml', 'm4v', 'mov', 'mp4', 'mpeg', 'mpg', 'webm', 'aac', 'aiff', 'caf', 'm4a', 'mp3', 'wav', 'html', 'pdf', 'yaml', 'yml', 'otf', 'ttf', 'zip'])
184
196
  const METRO_ASSET_RESOLUTIONS = ['1', '1.5', '2', '3', '4']
185
197
 
186
- // Metro's isAssetFile: any of the file name's dot-suffixes (`png`, or `tar.gz` and `gz`) an asset extension.
187
- function isMetroAssetFile(path) {
198
+ // Metro's isAssetFile: any of the file name's dot-suffixes (`png`, or `tar.gz` and `gz`) one of
199
+ // `assetExts` (Metro's defaults, else the project's).
200
+ export function isMetroAssetFile(path, assetExts = METRO_ASSET_EXTS) {
188
201
  const name = basename(path)
189
202
  for (let i = name.length - 1; i >= 0; i--) {
190
- if (name[i] === '.' && METRO_ASSET_EXTS.has(name.slice(i + 1))) return true
203
+ if (name[i] === '.' && assetExts.has(name.slice(i + 1))) return true
191
204
  }
192
205
  return false
193
206
  }
@@ -306,6 +319,15 @@ function metroExportsMap(exportsField) {
306
319
  return keys.some((key) => key.startsWith('.')) ? null : new Map([['.', flat]])
307
320
  }
308
321
 
322
+ // metroExportsMap of each `exports` object, as each resolver's manifests hold one (manifestReader),
323
+ // made once: a package's many imports each read it.
324
+ const exportsMaps = new WeakMap()
325
+ function metroExportsMapOf(exportsField) {
326
+ if (exportsField === null || typeof exportsField !== 'object') return metroExportsMap(exportsField)
327
+ if (!exportsMaps.has(exportsField)) exportsMaps.set(exportsField, metroExportsMap(exportsField))
328
+ return exportsMaps.get(exportsField)
329
+ }
330
+
309
331
  // Metro's relative specifier (isRelativeImport): `.`, `..`, or one starting `./` or `../`.
310
332
  const METRO_RELATIVE = /^\.\.?(?:\/|$)/u
311
333
 
@@ -323,12 +345,12 @@ function resolveSourceFile(base, opts) {
323
345
  // outright by falling back to a different index. The probe below still refuses a literal hit.
324
346
  base = stripTypeDeclaration(base)
325
347
  // Metro resolves an asset by its own rule, scale variants and all, not as a source file.
326
- if (opts.metro && isMetroAssetFile(base)) return metroAsset(base, opts.host)
348
+ if (opts.metro && isMetroAssetFile(base, opts.assetExts)) return metroAsset(base, opts.host)
327
349
  // Candidates share base's directory, hence its package scope; resolve it lazily, once.
328
350
  let scope
329
351
  const redirectCandidate = (p) => {
330
352
  if (scope === undefined) {
331
- const pkg = nearestPackage(base, opts.host)
353
+ const pkg = nearestPackage(base, opts.host, opts.read)
332
354
  scope = pkg ? { pkgDir: pkg.pkgDir, map: mergeRedirectMap(pkg.pkg, opts.mainFields) } : null
333
355
  }
334
356
  if (!scope || scope.map.size === 0) return p
@@ -397,7 +419,7 @@ function resolveFileOrDir(base, opts) {
397
419
  const file = asResolution(resolveSourceFile(base, opts))
398
420
  if (file) return file
399
421
  if (isDir(base, opts.host)) {
400
- const dpkg = readJson(join(base, 'package.json'), opts.host)
422
+ const dpkg = opts.read(join(base, 'package.json'))
401
423
  if (dpkg) {
402
424
  const r = resolveEntryThroughMap(mergeRedirectMap(dpkg, opts.mainFields), packageEntry(dpkg, opts.mainFields), opts)
403
425
  if (r.empty) return { empty: true }
@@ -419,26 +441,31 @@ function resolveFileOrDir(base, opts) {
419
441
  // packages (delegated to Node's algorithm), `mainFields`/`platform`/`preferNative`/`sourceExts`
420
442
  // drive the legacy-field + suffix probing.
421
443
  // `metro` opts into Metro's package-entry + candidate-redirect semantics (see
422
- // resolveEntryThroughMap and resolveSourceFile); leave it off for the esbuild-parity
423
- // `--mainFields` path. `typescript` adds tsc's extension substitution (a missing `x.js` probes
444
+ // resolveEntryThroughMap and resolveSourceFile), and its asset rule for `assetExts` (Metro's
445
+ // defaults, else the project's); leave it off for the esbuild-parity `--mainFields` path. `typescript` adds tsc's extension substitution (a missing `x.js` probes
424
446
  // its `x.ts` sibling; see resolveSourceFile) plus the shared miss fallback (see below), and
425
447
  // `typescriptPaths` (a tsconfig paths matcher; see resolveTypescriptFallback) its tsconfig alias mapping.
426
448
  // `metroKeepEntryOnBrowserFalse` overrides the module-level toggle
427
449
  // (METRO_KEEP_ENTRY_ON_BROWSER_FALSE) per resolver -- primarily so tests can cover both branches.
428
- // `host` is the filesystem view (@exodus/stasis-core/host), the real disk by default.
450
+ // `host` is the filesystem view (@exodus/stasis-core/host), the real disk by default. `kept`, a Map
451
+ // kept across a lazy tree's rounds, keeps there each resolution that read nothing the tree does not
452
+ // hold yet (untouchedMemo's `across`), for the rounds after.
429
453
  export function createFieldResolver({
430
454
  conditions = [],
431
455
  mainFields = ['main'],
432
456
  platform = null,
433
457
  preferNative = false,
434
- sourceExts = ['js', 'jsx', 'json', 'ts', 'tsx'],
458
+ sourceExts = METRO_SOURCE_EXTS,
435
459
  metro = false,
460
+ assetExts = METRO_ASSET_EXTS,
436
461
  typescript = false,
437
462
  typescriptPaths = null,
438
463
  metroKeepEntryOnBrowserFalse = METRO_KEEP_ENTRY_ON_BROWSER_FALSE,
439
464
  host = diskHost,
465
+ kept,
440
466
  } = {}) {
441
- const opts = { platform, preferNative, sourceExts, mainFields, metro, typescript, metroKeepEntryOnBrowserFalse, host }
467
+ const read = manifestReader(host)
468
+ const opts = { platform, preferNative, sourceExts, mainFields, metro, assetExts, typescript, metroKeepEntryOnBrowserFalse, host, read }
442
469
  // An importer in node_modules gets no --typescript mapping (inNodeModules), wherever its import lands.
443
470
  const fromNodeModules = { ...opts, typescript: false }
444
471
  // Node answers a builtin name with the builtin itself, so an `exports`-bearing package named like
@@ -451,7 +478,7 @@ export function createFieldResolver({
451
478
  // else that file itself, no probing; null where it names none.
452
479
  const metroTargetFile = (pkgDir, target) => {
453
480
  const path = join(pkgDir, target)
454
- const asset = isMetroAssetFile(path) ? metroAsset(path, host) : null
481
+ const asset = isMetroAssetFile(path, assetExts) ? metroAsset(path, host) : null
455
482
  if (asset !== null) return fileResolution(asset, host)
456
483
  return isFile(path, host) && !isTypeDeclaration(path) ? fileResolution(path, host) : null
457
484
  }
@@ -460,7 +487,7 @@ export function createFieldResolver({
460
487
  // or with one not starting `#`; null where it names none. With no such package or no `imports`
461
488
  // in it, Metro fails the specifier, with no file-based fallback.
462
489
  const viaMetroImports = (parentFile, specifier, conds) => {
463
- const scope = metroPackageScope(parentFile, host)
490
+ const scope = metroPackageScope(parentFile, host, read)
464
491
  const imports = scope?.pkg.imports
465
492
  if (imports == null) throw new MetroFailure(`No "imports" for ${specifier} from ${parentFile}`)
466
493
  const keys = Object.keys(imports)
@@ -484,13 +511,13 @@ export function createFieldResolver({
484
511
  }
485
512
  // The file `subpath` names through the package at `pkgDir`'s `exports` field, as Metro maps it.
486
513
  const viaExports = (pkgDir, exportsField, subpath) => {
487
- const map = metroExportsMap(exportsField)
514
+ const map = metroExportsMapOf(exportsField)
488
515
  const target = map === null ? null : metroMapTarget(map, subpath, conds, { exports: true })
489
516
  return target === null ? null : metroTargetFile(pkgDir, target)
490
517
  }
491
518
  const viaPackage = (spec) => {
492
519
  const { name, firstPart, subpath } = parseBare(spec)
493
- const own = nearestPackage(parentFile, host)
520
+ const own = nearestPackage(parentFile, host, read)
494
521
  if (own?.pkg.exports != null && own.pkg.name === name) {
495
522
  const hit = viaExports(own.pkgDir, own.pkg.exports, subpath)
496
523
  if (hit) return hit
@@ -501,7 +528,7 @@ export function createFieldResolver({
501
528
  if (isDir(subpath !== '.' || name !== firstPart ? join(nm, firstPart) : nm, host)) {
502
529
  const target = metroRedirect(join(nm, spec), opts)
503
530
  if (target === false) return { empty: true }
504
- const scope = metroPackageScope(target, host)
531
+ const scope = metroPackageScope(target, host, read)
505
532
  const hit = (scope?.pkg.exports != null && viaExports(scope.pkgDir, scope.pkg.exports, target === scope.pkgDir ? '.' : `./${toPosix(relative(scope.pkgDir, target))}`)) || viaPath(target)
506
533
  if (hit) return hit
507
534
  // A package there whose entry resolves to nothing ends the lookup: Metro throws on it
@@ -515,7 +542,7 @@ export function createFieldResolver({
515
542
  return viaPath(isAbsolute(specifier) ? specifier : resolvePath(dirname(parentFile), specifier))
516
543
  }
517
544
  let spec = specifier
518
- const imp = nearestPackage(parentFile, host)
545
+ const imp = nearestPackage(parentFile, host, read)
519
546
  const r = imp ? matchRedirect(mergeRedirectMap(imp.pkg, mainFields), spec) : undefined
520
547
  if (r === false) return { empty: true }
521
548
  if (typeof r === 'string') {
@@ -543,7 +570,7 @@ export function createFieldResolver({
543
570
  const viaPackage = (spec) => {
544
571
  const loc = locatePackage(dirname(parentFile), spec, host)
545
572
  if (!loc) return null
546
- const pkg = readJson(join(loc.pkgDir, 'package.json'), host) ?? {}
573
+ const pkg = read(join(loc.pkgDir, 'package.json')) ?? {}
547
574
  // `exports` wins over mainFields; Node's algorithm resolves it (with conditions) correctly.
548
575
  if (pkg.exports != null) return viaNode(spec)
549
576
  // A bare package import resolves its directory via the same dir algorithm as any other.
@@ -569,7 +596,7 @@ export function createFieldResolver({
569
596
  let spec = specifier
570
597
  // Redirect via the IMPORTER's browser/react-native map, BEFORE the builtin check on purpose:
571
598
  // a map entry can disable or shim a builtin (`{"crypto": false}` / `"crypto-browserify"`).
572
- const imp = nearestPackage(parentFile, host)
599
+ const imp = nearestPackage(parentFile, host, read)
573
600
  if (imp) {
574
601
  const map = mergeRedirectMap(imp.pkg, mainFields)
575
602
  let r
@@ -600,25 +627,30 @@ export function createFieldResolver({
600
627
  if (isBuiltin(spec)) return spec.startsWith('node:') ? { builtin: true } : (viaPackage(spec) ?? { builtin: true })
601
628
  return viaPackage(spec)
602
629
  }
603
- if (!typescript) return resolve
630
+ // Each resolution made once for each directory, specifier and conditions: a file's directory is all
631
+ // of it resolving from it reads (its package, the node_modules above it, its tsconfig `paths`).
632
+ // Kept as the resolver's manifests are (manifestReader), and in `kept` across rounds.
633
+ const resolutions = untouchedMemo(host.touches, { retouch: host.retouch, across: kept })
634
+ const once = (resolveFn) => (parentFile, specifier, callConditions) => resolutions(`${dirname(parentFile)}\0${specifier}\0${[...callConditions ?? conditions].join('\0')}`, () => resolveFn(parentFile, specifier, callConditions))
635
+ if (!typescript) return once(resolve)
604
636
  // --typescript: when the whole field flow leaves the specifier unresolved, give tsc's mapping
605
637
  // the same shot the scanner's fallback gets, via the shared dispatcher. On this path it covers
606
638
  // the layers the field resolver delegates to Node -- `exports`-bearing packages and `#` subpath
607
639
  // imports, whose targets may name compiled files existing only as TS source -- plus tsconfig
608
640
  // `paths` aliases; misses only, so no field/redirect/suffix resolution is ever overridden.
609
641
  // (Relative paths re-probe tsc-style too -- redundant after resolveSourceFile, but harmless.)
610
- return function resolveWithTypescriptFallback(parentFile, specifier, callConditions) {
642
+ return once(function resolveWithTypescriptFallback(parentFile, specifier, callConditions) {
611
643
  const resolved = resolve(parentFile, specifier, callConditions)
612
644
  if (resolved) return resolved
613
645
  // Under --metro an asset is Metro's asset rule's to resolve (resolveSourceFile), as tsc never does.
614
- if (metro && isMetroAssetFile(specifier)) return null
646
+ if (metro && isMetroAssetFile(specifier, assetExts)) return null
615
647
  const hit = resolveTypescriptFallback(parentFile, specifier, {
616
648
  conditions: new Set(callConditions ?? conditions),
617
649
  paths: typescriptPaths,
618
650
  host,
619
651
  })
620
652
  return hit == null ? null : fileResolution(hit, host)
621
- }
653
+ })
622
654
  }
623
655
 
624
656
  // True when `format` is an ESM family (drives the base condition set, mirroring scan).
@@ -3,6 +3,7 @@ import { basename, dirname, isAbsolute, join, normalize, resolve } from 'node:pa
3
3
  import { fileURLToPath, pathToFileURL } from 'node:url'
4
4
 
5
5
  import { packageJSONStat, packageJSONText } from '@exodus/stasis-core/bundle-util'
6
+ import { untouchedMemo } from './vfs-bundle/lazy.js'
6
7
 
7
8
  // Node's CommonJS resolution (`createRequire(parent).resolve(spec, { conditions })`) over a
8
9
  // `host` (@exodus/stasis-core/host), mirroring lib/internal/modules/cjs/loader.js and
@@ -87,7 +88,11 @@ function parsePackageName(specifier, base) {
87
88
  return { packageName, packageSubpath: `.${separatorIndex === -1 ? '' : specifier.slice(separatorIndex)}`, isScoped }
88
89
  }
89
90
 
91
+ // `host.touches`, where it has one, counts the reads it makes of what a tree does not hold yet
92
+ // (vfs-bundle/tree.js's vfsHost of a lazy tree): what is read with none is cached alone, or with
93
+ // `host.retouch`, what is read with some too (untouchedMemo's `retouch`).
90
94
  export function createNodeResolver(host) {
95
+ const touches = host.touches ?? (() => 0)
91
96
  // package.json reads, memoized per path; a malformed manifest, or one there that can't be read,
92
97
  // throws ERR_INVALID_PACKAGE_CONFIG on every access.
93
98
  const parsePackage = (pjsonPath) => {
@@ -110,10 +115,9 @@ export function createNodeResolver(host) {
110
115
  const map = (value) => (typeof value === 'string' || (typeof value === 'object' && value !== null) ? value : undefined)
111
116
  return { exists: true, path: pjsonPath, name: string(data.name), main: string(data.main), exports: map(data.exports), imports: map(data.imports) }
112
117
  }
113
- const pkgCache = new Map()
118
+ const packages = untouchedMemo(touches, { retouch: host.retouch })
114
119
  const readPackage = (pjsonPath) => {
115
- let pkg = pkgCache.get(pjsonPath)
116
- if (pkg === undefined) pkgCache.set(pjsonPath, pkg = parsePackage(pjsonPath))
120
+ const pkg = packages(pjsonPath, () => parsePackage(pjsonPath))
117
121
  if (pkg instanceof Error) throw pkg
118
122
  return pkg
119
123
  }
@@ -401,7 +405,7 @@ export function createNodeResolver(host) {
401
405
  }
402
406
 
403
407
  // Node's Module._pathCache: the tree is immutable for the build.
404
- const memo = new Map()
408
+ const memo = untouchedMemo(touches, { retouch: host.retouch })
405
409
 
406
410
  return {
407
411
  // `conditions`: `require.resolve`'s own by default. A builtin returns its id, as it does.
@@ -411,9 +415,7 @@ export function createNodeResolver(host) {
411
415
  parentFile = resolve(parentFile)
412
416
  if (!(conditions instanceof Set)) conditions = new Set(conditions)
413
417
  const key = `${dirname(parentFile)}\0${request}\0${[...conditions].join(',')}${builtins ? '' : '\0nobuiltins'}`
414
- let hit = memo.get(key)
415
- if (hit === undefined) memo.set(key, hit = resolveFilename(parentFile, request, conditions, builtins))
416
- return hit
418
+ return memo(key, () => resolveFilename(parentFile, request, conditions, builtins))
417
419
  },
418
420
  }
419
421
  }
@@ -103,13 +103,13 @@ export function locatePackage(fromDir, spec, host = diskHost) {
103
103
  }
104
104
 
105
105
  // Nearest package.json at/above `file`'s dir — the package whose fields (browser/RN map, imports)
106
- // govern the file's own imports.
107
- export function nearestPackage(file, host = diskHost) {
106
+ // govern the file's own imports. `read` reads a manifest, as readJson does.
107
+ export function nearestPackage(file, host = diskHost, read = readJson) {
108
108
  let dir = dirname(file)
109
109
  while (true) {
110
110
  const pkgPath = join(dir, 'package.json')
111
111
  if (isFile(pkgPath, host)) {
112
- const pkg = readJson(pkgPath, host)
112
+ const pkg = read(pkgPath, host)
113
113
  if (pkg) return { pkgDir: dir, pkg }
114
114
  }
115
115
  const parent = dirname(dir)
package/src/scan.js CHANGED
@@ -3,10 +3,12 @@ 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'
6
+ import { classifyExtension, classifyFormat, isTypeDeclaration, nestedMap, relativeEscapes } from '@exodus/stasis-core/util'
7
7
  import { inNodeModules, resolveTypescriptFallback, typescriptSiblings } from './resolve-typescript.js'
8
8
  import { branchFolder, parseDefine } from './define.js'
9
9
  import { diskHost } from '@exodus/stasis-core/host'
10
+ import { untouchedMemo } from './vfs-bundle/lazy.js'
11
+ import { FLOW_EXTS, stripFlowTypes } from './flow.js'
10
12
 
11
13
  // Static require/import graph walker: parses source, never loads or executes user code.
12
14
  // Dynamic specifiers (`require(name)`, `import('./'+x)`) are recorded as unresolved. With
@@ -21,36 +23,28 @@ const RESOLVABLE_EXTS = new Set([...SCRIPT_EXTS, '.json'])
21
23
  // generics collide with JSX, so tsc/oxc reserve JSX for .tsx — JSX-in-TS belongs in a .tsx file.
22
24
  const JSX_EXTS = new Set(['.js', '.cjs', '.mjs'])
23
25
 
24
- // Flow type syntax lives in the JS (non-TS) family; oxc parses TS natively, and running
25
- // flow-remove-types over a .ts/.tsx file would corrupt TS-only constructs, so --flow only strips
26
- // these. .jsx is included (it's JS + JSX, and can carry Flow types like .js); .tsx is not (it's
27
- // TypeScript). flow-remove-types preserves JSX, so a stripped .jsx re-parses under lang:jsx.
28
- const FLOW_EXTS = new Set(['.js', '.cjs', '.mjs', '.jsx'])
29
26
  const TS_EXTS = new Set(['.ts', '.cts', '.mts', '.tsx'])
30
27
 
31
- // Required lazily so non-JS bundlers don't load the native oxc parser.
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.
32
31
  let _parser
33
32
  export function getParser() {
34
- _parser ??= createRequire(import.meta.url)('oxc-parser')
33
+ if (_parser === undefined) {
34
+ const oxc = createRequire(import.meta.url)('oxc-parser')
35
+ _parser = oxc.rawTransferSupported() ? { ...oxc, parseSync: (file, source, options) => oxc.parseSync(file, source, { experimentalRawTransfer: true, ...options }) } : oxc
36
+ }
35
37
  return _parser
36
38
  }
37
39
 
38
- // flow-remove-types is stasis's own dependency, resolved from stasis itself (never from the project
39
- // being bundled) and loaded lazily, only under --flow: it strips Flow type syntax down to plain JS
40
- // before oxc (which parses JS/TS, not Flow) sees it.
41
- let _flowRemoveTypes
42
- function getFlowRemoveTypes() {
43
- _flowRemoveTypes ??= createRequire(import.meta.url)('flow-remove-types')
44
- return _flowRemoveTypes
45
- }
46
40
 
47
41
  // classifyFormat is the shared name->format authority (static and runtime bundles must agree); it
48
42
  // returns null for the .js/.ts family whose module system comes from package.json `type` (null =
49
43
  // detect from syntax, signaled up as null).
50
- function formatForFile(file, host) {
44
+ function formatForFile(file, host, kept) {
51
45
  const format = classifyFormat(file)
52
46
  if (format != null) return format
53
- const type = packageType(file, host)
47
+ const type = packageType(file, host, kept)
54
48
  if (type === null) return null
55
49
  return `${type}${extname(file) === '.ts' ? '-typescript' : ''}`
56
50
  }
@@ -122,14 +116,15 @@ function findCallSpecifiers(ast, push, fold = null) {
122
116
  // (`import type { A }` and `import { type A }` yield identical all-isType entries, and
123
117
  // `export {} from` yields no entry at all), so the statements are read off the AST, where
124
118
  // importKind/exportKind carry it. `defines` (parseDefine's) leave out the calls in branches they
125
- // keep from running; static import statements are never in one.
126
- function collectSpecifiers(program, defines = new Map()) {
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).
121
+ function collectSpecifiers(program, defines = new Map(), typesOnly = () => false) {
127
122
  const specs = []
128
123
  for (const node of program.body) {
129
124
  if (node.type === 'ImportDeclaration') {
130
- if (node.importKind !== 'type') specs.push({ kind: 'import', spec: node.source.value })
125
+ if (node.importKind !== 'type' && !typesOnly(node)) specs.push({ kind: 'import', spec: node.source.value })
131
126
  } else if (node.type === 'ExportNamedDeclaration') {
132
- if (node.source != null && node.exportKind !== 'type') specs.push({ kind: 'export-from', spec: node.source.value })
127
+ if (node.source != null && node.exportKind !== 'type' && !typesOnly(node)) specs.push({ kind: 'export-from', spec: node.source.value })
133
128
  } else if (node.type === 'ExportAllDeclaration') {
134
129
  if (node.exportKind !== 'type') specs.push({ kind: 'export-from', spec: node.source.value })
135
130
  }
@@ -175,10 +170,29 @@ export function importsTypescriptByOutputName(entries, host = diskHost) {
175
170
  return targets.length > 0 && targets.every((target) => host.stat(target) === null && mapsTo(target))
176
171
  }
177
172
 
173
+ const TYPE_KINDS = new Set(['type', 'typeof'])
174
+
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).
182
+ function listsTypesOnly(node, { source, strippedSource }) {
183
+ const specifiers = node.specifiers ?? []
184
+ if (specifiers.length > 0) return specifiers.every((specifier) => TYPE_KINDS.has(specifier.importKind ?? specifier.exportKind))
185
+ return strippedSource !== null && source.slice(node.start, node.end) !== strippedSource.slice(node.start, node.end)
186
+ }
187
+
178
188
  function condKey(set) {
179
189
  return [...set].join(', ')
180
190
  }
181
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).
194
+ export const scanCounts = { parsed: 0, resolved: 0, replayed: 0 }
195
+
182
196
  export class Scan {
183
197
  files = new Map()
184
198
  imports = new Map()
@@ -204,7 +218,13 @@ export class Scan {
204
218
  // `host`: the filesystem the walk reads and resolves through (@exodus/stasis-core/host).
205
219
  // `define` (each global name -> its value, see define.js): a require()/import() in a branch they
206
220
  // keep from running (`if (__DEV__)` under `{ __DEV__: false }`) is no edge.
207
- constructor({ conditions = [], resolve = null, jsx = false, flow = false, typescript = false, typescriptPaths = null, typescriptResolve = null, resources = new Set(), define, host = diskHost } = {}) {
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 } = {}) {
208
228
  this.extraConditions = [...conditions]
209
229
  this.customResolve = resolve
210
230
  this.typescriptResolve = typescriptResolve
@@ -214,9 +234,17 @@ export class Scan {
214
234
  this.typescriptPaths = typescriptPaths
215
235
  this.resources = resources
216
236
  this.defines = parseDefine('scan', define)
237
+ this.babel = babel
238
+ this.keepsTypeImports = keepsTypeImports
217
239
  this.host = host
240
+ this.memo = memo
241
+ this.#types = untouchedMemo(host.touches, { retouch: host.retouch })
218
242
  }
219
243
 
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).
246
+ #types
247
+
220
248
  // A reached file whose extension/filename is in the --resources allowlist: carried as an opaque
221
249
  // resource (bytes only, no parse, no edges), not rejected as "a source bundle can't carry".
222
250
  // classifyExtension returns 'resource' only for non-code names in the allowlist, so this never
@@ -234,14 +262,75 @@ export class Scan {
234
262
  this.entries.add(url)
235
263
  queue.push(url)
236
264
  }
237
- while (queue.length > 0) {
238
- const url = queue.shift()
265
+ // FIFO by an index, not shift(): the queue holds every edge's target, and shift() copies a large one.
266
+ for (let head = 0; head < queue.length; head++) {
267
+ const url = queue[head]
239
268
  if (this.files.has(url)) continue
240
- this.#scanFile(url, queue)
269
+ if (this.memo === null) {
270
+ scanCounts.parsed++
271
+ this.#scanFile(url, queue)
272
+ } else {
273
+ this.#scanKept(url, queue)
274
+ }
241
275
  }
242
276
  return this
243
277
  }
244
278
 
279
+ // The parse of the file being scanned a scan before kept (#scanKept), and the parse of it made here.
280
+ #reparse
281
+ #parse
282
+
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.
291
+ #scanKept(url, queue) {
292
+ const kept = this.memo.get(url)
293
+ scanCounts[kept?.queued !== undefined ? 'replayed' : kept?.parse === undefined ? 'parsed' : 'resolved'] += 1
294
+ if (kept?.queued !== undefined) {
295
+ this.files.set(url, kept.file)
296
+ for (const [key, specMap] of kept.imports) nestedMap(this.imports, key).set(url, specMap)
297
+ this.unresolved.push(...kept.unresolved)
298
+ this.parseErrors.push(...kept.parseErrors)
299
+ queue.push(...kept.queued)
300
+ for (const args of kept.warnings) console.warn(...args)
301
+ return
302
+ }
303
+ const touches = this.host.touches()
304
+ const at = { unresolved: this.unresolved.length, parseErrors: this.parseErrors.length, queue: queue.length }
305
+ // The scan of a file is synchronous: what console.warn is given meanwhile is the file's.
306
+ const warnings = []
307
+ const warn = console.warn
308
+ console.warn = (...args) => {
309
+ warnings.push(args)
310
+ warn.apply(console, args)
311
+ }
312
+ this.#reparse = kept?.parse
313
+ this.#parse = undefined
314
+ try {
315
+ this.#scanFile(url, queue)
316
+ } finally {
317
+ console.warn = warn
318
+ this.#reparse = undefined
319
+ }
320
+ if (this.host.touches() !== touches) {
321
+ if (this.#parse !== undefined) this.memo.set(url, { parse: this.#parse })
322
+ return
323
+ }
324
+ this.memo.set(url, {
325
+ file: this.files.get(url),
326
+ imports: [...this.imports].filter(([, byParent]) => byParent.has(url)).map(([key, byParent]) => [key, byParent.get(url)]),
327
+ unresolved: this.unresolved.slice(at.unresolved),
328
+ parseErrors: this.parseErrors.slice(at.parseErrors),
329
+ queued: queue.slice(at.queue),
330
+ warnings,
331
+ })
332
+ }
333
+
245
334
  // Node's resolver: must match the runtime resolver's condition set (omitting one resolves
246
335
  // `exports`-gated packages to a different file) AND its order: the recorded key must equal the
247
336
  // runtime hooks' string for exact lookups. Extras go where Node puts user conditions in each set.
@@ -294,16 +383,13 @@ export class Scan {
294
383
  return hit == null ? null : pathToFileURL(hit).toString()
295
384
  }
296
385
 
297
- // Strip Flow type syntax to plain JS via stasis's flow-remove-types (loaded lazily). Only invoked
386
+ // Strip Flow type syntax to plain JS (flow.js's stripFlowTypes). Only invoked
298
387
  // as a parse fallback (see #scanFile), so plain-JS files never reach it. Returns the rewritten
299
388
  // source, or null when the transform itself fails on this file -- the caller then keeps oxc's
300
389
  // original parse error.
301
390
  #stripFlow(src, file) {
302
- const flowRemoveTypes = getFlowRemoveTypes()
303
391
  try {
304
- // `all: true` ignores the @flow/@noflow pragma (RN sources mix both); the transform blanks
305
- // types with whitespace, so byte offsets and every import specifier are preserved.
306
- return flowRemoveTypes(src, { all: true }).toString()
392
+ return stripFlowTypes(src)
307
393
  } catch (cause) {
308
394
  console.warn(`[stasis] --flow: could not strip Flow types from ${file}; leaving it to oxc (${cause.message})`)
309
395
  return null
@@ -317,7 +403,7 @@ export class Scan {
317
403
  parseFile(file) {
318
404
  const ext = extname(file)
319
405
  // declared === null: typeless package; Node decides .js/.ts by syntax, resolved after parse.
320
- const declared = formatForFile(file, this.host)
406
+ const declared = formatForFile(file, this.host, this.#types)
321
407
  const src = this.host.readFile(file).toString('utf8')
322
408
 
323
409
  // Resolve the parser outside the try: a missing oxc-parser is an env error, not this
@@ -384,6 +470,7 @@ export class Scan {
384
470
  // the bundle stores the pristine on-disk source (State/buildResolvedJsBundle re-read it), so
385
471
  // attestation still covers the real bytes. A clean re-parse replaces the errored one; otherwise
386
472
  // we keep the original result so a genuine (non-Flow) parse error still surfaces.
473
+ let strippedSource = null
387
474
  if (errors.length > 0 && this.flow && FLOW_EXTS.has(ext)) {
388
475
  const flowSrc = this.#stripFlow(src, file)
389
476
  if (flowSrc !== null) {
@@ -393,12 +480,14 @@ export class Scan {
393
480
  parsed = asFlow
394
481
  format = declared ?? detectedFormat(parsed)
395
482
  errors = []
483
+ strippedSource = flowSrc
396
484
  }
397
485
  } catch { /* keep the original parse + its errors */ }
398
486
  }
399
487
  }
400
488
 
401
- return { format, parsed, errors }
489
+ // `source`, and `strippedSource` where `parsed` is of its Flow-stripped form, for listsTypesOnly.
490
+ return { format, parsed, errors, source: src, strippedSource }
402
491
  }
403
492
 
404
493
  #scanFile(url, queue) {
@@ -420,7 +509,8 @@ export class Scan {
420
509
  return
421
510
  }
422
511
 
423
- const result = this.parseFile(file)
512
+ const result = this.#reparse ?? this.parseFile(file)
513
+ this.#parse = result
424
514
  if (result.crash !== undefined) {
425
515
  this.#recordParseError(url, result.declared, result.crash, false)
426
516
  return
@@ -435,11 +525,20 @@ export class Scan {
435
525
  this.#recordParseError(url, format, parseError, true)
436
526
  return
437
527
  }
438
- // Script (CJS) files: oxc's recovered AST still yields the require()/import() edges — salvage them.
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).
439
530
  this.parseErrors.push({ url, format, message: parseError, recovered: true })
440
531
  }
441
532
 
442
- const specs = collectSpecifiers(parsed.program, this.defines)
533
+ const typesErased = () => result.strippedSource !== null || !this.keepsTypeImports?.(file)
534
+ let { specs } = result
535
+ if (specs === undefined) {
536
+ const touches = this.memo === null ? 0 : this.host.touches()
537
+ 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.
540
+ if (this.memo !== null && this.host.touches() === touches) this.#parse = { format, errors, specs }
541
+ }
443
542
 
444
543
  // The edge KIND picks the resolution context, not the parent's format: createRequire()d
445
544
  // require() inside ESM resolves under REQUIRE conditions, literal import() inside CJS under
@@ -450,8 +549,7 @@ export class Scan {
450
549
  const specMaps = new Map() // condition key -> specifier -> child URL
451
550
  const addChild = (s, key, childURL, childPath) => {
452
551
  edges.push({ ...s, child: childURL })
453
- if (!specMaps.has(key)) specMaps.set(key, new Map())
454
- specMaps.get(key).set(s.spec, childURL)
552
+ nestedMap(specMaps, key).set(s.spec, childURL)
455
553
  if (RESOLVABLE_EXTS.has(extname(childPath)) || this.#isResource(childPath)) queue.push(childURL)
456
554
  }
457
555
  // Records the file --typescript would land the miss on too (typescriptURL), resolved as the miss was.
@@ -504,10 +602,7 @@ export class Scan {
504
602
  addChild(s, key, pathToFileURL(childPath).toString(), childPath)
505
603
  }
506
604
 
507
- for (const [key, specMap] of specMaps) {
508
- if (!this.imports.has(key)) this.imports.set(key, new Map())
509
- this.imports.get(key).set(url, specMap)
510
- }
605
+ for (const [key, specMap] of specMaps) nestedMap(this.imports, key).set(url, specMap)
511
606
  this.files.set(url, parseError ? { format, edges, parseError } : { format, edges })
512
607
  }
513
608