@exodus/stasis 1.0.0-beta.14 → 1.0.0-beta.15
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 +1 -0
- package/bin/stasis.js +84 -14
- package/package.json +17 -5
- package/src/audit.js +222 -51
- package/src/cmd/build.js +53 -29
- package/src/cmd/bundle-options.js +10 -7
- package/src/cmd/bundle.js +255 -116
- package/src/cmd/github-bundle.js +7 -8
- package/src/cmd/tokens.js +12 -6
- package/src/define.js +5 -0
- package/src/flow.js +24 -4
- package/src/metro-assets.js +4 -354
- package/src/metro-resolver.js +75 -124
- package/src/parse.js +3 -0
- package/src/resolve-fields.js +107 -372
- package/src/resolve-typescript.js +99 -27
- package/src/scan.js +19 -14
- package/src/sixel.js +333 -0
- package/src/table.js +66 -13
- package/src/validate-notes.d.ts +18 -0
- package/src/validate-notes.js +66 -0
- package/src/validate.d.ts +132 -0
- package/src/validate.js +221 -113
- package/src/vfs-bundle/github.js +3 -1
- package/src/vfs-bundle/prisma.js +102 -29
- package/src/why.js +9 -7
package/src/resolve-fields.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
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 { isAssetFile } from '@preventive/rn/metro-assets.js'
|
|
6
|
+
import { createMetroResolver, defaults as metroDefaults } from '@preventive/rn/metro-resolver.js'
|
|
5
7
|
import { isTypeDeclaration, stripTypeDeclaration, toPosix } from '@exodus/stasis-core/util'
|
|
6
8
|
import {
|
|
9
|
+
fromOutputSource,
|
|
7
10
|
inNodeModules,
|
|
8
11
|
isDir,
|
|
9
12
|
isFile,
|
|
@@ -11,9 +14,11 @@ import {
|
|
|
11
14
|
nearestPackage,
|
|
12
15
|
readJson,
|
|
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 } from './metro-resolver.js'
|
|
17
22
|
import { createNodeResolver } from './resolve-node.js'
|
|
18
23
|
import { untouchedMemo } from './vfs-bundle/lazy.js'
|
|
19
24
|
|
|
@@ -21,8 +26,8 @@ import { untouchedMemo } from './vfs-bundle/lazy.js'
|
|
|
21
26
|
// redirect maps) and platform suffixes (`.ios`/`.android`/`.native`), reproducing Metro/React-Native
|
|
22
27
|
// resolution that Node's own resolver (used by scan.js) can't. `exports`-bearing packages are
|
|
23
28
|
// delegated to Node's algorithm, under a bundler's conditions rather than Node's (see
|
|
24
|
-
// resolveConditions)
|
|
25
|
-
//
|
|
29
|
+
// resolveConditions). Under `metro`, Metro's own resolution instead: metro-resolver's steps, as
|
|
30
|
+
// @preventive/rn takes them (viaMetro).
|
|
26
31
|
// No user code is executed (only package.json is stat/read).
|
|
27
32
|
//
|
|
28
33
|
// Returns, for each specifier:
|
|
@@ -37,17 +42,6 @@ import { untouchedMemo } from './vfs-bundle/lazy.js'
|
|
|
37
42
|
// stays a builtin, as the --metro-resolver adapter reports Metro's miss on one (metro-resolver.js),
|
|
38
43
|
// rather than an unresolved edge: Metro and webpack fail the build there.
|
|
39
44
|
|
|
40
|
-
// Metro divergence toggle. When a package's browser map maps its OWN entry to `false` (under
|
|
41
|
-
// Metro's matching rules, which include bare keys like {"buf": false} for main "./buf.js"),
|
|
42
|
-
// Metro's `getPackageEntryPoint` ignores the non-string replacement and keeps `main`. The
|
|
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
|
|
50
|
-
|
|
51
45
|
// First mainFields entry that's a non-empty string, else `index` (object-valued fields are redirect maps).
|
|
52
46
|
function packageEntry(pkg, mainFields) {
|
|
53
47
|
for (const name of mainFields) {
|
|
@@ -71,48 +65,25 @@ function mergeRedirectMap(pkg, mainFields) {
|
|
|
71
65
|
return map
|
|
72
66
|
}
|
|
73
67
|
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
//
|
|
77
|
-
//
|
|
78
|
-
|
|
79
|
-
|
|
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) {
|
|
68
|
+
// Resolve the package ENTRY (from mainFields) through the package's own redirect map, as esbuild
|
|
69
|
+
// does. Returns { entry } (the redirected or original entry to resolve) or { empty: true }: a string
|
|
70
|
+
// redirects, `false` empties the entry. The extension-STRIPPED bare spelling is not probed: for an
|
|
71
|
+
// extensioned main, esbuild treats it as a bare module name, not the entry. (For an extensionless
|
|
72
|
+
// main the `bare` candidate covers it -- esbuild does match that spelling; verified empirically.)
|
|
73
|
+
function resolveEntryThroughMap(map, main) {
|
|
95
74
|
if (map.size === 0) return { entry: main }
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
`./${stripped}`,
|
|
105
|
-
`./${stripped}.js`, `${stripped}.js`,
|
|
106
|
-
`./${stripped}.json`, `${stripped}.json`,
|
|
107
|
-
]
|
|
108
|
-
}
|
|
75
|
+
const bare = main.replace(/^\.\//u, '')
|
|
76
|
+
const stripped = bare.replace(/\.(?:js|json)$/u, '')
|
|
77
|
+
const cands = [
|
|
78
|
+
`./${bare}`, bare,
|
|
79
|
+
`./${stripped}`,
|
|
80
|
+
`./${stripped}.js`, `${stripped}.js`,
|
|
81
|
+
`./${stripped}.json`, `${stripped}.json`,
|
|
82
|
+
]
|
|
109
83
|
for (const cand of cands) {
|
|
110
84
|
const v = map.get(cand)
|
|
111
85
|
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
|
-
}
|
|
86
|
+
if (v === false) return { empty: true }
|
|
116
87
|
}
|
|
117
88
|
return { entry: main }
|
|
118
89
|
}
|
|
@@ -120,9 +91,8 @@ function resolveEntryThroughMap(map, main, opts) {
|
|
|
120
91
|
// A resolved file by its REAL path (through `host`, so a Vfs host's links are followed too), as Node,
|
|
121
92
|
// Metro and esbuild record one: under pnpm's isolated layout `node_modules/debug` links into
|
|
122
93
|
// `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
|
|
124
|
-
//
|
|
125
|
-
// realpathed. null where the file has no real path (gone since it was probed).
|
|
94
|
+
// from where it really lies. Probing stays on the path as reached; only the hit is realpathed. null
|
|
95
|
+
// where the file has no real path (gone since it was probed).
|
|
126
96
|
function fileResolution(path, host) {
|
|
127
97
|
let real
|
|
128
98
|
try {
|
|
@@ -151,229 +121,48 @@ function manifestReader(host) {
|
|
|
151
121
|
return (path) => manifests(path, () => readJson(path, host))
|
|
152
122
|
}
|
|
153
123
|
|
|
154
|
-
//
|
|
155
|
-
//
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
// -> the path, redirected where the map says so, or false (an empty module)
|
|
174
|
-
function metroRedirect(path, opts) {
|
|
175
|
-
const scope = metroPackageScope(path, opts.host, opts.read)
|
|
176
|
-
if (!scope) return path
|
|
177
|
-
const rel = path === scope.pkgDir ? '' : toPosix(relative(scope.pkgDir, path))
|
|
178
|
-
const r = matchRedirect(mergeRedirectMap(scope.pkg, opts.mainFields), `./${rel}`)
|
|
179
|
-
if (r === false) return false
|
|
180
|
-
if (typeof r === 'string') return isAbsolute(r) ? normalize(r) : join(scope.pkgDir, r)
|
|
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
|
|
204
|
-
}
|
|
205
|
-
return false
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// Metro's own resolveAsset (metro's DependencyGraph): `assetName` with `ext` in `dirPath`, and its
|
|
209
|
-
// scale variants (`a@1x.png` ... `a@4x.png`), those that exist, by real path. -> the paths, or null
|
|
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
|
|
124
|
+
// React Native's Metro config defaults: the extensions its resolver probes, in order, those it
|
|
125
|
+
// takes as assets, and the scales an asset's files are of.
|
|
126
|
+
export const METRO_SOURCE_EXTS = metroDefaults.sourceExts
|
|
127
|
+
export const METRO_ASSET_EXTS = new Set(metroDefaults.assetExts)
|
|
128
|
+
export const METRO_ASSET_RESOLUTIONS = metroDefaults.assetResolutions
|
|
129
|
+
// The conditions Metro asserts itself, by the kind of import.
|
|
130
|
+
const METRO_OWN_CONDITIONS = new Set(['default', 'import', 'require'])
|
|
131
|
+
|
|
132
|
+
// --metro --typescript: `view` (a metroHost over `host`) with tsc's extension substitution, as
|
|
133
|
+
// resolveSourceFile's: a file named for a JS output that is absent (`native.js`) is the first of its TS
|
|
134
|
+
// source siblings on disk (`native.ts`), unless that lies in node_modules by its real path, else an
|
|
135
|
+
// unbuilt output's source (`dist/native.js` -> `src/native.ts`; fromOutputSource). So Metro's own
|
|
136
|
+
// steps -- the entry its mainFields pick, a browser-map or `exports` target, the path itself, each
|
|
137
|
+
// platform file it tries -- land on the source of a compiled file that exists only as that source.
|
|
138
|
+
function typescriptView(view, { outputs, host }) {
|
|
139
|
+
const sibling = (path) => typescriptSiblings(path).find((candidate) => view.isFile(candidate)) ?? null
|
|
140
|
+
const source = (path) => {
|
|
141
|
+
const pick = sibling(path) ?? fromOutputSource(path, (input) => (view.isFile(input) ? input : sibling(input)), { outputs, host })
|
|
142
|
+
return pick === null || inNodeModules(pick, host) ? null : pick
|
|
261
143
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
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)
|
|
144
|
+
const substituted = (path) => (view.isFile(path) || view.isDirectory(path) ? null : source(path))
|
|
145
|
+
return {
|
|
146
|
+
...view,
|
|
147
|
+
isFile: (path) => view.isFile(path) || substituted(path) !== null,
|
|
148
|
+
realpath: (path) => view.realpath(substituted(path) ?? path),
|
|
270
149
|
}
|
|
271
|
-
return null
|
|
272
150
|
}
|
|
273
151
|
|
|
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
|
|
316
|
-
}
|
|
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
|
-
}
|
|
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
|
-
|
|
331
|
-
// Metro's relative specifier (isRelativeImport): `.`, `..`, or one starting `./` or `../`.
|
|
332
|
-
const METRO_RELATIVE = /^\.\.?(?:\/|$)/u
|
|
333
|
-
|
|
334
152
|
// Probe `base` for a source file in platform order: bare name first, then per ext
|
|
335
153
|
// `base.<platform>.<ext>`, `base.native.<ext>` (native only), `base.<ext>`. `platform: null`
|
|
336
|
-
// disables suffixes.
|
|
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.
|
|
154
|
+
// disables suffixes. Returns a path, or null.
|
|
341
155
|
function resolveSourceFile(base, opts) {
|
|
342
156
|
// A path that literally names a `.d.ts` names no runtime module (types are erased). Probe its
|
|
343
157
|
// declaration-free stem instead, so an entry field pointing at `dist/index.d.ts` lands on the real
|
|
344
158
|
// `dist/index.js` beside it rather than bundling the declaration -- or failing the package
|
|
345
159
|
// outright by falling back to a different index. The probe below still refuses a literal hit.
|
|
346
160
|
base = stripTypeDeclaration(base)
|
|
347
|
-
//
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
let scope
|
|
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
|
-
}
|
|
161
|
+
// A `.d.ts` is types-only, erased at runtime: never a resolution target, so a package whose
|
|
162
|
+
// entry field points at one falls through to its real `.js` instead of bundling the declaration.
|
|
163
|
+
const probePath = (p) => (!isTypeDeclaration(p) && isFile(p, opts.host) ? p : null)
|
|
375
164
|
// `suffix` is everything appended to `base`.
|
|
376
|
-
const probe = (suffix) => probePath(`${base}${suffix}
|
|
165
|
+
const probe = (suffix) => probePath(`${base}${suffix}`)
|
|
377
166
|
const tryExt = (sourceExt) => {
|
|
378
167
|
if (opts.platform) {
|
|
379
168
|
const hit = probe(`.${opts.platform}${sourceExt}`)
|
|
@@ -398,15 +187,24 @@ function resolveSourceFile(base, opts) {
|
|
|
398
187
|
// `typescript` for an importer there.
|
|
399
188
|
if (opts.typescript) {
|
|
400
189
|
for (const cand of typescriptSiblings(base)) {
|
|
401
|
-
const hit = probePath(cand
|
|
190
|
+
const hit = probePath(cand)
|
|
402
191
|
if (hit === null) continue
|
|
403
|
-
return
|
|
192
|
+
return inNodeModules(hit, opts.host) ? null : hit
|
|
404
193
|
}
|
|
405
194
|
}
|
|
406
195
|
for (const ext of opts.sourceExts) {
|
|
407
196
|
const hit = tryExt(`.${ext}`)
|
|
408
197
|
if (hit) return hit
|
|
409
198
|
}
|
|
199
|
+
// --typescript: an unbuilt output's source, probed the same way (fromOutputSource), never in
|
|
200
|
+
// node_modules. Not for a directory: resolveFileOrDir probes its index, which maps.
|
|
201
|
+
if (opts.typescript && !isDir(base, opts.host)) {
|
|
202
|
+
const source = (input) => {
|
|
203
|
+
const hit = resolveSourceFile(input, { ...opts, outputs: null })
|
|
204
|
+
return hit === null || inNodeModules(hit, opts.host) ? null : hit
|
|
205
|
+
}
|
|
206
|
+
return fromOutputSource(base, source, opts)
|
|
207
|
+
}
|
|
410
208
|
return null
|
|
411
209
|
}
|
|
412
210
|
|
|
@@ -414,22 +212,20 @@ function resolveSourceFile(base, opts) {
|
|
|
414
212
|
// mainFields + entry redirect, else `index`). `exports` is NOT consulted — Node applies it only to
|
|
415
213
|
// bare package-name imports, not to a path landing on a directory.
|
|
416
214
|
function resolveFileOrDir(base, opts) {
|
|
417
|
-
|
|
418
|
-
const asResolution = (hit) => (hit == null ? null : typeof hit === 'string' ? fileResolution(hit, opts.host) : hit)
|
|
215
|
+
const asResolution = (hit) => (hit === null ? null : fileResolution(hit, opts.host))
|
|
419
216
|
const file = asResolution(resolveSourceFile(base, opts))
|
|
420
217
|
if (file) return file
|
|
421
218
|
if (isDir(base, opts.host)) {
|
|
422
219
|
const dpkg = opts.read(join(base, 'package.json'))
|
|
423
220
|
if (dpkg) {
|
|
424
|
-
const r = resolveEntryThroughMap(mergeRedirectMap(dpkg, opts.mainFields), packageEntry(dpkg, opts.mainFields)
|
|
221
|
+
const r = resolveEntryThroughMap(mergeRedirectMap(dpkg, opts.mainFields), packageEntry(dpkg, opts.mainFields))
|
|
425
222
|
if (r.empty) return { empty: true }
|
|
426
223
|
// Node's LOAD_AS_DIRECTORY resolves `main` as a file, else as a directory index
|
|
427
224
|
// (`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).
|
|
429
225
|
const entryBase = join(base, r.entry)
|
|
430
|
-
const
|
|
226
|
+
const entryOpts = { ...opts, pkgDir: base } // the package naming the entry, for its unbuilt output
|
|
227
|
+
const inner = asResolution(resolveSourceFile(entryBase, entryOpts) ?? resolveSourceFile(join(entryBase, 'index'), entryOpts))
|
|
431
228
|
if (inner) return inner
|
|
432
|
-
if (opts.metro) return null
|
|
433
229
|
}
|
|
434
230
|
const index = asResolution(resolveSourceFile(join(base, 'index'), opts))
|
|
435
231
|
if (index) return index
|
|
@@ -440,13 +236,13 @@ function resolveFileOrDir(base, opts) {
|
|
|
440
236
|
// Build a resolver bound to options: `conditions` (a resolveConditions set) gates `exports`
|
|
441
237
|
// packages (delegated to Node's algorithm), `mainFields`/`platform`/`preferNative`/`sourceExts`
|
|
442
238
|
// drive the legacy-field + suffix probing.
|
|
443
|
-
// `metro`
|
|
444
|
-
//
|
|
445
|
-
//
|
|
446
|
-
// its `x.ts` sibling
|
|
447
|
-
//
|
|
448
|
-
// `
|
|
449
|
-
//
|
|
239
|
+
// `metro` resolves as Metro does instead (viaMetro), taking `assetExts` as assets, at the scales
|
|
240
|
+
// `assetResolutions` (Metro's defaults, else the project's, each); leave it off for the
|
|
241
|
+
// esbuild-parity `--mainFields` path. `typescript` adds
|
|
242
|
+
// tsc's extension substitution (a missing `x.js` probes its `x.ts` sibling) and unbuilt outputs'
|
|
243
|
+
// sources (see resolveSourceFile; under `metro`, typescriptView) plus the shared miss fallback (see
|
|
244
|
+
// below), and `typescriptPaths` (a tsconfig paths matcher, with the packages' layouts; see
|
|
245
|
+
// resolveTypescriptFallback) its tsconfig alias mapping.
|
|
450
246
|
// `host` is the filesystem view (@exodus/stasis-core/host), the real disk by default. `kept`, a Map
|
|
451
247
|
// kept across a lazy tree's rounds, keeps there each resolution that read nothing the tree does not
|
|
452
248
|
// hold yet (untouchedMemo's `across`), for the rounds after.
|
|
@@ -458,100 +254,47 @@ export function createFieldResolver({
|
|
|
458
254
|
sourceExts = METRO_SOURCE_EXTS,
|
|
459
255
|
metro = false,
|
|
460
256
|
assetExts = METRO_ASSET_EXTS,
|
|
257
|
+
assetResolutions = METRO_ASSET_RESOLUTIONS,
|
|
461
258
|
typescript = false,
|
|
462
259
|
typescriptPaths = null,
|
|
463
|
-
metroKeepEntryOnBrowserFalse = METRO_KEEP_ENTRY_ON_BROWSER_FALSE,
|
|
464
260
|
host = diskHost,
|
|
465
261
|
kept,
|
|
466
262
|
} = {}) {
|
|
467
263
|
const read = manifestReader(host)
|
|
468
|
-
const
|
|
264
|
+
const outputs = typescript ? typescriptPaths?.outputs ?? tsconfigOutputsOf({ host }) : null
|
|
265
|
+
const opts = { platform, preferNative, sourceExts, mainFields, typescript, outputs, host, read }
|
|
469
266
|
// An importer in node_modules gets no --typescript mapping (inNodeModules), wherever its import lands.
|
|
470
267
|
const fromNodeModules = { ...opts, typescript: false }
|
|
471
268
|
// Node answers a builtin name with the builtin itself, so an `exports`-bearing package named like
|
|
472
269
|
// one (see the header) resolves through this instead, with builtins off; built on first use.
|
|
473
270
|
let packageResolver
|
|
474
271
|
const nodeResolver = () => (packageResolver ??= createNodeResolver(host))
|
|
475
|
-
// --metro:
|
|
476
|
-
//
|
|
477
|
-
//
|
|
478
|
-
//
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
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
|
-
}
|
|
540
|
-
}
|
|
541
|
-
if (METRO_RELATIVE.test(specifier) || isAbsolute(specifier)) {
|
|
542
|
-
return viaPath(isAbsolute(specifier) ? specifier : resolvePath(dirname(parentFile), specifier))
|
|
272
|
+
// --metro: Metro's own resolution, @preventive/rn's, over `host` as stasis reads it (metroHost). Metro
|
|
273
|
+
// asserts `default` and `import` or `require` by the kind of import itself, and takes the rest of an
|
|
274
|
+
// importer's conditions as its `conditionNames`: one resolver for each such set, made on first use.
|
|
275
|
+
// Off web it prefers `.native` files (preferNative). --typescript resolves what that misses again,
|
|
276
|
+
// over the view of tsc's extension substitution (typescriptView), but for an importer in
|
|
277
|
+
// node_modules (inNodeModules), so it never overrides what Metro resolves. A builtin it fails to
|
|
278
|
+
// resolve, one the importer's browser map doesn't remap, is a builtin (see the header).
|
|
279
|
+
const metroResolvers = new Map()
|
|
280
|
+
const metroResolver = (conditionNames, substituting) => {
|
|
281
|
+
const key = `${substituting}\0${conditionNames.join('\0')}`
|
|
282
|
+
if (!metroResolvers.has(key)) {
|
|
283
|
+
const view = metroHost(host, mainFields)
|
|
284
|
+
metroResolvers.set(key, createMetroResolver({ host: substituting ? typescriptView(view, opts) : view, sourceExts, assetExts, assetResolutions, mainFields, conditionNames, conditionsByPlatform: {}, preferNativePlatform: preferNative }))
|
|
543
285
|
}
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
286
|
+
return metroResolvers.get(key)
|
|
287
|
+
}
|
|
288
|
+
const viaMetro = (parentFile, specifier, conds) => {
|
|
289
|
+
const conditionNames = [...conds].filter((name) => !METRO_OWN_CONDITIONS.has(name))
|
|
290
|
+
const request = { platform, isESMImport: conds.has('import') }
|
|
291
|
+
let resolution = metroResolver(conditionNames, false)(parentFile, specifier, request)
|
|
292
|
+
if (resolution === null && typescript && !inNodeModules(parentFile, host)) resolution = metroResolver(conditionNames, true)(parentFile, specifier, request)
|
|
293
|
+
const remapped = () => {
|
|
294
|
+
const imp = nearestPackage(parentFile, host, read)
|
|
295
|
+
return imp !== null && matchRedirect(mergeRedirectMap(imp.pkg, mainFields), specifier) !== undefined
|
|
551
296
|
}
|
|
552
|
-
|
|
553
|
-
if (isBuiltin(spec)) return spec.startsWith('node:') ? { builtin: true } : (viaPackage(spec) ?? { builtin: true })
|
|
554
|
-
return viaPackage(spec)
|
|
297
|
+
return scanResolution(resolution, resolution === null && isBuiltin(specifier) && !remapped())
|
|
555
298
|
}
|
|
556
299
|
// `callConditions` (from scan) is the parent's format-driven condition set, so `exports`
|
|
557
300
|
// delegation matches Node resolving from THAT file; falls back to configured `conditions`.
|
|
@@ -581,16 +324,8 @@ export function createFieldResolver({
|
|
|
581
324
|
const target = typeof r === 'string' ? r : sub
|
|
582
325
|
return resolveFileOrDir(join(loc.pkgDir, target), fileOpts)
|
|
583
326
|
}
|
|
584
|
-
|
|
585
|
-
//
|
|
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
|
-
}
|
|
593
|
-
}
|
|
327
|
+
if (metro) return viaMetro(parentFile, specifier, conds)
|
|
328
|
+
// `#name` subpath imports use the `imports` field + conditions; Node's algorithm handles them.
|
|
594
329
|
if (specifier.startsWith('#')) return viaNode(specifier)
|
|
595
330
|
|
|
596
331
|
let spec = specifier
|
|
@@ -642,8 +377,8 @@ export function createFieldResolver({
|
|
|
642
377
|
return once(function resolveWithTypescriptFallback(parentFile, specifier, callConditions) {
|
|
643
378
|
const resolved = resolve(parentFile, specifier, callConditions)
|
|
644
379
|
if (resolved) return resolved
|
|
645
|
-
// Under --metro an asset is Metro's asset rule's to resolve
|
|
646
|
-
if (metro &&
|
|
380
|
+
// Under --metro an asset is Metro's asset rule's to resolve, as tsc never does.
|
|
381
|
+
if (metro && isAssetFile(specifier, assetExts)) return null
|
|
647
382
|
const hit = resolveTypescriptFallback(parentFile, specifier, {
|
|
648
383
|
conditions: new Set(callConditions ?? conditions),
|
|
649
384
|
paths: typescriptPaths,
|