@scriptc/compiler 0.0.0 → 0.0.2
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/LICENSE +202 -0
- package/ambient/package.json +3 -0
- package/ambient/scriptc-node-fallback.d.ts +3001 -0
- package/ambient/scriptc-overrides.d.ts +95 -0
- package/ambient/scriptc.d.ts +74 -0
- package/dist/backend/cc.d.ts +169 -0
- package/dist/backend/cc.js +897 -0
- package/dist/backend/cc.js.map +1 -0
- package/dist/backend/emission/emit-async.d.ts +145 -0
- package/dist/backend/emission/emit-async.js +996 -0
- package/dist/backend/emission/emit-async.js.map +1 -0
- package/dist/backend/emission/emit-exprs.d.ts +3 -0
- package/dist/backend/emission/emit-exprs.js +5949 -0
- package/dist/backend/emission/emit-exprs.js.map +1 -0
- package/dist/backend/emission/emit-island.d.ts +45 -0
- package/dist/backend/emission/emit-island.js +271 -0
- package/dist/backend/emission/emit-island.js.map +1 -0
- package/dist/backend/emission/emit-shapes.d.ts +142 -0
- package/dist/backend/emission/emit-shapes.js +575 -0
- package/dist/backend/emission/emit-shapes.js.map +1 -0
- package/dist/backend/emission/emit-stmts.d.ts +78 -0
- package/dist/backend/emission/emit-stmts.js +960 -0
- package/dist/backend/emission/emit-stmts.js.map +1 -0
- package/dist/backend/emission/emit-types.d.ts +65 -0
- package/dist/backend/emission/emit-types.js +652 -0
- package/dist/backend/emission/emit-types.js.map +1 -0
- package/dist/backend/emission/emit-walkers.d.ts +154 -0
- package/dist/backend/emission/emit-walkers.js +1587 -0
- package/dist/backend/emission/emit-walkers.js.map +1 -0
- package/dist/backend/emission/emitter.d.ts +452 -0
- package/dist/backend/emission/emitter.js +1249 -0
- package/dist/backend/emission/emitter.js.map +1 -0
- package/dist/backend/emission/may-throw.d.ts +15 -0
- package/dist/backend/emission/may-throw.js +250 -0
- package/dist/backend/emission/may-throw.js.map +1 -0
- package/dist/backend/llvm/blocks.d.ts +21 -0
- package/dist/backend/llvm/blocks.js +68 -0
- package/dist/backend/llvm/blocks.js.map +1 -0
- package/dist/backend/llvm/classes.d.ts +84 -0
- package/dist/backend/llvm/classes.js +438 -0
- package/dist/backend/llvm/classes.js.map +1 -0
- package/dist/backend/llvm/dyn.d.ts +123 -0
- package/dist/backend/llvm/dyn.js +2624 -0
- package/dist/backend/llvm/dyn.js.map +1 -0
- package/dist/backend/llvm/emitter.d.ts +3 -0
- package/dist/backend/llvm/emitter.js +10102 -0
- package/dist/backend/llvm/emitter.js.map +1 -0
- package/dist/backend/llvm/shapes.d.ts +78 -0
- package/dist/backend/llvm/shapes.js +754 -0
- package/dist/backend/llvm/shapes.js.map +1 -0
- package/dist/backend/llvm/unsupported.d.ts +6 -0
- package/dist/backend/llvm/unsupported.js +12 -0
- package/dist/backend/llvm/unsupported.js.map +1 -0
- package/dist/backend/llvm/walkers.d.ts +58 -0
- package/dist/backend/llvm/walkers.js +793 -0
- package/dist/backend/llvm/walkers.js.map +1 -0
- package/dist/backend/mangle.d.ts +124 -0
- package/dist/backend/mangle.js +232 -0
- package/dist/backend/mangle.js.map +1 -0
- package/dist/coverage/report.d.ts +65 -0
- package/dist/coverage/report.js +238 -0
- package/dist/coverage/report.js.map +1 -0
- package/dist/diagnostics/diagnostic.d.ts +140 -0
- package/dist/diagnostics/diagnostic.js +458 -0
- package/dist/diagnostics/diagnostic.js.map +1 -0
- package/dist/diagnostics/render.d.ts +11 -0
- package/dist/diagnostics/render.js +58 -0
- package/dist/diagnostics/render.js.map +1 -0
- package/dist/frontend/cjs-lexer.d.ts +20 -0
- package/dist/frontend/cjs-lexer.js +813 -0
- package/dist/frontend/cjs-lexer.js.map +1 -0
- package/dist/frontend/lowering/http2-constants.d.ts +1 -0
- package/dist/frontend/lowering/http2-constants.js +251 -0
- package/dist/frontend/lowering/http2-constants.js.map +1 -0
- package/dist/frontend/lowering/lib-boundary.d.ts +6 -0
- package/dist/frontend/lowering/lib-boundary.js +143 -0
- package/dist/frontend/lowering/lib-boundary.js.map +1 -0
- package/dist/frontend/lowering/lower-assert.d.ts +18 -0
- package/dist/frontend/lowering/lower-assert.js +1269 -0
- package/dist/frontend/lowering/lower-assert.js.map +1 -0
- package/dist/frontend/lowering/lower-builtins.d.ts +511 -0
- package/dist/frontend/lowering/lower-builtins.js +5249 -0
- package/dist/frontend/lowering/lower-builtins.js.map +1 -0
- package/dist/frontend/lowering/lower-calls.d.ts +581 -0
- package/dist/frontend/lowering/lower-calls.js +6286 -0
- package/dist/frontend/lowering/lower-calls.js.map +1 -0
- package/dist/frontend/lowering/lower-classes.d.ts +727 -0
- package/dist/frontend/lowering/lower-classes.js +4701 -0
- package/dist/frontend/lowering/lower-classes.js.map +1 -0
- package/dist/frontend/lowering/lower-comptime.d.ts +53 -0
- package/dist/frontend/lowering/lower-comptime.js +244 -0
- package/dist/frontend/lowering/lower-comptime.js.map +1 -0
- package/dist/frontend/lowering/lower-containers.d.ts +486 -0
- package/dist/frontend/lowering/lower-containers.js +6345 -0
- package/dist/frontend/lowering/lower-containers.js.map +1 -0
- package/dist/frontend/lowering/lower-dgram.d.ts +20 -0
- package/dist/frontend/lowering/lower-dgram.js +319 -0
- package/dist/frontend/lowering/lower-dgram.js.map +1 -0
- package/dist/frontend/lowering/lower-emitter.d.ts +10 -0
- package/dist/frontend/lowering/lower-emitter.js +688 -0
- package/dist/frontend/lowering/lower-emitter.js.map +1 -0
- package/dist/frontend/lowering/lower-enums.d.ts +22 -0
- package/dist/frontend/lowering/lower-enums.js +235 -0
- package/dist/frontend/lowering/lower-enums.js.map +1 -0
- package/dist/frontend/lowering/lower-expando.d.ts +35 -0
- package/dist/frontend/lowering/lower-expando.js +276 -0
- package/dist/frontend/lowering/lower-expando.js.map +1 -0
- package/dist/frontend/lowering/lower-exprs.d.ts +524 -0
- package/dist/frontend/lowering/lower-exprs.js +8111 -0
- package/dist/frontend/lowering/lower-exprs.js.map +1 -0
- package/dist/frontend/lowering/lower-generators.d.ts +50 -0
- package/dist/frontend/lowering/lower-generators.js +421 -0
- package/dist/frontend/lowering/lower-generators.js.map +1 -0
- package/dist/frontend/lowering/lower-inspect.d.ts +20 -0
- package/dist/frontend/lowering/lower-inspect.js +1074 -0
- package/dist/frontend/lowering/lower-inspect.js.map +1 -0
- package/dist/frontend/lowering/lower-island.d.ts +108 -0
- package/dist/frontend/lowering/lower-island.js +650 -0
- package/dist/frontend/lowering/lower-island.js.map +1 -0
- package/dist/frontend/lowering/lower-mixins.d.ts +96 -0
- package/dist/frontend/lowering/lower-mixins.js +534 -0
- package/dist/frontend/lowering/lower-mixins.js.map +1 -0
- package/dist/frontend/lowering/lower-modules.d.ts +135 -0
- package/dist/frontend/lowering/lower-modules.js +1613 -0
- package/dist/frontend/lowering/lower-modules.js.map +1 -0
- package/dist/frontend/lowering/lower-namespaces.d.ts +168 -0
- package/dist/frontend/lowering/lower-namespaces.js +706 -0
- package/dist/frontend/lowering/lower-namespaces.js.map +1 -0
- package/dist/frontend/lowering/lower-server.d.ts +90 -0
- package/dist/frontend/lowering/lower-server.js +3471 -0
- package/dist/frontend/lowering/lower-server.js.map +1 -0
- package/dist/frontend/lowering/lower-stmts.d.ts +361 -0
- package/dist/frontend/lowering/lower-stmts.js +5341 -0
- package/dist/frontend/lowering/lower-stmts.js.map +1 -0
- package/dist/frontend/lowering/lower-stream.d.ts +88 -0
- package/dist/frontend/lowering/lower-stream.js +1533 -0
- package/dist/frontend/lowering/lower-stream.js.map +1 -0
- package/dist/frontend/lowering/lower-test.d.ts +27 -0
- package/dist/frontend/lowering/lower-test.js +353 -0
- package/dist/frontend/lowering/lower-test.js.map +1 -0
- package/dist/frontend/lowering/lowerer.d.ts +1748 -0
- package/dist/frontend/lowering/lowerer.js +6257 -0
- package/dist/frontend/lowering/lowerer.js.map +1 -0
- package/dist/frontend/lowering/surfaces.d.ts +272 -0
- package/dist/frontend/lowering/surfaces.js +1096 -0
- package/dist/frontend/lowering/surfaces.js.map +1 -0
- package/dist/frontend/npm-static.d.ts +55 -0
- package/dist/frontend/npm-static.js +300 -0
- package/dist/frontend/npm-static.js.map +1 -0
- package/dist/frontend/npm.d.ts +330 -0
- package/dist/frontend/npm.js +1259 -0
- package/dist/frontend/npm.js.map +1 -0
- package/dist/frontend/program.d.ts +186 -0
- package/dist/frontend/program.js +2255 -0
- package/dist/frontend/program.js.map +1 -0
- package/dist/frontend/provenance-registry.d.ts +48 -0
- package/dist/frontend/provenance-registry.js +87 -0
- package/dist/frontend/provenance-registry.js.map +1 -0
- package/dist/frontend/provenance.d.ts +6 -0
- package/dist/frontend/provenance.js +459 -0
- package/dist/frontend/provenance.js.map +1 -0
- package/dist/frontend/resolve.d.ts +56 -0
- package/dist/frontend/resolve.js +682 -0
- package/dist/frontend/resolve.js.map +1 -0
- package/dist/frontend/shared.d.ts +67 -0
- package/dist/frontend/shared.js +241 -0
- package/dist/frontend/shared.js.map +1 -0
- package/dist/frontend/ts7/adapter.d.ts +7 -0
- package/dist/frontend/ts7/adapter.js +54 -0
- package/dist/frontend/ts7/adapter.js.map +1 -0
- package/dist/frontend/ts7/ast.d.ts +50 -0
- package/dist/frontend/ts7/ast.js +211 -0
- package/dist/frontend/ts7/ast.js.map +1 -0
- package/dist/frontend/ts7/census-check.d.ts +1 -0
- package/dist/frontend/ts7/census-check.js +12 -0
- package/dist/frontend/ts7/census-check.js.map +1 -0
- package/dist/frontend/ts7/checker.d.ts +140 -0
- package/dist/frontend/ts7/checker.js +544 -0
- package/dist/frontend/ts7/checker.js.map +1 -0
- package/dist/frontend/ts7/enums.d.ts +18 -0
- package/dist/frontend/ts7/enums.js +43 -0
- package/dist/frontend/ts7/enums.js.map +1 -0
- package/dist/frontend/ts7/program.d.ts +99 -0
- package/dist/frontend/ts7/program.js +278 -0
- package/dist/frontend/ts7/program.js.map +1 -0
- package/dist/frontend/ts7/world-check.d.ts +3 -0
- package/dist/frontend/ts7/world-check.js +34 -0
- package/dist/frontend/ts7/world-check.js.map +1 -0
- package/dist/frontend/types.d.ts +205 -0
- package/dist/frontend/types.js +2485 -0
- package/dist/frontend/types.js.map +1 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.js +429 -0
- package/dist/index.js.map +1 -0
- package/dist/ir/nodes.d.ts +4334 -0
- package/dist/ir/nodes.js +1906 -0
- package/dist/ir/nodes.js.map +1 -0
- package/dist/ir/serialize.d.ts +4 -0
- package/dist/ir/serialize.js +34 -0
- package/dist/ir/serialize.js.map +1 -0
- package/dist/ir/validate.d.ts +36 -0
- package/dist/ir/validate.js +4885 -0
- package/dist/ir/validate.js.map +1 -0
- package/package.json +30 -6
- package/README.md +0 -3
- package/index.js +0 -2
|
@@ -0,0 +1,682 @@
|
|
|
1
|
+
/* scriptc's OWN module resolver — the bounded reimplementation of the three
|
|
2
|
+
* ts.resolve* entry points the program lifecycle leans on, for the TS7 world
|
|
3
|
+
* (typescript@7.0.2 ships no client-side resolution API at all). Pure
|
|
4
|
+
* strings and node:fs — no AST, checker, or enum objects from either
|
|
5
|
+
* TypeScript world cross this module, so both lanes may share it.
|
|
6
|
+
*
|
|
7
|
+
* The contract is PARITY with typescript@5.9.3's answers under scriptc's
|
|
8
|
+
* fixed resolution options (moduleResolution bundler, allowJs, checkJs,
|
|
9
|
+
* resolveJsonModule, allowImportingTsExtensions — program.ts's
|
|
10
|
+
* COMPILER_OPTIONS): candidate lists below are transcribed from probed
|
|
11
|
+
* failedLookupLocations of ts.resolveModuleName / the resolved answers on
|
|
12
|
+
* the fixture tree, and the resolver parity suite (test/ts7/resolver-
|
|
13
|
+
* parity.test.ts) sweeps every specifier of the whole corpus through BOTH
|
|
14
|
+
* implementations and requires identical answers. Change 5.9.3's options
|
|
15
|
+
* and these tables are wrong — that is what the suite is for. */
|
|
16
|
+
import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
17
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
18
|
+
import { isNpmStaticPackage, npmStaticPackageOfPath, npmStaticTransformPkgJson } from "./npm-static.js";
|
|
19
|
+
import { provenanceEntryFor } from "./provenance-registry.js";
|
|
20
|
+
function isFile(path) {
|
|
21
|
+
try {
|
|
22
|
+
return statSync(path).isFile();
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function isDirectory(path) {
|
|
29
|
+
try {
|
|
30
|
+
return statSync(path).isDirectory();
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function realpathOr(path) {
|
|
37
|
+
try {
|
|
38
|
+
return realpathSync(path);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return path;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const pkgJsonCache = new Map();
|
|
45
|
+
function pkgJsonOf(dir) {
|
|
46
|
+
let cached = pkgJsonCache.get(dir);
|
|
47
|
+
if (cached === undefined) {
|
|
48
|
+
const path = join(dir, "package.json");
|
|
49
|
+
let parsed = null;
|
|
50
|
+
if (existsSync(path)) {
|
|
51
|
+
try {
|
|
52
|
+
parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
parsed = null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
cached = parsed;
|
|
59
|
+
pkgJsonCache.set(dir, cached);
|
|
60
|
+
}
|
|
61
|
+
return cached;
|
|
62
|
+
}
|
|
63
|
+
/* ── relative specifiers ─────────────────────────────────────────────────
|
|
64
|
+
*
|
|
65
|
+
* Probed 5.9.3 bundler candidate orders (failedLookupLocations, exhaustive
|
|
66
|
+
* for each specifier shape; "stem" is the specifier minus its extension):
|
|
67
|
+
* ./x → x.ts x.tsx x.d.ts x.js x.jsx, then directory
|
|
68
|
+
* ./x.js → stem+[.ts .tsx .d.ts .js .jsx], x.js+[.ts .tsx .d.ts .js
|
|
69
|
+
* .jsx], then directory (.ts and .tsx specifiers identical:
|
|
70
|
+
* the exact name sits in the first group)
|
|
71
|
+
* ./x.jsx → stem+[.tsx .ts .d.ts .jsx .js], then as above
|
|
72
|
+
* ./x.mjs → stem+[.mts .d.mts .mjs], full-name group, directory
|
|
73
|
+
* ./x.cjs → stem+[.cts .d.cts .cjs], full-name group, directory
|
|
74
|
+
* ./x.json → stem+.d.json.ts, x.json, full-name group, directory
|
|
75
|
+
* Directory: package.json probed (types/typings, then main, each resolved
|
|
76
|
+
* like a relative FILE), then index.[ts tsx d.ts js jsx]. */
|
|
77
|
+
const TS_FIRST = [".ts", ".tsx", ".d.ts", ".js", ".jsx"];
|
|
78
|
+
const TSX_FIRST = [".tsx", ".ts", ".d.ts", ".jsx", ".js"];
|
|
79
|
+
/** The extension-substitution group for a specifier, or null when the
|
|
80
|
+
* specifier's extension is unrecognized (full-name candidates only). */
|
|
81
|
+
function substitutionCandidates(base) {
|
|
82
|
+
const stemOf = (ext) => base.slice(0, -ext.length);
|
|
83
|
+
for (const ext of [".ts", ".js"]) {
|
|
84
|
+
if (base.endsWith(ext))
|
|
85
|
+
return TS_FIRST.map((e) => stemOf(ext) + e);
|
|
86
|
+
}
|
|
87
|
+
for (const ext of [".tsx", ".jsx"]) {
|
|
88
|
+
if (base.endsWith(ext))
|
|
89
|
+
return TSX_FIRST.map((e) => stemOf(ext) + e);
|
|
90
|
+
}
|
|
91
|
+
if (base.endsWith(".mts") || base.endsWith(".mjs")) {
|
|
92
|
+
const stem = stemOf(".mjs");
|
|
93
|
+
return [stem + ".mts", stem + ".d.mts", stem + ".mjs"];
|
|
94
|
+
}
|
|
95
|
+
if (base.endsWith(".cts") || base.endsWith(".cjs")) {
|
|
96
|
+
const stem = stemOf(".cjs");
|
|
97
|
+
return [stem + ".cts", stem + ".d.cts", stem + ".cjs"];
|
|
98
|
+
}
|
|
99
|
+
if (base.endsWith(".json")) {
|
|
100
|
+
return [stemOf(".json") + ".d.json.ts", base];
|
|
101
|
+
}
|
|
102
|
+
return [];
|
|
103
|
+
}
|
|
104
|
+
/** Loads `base` as a FILE with 5.9.3's bundler candidate order: the
|
|
105
|
+
* extension-substitution group when the extension is recognized, then the
|
|
106
|
+
* full name with every extension appended. */
|
|
107
|
+
function loadAsFile(base) {
|
|
108
|
+
for (const candidate of substitutionCandidates(base)) {
|
|
109
|
+
if (isFile(candidate))
|
|
110
|
+
return candidate;
|
|
111
|
+
}
|
|
112
|
+
for (const ext of TS_FIRST) {
|
|
113
|
+
if (isFile(base + ext))
|
|
114
|
+
return base + ext;
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
/** Loads `base` as a DIRECTORY: package.json types/typings then main (each
|
|
119
|
+
* like a relative file), then the index files. */
|
|
120
|
+
function loadAsDirectory(base) {
|
|
121
|
+
if (!isDirectory(base))
|
|
122
|
+
return null;
|
|
123
|
+
const pkg = pkgJsonOf(base);
|
|
124
|
+
if (pkg) {
|
|
125
|
+
for (const field of [pkg.types, pkg.typings, pkg.main]) {
|
|
126
|
+
if (typeof field === "string" && field !== "") {
|
|
127
|
+
const target = join(base, field);
|
|
128
|
+
const viaFile = loadAsFile(target) ?? (isFile(target) ? target : null);
|
|
129
|
+
if (viaFile)
|
|
130
|
+
return viaFile;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
for (const ext of TS_FIRST) {
|
|
135
|
+
const idx = join(base, "index" + ext);
|
|
136
|
+
if (isFile(idx))
|
|
137
|
+
return idx;
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
/** The RUNTIME sibling of a PROJECT declaration twin — "src/index.js" for
|
|
142
|
+
* "src/index.d.ts" when both exist OUTSIDE node_modules — or null. Node
|
|
143
|
+
* loads the JS (declaration files do not exist in its world), and a
|
|
144
|
+
* project module's implementation is what scriptc compiles: a hand-kept
|
|
145
|
+
* .d.ts beside runtime JS (prettier's src/index.d.ts, the classic
|
|
146
|
+
* typed-JS-library pattern) is the author's claim about the code, not
|
|
147
|
+
* the code. EXACTLY the .d.ts/.js pair: the per-format spellings
|
|
148
|
+
* (.d.mts/.d.cts twins — an openh264.d.mts beside its island-bound
|
|
149
|
+
* wasm loader, the npm fixtures' shape.d.mts) are the established
|
|
150
|
+
* scriptc idiom for declaring a typed surface over JS that deliberately
|
|
151
|
+
* runs in the island, and stay authoritative. npm packages keep the npm
|
|
152
|
+
* story (their declarations ARE the consumer surface; --npm-static opts
|
|
153
|
+
* into the JS per package), and a declaration with NO runtime sibling
|
|
154
|
+
* stays a type-only module exactly as before. The tsgo host hides the
|
|
155
|
+
* same twins (loadProgram's composed fsShadow), so both resolution
|
|
156
|
+
* worlds answer the JS.
|
|
157
|
+
*
|
|
158
|
+
* "PROJECT" means the entry's own package realm: a workspace-linked
|
|
159
|
+
* package's realpaths carry no node_modules segment either, but its
|
|
160
|
+
* declarations ARE the consumer surface (the npm story), so twins hide
|
|
161
|
+
* only under the same package.json as the entry. */
|
|
162
|
+
let projectRealmPkgJson = null;
|
|
163
|
+
export function setProjectRealm(entryPath) {
|
|
164
|
+
projectRealmPkgJson = nearestPkgJsonPath(entryPath);
|
|
165
|
+
}
|
|
166
|
+
export function projectDtsRuntimeSibling(path) {
|
|
167
|
+
if (path.includes("/node_modules/") || path.includes("\\node_modules\\"))
|
|
168
|
+
return null;
|
|
169
|
+
if (!path.endsWith(".d.ts"))
|
|
170
|
+
return null;
|
|
171
|
+
if (projectRealmPkgJson === null || nearestPkgJsonPath(path) !== projectRealmPkgJson)
|
|
172
|
+
return null;
|
|
173
|
+
const sibling = path.slice(0, -".d.ts".length) + ".js";
|
|
174
|
+
return isFile(sibling) ? sibling : null;
|
|
175
|
+
}
|
|
176
|
+
/** Resolves a RELATIVE import specifier from `fromFile` the way 5.9.3's
|
|
177
|
+
* ts.resolveModuleName does under scriptc's options. Returns the resolved
|
|
178
|
+
* absolute path (not realpath'd — matching 5.9.3, which keeps in-project
|
|
179
|
+
* paths as spelled), or null. */
|
|
180
|
+
export function resolveRelativeModule(fromFile, specifier) {
|
|
181
|
+
const base = resolve(dirname(resolve(fromFile)), specifier);
|
|
182
|
+
// Inside an opted-in --npm-static package, relative edges resolve to the
|
|
183
|
+
// shipped JS, never to a sibling declaration twin: the .d.ts is the
|
|
184
|
+
// claim, the JS is the code that compiles (npm-static.ts — the tsgo
|
|
185
|
+
// host hides the same files, so both worlds answer the JS).
|
|
186
|
+
if (npmStaticPackageOfPath(resolve(fromFile)) !== null) {
|
|
187
|
+
return loadAsJsFile(base) ?? loadAsJsDirectory(base);
|
|
188
|
+
}
|
|
189
|
+
const answer = loadAsFile(base) ?? loadAsDirectory(base);
|
|
190
|
+
// A project declaration TWIN answers its runtime sibling (see
|
|
191
|
+
// projectDtsRuntimeSibling — Node's truth for project code).
|
|
192
|
+
if (answer !== null) {
|
|
193
|
+
const sibling = projectDtsRuntimeSibling(answer);
|
|
194
|
+
if (sibling !== null)
|
|
195
|
+
return sibling;
|
|
196
|
+
}
|
|
197
|
+
return answer;
|
|
198
|
+
}
|
|
199
|
+
/** The JS-only twin of loadAsFile for --npm-static package internals:
|
|
200
|
+
* exact file first (JS/JSON only), then Node's extension candidates —
|
|
201
|
+
* declaration files never answer. */
|
|
202
|
+
function loadAsJsFile(base) {
|
|
203
|
+
if (/\.(js|mjs|cjs|jsx|json)$/.test(base) && isFile(base))
|
|
204
|
+
return base;
|
|
205
|
+
for (const ext of [".js", ".json", ".mjs", ".cjs"]) {
|
|
206
|
+
if (isFile(base + ext))
|
|
207
|
+
return base + ext;
|
|
208
|
+
}
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
/** The JS-only twin of loadAsDirectory: package.json "main" (types/typings
|
|
212
|
+
* deliberately skipped), then the JS index files. */
|
|
213
|
+
function loadAsJsDirectory(base) {
|
|
214
|
+
if (!isDirectory(base))
|
|
215
|
+
return null;
|
|
216
|
+
const pkg = pkgJsonOf(base);
|
|
217
|
+
if (pkg && typeof pkg.main === "string" && pkg.main !== "") {
|
|
218
|
+
const viaFile = loadAsJsFile(join(base, pkg.main));
|
|
219
|
+
if (viaFile)
|
|
220
|
+
return viaFile;
|
|
221
|
+
}
|
|
222
|
+
for (const ext of [".js", ".json", ".mjs", ".cjs"]) {
|
|
223
|
+
const idx = join(base, "index" + ext);
|
|
224
|
+
if (isFile(idx))
|
|
225
|
+
return idx;
|
|
226
|
+
}
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
/* ── bare specifiers (npm types) ─────────────────────────────────────────
|
|
230
|
+
*
|
|
231
|
+
* Probed 5.9.3 order per node_modules directory walking up from the
|
|
232
|
+
* importer: the package directory (exports map with the bundler condition
|
|
233
|
+
* set {types, import, default} in object-key order; else types/typings/
|
|
234
|
+
* main; else subpath as a file; else index), then node_modules/<spec> as a
|
|
235
|
+
* bare FILE (.ts .tsx .d.ts), then the mangled @types package. JavaScript
|
|
236
|
+
* is NEVER a candidate inside node_modules (probed: no .js/.jsx in any
|
|
237
|
+
* failedLookupLocations there), so substitution maps .js targets to their
|
|
238
|
+
* declaration twins only. */
|
|
239
|
+
/** The conditions 5.9.3's bundler resolution enables for import sites
|
|
240
|
+
* (probed: "module"/"node"/"browser"/"bundler" keys are NOT matched). */
|
|
241
|
+
const EXPORT_CONDITIONS = new Set(["types", "import", "default"]);
|
|
242
|
+
/** The bundler condition set minus "types" — what an opted-in --npm-static
|
|
243
|
+
* package resolves with (its declarations are hidden from resolution; the
|
|
244
|
+
* runtime JS is the compile target). Mirrors the types-stripped
|
|
245
|
+
* package.json the tsgo host serves for the same package. */
|
|
246
|
+
const JS_ONLY_CONDITIONS = new Set(["import", "default"]);
|
|
247
|
+
/** package.json "exports" lookup: exact subpath keys, then '*' patterns
|
|
248
|
+
* (longest literal prefix wins), condition objects in object-key order,
|
|
249
|
+
* arrays first-resolvable. Returns the target path relative to the package
|
|
250
|
+
* directory, or null. Mirrors npm.ts's runtime resolver with the TYPES
|
|
251
|
+
* condition set. */
|
|
252
|
+
function resolveExportsTypes(exports, subpath, conditions = EXPORT_CONDITIONS) {
|
|
253
|
+
const resolveTarget = (target, wildcard) => {
|
|
254
|
+
if (typeof target === "string") {
|
|
255
|
+
return wildcard === null ? target : target.split("*").join(wildcard);
|
|
256
|
+
}
|
|
257
|
+
if (Array.isArray(target)) {
|
|
258
|
+
for (const t of target) {
|
|
259
|
+
const r = resolveTarget(t, wildcard);
|
|
260
|
+
if (r)
|
|
261
|
+
return r;
|
|
262
|
+
}
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
if (target && typeof target === "object") {
|
|
266
|
+
for (const [key, value] of Object.entries(target)) {
|
|
267
|
+
if (key.startsWith("."))
|
|
268
|
+
continue;
|
|
269
|
+
if (conditions.has(key)) {
|
|
270
|
+
const r = resolveTarget(value, wildcard);
|
|
271
|
+
if (r)
|
|
272
|
+
return r;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return null;
|
|
277
|
+
};
|
|
278
|
+
if (typeof exports === "string" || Array.isArray(exports)) {
|
|
279
|
+
return subpath === "." ? resolveTarget(exports, null) : null;
|
|
280
|
+
}
|
|
281
|
+
if (exports && typeof exports === "object") {
|
|
282
|
+
const map = exports;
|
|
283
|
+
const keys = Object.keys(map);
|
|
284
|
+
if (!keys.every((k) => k.startsWith("."))) {
|
|
285
|
+
return subpath === "." ? resolveTarget(exports, null) : null;
|
|
286
|
+
}
|
|
287
|
+
if (Object.prototype.hasOwnProperty.call(map, subpath)) {
|
|
288
|
+
return resolveTarget(map[subpath], null);
|
|
289
|
+
}
|
|
290
|
+
let best = null;
|
|
291
|
+
for (const key of keys) {
|
|
292
|
+
const star = key.indexOf("*");
|
|
293
|
+
if (star < 0)
|
|
294
|
+
continue;
|
|
295
|
+
const prefix = key.slice(0, star);
|
|
296
|
+
const suffix = key.slice(star + 1);
|
|
297
|
+
if (subpath.startsWith(prefix) &&
|
|
298
|
+
subpath.length >= prefix.length + suffix.length &&
|
|
299
|
+
subpath.endsWith(suffix) &&
|
|
300
|
+
(!best || prefix.length > best.prefix.length)) {
|
|
301
|
+
best = { key, prefix, suffix };
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (!best)
|
|
305
|
+
return null;
|
|
306
|
+
const wildcard = subpath.slice(best.prefix.length, subpath.length - best.suffix.length);
|
|
307
|
+
return resolveTarget(map[best.key], wildcard);
|
|
308
|
+
}
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
/* ── project imports (package.json "imports" / self-name "exports") ─────
|
|
312
|
+
*
|
|
313
|
+
* The two package.json-mediated specifier families that resolve INSIDE the
|
|
314
|
+
* project rather than into node_modules — Node's `#alias` imports field and
|
|
315
|
+
* the self-name reference (a bare specifier naming the nearest package.json's
|
|
316
|
+
* own "name", resolved through its "exports"). 5.9.3's bundler resolution
|
|
317
|
+
* answers both (probed: `#cjs` → ./index.cjs → index.cts by extension
|
|
318
|
+
* substitution; `pkg/sub/x.js` through a `./sub/*` exports pattern →
|
|
319
|
+
* ./src/x.ts), with the SAME condition set and pattern rules as the
|
|
320
|
+
* node_modules exports lookup — so the machinery is shared. Two Node rules
|
|
321
|
+
* carry over verbatim: only the specifier exactly "#" is invalid ("#/..."
|
|
322
|
+
* resolves through "#/*"-style pattern keys — probed on Node v24.15.0,
|
|
323
|
+
* which the ERR_INVALID_MODULE_SPECIFIER carve-out no longer covers), and
|
|
324
|
+
* self-name resolution requires the nearest package.json to carry BOTH a
|
|
325
|
+
* matching "name" and an "exports" field (no exports → no self-reference;
|
|
326
|
+
* the walk stops at the nearest package.json either way). An imports-field
|
|
327
|
+
* TARGET that is not "./"-relative is Node's PACKAGE_RESOLVE re-entry (an
|
|
328
|
+
* alias may name another package — including the package's own self-name):
|
|
329
|
+
* the project half of that re-entry resolves here; bare targets naming
|
|
330
|
+
* real node_modules packages stay other resolvers' business. */
|
|
331
|
+
/** The nearest directory at or above `dir` holding a package.json, or
|
|
332
|
+
* null. Node's LOOKUP_PACKAGE_SCOPE, minus the node_modules stop (callers
|
|
333
|
+
* hand in project files only). */
|
|
334
|
+
function nearestPkgDir(dir) {
|
|
335
|
+
let d = dir;
|
|
336
|
+
for (;;) {
|
|
337
|
+
if (pkgJsonOf(d))
|
|
338
|
+
return d;
|
|
339
|
+
const parent = dirname(d);
|
|
340
|
+
if (parent === d)
|
|
341
|
+
return null;
|
|
342
|
+
d = parent;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
/** package.json "imports" lookup — the exports machinery over "#" keys
|
|
346
|
+
* (exact keys, then '*' patterns, condition objects in key order). */
|
|
347
|
+
function resolveImportsField(imports, specifier) {
|
|
348
|
+
if (!imports || typeof imports !== "object" || Array.isArray(imports))
|
|
349
|
+
return null;
|
|
350
|
+
const map = imports;
|
|
351
|
+
// Reuse the exports resolver by re-rooting the key space: "./" + the
|
|
352
|
+
// specifier past "#" against keys with their "#" swapped for ".".
|
|
353
|
+
const rekeyed = {};
|
|
354
|
+
for (const [k, v] of Object.entries(map)) {
|
|
355
|
+
if (!k.startsWith("#"))
|
|
356
|
+
continue;
|
|
357
|
+
rekeyed["." + k.slice(1)] = v;
|
|
358
|
+
}
|
|
359
|
+
return resolveExportsTypes(rekeyed, "." + specifier.slice(1));
|
|
360
|
+
}
|
|
361
|
+
/** Absolute path of the nearest package.json at or above `fromFile`'s
|
|
362
|
+
* directory — the package scope Node's imports-field refusals name
|
|
363
|
+
* (ERR_PACKAGE_IMPORT_NOT_DEFINED carries it). Null outside any scope. */
|
|
364
|
+
export function nearestPkgJsonPath(fromFile) {
|
|
365
|
+
const dir = nearestPkgDir(dirname(resolve(fromFile)));
|
|
366
|
+
return dir === null ? null : join(dir, "package.json");
|
|
367
|
+
}
|
|
368
|
+
/** Resolves a PROJECT-INTERNAL package.json-mediated specifier from
|
|
369
|
+
* `fromFile` — `#alias` (the imports field) or a self-name reference (the
|
|
370
|
+
* nearest package.json's own name through its exports) — to an absolute
|
|
371
|
+
* source path with the usual bundler extension substitution, or null.
|
|
372
|
+
* Relative specifiers, real node_modules packages, and builtins are other
|
|
373
|
+
* resolvers' business; callers try those first. */
|
|
374
|
+
export function resolveProjectImport(fromFile, specifier) {
|
|
375
|
+
// --provenance-sources (flag-gated; the registry is empty otherwise): a
|
|
376
|
+
// registered bare specifier answers its attested SOURCE entry — the one
|
|
377
|
+
// chokepoint that makes preflight's user-module edges, the module
|
|
378
|
+
// order, and the CJS link check all agree the package compiles as
|
|
379
|
+
// program modules instead of island-embedding its published dist.
|
|
380
|
+
const provenance = provenanceEntryFor(specifier);
|
|
381
|
+
if (provenance !== null)
|
|
382
|
+
return provenance;
|
|
383
|
+
const pkgDir = nearestPkgDir(dirname(resolve(fromFile)));
|
|
384
|
+
if (pkgDir === null)
|
|
385
|
+
return null;
|
|
386
|
+
const pkg = pkgJsonOf(pkgDir);
|
|
387
|
+
if (!pkg)
|
|
388
|
+
return null;
|
|
389
|
+
let target = null;
|
|
390
|
+
if (specifier.startsWith("#")) {
|
|
391
|
+
// Node's validity rule: only the bare "#" is never resolvable ("#/..."
|
|
392
|
+
// matches "#/*"-pattern keys on current Node).
|
|
393
|
+
if (specifier === "#")
|
|
394
|
+
return null;
|
|
395
|
+
target = resolveImportsField(pkg.imports, specifier);
|
|
396
|
+
// An imports target that is not "./"-relative is PACKAGE_RESOLVE'd as
|
|
397
|
+
// a bare specifier (Node's PACKAGE_IMPORTS_EXPORTS_RESOLVE): the
|
|
398
|
+
// project-internal half of that re-entry is the self-name rule below —
|
|
399
|
+
// recurse with the target as the specifier (from the same package
|
|
400
|
+
// scope). Targets naming real node_modules packages answer null here;
|
|
401
|
+
// callers' npm resolution owns those.
|
|
402
|
+
if (target !== null && !target.startsWith("./") && !target.startsWith("#")) {
|
|
403
|
+
return resolveProjectImport(join(pkgDir, "package.json"), target);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
else if (typeof pkg.name === "string" && pkg.name !== "" && pkg.exports !== undefined) {
|
|
407
|
+
// Self-name: the specifier's package-name part must equal the nearest
|
|
408
|
+
// package.json's name; the remainder is an exports subpath.
|
|
409
|
+
const parts = specifier.split("/");
|
|
410
|
+
const nameLen = specifier.startsWith("@") ? 2 : 1;
|
|
411
|
+
const pkgName = parts.slice(0, nameLen).join("/");
|
|
412
|
+
if (pkgName !== pkg.name || parts.length < nameLen)
|
|
413
|
+
return null;
|
|
414
|
+
const subpath = parts.length === nameLen ? "." : "./" + parts.slice(nameLen).join("/");
|
|
415
|
+
target = resolveExportsTypes(pkg.exports, subpath);
|
|
416
|
+
}
|
|
417
|
+
if (target === null)
|
|
418
|
+
return null;
|
|
419
|
+
const path = join(pkgDir, target);
|
|
420
|
+
return loadAsFile(path) ?? (isFile(path) ? path : null);
|
|
421
|
+
}
|
|
422
|
+
function extensionsFor(pass, flavor) {
|
|
423
|
+
if (pass === "types") {
|
|
424
|
+
switch (flavor) {
|
|
425
|
+
case "m": return [".mts", ".d.mts"];
|
|
426
|
+
case "c": return [".cts", ".d.cts"];
|
|
427
|
+
case "x": return [".tsx", ".ts", ".d.ts"];
|
|
428
|
+
default: return [".ts", ".tsx", ".d.ts"];
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
switch (flavor) {
|
|
432
|
+
case "m": return [".mjs"];
|
|
433
|
+
case "c": return [".cjs"];
|
|
434
|
+
case "x": return [".jsx", ".js"];
|
|
435
|
+
default: return [".js", ".jsx"];
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
/** File resolution of an exports-map target (or types/typings/main field)
|
|
439
|
+
* inside node_modules, for one pass: recognized-extension substitution,
|
|
440
|
+
* extension addition, then directory index. */
|
|
441
|
+
function loadTargetInPass(pkgDir, target, pass) {
|
|
442
|
+
const path = join(pkgDir, target);
|
|
443
|
+
if (pass === "types") {
|
|
444
|
+
if (/\.(d\.ts|d\.mts|d\.cts|ts|tsx|mts|cts)$/.test(path) && isFile(path))
|
|
445
|
+
return path;
|
|
446
|
+
}
|
|
447
|
+
else if (/\.(js|jsx|mjs|cjs)$/.test(path) && isFile(path)) {
|
|
448
|
+
return path;
|
|
449
|
+
}
|
|
450
|
+
const sub = (ext, flavor) => {
|
|
451
|
+
const stem = path.slice(0, -ext.length);
|
|
452
|
+
for (const r of extensionsFor(pass, flavor)) {
|
|
453
|
+
if (isFile(stem + r))
|
|
454
|
+
return stem + r;
|
|
455
|
+
}
|
|
456
|
+
return null;
|
|
457
|
+
};
|
|
458
|
+
if (path.endsWith(".mjs"))
|
|
459
|
+
return sub(".mjs", "m");
|
|
460
|
+
if (path.endsWith(".cjs"))
|
|
461
|
+
return sub(".cjs", "c");
|
|
462
|
+
if (path.endsWith(".js"))
|
|
463
|
+
return sub(".js", "plain");
|
|
464
|
+
if (path.endsWith(".jsx"))
|
|
465
|
+
return sub(".jsx", "x");
|
|
466
|
+
// No/unknown extension: extension addition, then a directory — a NESTED
|
|
467
|
+
// package.json's types/typings/main first (probed: 5.9.3 resolves
|
|
468
|
+
// "@restart/hooks/useMergedRefs" through the subdirectory's own
|
|
469
|
+
// package.json, "module" ignored), then the index files.
|
|
470
|
+
for (const ext of extensionsFor(pass, "plain")) {
|
|
471
|
+
if (isFile(path + ext))
|
|
472
|
+
return path + ext;
|
|
473
|
+
}
|
|
474
|
+
if (isDirectory(path)) {
|
|
475
|
+
const nested = pkgJsonOf(path);
|
|
476
|
+
if (nested) {
|
|
477
|
+
for (const field of [nested.types, nested.typings, nested.main]) {
|
|
478
|
+
if (typeof field === "string" && field !== "") {
|
|
479
|
+
const via = loadTargetInPass(path, field, pass);
|
|
480
|
+
if (via)
|
|
481
|
+
return via;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
for (const ext of extensionsFor(pass, "plain")) {
|
|
486
|
+
const idx = join(path, "index" + ext);
|
|
487
|
+
if (isFile(idx))
|
|
488
|
+
return idx;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return null;
|
|
492
|
+
}
|
|
493
|
+
/** node_modules file-or-directory resolution for a package-internal path (a
|
|
494
|
+
* subpath without exports, the root lookup), for one pass. */
|
|
495
|
+
function loadPathInPass(pkgDir, rel, pass) {
|
|
496
|
+
const base = rel === "." ? pkgDir : join(pkgDir, rel);
|
|
497
|
+
if (rel !== ".") {
|
|
498
|
+
const viaFile = loadTargetInPass(pkgDir, rel, pass);
|
|
499
|
+
if (viaFile)
|
|
500
|
+
return viaFile;
|
|
501
|
+
return null;
|
|
502
|
+
}
|
|
503
|
+
if (!isDirectory(base))
|
|
504
|
+
return null;
|
|
505
|
+
const pkg = pkgJsonOf(base);
|
|
506
|
+
if (pkg) {
|
|
507
|
+
for (const field of [pkg.types, pkg.typings]) {
|
|
508
|
+
if (typeof field === "string" && field !== "") {
|
|
509
|
+
const viaTypes = loadTargetInPass(base, field, pass);
|
|
510
|
+
if (viaTypes)
|
|
511
|
+
return viaTypes;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
if (typeof pkg.main === "string" && pkg.main !== "") {
|
|
515
|
+
const viaMain = loadTargetInPass(base, pkg.main, pass);
|
|
516
|
+
if (viaMain)
|
|
517
|
+
return viaMain;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
for (const ext of extensionsFor(pass, "plain")) {
|
|
521
|
+
const idx = join(base, "index" + ext);
|
|
522
|
+
if (isFile(idx))
|
|
523
|
+
return idx;
|
|
524
|
+
}
|
|
525
|
+
return null;
|
|
526
|
+
}
|
|
527
|
+
/** Package name prefix of a bare specifier ("@scope/pkg/sub" → "@scope/pkg",
|
|
528
|
+
* "pkg/sub" → "pkg"). */
|
|
529
|
+
function packagePrefixOf(specifier) {
|
|
530
|
+
const parts = specifier.split("/");
|
|
531
|
+
return specifier.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
|
|
532
|
+
}
|
|
533
|
+
/** The DefinitelyTyped name mangling: "@scope/pkg" → "scope__pkg". */
|
|
534
|
+
function mangleScopedName(name) {
|
|
535
|
+
return name.startsWith("@") ? name.slice(1).replace("/", "__") : name;
|
|
536
|
+
}
|
|
537
|
+
function packageAnswer(pkgDir, fallbackName, typesFile) {
|
|
538
|
+
const pkg = pkgJsonOf(pkgDir);
|
|
539
|
+
const name = typeof pkg?.name === "string" && pkg.name !== "" ? pkg.name : fallbackName;
|
|
540
|
+
const version = typeof pkg?.version === "string" ? pkg.version : undefined;
|
|
541
|
+
return {
|
|
542
|
+
typesFile: realpathOr(typesFile),
|
|
543
|
+
packageName: version !== undefined ? name : fallbackName,
|
|
544
|
+
...(version !== undefined ? { version } : {}),
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
/** Resolves a BARE import specifier to its npm TYPES file the way 5.9.3's
|
|
548
|
+
* ts.resolveModuleName does under scriptc's options (bundler, types-only in
|
|
549
|
+
* node_modules), walking node_modules directories up from the importing
|
|
550
|
+
* file. Null when nothing resolves. */
|
|
551
|
+
export function resolveBareModule(fromFile, specifier,
|
|
552
|
+
/** "js-only" forces the runtime-JS resolution regardless of the active
|
|
553
|
+
* --npm-static set (the auto-detection probe); default follows the set. */
|
|
554
|
+
mode) {
|
|
555
|
+
const pkgName = packagePrefixOf(specifier);
|
|
556
|
+
const rest = specifier.slice(pkgName.length).replace(/^\//, "");
|
|
557
|
+
const subpath = rest === "" ? "." : `./${rest}`;
|
|
558
|
+
// An opted-in --npm-static package resolves to its RUNTIME JS: the js
|
|
559
|
+
// pass only, the "types" export condition dropped, the @types mangling
|
|
560
|
+
// never consulted — mirroring the shadowed world the tsgo host serves.
|
|
561
|
+
const npmStatic = mode === "js-only" || isNpmStaticPackage(pkgName);
|
|
562
|
+
const conditions = npmStatic ? JS_ONLY_CONDITIONS : EXPORT_CONDITIONS;
|
|
563
|
+
const inPackage = (nmPkgDir, name, pass) => {
|
|
564
|
+
// A workspace link: the answer's realpath escaped node_modules, so the
|
|
565
|
+
// package directory is a symlink into the project. Stamp the realpath'd
|
|
566
|
+
// package root so callers can classify (and register) the package.
|
|
567
|
+
const withWorkspace = (r) => {
|
|
568
|
+
if (r === null || isNodeModulesPath(r.typesFile))
|
|
569
|
+
return r;
|
|
570
|
+
const realDir = realpathOr(nmPkgDir);
|
|
571
|
+
return isNodeModulesPath(realDir) ? r : { ...r, workspaceDir: realDir };
|
|
572
|
+
};
|
|
573
|
+
const rawPkg = pkgJsonOf(nmPkgDir);
|
|
574
|
+
// The opted-in exports lookup runs over the SAME transformed document
|
|
575
|
+
// the tsgo host serves (types stripped, import:=require) — the two
|
|
576
|
+
// resolvers must answer one file. Cloned per lookup; never cached, so
|
|
577
|
+
// the raw cache stays clean for flagless compiles.
|
|
578
|
+
const pkg = npmStatic && rawPkg !== null
|
|
579
|
+
? (() => {
|
|
580
|
+
const clone = JSON.parse(JSON.stringify(rawPkg));
|
|
581
|
+
npmStaticTransformPkgJson(clone);
|
|
582
|
+
return clone;
|
|
583
|
+
})()
|
|
584
|
+
: rawPkg;
|
|
585
|
+
if (pkg?.exports !== undefined) {
|
|
586
|
+
const target = resolveExportsTypes(pkg.exports, subpath, conditions);
|
|
587
|
+
if (target !== null) {
|
|
588
|
+
const file = loadTargetInPass(nmPkgDir, target, pass);
|
|
589
|
+
if (file)
|
|
590
|
+
return withWorkspace(packageAnswer(nmPkgDir, name, file));
|
|
591
|
+
}
|
|
592
|
+
return null;
|
|
593
|
+
}
|
|
594
|
+
const file = loadPathInPass(nmPkgDir, subpath === "." ? "." : `./${rest}`, pass);
|
|
595
|
+
if (!file)
|
|
596
|
+
return null;
|
|
597
|
+
// A SUBPATH answered through a nested package.json (the
|
|
598
|
+
// @restart/hooks/useMergedRefs shape): 5.9.3 forms the packageId from
|
|
599
|
+
// THAT package.json — its (usually absent) version, not the root's.
|
|
600
|
+
const subDir = subpath === "." ? null : join(nmPkgDir, rest);
|
|
601
|
+
const answerDir = subDir !== null && isDirectory(subDir) && pkgJsonOf(subDir) ? subDir : nmPkgDir;
|
|
602
|
+
return withWorkspace(packageAnswer(answerDir, name, file));
|
|
603
|
+
};
|
|
604
|
+
const passOnce = (pass) => {
|
|
605
|
+
for (let dir = dirname(resolve(fromFile));;) {
|
|
606
|
+
const nm = join(dir, "node_modules");
|
|
607
|
+
if (isDirectory(nm)) {
|
|
608
|
+
// 1. The package directory.
|
|
609
|
+
const viaPkg = inPackage(join(nm, pkgName), pkgName, pass);
|
|
610
|
+
if (viaPkg)
|
|
611
|
+
return viaPkg;
|
|
612
|
+
// 2. The specifier as a bare FILE under node_modules.
|
|
613
|
+
for (const ext of extensionsFor(pass, "plain")) {
|
|
614
|
+
const asFile = join(nm, specifier + ext);
|
|
615
|
+
if (isFile(asFile)) {
|
|
616
|
+
return { typesFile: realpathOr(asFile), packageName: pkgName };
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
// 3. The mangled @types package (declaration pass only).
|
|
620
|
+
if (pass === "types" && !npmStatic) {
|
|
621
|
+
const typesPkgName = `@types/${mangleScopedName(pkgName)}`;
|
|
622
|
+
const viaTypes = inPackage(join(nm, typesPkgName), typesPkgName, pass);
|
|
623
|
+
if (viaTypes)
|
|
624
|
+
return viaTypes;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
const parent = dirname(dir);
|
|
628
|
+
if (parent === dir)
|
|
629
|
+
return null;
|
|
630
|
+
dir = parent;
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
return npmStatic ? passOnce("js") : (passOnce("types") ?? passOnce("js"));
|
|
634
|
+
}
|
|
635
|
+
/** Resolves a `/// <reference types="name" />`-style TYPE DIRECTIVE the way
|
|
636
|
+
* ts.resolveTypeReferenceDirective does with typeRoots emptied (the
|
|
637
|
+
* secondary lookup only): walking up from the anchor file, per directory
|
|
638
|
+
* node_modules/<name> then node_modules/@types/<name>, each via package.json
|
|
639
|
+
* types/typings then index.d.ts. Returns the realpath'd file or null. */
|
|
640
|
+
export function resolveTypeDirective(name, fromFile) {
|
|
641
|
+
const tryPkg = (base) => {
|
|
642
|
+
const pkg = pkgJsonOf(base);
|
|
643
|
+
for (const field of [pkg?.types, pkg?.typings]) {
|
|
644
|
+
if (typeof field === "string" && field !== "") {
|
|
645
|
+
const target = join(base, field);
|
|
646
|
+
if (isFile(target))
|
|
647
|
+
return target;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
const asFile = base + ".d.ts";
|
|
651
|
+
if (isFile(asFile))
|
|
652
|
+
return asFile;
|
|
653
|
+
const idx = join(base, "index.d.ts");
|
|
654
|
+
return isFile(idx) ? idx : null;
|
|
655
|
+
};
|
|
656
|
+
for (let dir = dirname(resolve(fromFile));;) {
|
|
657
|
+
const nm = join(dir, "node_modules");
|
|
658
|
+
if (isDirectory(nm)) {
|
|
659
|
+
const viaSelf = tryPkg(join(nm, name));
|
|
660
|
+
if (viaSelf)
|
|
661
|
+
return realpathOr(viaSelf);
|
|
662
|
+
const viaTypes = tryPkg(join(nm, "@types", mangleScopedName(name)));
|
|
663
|
+
if (viaTypes)
|
|
664
|
+
return realpathOr(viaTypes);
|
|
665
|
+
}
|
|
666
|
+
const parent = dirname(dir);
|
|
667
|
+
if (parent === dir)
|
|
668
|
+
return null;
|
|
669
|
+
dir = parent;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
/** Test hook: the package.json cache holds across programs (fine within one
|
|
673
|
+
* compile; a long-lived test process editing fixtures must reset it). */
|
|
674
|
+
export function clearResolveCaches() {
|
|
675
|
+
pkgJsonCache.clear();
|
|
676
|
+
}
|
|
677
|
+
/** True when `path` is under a node_modules directory (the
|
|
678
|
+
* isExternalLibraryImport test 5.9.3 answers on resolutions). */
|
|
679
|
+
export function isNodeModulesPath(path) {
|
|
680
|
+
return isAbsolute(path) && path.split("/").includes("node_modules");
|
|
681
|
+
}
|
|
682
|
+
//# sourceMappingURL=resolve.js.map
|