@exodus/stasis 1.0.0-beta.8 → 1.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -2
- package/bin/stasis.js +263 -446
- package/package.json +38 -13
- package/src/audit-corrections.js +11 -42
- package/src/audit.js +256 -212
- package/src/babel.js +119 -82
- package/src/bundle-from-lockfile.js +9 -21
- package/src/bundle-reader.js +24 -8
- package/src/bundle.js +0 -3
- package/src/cmd/build.js +211 -117
- package/src/cmd/bundle-options.js +347 -0
- package/src/cmd/bundle.js +1718 -908
- package/src/cmd/diff.js +1 -4
- package/src/cmd/github-bundle.js +48 -21
- package/src/cmd/output.js +24 -12
- package/src/cmd/sbom.js +6 -9
- package/src/cmd/tokens.js +60 -0
- package/src/define.js +218 -0
- package/src/diff.js +73 -129
- package/src/esbuild.js +0 -4
- package/src/flow.js +27 -0
- package/src/installed.js +265 -0
- package/src/json-stream.js +4 -9
- package/src/loader-mock.js +3 -8
- package/src/loader.js +1 -5
- package/src/loaders/bash.js +43 -120
- package/src/loaders/cargo.js +301 -808
- package/src/loaders/foundry.js +112 -290
- package/src/loaders/paths.js +15 -26
- package/src/loaders/php.js +221 -475
- package/src/loaders/rust.js +518 -1461
- package/src/loaders/solidity-ownership.js +51 -142
- package/src/loaders/solidity.js +42 -188
- package/src/loaders/toml.js +7 -17
- package/src/lockfile.js +0 -3
- package/src/metro-assets.js +41 -0
- package/src/metro-resolver.js +58 -133
- package/src/metro-transformer.js +0 -6
- package/src/metro.js +0 -4
- package/src/mock.js +32 -80
- package/src/parse.js +8 -7
- package/src/prune.js +0 -3
- package/src/resolve-fields.js +170 -235
- package/src/resolve-node.js +33 -46
- package/src/resolve-typescript.js +166 -298
- package/src/rollup.js +0 -4
- package/src/run-installed.js +10 -0
- package/src/sbom.js +70 -97
- package/src/scan.js +241 -278
- package/src/sixel.js +310 -0
- package/src/table.js +95 -0
- package/src/validate-notes.d.ts +19 -0
- package/src/validate-notes.js +72 -0
- package/src/validate.d.ts +159 -0
- package/src/validate.js +1315 -0
- package/src/vfs-bundle/entries.js +54 -46
- package/src/vfs-bundle/github.js +115 -92
- package/src/vfs-bundle/glob.js +379 -0
- package/src/vfs-bundle/lazy.js +99 -0
- package/src/vfs-bundle/prisma.js +235 -105
- package/src/vfs-bundle/soldeer.js +28 -49
- package/src/vfs-bundle/tree.js +434 -259
- package/src/vfs-bundle/yarnclean.js +79 -0
- package/src/vfs-bundle.js +25 -51
- package/src/webpack.js +0 -4
- package/src/why.js +79 -231
package/src/resolve-fields.js
CHANGED
|
@@ -2,216 +2,145 @@ import { isBuiltin } from 'node:module'
|
|
|
2
2
|
import { dirname, isAbsolute, join, relative, resolve as resolvePath } from 'node:path'
|
|
3
3
|
import { pathToFileURL } from 'node:url'
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { isAssetFile } from '@preventive/rn/metro-assets.js'
|
|
6
|
+
import { createMetroResolver, defaults as metroDefaults } from '@preventive/rn/metro-resolver.js'
|
|
7
|
+
import { memoized, toPosix } from '@exodus/stasis-core/util'
|
|
6
8
|
import {
|
|
9
|
+
fromOutputSource,
|
|
7
10
|
inNodeModules,
|
|
8
11
|
isDir,
|
|
9
|
-
|
|
12
|
+
isSourceFile,
|
|
10
13
|
locatePackage,
|
|
11
14
|
nearestPackage,
|
|
12
|
-
|
|
15
|
+
readManifest,
|
|
13
16
|
resolveTypescriptFallback,
|
|
17
|
+
tsconfigOutputsOf,
|
|
14
18
|
typescriptSiblings,
|
|
15
19
|
} from './resolve-typescript.js'
|
|
16
20
|
import { diskHost } from '@exodus/stasis-core/host'
|
|
21
|
+
import { metroHost, scanResolution, stripTypeDeclaration } from './metro-resolver.js'
|
|
22
|
+
import { RELATIVE_REQUEST, createNodeResolver } from './resolve-node.js'
|
|
23
|
+
import { hostMemo } from './vfs-bundle/lazy.js'
|
|
17
24
|
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
// delegated to Node. No user code is executed (only package.json is stat/read).
|
|
22
|
-
//
|
|
23
|
-
// Returns, for each specifier:
|
|
24
|
-
// { url } resolved to a real file (file: URL string)
|
|
25
|
-
// { empty } a browser/react-native field mapped it to `false` (empty module)
|
|
26
|
-
// { builtin } a Node builtin
|
|
27
|
-
// null unresolved
|
|
25
|
+
// Legacy package fields (`react-native`/`browser`/`main`, their redirect maps) and platform suffixes, as Metro resolves
|
|
26
|
+
// them: -> { url } | { empty } (mapped to `false`) | { builtin } | null. Targets without Node builtins take a bare builtin
|
|
27
|
+
// name (`buffer`) for the installed package so named, as esbuild, webpack 5 and Metro do, else for the builtin.
|
|
28
28
|
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
// `--metro` resolver matches that by default; flip this to `false` to fail closed instead:
|
|
33
|
-
// a `false` match on the entry then yields an EMPTY module. Note the off state is deliberately
|
|
34
|
-
// stricter than both tools -- for a bare-key `false`, Metro keeps `main` and esbuild would not
|
|
35
|
-
// match the entry at all -- it exists as an escape hatch, not an esbuild-parity mode. This only
|
|
36
|
-
// affects the `--metro` path; the `--mainFields` path always empties (matching esbuild).
|
|
37
|
-
// Tests can override per-resolver via createFieldResolver's `metroKeepEntryOnBrowserFalse`.
|
|
38
|
-
const METRO_KEEP_ENTRY_ON_BROWSER_FALSE = true
|
|
29
|
+
// Object-valued fields are redirect maps, never an entry.
|
|
30
|
+
const packageEntry = (pkg, mainFields) =>
|
|
31
|
+
mainFields.map((name) => pkg[name]).find((entry) => typeof entry === 'string' && entry.length > 0) ?? 'index'
|
|
39
32
|
|
|
40
|
-
//
|
|
41
|
-
function packageEntry(pkg, mainFields) {
|
|
42
|
-
for (const name of mainFields) {
|
|
43
|
-
if (typeof pkg?.[name] === 'string' && pkg[name].length > 0) return pkg[name]
|
|
44
|
-
}
|
|
45
|
-
return 'index'
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Merge object-valued mainFields (browser-spec maps) into one redirect map. Iterate in REVERSE
|
|
49
|
-
// so earlier mainFields win on conflict. Keys are verbatim: `./x` relative vs bare `x` (kept bare).
|
|
33
|
+
// Earlier mainFields win on conflict. Keys are verbatim: `./x` relative vs bare `x`.
|
|
50
34
|
function mergeRedirectMap(pkg, mainFields) {
|
|
51
35
|
const map = new Map()
|
|
52
|
-
for (
|
|
53
|
-
const field = pkg?.[
|
|
36
|
+
for (const name of mainFields.toReversed()) {
|
|
37
|
+
const field = pkg?.[name]
|
|
54
38
|
if (!field || typeof field !== 'object') continue
|
|
55
39
|
for (const [key, value] of Object.entries(field)) {
|
|
56
|
-
if (typeof value
|
|
57
|
-
map.set(key, value)
|
|
40
|
+
if (typeof value === 'string' || value === false) map.set(key, value)
|
|
58
41
|
}
|
|
59
42
|
}
|
|
60
43
|
return map
|
|
61
44
|
}
|
|
62
45
|
|
|
63
|
-
//
|
|
64
|
-
//
|
|
65
|
-
|
|
66
|
-
// (string or false) decides, so this must reproduce Metro's list exactly, duplicates included.
|
|
67
|
-
function metroEntryVariants(main) {
|
|
68
|
-
const toggled = main.startsWith('./') ? main.slice(2) : `./${main}`
|
|
69
|
-
return [main, toggled].flatMap((v) => [v, `${v}.js`, `${v}.json`, v.replace(/\.(?:js|json)$/u, '')])
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Resolve the package ENTRY (from mainFields) through the package's own redirect map.
|
|
73
|
-
// Returns { entry } (the redirected or original entry to resolve) or { empty: true }.
|
|
74
|
-
// Both halves of the entry semantics -- WHICH keys match, and what `false` means -- live here
|
|
75
|
-
// so no caller can get one without the other:
|
|
76
|
-
// - metro: Metro's getPackageEntryPoint. First matching variant (Metro's order) wins; a string
|
|
77
|
-
// redirects; `false` (any non-string) keeps `main`, unless the keep-on-false toggle is off,
|
|
78
|
-
// in which case it fails closed to an empty module (see METRO_KEEP_ENTRY_ON_BROWSER_FALSE).
|
|
79
|
-
// - default (--mainFields): esbuild-shaped candidates; `false` empties the entry, matching
|
|
80
|
-
// esbuild. The extension-STRIPPED bare spelling is not probed: for an extensioned main,
|
|
81
|
-
// esbuild treats it as a bare module name, not the entry. (For an extensionless main the
|
|
82
|
-
// `bare` candidate covers it -- esbuild does match that spelling; verified empirically.)
|
|
83
|
-
function resolveEntryThroughMap(map, main, opts) {
|
|
46
|
+
// As esbuild maps the package entry: the extension-stripped bare spelling is no candidate, esbuild taking it as a
|
|
47
|
+
// bare module name (an extensionless main's is `bare` itself, which it matches).
|
|
48
|
+
function resolveEntryThroughMap(map, main) {
|
|
84
49
|
if (map.size === 0) return { entry: main }
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
} else {
|
|
89
|
-
const bare = main.replace(/^\.\//u, '')
|
|
90
|
-
const stripped = bare.replace(/\.(?:js|json)$/u, '')
|
|
91
|
-
cands = [
|
|
92
|
-
`./${bare}`, bare,
|
|
93
|
-
`./${stripped}`,
|
|
94
|
-
`./${stripped}.js`, `${stripped}.js`,
|
|
95
|
-
`./${stripped}.json`, `${stripped}.json`,
|
|
96
|
-
]
|
|
97
|
-
}
|
|
98
|
-
for (const cand of cands) {
|
|
50
|
+
const bare = main.replace(/^\.\//u, '')
|
|
51
|
+
const stripped = bare.replace(/\.(?:js|json)$/u, '')
|
|
52
|
+
for (const cand of [`./${bare}`, bare, `./${stripped}`, `./${stripped}.js`, `${stripped}.js`, `./${stripped}.json`, `${stripped}.json`]) {
|
|
99
53
|
const v = map.get(cand)
|
|
100
54
|
if (typeof v === 'string') return { entry: v }
|
|
101
|
-
if (v === false) {
|
|
102
|
-
if (opts.metro && opts.metroKeepEntryOnBrowserFalse) return { entry: main }
|
|
103
|
-
return { empty: true }
|
|
104
|
-
}
|
|
55
|
+
if (v === false) return { empty: true }
|
|
105
56
|
}
|
|
106
57
|
return { entry: main }
|
|
107
58
|
}
|
|
108
59
|
|
|
109
|
-
|
|
60
|
+
// By real path, as Node, Metro and esbuild record a file: under pnpm a package's imports resolve only from where it lies.
|
|
61
|
+
function fileResolution(path, host) {
|
|
62
|
+
try {
|
|
63
|
+
return { url: pathToFileURL(host.realpath(path)).toString() }
|
|
64
|
+
} catch {
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Map keys are strings, so a miss gets undefined.
|
|
70
|
+
const matchRedirect = (map, relPath) => map.get([relPath, `${relPath}.js`, `${relPath}.json`].find((cand) => map.has(cand)))
|
|
110
71
|
|
|
111
|
-
//
|
|
112
|
-
function
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
72
|
+
// Each resolver reads its own manifests, so a build on the disk reads each as it is then.
|
|
73
|
+
function manifestReader(host) {
|
|
74
|
+
const manifests = hostMemo(host)
|
|
75
|
+
return (path) => manifests(path, () => readManifest(path, host))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export const METRO_SOURCE_EXTS = metroDefaults.sourceExts
|
|
79
|
+
export const METRO_ASSET_EXTS = new Set(metroDefaults.assetExts)
|
|
80
|
+
export const METRO_ASSET_RESOLUTIONS = metroDefaults.assetResolutions
|
|
81
|
+
const METRO_OWN_CONDITIONS = new Set(['default', 'import', 'require'])
|
|
82
|
+
|
|
83
|
+
// --metro --typescript: an absent JS output is its first TS sibling on disk, else an unbuilt output's source.
|
|
84
|
+
function typescriptView(view, { outputs, host }) {
|
|
85
|
+
const sibling = (path) => typescriptSiblings(path).find((candidate) => view.isFile(candidate)) ?? null
|
|
86
|
+
const source = (path) => {
|
|
87
|
+
const pick = sibling(path) ?? fromOutputSource(path, (input) => (view.isFile(input) ? input : sibling(input)), { outputs, host })
|
|
88
|
+
return pick === null || inNodeModules(pick, host) ? null : pick
|
|
89
|
+
}
|
|
90
|
+
const substituted = (path) => (view.isFile(path) || view.isDirectory(path) ? null : source(path))
|
|
91
|
+
return {
|
|
92
|
+
...view,
|
|
93
|
+
isFile: (path) => view.isFile(path) || substituted(path) !== null,
|
|
94
|
+
realpath: (path) => view.realpath(substituted(path) ?? path),
|
|
116
95
|
}
|
|
117
|
-
return undefined
|
|
118
96
|
}
|
|
119
97
|
|
|
120
|
-
// Probe `base` for a source file in platform order: bare name first, then per ext
|
|
121
|
-
// `base.<platform>.<ext>`, `base.native.<ext>` (native only), `base.<ext>`. `platform: null`
|
|
122
|
-
// disables suffixes. Under metro, every SUFFIXED candidate first passes through the candidate's
|
|
123
|
-
// own package redirect map -- mirroring Metro's resolveSourceFileForExt, which runs
|
|
124
|
-
// redirectModulePath on each non-bare candidate: `false` short-circuits the whole probe to an
|
|
125
|
-
// empty module (e.g. browser { "./index.ios.js": false }), and a string redirect re-lands the
|
|
126
|
-
// candidate package-root-relative. Returns a path, { empty: true } (metro only), or null.
|
|
127
98
|
function resolveSourceFile(base, opts) {
|
|
128
|
-
//
|
|
129
|
-
// declaration-free stem instead, so an entry field pointing at `dist/index.d.ts` lands on the real
|
|
130
|
-
// `dist/index.js` beside it rather than bundling the declaration -- or failing the package
|
|
131
|
-
// outright by falling back to a different index. The probe below still refuses a literal hit.
|
|
99
|
+
// An entry field naming `dist/index.d.ts` lands on the `dist/index.js` beside it.
|
|
132
100
|
base = stripTypeDeclaration(base)
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const pkg = nearestPackage(base, opts.host)
|
|
138
|
-
scope = pkg ? { pkgDir: pkg.pkgDir, map: mergeRedirectMap(pkg.pkg, opts.mainFields) } : null
|
|
139
|
-
}
|
|
140
|
-
if (!scope || scope.map.size === 0) return p
|
|
141
|
-
const rel = `./${toPosix(relative(scope.pkgDir, p))}`
|
|
142
|
-
const r = matchRedirect(scope.map, rel)
|
|
143
|
-
if (r === false) return false
|
|
144
|
-
if (typeof r === 'string') return isAbsolute(r) ? r : resolvePath(scope.pkgDir, r)
|
|
145
|
-
return p
|
|
146
|
-
}
|
|
147
|
-
// `derived` marks a candidate that is not the literal base; Metro redirect-checks those only.
|
|
148
|
-
const probePath = (p, derived) => {
|
|
149
|
-
if (opts.metro && derived) {
|
|
150
|
-
const r = redirectCandidate(p)
|
|
151
|
-
if (r === false) return { empty: true }
|
|
152
|
-
p = r
|
|
153
|
-
}
|
|
154
|
-
// A `.d.ts` is types-only, erased at runtime: never a resolution target, so a package whose
|
|
155
|
-
// entry field points at one falls through to its real `.js` instead of bundling the declaration.
|
|
156
|
-
if (isTypeDeclaration(p)) return null
|
|
157
|
-
return isFile(p, opts.host) ? p : null
|
|
158
|
-
}
|
|
159
|
-
// `suffix` is everything appended to `base`.
|
|
160
|
-
const probe = (suffix) => probePath(`${base}${suffix}`, suffix !== '')
|
|
161
|
-
const tryExt = (sourceExt) => {
|
|
162
|
-
if (opts.platform) {
|
|
163
|
-
const hit = probe(`.${opts.platform}${sourceExt}`)
|
|
164
|
-
if (hit) return hit
|
|
165
|
-
}
|
|
166
|
-
if (opts.preferNative && sourceExt !== '') {
|
|
167
|
-
const hit = probe(`.native${sourceExt}`)
|
|
168
|
-
if (hit) return hit
|
|
169
|
-
}
|
|
170
|
-
return probe(sourceExt)
|
|
171
|
-
}
|
|
101
|
+
const probe = (suffix) => (isSourceFile(`${base}${suffix}`, opts.host) ? `${base}${suffix}` : null)
|
|
102
|
+
const tryExt = (ext) => (opts.platform && probe(`.${opts.platform}${ext}`))
|
|
103
|
+
|| (opts.preferNative && ext !== '' && probe(`.native${ext}`))
|
|
104
|
+
|| probe(ext)
|
|
172
105
|
const bare = tryExt('')
|
|
173
106
|
if (bare) return bare
|
|
174
|
-
//
|
|
175
|
-
//
|
|
176
|
-
// always wins over its `.ts` twin. Literal siblings only -- no platform suffixes (tsc has none);
|
|
177
|
-
// extensionless bases keep going through the appended-extension loop below (sourceExts carries
|
|
178
|
-
// ts and tsx). Placed before that loop so `x.js` -> `x.ts` beats a pathological
|
|
179
|
-
// `x.js.<ext>`, matching tsc's candidate order. Never into node_modules: where tsc's pick, the
|
|
180
|
-
// first sibling on disk, lies there by its real path, nothing maps (inNodeModules), as the shared
|
|
181
|
-
// fallback refuses its own -- never a later sibling tsc wouldn't pick. The resolver drops
|
|
182
|
-
// `typescript` for an importer there.
|
|
107
|
+
// tsc's substitution comes once the literal file is absent, before the appended extensions, as tsc orders them.
|
|
108
|
+
// Its pick, the first sibling on disk, maps nothing in node_modules, never to a later sibling.
|
|
183
109
|
if (opts.typescript) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
if (hit === null) continue
|
|
187
|
-
return typeof hit === 'string' && inNodeModules(hit, opts.host) ? null : hit
|
|
188
|
-
}
|
|
110
|
+
const sibling = typescriptSiblings(base).find((candidate) => isSourceFile(candidate, opts.host))
|
|
111
|
+
if (sibling !== undefined) return inNodeModules(sibling, opts.host) ? null : sibling
|
|
189
112
|
}
|
|
190
113
|
for (const ext of opts.sourceExts) {
|
|
191
114
|
const hit = tryExt(`.${ext}`)
|
|
192
115
|
if (hit) return hit
|
|
193
116
|
}
|
|
117
|
+
// Not for a directory: resolveFileOrDir probes its index, which maps.
|
|
118
|
+
if (opts.typescript && !isDir(base, opts.host)) {
|
|
119
|
+
const source = (input) => {
|
|
120
|
+
const hit = resolveSourceFile(input, { ...opts, outputs: null })
|
|
121
|
+
return hit === null || inNodeModules(hit, opts.host) ? null : hit
|
|
122
|
+
}
|
|
123
|
+
return fromOutputSource(base, source, opts)
|
|
124
|
+
}
|
|
194
125
|
return null
|
|
195
126
|
}
|
|
196
127
|
|
|
197
|
-
//
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
function resolveFileOrDir(base, opts) {
|
|
201
|
-
// resolveSourceFile yields a path, { empty: true } (a metro candidate-redirect hit), or null.
|
|
202
|
-
const asResolution = (hit) => (hit == null ? null : typeof hit === 'string' ? fileResolution(hit) : hit)
|
|
128
|
+
// `exports` is not consulted: Node applies it only to bare package names.
|
|
129
|
+
function resolveFileOrDir(base, opts, tried) {
|
|
130
|
+
const asResolution = (hit) => (hit === null ? null : fileResolution(hit, opts.host))
|
|
203
131
|
const file = asResolution(resolveSourceFile(base, opts))
|
|
204
132
|
if (file) return file
|
|
205
133
|
if (isDir(base, opts.host)) {
|
|
206
|
-
const dpkg =
|
|
134
|
+
const dpkg = opts.read(join(base, 'package.json'))
|
|
207
135
|
if (dpkg) {
|
|
208
|
-
const r = resolveEntryThroughMap(mergeRedirectMap(dpkg, opts.mainFields), packageEntry(dpkg, opts.mainFields)
|
|
136
|
+
const r = resolveEntryThroughMap(mergeRedirectMap(dpkg, opts.mainFields), packageEntry(dpkg, opts.mainFields))
|
|
209
137
|
if (r.empty) return { empty: true }
|
|
210
|
-
// Node's LOAD_AS_DIRECTORY
|
|
211
|
-
// (`main: "./lib/"` -> lib/index.js); a broken `main` falls back to the package index below.
|
|
138
|
+
// Node's LOAD_AS_DIRECTORY: `main` as a file, else as a directory index; a broken one falls back to the index.
|
|
212
139
|
const entryBase = join(base, r.entry)
|
|
213
|
-
const
|
|
140
|
+
const entryOpts = { ...opts, pkgDir: base } // the package naming the entry, for its unbuilt output
|
|
141
|
+
const inner = asResolution(resolveSourceFile(entryBase, entryOpts) ?? resolveSourceFile(join(entryBase, 'index'), entryOpts))
|
|
214
142
|
if (inner) return inner
|
|
143
|
+
tried.push(entryBase)
|
|
215
144
|
}
|
|
216
145
|
const index = asResolution(resolveSourceFile(join(base, 'index'), opts))
|
|
217
146
|
if (index) return index
|
|
@@ -219,50 +148,80 @@ function resolveFileOrDir(base, opts) {
|
|
|
219
148
|
return null
|
|
220
149
|
}
|
|
221
150
|
|
|
222
|
-
//
|
|
223
|
-
// `mainFields`/`platform`/`preferNative`/`sourceExts` drive the legacy-field + suffix probing.
|
|
224
|
-
// `metro` opts into Metro's package-entry + candidate-redirect semantics (see
|
|
225
|
-
// resolveEntryThroughMap and resolveSourceFile); leave it off for the esbuild-parity
|
|
226
|
-
// `--mainFields` path. `typescript` adds tsc's extension substitution (a missing `x.js` probes
|
|
227
|
-
// its `x.ts` sibling; see resolveSourceFile) plus the shared miss fallback (see below), and
|
|
228
|
-
// `typescriptPaths` (a tsconfig paths matcher; see resolveTypescriptFallback) its tsconfig alias mapping.
|
|
229
|
-
// `metroKeepEntryOnBrowserFalse` overrides the module-level toggle
|
|
230
|
-
// (METRO_KEEP_ENTRY_ON_BROWSER_FALSE) per resolver -- primarily so tests can cover both branches.
|
|
231
|
-
// `host` is the filesystem view (@exodus/stasis-core/host), the real disk by default.
|
|
151
|
+
// `kept`: a Map kept across a lazy tree's rounds (untouchedMemo's `across`).
|
|
232
152
|
export function createFieldResolver({
|
|
233
153
|
conditions = [],
|
|
234
154
|
mainFields = ['main'],
|
|
235
155
|
platform = null,
|
|
236
156
|
preferNative = false,
|
|
237
|
-
sourceExts =
|
|
157
|
+
sourceExts = METRO_SOURCE_EXTS,
|
|
238
158
|
metro = false,
|
|
159
|
+
assetExts = METRO_ASSET_EXTS,
|
|
160
|
+
assetResolutions = METRO_ASSET_RESOLUTIONS,
|
|
239
161
|
typescript = false,
|
|
240
162
|
typescriptPaths = null,
|
|
241
|
-
metroKeepEntryOnBrowserFalse = METRO_KEEP_ENTRY_ON_BROWSER_FALSE,
|
|
242
163
|
host = diskHost,
|
|
164
|
+
kept,
|
|
243
165
|
} = {}) {
|
|
244
|
-
const
|
|
245
|
-
|
|
166
|
+
const read = manifestReader(host)
|
|
167
|
+
const outputs = typescript ? typescriptPaths?.outputs ?? tsconfigOutputsOf({ host }) : null
|
|
168
|
+
const opts = { platform, preferNative, sourceExts, mainFields, typescript, outputs, host, read }
|
|
169
|
+
// An importer in node_modules gets no --typescript mapping, wherever its import lands.
|
|
246
170
|
const fromNodeModules = { ...opts, typescript: false }
|
|
247
|
-
//
|
|
248
|
-
|
|
249
|
-
const
|
|
171
|
+
// Node answers a builtin name with the builtin, so a package named like one resolves through this, builtins off.
|
|
172
|
+
let packageResolver
|
|
173
|
+
const nodeResolver = () => (packageResolver ??= createNodeResolver(host))
|
|
174
|
+
// --typescript resolves Metro's misses again over typescriptView, never from node_modules, so it never overrides Metro.
|
|
175
|
+
const metroResolver = memoized(({ conditionNames, substituting }) => {
|
|
176
|
+
const view = metroHost(host, mainFields)
|
|
177
|
+
return createMetroResolver({ host: substituting ? typescriptView(view, opts) : view, sourceExts, assetExts, assetResolutions, mainFields, conditionNames, conditionsByPlatform: {}, preferNativePlatform: preferNative })
|
|
178
|
+
}, ({ conditionNames, substituting }) => `${substituting}\0${conditionNames.join('\0')}`)
|
|
179
|
+
const viaMetro = (parentFile, specifier, conds) => {
|
|
180
|
+
const conditionNames = [...conds].filter((name) => !METRO_OWN_CONDITIONS.has(name))
|
|
181
|
+
const request = { platform, isESMImport: conds.has('import') }
|
|
182
|
+
let resolution = metroResolver({ conditionNames, substituting: false })(parentFile, specifier, request)
|
|
183
|
+
if (resolution === null && typescript && !inNodeModules(parentFile, host)) resolution = metroResolver({ conditionNames, substituting: true })(parentFile, specifier, request)
|
|
184
|
+
const remapped = () => {
|
|
185
|
+
const imp = nearestPackage(parentFile, host, read)
|
|
186
|
+
return imp !== null && matchRedirect(mergeRedirectMap(imp.pkg, mainFields), specifier) !== undefined
|
|
187
|
+
}
|
|
188
|
+
return scanResolution(resolution, resolution === null && isBuiltin(specifier) && !remapped())
|
|
189
|
+
}
|
|
190
|
+
// `callConditions`: scan's, by the parent's format. What a miss tried goes in `tried`, the missTargets.
|
|
191
|
+
const resolve = function resolve(parentFile, specifier, callConditions, tried) {
|
|
250
192
|
const fileOpts = typescript && inNodeModules(parentFile, host) ? fromNodeModules : opts
|
|
251
193
|
const conds = new Set(callConditions ?? conditions)
|
|
194
|
+
const probe = (base) => {
|
|
195
|
+
const hit = resolveFileOrDir(base, fileOpts, tried)
|
|
196
|
+
if (hit === null) tried.push(base)
|
|
197
|
+
return hit
|
|
198
|
+
}
|
|
252
199
|
const viaNode = (spec) => {
|
|
253
200
|
try {
|
|
254
|
-
return fileResolution(host.resolve(parentFile, spec, conds))
|
|
255
|
-
|
|
201
|
+
if (!isBuiltin(spec)) return fileResolution(host.resolve(parentFile, spec, conds), host)
|
|
202
|
+
return fileResolution(nodeResolver().resolve(parentFile, spec, conds, { builtins: false }), host)
|
|
203
|
+
} catch (error) {
|
|
204
|
+
if (typeof error.target === 'string') tried.push(error.target)
|
|
256
205
|
return null
|
|
257
206
|
}
|
|
258
207
|
}
|
|
259
|
-
|
|
208
|
+
const viaPackage = (spec) => {
|
|
209
|
+
const loc = locatePackage(dirname(parentFile), spec, host)
|
|
210
|
+
if (!loc) return null
|
|
211
|
+
const pkg = read(join(loc.pkgDir, 'package.json')) ?? {}
|
|
212
|
+
if (pkg.exports != null) return viaNode(spec)
|
|
213
|
+
if (loc.subpath === '') return probe(loc.pkgDir)
|
|
214
|
+
const sub = `./${loc.subpath}`
|
|
215
|
+
const r = matchRedirect(mergeRedirectMap(pkg, mainFields), sub)
|
|
216
|
+
if (r === false) return { empty: true }
|
|
217
|
+
return probe(join(loc.pkgDir, typeof r === 'string' ? r : sub))
|
|
218
|
+
}
|
|
219
|
+
if (metro) return viaMetro(parentFile, specifier, conds)
|
|
260
220
|
if (specifier.startsWith('#')) return viaNode(specifier)
|
|
261
221
|
|
|
262
222
|
let spec = specifier
|
|
263
|
-
//
|
|
264
|
-
|
|
265
|
-
const imp = nearestPackage(parentFile, host)
|
|
223
|
+
// The importer's map applies before the builtin check: it can disable or shim a builtin (`{"crypto": false}`).
|
|
224
|
+
const imp = nearestPackage(parentFile, host, read)
|
|
266
225
|
if (imp) {
|
|
267
226
|
const map = mergeRedirectMap(imp.pkg, mainFields)
|
|
268
227
|
let r
|
|
@@ -270,69 +229,45 @@ export function createFieldResolver({
|
|
|
270
229
|
const abs = isAbsolute(spec) ? spec : resolvePath(dirname(parentFile), spec)
|
|
271
230
|
r = matchRedirect(map, `./${toPosix(relative(imp.pkgDir, abs))}`)
|
|
272
231
|
} else {
|
|
273
|
-
r = map.get(spec)
|
|
232
|
+
r = map.get(spec)
|
|
274
233
|
}
|
|
275
234
|
if (r === false) return { empty: true }
|
|
276
235
|
if (typeof r === 'string') {
|
|
277
|
-
//
|
|
278
|
-
|
|
279
|
-
if (r.startsWith('.') || isAbsolute(r)) {
|
|
280
|
-
return resolveFileOrDir(isAbsolute(r) ? r : resolvePath(imp.pkgDir, r), fileOpts)
|
|
281
|
-
}
|
|
236
|
+
// A path target is relative to the package root, not the importing file; a bare one is resolved below.
|
|
237
|
+
if (r.startsWith('.') || isAbsolute(r)) return probe(isAbsolute(r) ? r : resolvePath(imp.pkgDir, r))
|
|
282
238
|
spec = r
|
|
283
239
|
}
|
|
284
240
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
if (spec.startsWith('.') || isAbsolute(spec)) {
|
|
290
|
-
const base = isAbsolute(spec) ? spec : resolvePath(dirname(parentFile), spec)
|
|
291
|
-
return resolveFileOrDir(base, fileOpts)
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
const loc = locatePackage(dirname(parentFile), spec, host)
|
|
295
|
-
if (!loc) return null
|
|
296
|
-
const pkg = readJson(join(loc.pkgDir, 'package.json'), host) ?? {}
|
|
297
|
-
// `exports` wins over mainFields; Node's algorithm resolves it (with conditions) correctly.
|
|
298
|
-
if (pkg.exports != null) return viaNode(spec)
|
|
299
|
-
// A bare package import resolves its directory via the same dir algorithm as any other.
|
|
300
|
-
if (loc.subpath === '') return resolveFileOrDir(loc.pkgDir, fileOpts)
|
|
301
|
-
const sub = `./${loc.subpath}`
|
|
302
|
-
const r = matchRedirect(mergeRedirectMap(pkg, mainFields), sub)
|
|
303
|
-
if (r === false) return { empty: true }
|
|
304
|
-
const target = typeof r === 'string' ? r : sub
|
|
305
|
-
return resolveFileOrDir(join(loc.pkgDir, target), fileOpts)
|
|
241
|
+
if (spec.startsWith('.') || isAbsolute(spec)) return probe(isAbsolute(spec) ? spec : resolvePath(dirname(parentFile), spec))
|
|
242
|
+
if (isBuiltin(spec)) return spec.startsWith('node:') ? { builtin: true } : (viaPackage(spec) ?? { builtin: true })
|
|
243
|
+
return viaPackage(spec)
|
|
306
244
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
const
|
|
316
|
-
if (
|
|
317
|
-
|
|
318
|
-
conditions: new Set(callConditions ?? conditions),
|
|
319
|
-
paths: typescriptPaths,
|
|
320
|
-
host,
|
|
321
|
-
})
|
|
322
|
-
return hit == null ? null : fileResolution(hit)
|
|
245
|
+
// --typescript: a miss of the whole field flow, and only a miss, gets the scanner's fallback too.
|
|
246
|
+
const resolveWithTypescriptFallback = (parentFile, specifier, callConditions, tried) => {
|
|
247
|
+
const resolved = resolve(parentFile, specifier, callConditions, tried)
|
|
248
|
+
// Under --metro an asset is Metro's asset rule's to resolve, as tsc never does.
|
|
249
|
+
if (resolved || !typescript || (metro && isAssetFile(specifier, assetExts))) return resolved
|
|
250
|
+
const conds = new Set(callConditions ?? conditions)
|
|
251
|
+
const hit = resolveTypescriptFallback(parentFile, specifier, { conditions: conds, paths: typescriptPaths, host })
|
|
252
|
+
if (hit != null) return fileResolution(hit, host)
|
|
253
|
+
const aliased = typescriptPaths !== null && !RELATIVE_REQUEST.test(specifier) && !isAbsolute(specifier) && !specifier.startsWith('#') && !inNodeModules(parentFile, host)
|
|
254
|
+
if (aliased) tried.push(...typescriptPaths.matchPaths(specifier, parentFile))
|
|
255
|
+
return null
|
|
323
256
|
}
|
|
257
|
+
// Once per directory (all a file's resolving reads), specifier and conditions, kept as the manifests are and in `kept`.
|
|
258
|
+
const resolutions = hostMemo(host, { across: kept })
|
|
259
|
+
const of = (parentFile, specifier, callConditions) => resolutions(`${dirname(parentFile)}\0${specifier}\0${[...callConditions ?? conditions].join('\0')}`, () => {
|
|
260
|
+
const tried = []
|
|
261
|
+
return { resolution: resolveWithTypescriptFallback(parentFile, specifier, callConditions, tried), tried }
|
|
262
|
+
})
|
|
263
|
+
return Object.assign((parentFile, specifier, callConditions) => of(parentFile, specifier, callConditions).resolution, {
|
|
264
|
+
missTargets: (parentFile, specifier, callConditions) => of(parentFile, specifier, callConditions).tried,
|
|
265
|
+
})
|
|
324
266
|
}
|
|
325
267
|
|
|
326
|
-
//
|
|
327
|
-
|
|
328
|
-
return format === 'module' || format === 'module-typescript'
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// Condition set for a parent of the given format: Node's format-driven base (matching scan's
|
|
332
|
-
// #conditionsFor) plus extras. Order is irrelevant (Node membership-tests against exports key order).
|
|
268
|
+
// As scan's #conditionSet gives a custom resolver: a bundler's base, not Node's. esbuild, webpack and Metro assert
|
|
269
|
+
// `import`/`require` and `default`, never `node`, so an `exports` map listing `node` first can't win over the extras.
|
|
333
270
|
export function resolveConditions(format, extras = []) {
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
: ['node', 'require', 'module-sync', 'node-addons']
|
|
337
|
-
return [...new Set([...base, ...extras])]
|
|
271
|
+
const esm = format === 'module' || format === 'module-typescript'
|
|
272
|
+
return [...new Set([esm ? 'import' : 'require', 'default', ...extras])]
|
|
338
273
|
}
|