@exodus/stasis 1.0.0-beta.14 → 1.0.0-beta.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/bin/stasis.js +99 -16
- package/package.json +22 -9
- package/src/audit-corrections.js +11 -42
- package/src/audit.js +241 -190
- package/src/babel.js +39 -122
- package/src/bundle-from-lockfile.js +9 -21
- package/src/bundle-reader.js +5 -9
- package/src/bundle.js +0 -3
- package/src/cmd/build.js +82 -174
- package/src/cmd/bundle-options.js +113 -167
- package/src/cmd/bundle.js +942 -1294
- package/src/cmd/diff.js +1 -4
- package/src/cmd/github-bundle.js +21 -26
- package/src/cmd/output.js +15 -12
- package/src/cmd/sbom.js +3 -8
- package/src/cmd/tokens.js +16 -23
- package/src/define.js +36 -94
- package/src/diff.js +72 -128
- package/src/esbuild.js +0 -4
- package/src/flow.js +18 -13
- 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 +41 -93
- package/src/loaders/cargo.js +298 -805
- package/src/loaders/foundry.js +109 -285
- package/src/loaders/paths.js +13 -13
- package/src/loaders/php.js +210 -489
- package/src/loaders/rust.js +507 -1430
- package/src/loaders/solidity-ownership.js +48 -128
- package/src/loaders/solidity.js +39 -122
- package/src/loaders/toml.js +7 -17
- package/src/lockfile.js +0 -3
- package/src/metro-assets.js +10 -384
- package/src/metro-resolver.js +58 -134
- package/src/metro-transformer.js +0 -5
- package/src/metro.js +0 -4
- package/src/mock.js +32 -80
- package/src/parse.js +4 -4
- package/src/prune.js +0 -3
- package/src/resolve-fields.js +137 -532
- package/src/resolve-node.js +27 -45
- package/src/resolve-typescript.js +166 -298
- package/src/rollup.js +0 -4
- package/src/run-installed.js +10 -0
- package/src/sbom.js +52 -128
- package/src/scan.js +142 -284
- package/src/sixel.js +310 -0
- package/src/table.js +63 -31
- 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 +596 -522
- package/src/vfs-bundle/entries.js +40 -57
- package/src/vfs-bundle/github.js +80 -122
- package/src/vfs-bundle/glob.js +4 -45
- package/src/vfs-bundle/lazy.js +27 -56
- package/src/vfs-bundle/prisma.js +235 -105
- package/src/vfs-bundle/soldeer.js +28 -49
- package/src/vfs-bundle/tree.js +295 -318
- package/src/vfs-bundle/yarnclean.js +10 -31
- package/src/vfs-bundle.js +23 -64
- package/src/webpack.js +0 -4
- package/src/why.js +78 -232
- package/src/installed-commits.js +0 -184
package/src/resolve-fields.js
CHANGED
|
@@ -1,435 +1,146 @@
|
|
|
1
1
|
import { isBuiltin } from 'node:module'
|
|
2
|
-
import {
|
|
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'
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
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'
|
|
19
24
|
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
// delegated to Node's algorithm, under a bundler's conditions rather than Node's (see
|
|
24
|
-
// resolveConditions); under `metro`, metro-resolver's own steps around them (viaMetro) and its own
|
|
25
|
-
// reading of `exports` and `imports` maps (metroMapTarget).
|
|
26
|
-
// No user code is executed (only package.json is stat/read).
|
|
27
|
-
//
|
|
28
|
-
// Returns, for each specifier:
|
|
29
|
-
// { url } resolved to a file, by its real path (file: URL string; see fileResolution)
|
|
30
|
-
// { empty } a browser/react-native field mapped it to `false` (empty module)
|
|
31
|
-
// { builtin } a Node builtin: a `node:` specifier, or a builtin name no installed package resolves
|
|
32
|
-
// null unresolved
|
|
33
|
-
//
|
|
34
|
-
// Browser and React Native targets have no Node builtins, so a bare builtin name (`buffer`,
|
|
35
|
-
// `events`, `util`, `process`, ...) resolves to the installed npm package of that name, as esbuild
|
|
36
|
-
// (platform browser), webpack 5 (target web) and Metro bundle it. Where nothing is installed it
|
|
37
|
-
// stays a builtin, as the --metro-resolver adapter reports Metro's miss on one (metro-resolver.js),
|
|
38
|
-
// rather than an unresolved edge: Metro and webpack fail the build there.
|
|
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.
|
|
39
28
|
|
|
40
|
-
//
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
// `--metro` resolver matches that by default; flip this to `false` to fail closed instead:
|
|
44
|
-
// a `false` match on the entry then yields an EMPTY module. Note the off state is deliberately
|
|
45
|
-
// stricter than both tools -- for a bare-key `false`, Metro keeps `main` and esbuild would not
|
|
46
|
-
// match the entry at all -- it exists as an escape hatch, not an esbuild-parity mode. This only
|
|
47
|
-
// affects the `--metro` path; the `--mainFields` path always empties (matching esbuild).
|
|
48
|
-
// Tests can override per-resolver via createFieldResolver's `metroKeepEntryOnBrowserFalse`.
|
|
49
|
-
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'
|
|
50
32
|
|
|
51
|
-
//
|
|
52
|
-
function packageEntry(pkg, mainFields) {
|
|
53
|
-
for (const name of mainFields) {
|
|
54
|
-
if (typeof pkg?.[name] === 'string' && pkg[name].length > 0) return pkg[name]
|
|
55
|
-
}
|
|
56
|
-
return 'index'
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Merge object-valued mainFields (browser-spec maps) into one redirect map. Iterate in REVERSE
|
|
60
|
-
// 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`.
|
|
61
34
|
function mergeRedirectMap(pkg, mainFields) {
|
|
62
35
|
const map = new Map()
|
|
63
|
-
for (
|
|
64
|
-
const field = pkg?.[
|
|
36
|
+
for (const name of mainFields.toReversed()) {
|
|
37
|
+
const field = pkg?.[name]
|
|
65
38
|
if (!field || typeof field !== 'object') continue
|
|
66
39
|
for (const [key, value] of Object.entries(field)) {
|
|
67
|
-
if (typeof value
|
|
68
|
-
map.set(key, value)
|
|
40
|
+
if (typeof value === 'string' || value === false) map.set(key, value)
|
|
69
41
|
}
|
|
70
42
|
}
|
|
71
43
|
return map
|
|
72
44
|
}
|
|
73
45
|
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
// (string or false) decides, so this must reproduce Metro's list exactly, duplicates included.
|
|
78
|
-
function metroEntryVariants(main) {
|
|
79
|
-
const toggled = main.startsWith('./') ? main.slice(2) : `./${main}`
|
|
80
|
-
return [main, toggled].flatMap((v) => [v, `${v}.js`, `${v}.json`, v.replace(/\.(?:js|json)$/u, '')])
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Resolve the package ENTRY (from mainFields) through the package's own redirect map.
|
|
84
|
-
// Returns { entry } (the redirected or original entry to resolve) or { empty: true }.
|
|
85
|
-
// Both halves of the entry semantics -- WHICH keys match, and what `false` means -- live here
|
|
86
|
-
// so no caller can get one without the other:
|
|
87
|
-
// - metro: Metro's getPackageEntryPoint. First matching variant (Metro's order) wins; a string
|
|
88
|
-
// redirects; `false` (any non-string) keeps `main`, unless the keep-on-false toggle is off,
|
|
89
|
-
// in which case it fails closed to an empty module (see METRO_KEEP_ENTRY_ON_BROWSER_FALSE).
|
|
90
|
-
// - default (--mainFields): esbuild-shaped candidates; `false` empties the entry, matching
|
|
91
|
-
// esbuild. The extension-STRIPPED bare spelling is not probed: for an extensioned main,
|
|
92
|
-
// esbuild treats it as a bare module name, not the entry. (For an extensionless main the
|
|
93
|
-
// `bare` candidate covers it -- esbuild does match that spelling; verified empirically.)
|
|
94
|
-
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) {
|
|
95
49
|
if (map.size === 0) return { entry: main }
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
} else {
|
|
100
|
-
const bare = main.replace(/^\.\//u, '')
|
|
101
|
-
const stripped = bare.replace(/\.(?:js|json)$/u, '')
|
|
102
|
-
cands = [
|
|
103
|
-
`./${bare}`, bare,
|
|
104
|
-
`./${stripped}`,
|
|
105
|
-
`./${stripped}.js`, `${stripped}.js`,
|
|
106
|
-
`./${stripped}.json`, `${stripped}.json`,
|
|
107
|
-
]
|
|
108
|
-
}
|
|
109
|
-
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`]) {
|
|
110
53
|
const v = map.get(cand)
|
|
111
54
|
if (typeof v === 'string') return { entry: v }
|
|
112
|
-
if (v === false) {
|
|
113
|
-
if (opts.metro && opts.metroKeepEntryOnBrowserFalse) return { entry: main }
|
|
114
|
-
return { empty: true }
|
|
115
|
-
}
|
|
55
|
+
if (v === false) return { empty: true }
|
|
116
56
|
}
|
|
117
57
|
return { entry: main }
|
|
118
58
|
}
|
|
119
59
|
|
|
120
|
-
//
|
|
121
|
-
// Metro and esbuild record one: under pnpm's isolated layout `node_modules/debug` links into
|
|
122
|
-
// `node_modules/.pnpm/debug@x/node_modules/debug`, whose own imports (a sibling `ms`) resolve only
|
|
123
|
-
// from where it really lies. Probing stays on the path as reached, so a candidate and the package
|
|
124
|
-
// scope read from it (resolveSourceFile's metro redirect) are one spelling; only the hit is
|
|
125
|
-
// realpathed. null where the file has no real path (gone since it was probed).
|
|
60
|
+
// By real path, as Node, Metro and esbuild record a file: under pnpm a package's imports resolve only from where it lies.
|
|
126
61
|
function fileResolution(path, host) {
|
|
127
|
-
let real
|
|
128
62
|
try {
|
|
129
|
-
|
|
63
|
+
return { url: pathToFileURL(host.realpath(path)).toString() }
|
|
130
64
|
} catch {
|
|
131
65
|
return null
|
|
132
66
|
}
|
|
133
|
-
return { url: pathToFileURL(real).toString() }
|
|
134
67
|
}
|
|
135
68
|
|
|
136
|
-
//
|
|
137
|
-
|
|
138
|
-
if (map.size === 0) return undefined
|
|
139
|
-
for (const cand of [relPath, `${relPath}.js`, `${relPath}.json`]) {
|
|
140
|
-
if (map.has(cand)) return map.get(cand)
|
|
141
|
-
}
|
|
142
|
-
return undefined
|
|
143
|
-
}
|
|
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)))
|
|
144
71
|
|
|
145
|
-
//
|
|
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.
|
|
72
|
+
// Each resolver reads its own manifests, so a build on the disk reads each as it is then.
|
|
149
73
|
function manifestReader(host) {
|
|
150
|
-
const manifests =
|
|
151
|
-
return (path) => manifests(path, () =>
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// The package.json closest to `path` as Metro finds a module's package (getPackageForModule, through
|
|
155
|
-
// metro-file-map's hierarchicalLookup): at the path itself where it is a directory, else above it,
|
|
156
|
-
// none past a `node_modules` segment. A nested package.json (`dist/esm/package.json`, or a subpath
|
|
157
|
-
// directory's own) is one, read by `read`. -> { pkgDir, pkg } or null
|
|
158
|
-
function metroPackageScope(path, host, read = readJson) {
|
|
159
|
-
for (let dir = isDir(path, host) ? path : dirname(path); basename(dir) !== 'node_modules'; dir = dirname(dir)) {
|
|
160
|
-
const pkgPath = join(dir, 'package.json')
|
|
161
|
-
if (isFile(pkgPath, host)) {
|
|
162
|
-
const pkg = read(pkgPath, host)
|
|
163
|
-
if (pkg) return { pkgDir: dir, pkg }
|
|
164
|
-
}
|
|
165
|
-
if (dirname(dir) === dir) break
|
|
166
|
-
}
|
|
167
|
-
return null
|
|
74
|
+
const manifests = hostMemo(host)
|
|
75
|
+
return (path) => manifests(path, () => readManifest(path, host))
|
|
168
76
|
}
|
|
169
77
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
return path
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// A bare specifier's package name, first segment and `exports` subpath ('.' or './x'), as Metro
|
|
185
|
-
// parses one (parseBareSpecifier).
|
|
186
|
-
function parseBare(spec) {
|
|
187
|
-
const parts = spec.split('/')
|
|
188
|
-
const n = spec.startsWith('@') && parts.length > 1 ? 2 : 1
|
|
189
|
-
return { name: parts.slice(0, n).join('/'), firstPart: parts[0], subpath: parts.length > n ? `./${parts.slice(n).join('/')}` : '.' }
|
|
190
|
-
}
|
|
191
|
-
|
|
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']
|
|
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'])
|
|
196
|
-
const METRO_ASSET_RESOLUTIONS = ['1', '1.5', '2', '3', '4']
|
|
197
|
-
|
|
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) {
|
|
201
|
-
const name = basename(path)
|
|
202
|
-
for (let i = name.length - 1; i >= 0; i--) {
|
|
203
|
-
if (name[i] === '.' && assetExts.has(name.slice(i + 1))) return true
|
|
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
|
|
204
89
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
export function metroAssetCandidates(dirPath, assetName, ext, host = diskHost) {
|
|
211
|
-
const base = join(dirPath, assetName)
|
|
212
|
-
const files = [`${base}${ext}`, ...METRO_ASSET_RESOLUTIONS.map((r) => `${base}@${r}x${ext}`)]
|
|
213
|
-
.filter((p) => isFile(p, host))
|
|
214
|
-
.map((p) => host.realpath(p))
|
|
215
|
-
return files.length > 0 ? files : null
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// The file Metro takes for an asset of several (getArrayLowestItem): the lowest path.
|
|
219
|
-
export const lowestPath = (paths) => paths.reduce((lowest, p) => (p < lowest ? p : lowest))
|
|
220
|
-
|
|
221
|
-
// Metro's asset resolution of `path` (resolveFile's, through metro-resolver's resolveAsset): the
|
|
222
|
-
// lowest of it and its scale variants, so `./a.png` lands on `a@3x.png` where only that exists;
|
|
223
|
-
// nothing for a path naming a scale variant itself (`./a@2x.png`), which Metro refuses.
|
|
224
|
-
function metroAsset(path, host) {
|
|
225
|
-
const ext = extname(path)
|
|
226
|
-
const name = basename(path, ext)
|
|
227
|
-
if (/@\d+(?:\.\d+)?x$/u.test(name)) return null
|
|
228
|
-
const files = metroAssetCandidates(dirname(path), name, ext, host)
|
|
229
|
-
return files === null ? null : lowestPath(files)
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// --metro: metro-resolver's own reading of an `exports` or `imports` map (PackageExportsResolve,
|
|
233
|
-
// PackageImportsResolve and their utils), which Node's differs from. Each entry reduces by the
|
|
234
|
-
// conditions (reduceConditionalExport): an object to the first of its keys `conds` holds, again
|
|
235
|
-
// until a string or null, so a fallback array, whose indices no condition names, to null. The whole
|
|
236
|
-
// map is refused where an entry reduces to a value not starting `./` (reduceExportsLikeMap), as
|
|
237
|
-
// Metro refuses it: a bare or `../` target anywhere fails every specifier the map serves. A
|
|
238
|
-
// specifier takes its own entry, else (where that reduces to null as well) the pattern whose base
|
|
239
|
-
// before its `*` is longest, then the longest (matchSubpathFromExportsLike). Each miss -- the map
|
|
240
|
-
// refused, the specifier unmatched, its target null or naming no file -- is Metro's cue to fall
|
|
241
|
-
// back to file-based resolution, which the callers take.
|
|
242
|
-
|
|
243
|
-
// Metro failing a resolution outright, where a miss falls back: on a map value it can't reduce (a
|
|
244
|
-
// number or boolean, where its reduceConditionalExport throws), or a `#` specifier from a package
|
|
245
|
-
// with no `imports` (resolve.js throws before its fallback).
|
|
246
|
-
class MetroFailure extends Error {}
|
|
247
|
-
|
|
248
|
-
// reduceConditionalExport: the string or null `value` reduces to under `conds`, or 'no-match'.
|
|
249
|
-
function metroReduceConditional(value, conds) {
|
|
250
|
-
let reduced = value
|
|
251
|
-
while (reduced != null && typeof reduced !== 'string') {
|
|
252
|
-
if (typeof reduced !== 'object') throw new MetroFailure(`Can't reduce ${JSON.stringify(reduced)} by conditions`)
|
|
253
|
-
let match = 'default' in reduced ? 'no-match' : null
|
|
254
|
-
for (const name in reduced) {
|
|
255
|
-
if (conds.has(name)) {
|
|
256
|
-
match = reduced[name]
|
|
257
|
-
break
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
reduced = match
|
|
261
|
-
}
|
|
262
|
-
return reduced
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
// matchSubpathPattern: what `pattern`'s `*` stands for in `subpath`, else null.
|
|
266
|
-
function metroPatternMatch(pattern, subpath) {
|
|
267
|
-
const [base, trailer] = pattern.split('*')
|
|
268
|
-
if (subpath.startsWith(base) && (trailer.length === 0 || (subpath.endsWith(trailer) && subpath.length >= pattern.length))) {
|
|
269
|
-
return subpath.slice(base.length, subpath.length - trailer.length)
|
|
270
|
-
}
|
|
271
|
-
return null
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// The target `subpath` takes in `map` (a Map of a package map's entries) under `conds`, each `*` its
|
|
275
|
-
// match, as Metro maps it; null where it takes none. An `exports` target holding an empty, `.`,
|
|
276
|
-
// `..` or `node_modules` segment is refused besides (findInvalidPathSegment).
|
|
277
|
-
function metroMapTarget(map, subpath, conds, { exports = false } = {}) {
|
|
278
|
-
// isSubpathDefinedInExportsLike: listed, as itself or by a one-`*` pattern, before conditions apply.
|
|
279
|
-
if (!map.has(subpath) && ![...map.keys()].some((key) => key.split('*').length === 2 && metroPatternMatch(key, subpath) !== null)) return null
|
|
280
|
-
const reduced = new Map()
|
|
281
|
-
for (const [key, value] of map) {
|
|
282
|
-
const target = metroReduceConditional(value, conds)
|
|
283
|
-
if (target !== 'no-match') reduced.set(key, target)
|
|
284
|
-
}
|
|
285
|
-
if ([...reduced.values()].some((target) => target != null && !target.startsWith('./'))) return null
|
|
286
|
-
let target = reduced.get(subpath)
|
|
287
|
-
let match = null
|
|
288
|
-
if (target == null) {
|
|
289
|
-
const patterns = [...reduced.keys()].filter((key) => key.includes('*')).toSorted((a, b) => b.indexOf('*') - a.indexOf('*') || b.length - a.length)
|
|
290
|
-
for (const key of patterns) {
|
|
291
|
-
match = metroPatternMatch(key, subpath)
|
|
292
|
-
if (match !== null) {
|
|
293
|
-
target = reduced.get(key)
|
|
294
|
-
break
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
if (target == null) return null
|
|
299
|
-
if (exports && target.slice(2).split(/[\\/]/u).some((segment) => ['', '.', '..', 'node_modules'].includes(segment))) return null
|
|
300
|
-
// The match as a replacement pattern, as Metro's replaceAll takes it: a `$&` in it is the `*`.
|
|
301
|
-
return match === null ? target : target.replaceAll('*', match)
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
// normalizeExportsField: an `exports` field as Metro's Map of subpaths, null where it refuses it --
|
|
305
|
-
// an array to its first `./` string (or first value), each subpath's legacy array to its first item.
|
|
306
|
-
function metroExportsMap(exportsField) {
|
|
307
|
-
if (typeof exportsField === 'string') return new Map([['.', exportsField]])
|
|
308
|
-
let root = exportsField
|
|
309
|
-
if (Array.isArray(exportsField)) root = exportsField.every((value) => typeof value === 'string') ? exportsField.find((value) => value.startsWith('./')) : exportsField[0]
|
|
310
|
-
if (root == null || Array.isArray(root)) return null
|
|
311
|
-
if (typeof root === 'string') return new Map([['.', root]])
|
|
312
|
-
const flat = {}
|
|
313
|
-
for (const [key, value] of Object.entries(root)) {
|
|
314
|
-
if (Array.isArray(value) && (value.length === 0 || Array.isArray(value[0]))) return null
|
|
315
|
-
flat[key] = Array.isArray(value) ? value[0] : value
|
|
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),
|
|
316
95
|
}
|
|
317
|
-
const keys = Object.keys(root)
|
|
318
|
-
if (keys.every((key) => key.startsWith('.') || key.startsWith('#'))) return new Map(Object.entries(flat))
|
|
319
|
-
return keys.some((key) => key.startsWith('.')) ? null : new Map([['.', flat]])
|
|
320
96
|
}
|
|
321
97
|
|
|
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
|
-
|
|
331
|
-
// Metro's relative specifier (isRelativeImport): `.`, `..`, or one starting `./` or `../`.
|
|
332
|
-
const METRO_RELATIVE = /^\.\.?(?:\/|$)/u
|
|
333
|
-
|
|
334
|
-
// Probe `base` for a source file in platform order: bare name first, then per ext
|
|
335
|
-
// `base.<platform>.<ext>`, `base.native.<ext>` (native only), `base.<ext>`. `platform: null`
|
|
336
|
-
// disables suffixes. Under metro, every SUFFIXED candidate first passes through the candidate's
|
|
337
|
-
// own package redirect map -- mirroring Metro's resolveSourceFileForExt, which runs
|
|
338
|
-
// redirectModulePath on each non-bare candidate: `false` short-circuits the whole probe to an
|
|
339
|
-
// empty module (e.g. browser { "./index.ios.js": false }), and a string redirect re-lands the
|
|
340
|
-
// candidate package-root-relative. Returns a path, { empty: true } (metro only), or null.
|
|
341
98
|
function resolveSourceFile(base, opts) {
|
|
342
|
-
//
|
|
343
|
-
// declaration-free stem instead, so an entry field pointing at `dist/index.d.ts` lands on the real
|
|
344
|
-
// `dist/index.js` beside it rather than bundling the declaration -- or failing the package
|
|
345
|
-
// 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.
|
|
346
100
|
base = stripTypeDeclaration(base)
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
const redirectCandidate = (p) => {
|
|
352
|
-
if (scope === undefined) {
|
|
353
|
-
const pkg = nearestPackage(base, opts.host, opts.read)
|
|
354
|
-
scope = pkg ? { pkgDir: pkg.pkgDir, map: mergeRedirectMap(pkg.pkg, opts.mainFields) } : null
|
|
355
|
-
}
|
|
356
|
-
if (!scope || scope.map.size === 0) return p
|
|
357
|
-
const rel = `./${toPosix(relative(scope.pkgDir, p))}`
|
|
358
|
-
const r = matchRedirect(scope.map, rel)
|
|
359
|
-
if (r === false) return false
|
|
360
|
-
if (typeof r === 'string') return isAbsolute(r) ? r : resolvePath(scope.pkgDir, r)
|
|
361
|
-
return p
|
|
362
|
-
}
|
|
363
|
-
// `derived` marks a candidate that is not the literal base; Metro redirect-checks those only.
|
|
364
|
-
const probePath = (p, derived) => {
|
|
365
|
-
if (opts.metro && derived) {
|
|
366
|
-
const r = redirectCandidate(p)
|
|
367
|
-
if (r === false) return { empty: true }
|
|
368
|
-
p = r
|
|
369
|
-
}
|
|
370
|
-
// A `.d.ts` is types-only, erased at runtime: never a resolution target, so a package whose
|
|
371
|
-
// entry field points at one falls through to its real `.js` instead of bundling the declaration.
|
|
372
|
-
if (isTypeDeclaration(p)) return null
|
|
373
|
-
return isFile(p, opts.host) ? p : null
|
|
374
|
-
}
|
|
375
|
-
// `suffix` is everything appended to `base`.
|
|
376
|
-
const probe = (suffix) => probePath(`${base}${suffix}`, suffix !== '')
|
|
377
|
-
const tryExt = (sourceExt) => {
|
|
378
|
-
if (opts.platform) {
|
|
379
|
-
const hit = probe(`.${opts.platform}${sourceExt}`)
|
|
380
|
-
if (hit) return hit
|
|
381
|
-
}
|
|
382
|
-
if (opts.preferNative && sourceExt !== '') {
|
|
383
|
-
const hit = probe(`.native${sourceExt}`)
|
|
384
|
-
if (hit) return hit
|
|
385
|
-
}
|
|
386
|
-
return probe(sourceExt)
|
|
387
|
-
}
|
|
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)
|
|
388
105
|
const bare = tryExt('')
|
|
389
106
|
if (bare) return bare
|
|
390
|
-
//
|
|
391
|
-
//
|
|
392
|
-
// always wins over its `.ts` twin. Literal siblings only -- no platform suffixes (tsc has none);
|
|
393
|
-
// extensionless bases keep going through the appended-extension loop below (sourceExts carries
|
|
394
|
-
// ts and tsx). Placed before that loop so `x.js` -> `x.ts` beats a pathological
|
|
395
|
-
// `x.js.<ext>`, matching tsc's candidate order. Never into node_modules: where tsc's pick, the
|
|
396
|
-
// first sibling on disk, lies there by its real path, nothing maps (inNodeModules), as the shared
|
|
397
|
-
// fallback refuses its own -- never a later sibling tsc wouldn't pick. The resolver drops
|
|
398
|
-
// `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.
|
|
399
109
|
if (opts.typescript) {
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
if (hit === null) continue
|
|
403
|
-
return typeof hit === 'string' && inNodeModules(hit, opts.host) ? null : hit
|
|
404
|
-
}
|
|
110
|
+
const sibling = typescriptSiblings(base).find((candidate) => isSourceFile(candidate, opts.host))
|
|
111
|
+
if (sibling !== undefined) return inNodeModules(sibling, opts.host) ? null : sibling
|
|
405
112
|
}
|
|
406
113
|
for (const ext of opts.sourceExts) {
|
|
407
114
|
const hit = tryExt(`.${ext}`)
|
|
408
115
|
if (hit) return hit
|
|
409
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
|
+
}
|
|
410
125
|
return null
|
|
411
126
|
}
|
|
412
127
|
|
|
413
|
-
//
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
function resolveFileOrDir(base, opts) {
|
|
417
|
-
// resolveSourceFile yields a path, { empty: true } (a metro candidate-redirect hit), or null.
|
|
418
|
-
const asResolution = (hit) => (hit == null ? null : typeof hit === 'string' ? fileResolution(hit, opts.host) : 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))
|
|
419
131
|
const file = asResolution(resolveSourceFile(base, opts))
|
|
420
132
|
if (file) return file
|
|
421
133
|
if (isDir(base, opts.host)) {
|
|
422
134
|
const dpkg = opts.read(join(base, 'package.json'))
|
|
423
135
|
if (dpkg) {
|
|
424
|
-
const r = resolveEntryThroughMap(mergeRedirectMap(dpkg, opts.mainFields), packageEntry(dpkg, opts.mainFields)
|
|
136
|
+
const r = resolveEntryThroughMap(mergeRedirectMap(dpkg, opts.mainFields), packageEntry(dpkg, opts.mainFields))
|
|
425
137
|
if (r.empty) return { empty: true }
|
|
426
|
-
// Node's LOAD_AS_DIRECTORY
|
|
427
|
-
// (`main: "./lib/"` -> lib/index.js); a broken `main` falls back to the package index below.
|
|
428
|
-
// Metro's resolvePackageEntryPoint has no such fallback: it throws (InvalidPackageError).
|
|
138
|
+
// Node's LOAD_AS_DIRECTORY: `main` as a file, else as a directory index; a broken one falls back to the index.
|
|
429
139
|
const entryBase = join(base, r.entry)
|
|
430
|
-
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))
|
|
431
142
|
if (inner) return inner
|
|
432
|
-
|
|
143
|
+
tried.push(entryBase)
|
|
433
144
|
}
|
|
434
145
|
const index = asResolution(resolveSourceFile(join(base, 'index'), opts))
|
|
435
146
|
if (index) return index
|
|
@@ -437,19 +148,7 @@ function resolveFileOrDir(base, opts) {
|
|
|
437
148
|
return null
|
|
438
149
|
}
|
|
439
150
|
|
|
440
|
-
//
|
|
441
|
-
// packages (delegated to Node's algorithm), `mainFields`/`platform`/`preferNative`/`sourceExts`
|
|
442
|
-
// drive the legacy-field + suffix probing.
|
|
443
|
-
// `metro` opts into Metro's package-entry + candidate-redirect semantics (see
|
|
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
|
|
446
|
-
// its `x.ts` sibling; see resolveSourceFile) plus the shared miss fallback (see below), and
|
|
447
|
-
// `typescriptPaths` (a tsconfig paths matcher; see resolveTypescriptFallback) its tsconfig alias mapping.
|
|
448
|
-
// `metroKeepEntryOnBrowserFalse` overrides the module-level toggle
|
|
449
|
-
// (METRO_KEEP_ENTRY_ON_BROWSER_FALSE) per resolver -- primarily so tests can cover both branches.
|
|
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.
|
|
151
|
+
// `kept`: a Map kept across a lazy tree's rounds (untouchedMemo's `across`).
|
|
453
152
|
export function createFieldResolver({
|
|
454
153
|
conditions = [],
|
|
455
154
|
mainFields = ['main'],
|
|
@@ -458,144 +157,70 @@ export function createFieldResolver({
|
|
|
458
157
|
sourceExts = METRO_SOURCE_EXTS,
|
|
459
158
|
metro = false,
|
|
460
159
|
assetExts = METRO_ASSET_EXTS,
|
|
160
|
+
assetResolutions = METRO_ASSET_RESOLUTIONS,
|
|
461
161
|
typescript = false,
|
|
462
162
|
typescriptPaths = null,
|
|
463
|
-
metroKeepEntryOnBrowserFalse = METRO_KEEP_ENTRY_ON_BROWSER_FALSE,
|
|
464
163
|
host = diskHost,
|
|
465
164
|
kept,
|
|
466
165
|
} = {}) {
|
|
467
166
|
const read = manifestReader(host)
|
|
468
|
-
const
|
|
469
|
-
|
|
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.
|
|
470
170
|
const fromNodeModules = { ...opts, typescript: false }
|
|
471
|
-
// Node answers a builtin name with the builtin
|
|
472
|
-
// one (see the header) resolves through this instead, with builtins off; built on first use.
|
|
171
|
+
// Node answers a builtin name with the builtin, so a package named like one resolves through this, builtins off.
|
|
473
172
|
let packageResolver
|
|
474
173
|
const nodeResolver = () => (packageResolver ??= createNodeResolver(host))
|
|
475
|
-
// --
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
const
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
// in it, Metro fails the specifier, with no file-based fallback.
|
|
489
|
-
const viaMetroImports = (parentFile, specifier, conds) => {
|
|
490
|
-
const scope = metroPackageScope(parentFile, host, read)
|
|
491
|
-
const imports = scope?.pkg.imports
|
|
492
|
-
if (imports == null) throw new MetroFailure(`No "imports" for ${specifier} from ${parentFile}`)
|
|
493
|
-
const keys = Object.keys(imports)
|
|
494
|
-
if (keys.length === 0 || !keys.every((key) => key.startsWith('#'))) return null
|
|
495
|
-
const target = metroMapTarget(new Map(Object.entries(imports)), specifier, conds)
|
|
496
|
-
return target === null ? null : metroTargetFile(scope.pkgDir, target)
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
// --metro: metro-resolver's own steps (its resolve.js), so the two resolve alike where they can.
|
|
500
|
-
// A path takes the redirect of the package closest to it, then resolves as a file or directory
|
|
501
|
-
// (resolveModulePath). A bare specifier takes the importer's redirect, then the importer's own
|
|
502
|
-
// `exports` where it names its own package, then, in each node_modules up from the importer, the
|
|
503
|
-
// package there (resolveModuleFromTargetPath): the redirect of the package closest to the
|
|
504
|
-
// candidate path -- a nested package.json counts -- then the `exports` of the one closest to where
|
|
505
|
-
// that lands, else that path as a file or directory. Metro falls back so from any `exports` miss,
|
|
506
|
-
// which keeps `main` and the browser map in play where `exports` serves no condition it asserts.
|
|
507
|
-
const viaMetro = (parentFile, specifier, conds, fileOpts) => {
|
|
508
|
-
const viaPath = (abs) => {
|
|
509
|
-
const target = metroRedirect(abs, opts)
|
|
510
|
-
return target === false ? { empty: true } : resolveFileOrDir(target, fileOpts)
|
|
511
|
-
}
|
|
512
|
-
// The file `subpath` names through the package at `pkgDir`'s `exports` field, as Metro maps it.
|
|
513
|
-
const viaExports = (pkgDir, exportsField, subpath) => {
|
|
514
|
-
const map = metroExportsMapOf(exportsField)
|
|
515
|
-
const target = map === null ? null : metroMapTarget(map, subpath, conds, { exports: true })
|
|
516
|
-
return target === null ? null : metroTargetFile(pkgDir, target)
|
|
517
|
-
}
|
|
518
|
-
const viaPackage = (spec) => {
|
|
519
|
-
const { name, firstPart, subpath } = parseBare(spec)
|
|
520
|
-
const own = nearestPackage(parentFile, host, read)
|
|
521
|
-
if (own?.pkg.exports != null && own.pkg.name === name) {
|
|
522
|
-
const hit = viaExports(own.pkgDir, own.pkg.exports, subpath)
|
|
523
|
-
if (hit) return hit
|
|
524
|
-
}
|
|
525
|
-
for (let dir = dirname(parentFile); ; dir = dirname(dir)) {
|
|
526
|
-
const nm = join(dir, 'node_modules')
|
|
527
|
-
// The shallowest directory the package can be in must exist (resolveFromNodeModulesPath).
|
|
528
|
-
if (isDir(subpath !== '.' || name !== firstPart ? join(nm, firstPart) : nm, host)) {
|
|
529
|
-
const target = metroRedirect(join(nm, spec), opts)
|
|
530
|
-
if (target === false) return { empty: true }
|
|
531
|
-
const scope = metroPackageScope(target, host, read)
|
|
532
|
-
const hit = (scope?.pkg.exports != null && viaExports(scope.pkgDir, scope.pkg.exports, target === scope.pkgDir ? '.' : `./${toPosix(relative(scope.pkgDir, target))}`)) || viaPath(target)
|
|
533
|
-
if (hit) return hit
|
|
534
|
-
// A package there whose entry resolves to nothing ends the lookup: Metro throws on it
|
|
535
|
-
// (InvalidPackageError). Anything else that misses gives way to the next node_modules up.
|
|
536
|
-
if (isFile(join(target, 'package.json'), host)) return null
|
|
537
|
-
}
|
|
538
|
-
if (dirname(dir) === dir) return null
|
|
539
|
-
}
|
|
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
|
|
540
187
|
}
|
|
541
|
-
|
|
542
|
-
return viaPath(isAbsolute(specifier) ? specifier : resolvePath(dirname(parentFile), specifier))
|
|
543
|
-
}
|
|
544
|
-
let spec = specifier
|
|
545
|
-
const imp = nearestPackage(parentFile, host, read)
|
|
546
|
-
const r = imp ? matchRedirect(mergeRedirectMap(imp.pkg, mainFields), spec) : undefined
|
|
547
|
-
if (r === false) return { empty: true }
|
|
548
|
-
if (typeof r === 'string') {
|
|
549
|
-
if (METRO_RELATIVE.test(r) || isAbsolute(r)) return viaPath(isAbsolute(r) ? r : resolvePath(imp.pkgDir, r))
|
|
550
|
-
spec = r
|
|
551
|
-
}
|
|
552
|
-
// A builtin the importer's map did not remap: as for the field resolver (see the header).
|
|
553
|
-
if (isBuiltin(spec)) return spec.startsWith('node:') ? { builtin: true } : (viaPackage(spec) ?? { builtin: true })
|
|
554
|
-
return viaPackage(spec)
|
|
188
|
+
return scanResolution(resolution, resolution === null && isBuiltin(specifier) && !remapped())
|
|
555
189
|
}
|
|
556
|
-
// `callConditions
|
|
557
|
-
|
|
558
|
-
const resolve = function resolve(parentFile, specifier, callConditions) {
|
|
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) {
|
|
559
192
|
const fileOpts = typescript && inNodeModules(parentFile, host) ? fromNodeModules : opts
|
|
560
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
|
+
}
|
|
561
199
|
const viaNode = (spec) => {
|
|
562
200
|
try {
|
|
563
201
|
if (!isBuiltin(spec)) return fileResolution(host.resolve(parentFile, spec, conds), host)
|
|
564
202
|
return fileResolution(nodeResolver().resolve(parentFile, spec, conds, { builtins: false }), host)
|
|
565
|
-
} catch {
|
|
203
|
+
} catch (error) {
|
|
204
|
+
if (typeof error.target === 'string') tried.push(error.target)
|
|
566
205
|
return null
|
|
567
206
|
}
|
|
568
207
|
}
|
|
569
|
-
// A bare specifier (`mod`, `mod/sub`) through node_modules.
|
|
570
208
|
const viaPackage = (spec) => {
|
|
571
209
|
const loc = locatePackage(dirname(parentFile), spec, host)
|
|
572
210
|
if (!loc) return null
|
|
573
211
|
const pkg = read(join(loc.pkgDir, 'package.json')) ?? {}
|
|
574
|
-
// `exports` wins over mainFields; Node's algorithm resolves it (with conditions) correctly.
|
|
575
212
|
if (pkg.exports != null) return viaNode(spec)
|
|
576
|
-
|
|
577
|
-
if (loc.subpath === '') return resolveFileOrDir(loc.pkgDir, fileOpts)
|
|
213
|
+
if (loc.subpath === '') return probe(loc.pkgDir)
|
|
578
214
|
const sub = `./${loc.subpath}`
|
|
579
215
|
const r = matchRedirect(mergeRedirectMap(pkg, mainFields), sub)
|
|
580
216
|
if (r === false) return { empty: true }
|
|
581
|
-
|
|
582
|
-
return resolveFileOrDir(join(loc.pkgDir, target), fileOpts)
|
|
583
|
-
}
|
|
584
|
-
// `#name` subpath imports use the `imports` field + conditions; Node's algorithm handles them,
|
|
585
|
-
// Metro's own under --metro, which falls back to file-based resolution where `imports` misses.
|
|
586
|
-
if (metro) {
|
|
587
|
-
try {
|
|
588
|
-
return (specifier.startsWith('#') && viaMetroImports(parentFile, specifier, conds)) || viaMetro(parentFile, specifier, conds, fileOpts)
|
|
589
|
-
} catch (error) {
|
|
590
|
-
if (error instanceof MetroFailure) return null
|
|
591
|
-
throw error
|
|
592
|
-
}
|
|
217
|
+
return probe(join(loc.pkgDir, typeof r === 'string' ? r : sub))
|
|
593
218
|
}
|
|
219
|
+
if (metro) return viaMetro(parentFile, specifier, conds)
|
|
594
220
|
if (specifier.startsWith('#')) return viaNode(specifier)
|
|
595
221
|
|
|
596
222
|
let spec = specifier
|
|
597
|
-
//
|
|
598
|
-
// a map entry can disable or shim a builtin (`{"crypto": false}` / `"crypto-browserify"`).
|
|
223
|
+
// The importer's map applies before the builtin check: it can disable or shim a builtin (`{"crypto": false}`).
|
|
599
224
|
const imp = nearestPackage(parentFile, host, read)
|
|
600
225
|
if (imp) {
|
|
601
226
|
const map = mergeRedirectMap(imp.pkg, mainFields)
|
|
@@ -604,65 +229,45 @@ export function createFieldResolver({
|
|
|
604
229
|
const abs = isAbsolute(spec) ? spec : resolvePath(dirname(parentFile), spec)
|
|
605
230
|
r = matchRedirect(map, `./${toPosix(relative(imp.pkgDir, abs))}`)
|
|
606
231
|
} else {
|
|
607
|
-
r = map.get(spec)
|
|
232
|
+
r = map.get(spec)
|
|
608
233
|
}
|
|
609
234
|
if (r === false) return { empty: true }
|
|
610
235
|
if (typeof r === 'string') {
|
|
611
|
-
//
|
|
612
|
-
|
|
613
|
-
if (r.startsWith('.') || isAbsolute(r)) {
|
|
614
|
-
return resolveFileOrDir(isAbsolute(r) ? r : resolvePath(imp.pkgDir, r), fileOpts)
|
|
615
|
-
}
|
|
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))
|
|
616
238
|
spec = r
|
|
617
239
|
}
|
|
618
240
|
}
|
|
619
|
-
|
|
620
|
-
if (spec.startsWith('.') || isAbsolute(spec)) {
|
|
621
|
-
const base = isAbsolute(spec) ? spec : resolvePath(dirname(parentFile), spec)
|
|
622
|
-
return resolveFileOrDir(base, fileOpts)
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
// A builtin the browser map did not remap: its `node:` form names the builtin itself; a bare
|
|
626
|
-
// name is the installed package of that name (see the header), the builtin where none resolves.
|
|
241
|
+
if (spec.startsWith('.') || isAbsolute(spec)) return probe(isAbsolute(spec) ? spec : resolvePath(dirname(parentFile), spec))
|
|
627
242
|
if (isBuiltin(spec)) return spec.startsWith('node:') ? { builtin: true } : (viaPackage(spec) ?? { builtin: true })
|
|
628
243
|
return viaPackage(spec)
|
|
629
244
|
}
|
|
630
|
-
//
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
host,
|
|
651
|
-
})
|
|
652
|
-
return hit == null ? null : fileResolution(hit, host)
|
|
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
|
|
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,
|
|
653
265
|
})
|
|
654
266
|
}
|
|
655
267
|
|
|
656
|
-
//
|
|
657
|
-
|
|
658
|
-
return format === 'module' || format === 'module-typescript'
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
// Condition set for a parent of the given format, as scan's #conditionSet gives a custom resolver:
|
|
662
|
-
// `import` or `require` by format, `default` (which Node's resolver matches anyway), plus extras.
|
|
663
|
-
// A bundler's base, not Node's: esbuild, webpack and Metro assert `import`/`require` and
|
|
664
|
-
// `default`, never `node`, so an `exports` map listing `node` first can't win over the extras.
|
|
665
|
-
// 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.
|
|
666
270
|
export function resolveConditions(format, extras = []) {
|
|
667
|
-
|
|
271
|
+
const esm = format === 'module' || format === 'module-typescript'
|
|
272
|
+
return [...new Set([esm ? 'import' : 'require', 'default', ...extras])]
|
|
668
273
|
}
|