vitral 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/angular.d.ts +2 -2
- package/dist/angular.js +5 -5
- package/dist/auto.js +5 -5
- package/dist/{chunk-W6322KGP.js → chunk-4Z77XNBR.js} +2 -2
- package/dist/{chunk-EMSB2NVX.js → chunk-5JNV45BB.js} +4 -4
- package/dist/{chunk-IH3ZT3QU.js → chunk-C3QOBAUE.js} +1 -1
- package/dist/chunk-DAESKNQ6.js +207 -0
- package/dist/{chunk-BCC25ANK.js → chunk-K6EG7DMO.js} +8 -5
- package/dist/{chunk-6EX5KYT5.js → chunk-KU6HF2EW.js} +2 -2
- package/dist/{chunk-WD4CDGDZ.js → chunk-KZBH5MPD.js} +12 -11
- package/dist/chunk-NZEQGRBL.js +93 -0
- package/dist/chunk-PY7P5BF4.js +404 -0
- package/dist/cli.js +414 -44
- package/dist/dict.d.ts +69 -0
- package/dist/dict.js +8 -0
- package/dist/edge.d.ts +118 -0
- package/dist/edge.js +398 -0
- package/dist/element.d.ts +2 -2
- package/dist/element.js +5 -5
- package/dist/embed.js +2 -2
- package/dist/{events-Dkpkw_Sv.d.ts → events-0rxFlWJJ.d.ts} +1 -1
- package/dist/{expose-OVWTPHH5.js → expose-IAKYHJXF.js} +3 -3
- package/dist/federation.d.ts +1 -1
- package/dist/hydrate-YVZDJ2V7.js +168 -0
- package/dist/iframe.js +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +3 -3
- package/dist/otel.d.ts +29 -1
- package/dist/otel.js +13 -1
- package/dist/preact.d.ts +4 -4
- package/dist/preact.js +4 -4
- package/dist/prefetch.d.ts +25 -0
- package/dist/prefetch.js +178 -0
- package/dist/react.d.ts +3 -3
- package/dist/react.js +4 -4
- package/dist/reframe-LQL4DZYP.js +251 -0
- package/dist/routes.d.ts +2 -2
- package/dist/routes.js +3 -3
- package/dist/{shadow-PVSETNMZ.js → shadow-S4SKHTA7.js} +2 -2
- package/dist/single-spa.d.ts +1 -1
- package/dist/{slot-UPg6ob9v.d.ts → slot-D49L1rdU.d.ts} +1 -1
- package/dist/solid.d.ts +3 -3
- package/dist/solid.js +4 -4
- package/dist/{sri-M7ZRU57G.js → sri-576GTDIA.js} +2 -1
- package/dist/svelte.d.ts +4 -4
- package/dist/svelte.js +4 -4
- package/dist/testing.d.ts +171 -3
- package/dist/testing.js +191 -4
- package/dist/{types-CA2p9KHC.d.ts → types-DUtcF3HO.d.ts} +12 -1
- package/dist/vitals.d.ts +29 -0
- package/dist/vitals.js +152 -0
- package/dist/vite.js +28 -422
- package/dist/vue.d.ts +2 -2
- package/dist/vue.js +4 -4
- package/package.json +107 -16
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
// src/vite/types.ts
|
|
2
|
+
import { createRequire as createRequire2 } from "module";
|
|
3
|
+
import { join as join2 } from "path";
|
|
4
|
+
|
|
5
|
+
// src/vite/oxc.ts
|
|
6
|
+
import { existsSync, readFileSync } from "fs";
|
|
7
|
+
import { createRequire } from "module";
|
|
8
|
+
import { dirname, join, resolve } from "path";
|
|
9
|
+
import { pathToFileURL } from "url";
|
|
10
|
+
async function loadOxc(root) {
|
|
11
|
+
const req = createRequire(join(root, "package.json"));
|
|
12
|
+
const ids = [() => req.resolve("oxc-transform"), () => createRequire(req.resolve("vite/package.json")).resolve("rolldown/experimental")];
|
|
13
|
+
for (const id of ids) {
|
|
14
|
+
try {
|
|
15
|
+
const mod = await import(pathToFileURL(id()).href);
|
|
16
|
+
if (typeof mod.isolatedDeclarationSync === "function") return mod;
|
|
17
|
+
} catch {
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
var GLOBAL = /^(Date|RegExp|Error|Map|Set|WeakMap|WeakSet|Promise|PromiseLike|Record|Partial|Required|Readonly|Pick|Omit|URL|URLSearchParams|Blob|File|FormData|Headers|AbortSignal|EventTarget|Node|Element|Document|DocumentFragment|ShadowRoot|Window|(HTML|SVG)\w*Element|\w*Event)$/;
|
|
23
|
+
var IDENT = /^[A-Za-z_$][\w$]*$/;
|
|
24
|
+
var Unsupported = class extends Error {
|
|
25
|
+
};
|
|
26
|
+
var unsupported = () => {
|
|
27
|
+
throw new Unsupported();
|
|
28
|
+
};
|
|
29
|
+
function propsFromOxc(ts, oxc, file) {
|
|
30
|
+
try {
|
|
31
|
+
const reader = new Reader(ts, oxc);
|
|
32
|
+
const scope = reader.scope(file);
|
|
33
|
+
const props = reader.defaultProps(scope);
|
|
34
|
+
return props ? reader.print(props.node, props.scope, 0, { top: true }) : "Record<string, unknown>";
|
|
35
|
+
} catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
var Reader = class {
|
|
40
|
+
constructor(ts, oxc) {
|
|
41
|
+
this.ts = ts;
|
|
42
|
+
this.oxc = oxc;
|
|
43
|
+
this.scopes = /* @__PURE__ */ new Map();
|
|
44
|
+
this.seen = /* @__PURE__ */ new Set();
|
|
45
|
+
}
|
|
46
|
+
/** Declarações de um arquivo: `.d.ts` como está, o resto pelo Oxc. */
|
|
47
|
+
scope(file) {
|
|
48
|
+
const cached = this.scopes.get(file);
|
|
49
|
+
if (cached) return cached;
|
|
50
|
+
const { ts } = this;
|
|
51
|
+
let code = readFileSync(file, "utf8");
|
|
52
|
+
if (!/\.d\.[cm]?ts$/.test(file)) {
|
|
53
|
+
const out = this.oxc.isolatedDeclarationSync(file, code, {});
|
|
54
|
+
if (out.errors.length) unsupported();
|
|
55
|
+
code = out.code;
|
|
56
|
+
}
|
|
57
|
+
const source = ts.createSourceFile(file + ".d.ts", code, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
58
|
+
const scope = { file, types: /* @__PURE__ */ new Map(), values: /* @__PURE__ */ new Map(), imports: /* @__PURE__ */ new Map() };
|
|
59
|
+
const isDefault = (n) => ts.canHaveModifiers(n) && !!ts.getModifiers(n)?.some((m) => m.kind === ts.SyntaxKind.DefaultKeyword);
|
|
60
|
+
let defaultName;
|
|
61
|
+
for (const st of source.statements) {
|
|
62
|
+
if (ts.isInterfaceDeclaration(st) || ts.isTypeAliasDeclaration(st)) {
|
|
63
|
+
if (scope.types.has(st.name.text)) unsupported();
|
|
64
|
+
scope.types.set(st.name.text, st);
|
|
65
|
+
} else if (ts.isImportDeclaration(st) && ts.isStringLiteral(st.moduleSpecifier)) {
|
|
66
|
+
const from = st.moduleSpecifier.text;
|
|
67
|
+
const clause = st.importClause;
|
|
68
|
+
if (clause?.name) scope.imports.set(clause.name.text, { from, name: "default" });
|
|
69
|
+
const bindings = clause?.namedBindings;
|
|
70
|
+
if (bindings && ts.isNamespaceImport(bindings)) scope.imports.set(bindings.name.text, { from, name: "*" });
|
|
71
|
+
if (bindings && ts.isNamedImports(bindings)) for (const el of bindings.elements) scope.imports.set(el.name.text, { from, name: (el.propertyName ?? el.name).text });
|
|
72
|
+
} else if (ts.isFunctionDeclaration(st) || ts.isClassDeclaration(st)) {
|
|
73
|
+
if (isDefault(st)) scope.default ??= st;
|
|
74
|
+
else if (st.name && !scope.values.has(st.name.text)) scope.values.set(st.name.text, st);
|
|
75
|
+
} else if (ts.isVariableStatement(st)) {
|
|
76
|
+
for (const d of st.declarationList.declarations) if (ts.isIdentifier(d.name)) scope.values.set(d.name.text, d);
|
|
77
|
+
} else if (ts.isExportAssignment(st) && !st.isExportEquals) {
|
|
78
|
+
defaultName = ts.isIdentifier(st.expression) ? st.expression.text : unsupported();
|
|
79
|
+
} else if (ts.isExportDeclaration(st) && !st.moduleSpecifier && st.exportClause && ts.isNamedExports(st.exportClause)) {
|
|
80
|
+
const el = st.exportClause.elements.find((e) => e.name.text === "default");
|
|
81
|
+
if (el) defaultName = (el.propertyName ?? el.name).text;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (defaultName) scope.default = scope.values.get(defaultName);
|
|
85
|
+
this.scopes.set(file, scope);
|
|
86
|
+
return scope;
|
|
87
|
+
}
|
|
88
|
+
/** `mount(el, props)`, componente-função `(props)` ou classe com `props`. `undefined`: sem props. */
|
|
89
|
+
defaultProps(scope) {
|
|
90
|
+
const { ts } = this;
|
|
91
|
+
const node = scope.default ?? unsupported();
|
|
92
|
+
const param = (params, i) => {
|
|
93
|
+
const p = params[i];
|
|
94
|
+
return p && { node: p.type ?? unsupported(), scope };
|
|
95
|
+
};
|
|
96
|
+
if (ts.isFunctionDeclaration(node)) return param(node.parameters, 0);
|
|
97
|
+
if (ts.isClassDeclaration(node)) {
|
|
98
|
+
if (node.heritageClauses?.length) unsupported();
|
|
99
|
+
const props = node.members.find((m) => ts.isPropertyDeclaration(m) && m.name.getText() === "props");
|
|
100
|
+
return props && { node: props.type ?? unsupported(), scope };
|
|
101
|
+
}
|
|
102
|
+
if (ts.isVariableDeclaration(node)) return this.propsOfType(node.type ?? unsupported(), scope);
|
|
103
|
+
return unsupported();
|
|
104
|
+
}
|
|
105
|
+
propsOfType(node, scope) {
|
|
106
|
+
const { ts } = this;
|
|
107
|
+
const param = (params, i, at2) => {
|
|
108
|
+
const p = params[i];
|
|
109
|
+
return p && { node: p.type ?? unsupported(), scope: at2 };
|
|
110
|
+
};
|
|
111
|
+
if (ts.isParenthesizedTypeNode(node)) return this.propsOfType(node.type, scope);
|
|
112
|
+
if (ts.isFunctionTypeNode(node)) return param(node.parameters, 0, scope);
|
|
113
|
+
let members;
|
|
114
|
+
let at = scope;
|
|
115
|
+
if (ts.isTypeLiteralNode(node)) members = node.members;
|
|
116
|
+
else if (ts.isTypeReferenceNode(node) && ts.isIdentifier(node.typeName) && !node.typeArguments) {
|
|
117
|
+
const found = this.lookup(node.typeName.text, scope);
|
|
118
|
+
if (!found || found === "package" || found.decl.typeParameters) return unsupported();
|
|
119
|
+
if (ts.isTypeAliasDeclaration(found.decl)) return this.propsOfType(found.decl.type, found.scope);
|
|
120
|
+
if (found.decl.heritageClauses?.length) unsupported();
|
|
121
|
+
members = found.decl.members;
|
|
122
|
+
at = found.scope;
|
|
123
|
+
} else return unsupported();
|
|
124
|
+
const mount = members.find((m) => m.name?.getText() === "mount");
|
|
125
|
+
if (mount) {
|
|
126
|
+
if (ts.isMethodSignature(mount)) return param(mount.parameters, 1, at);
|
|
127
|
+
if (ts.isPropertySignature(mount) && mount.type) return this.mountProps(mount.type, at);
|
|
128
|
+
return unsupported();
|
|
129
|
+
}
|
|
130
|
+
const call = members.find(ts.isCallSignatureDeclaration);
|
|
131
|
+
if (call) return param(call.parameters, 0, at);
|
|
132
|
+
if (members.some(ts.isConstructSignatureDeclaration)) unsupported();
|
|
133
|
+
return void 0;
|
|
134
|
+
}
|
|
135
|
+
mountProps(node, scope) {
|
|
136
|
+
const { ts } = this;
|
|
137
|
+
if (ts.isParenthesizedTypeNode(node)) return this.mountProps(node.type, scope);
|
|
138
|
+
if (!ts.isFunctionTypeNode(node)) return unsupported();
|
|
139
|
+
const p = node.parameters[1];
|
|
140
|
+
return p && { node: p.type ?? unsupported(), scope };
|
|
141
|
+
}
|
|
142
|
+
/** Acha um tipo pelo nome: no arquivo, num import relativo ou num pacote. */
|
|
143
|
+
lookup(name, scope) {
|
|
144
|
+
const decl = scope.types.get(name);
|
|
145
|
+
if (decl) return { decl, scope };
|
|
146
|
+
const imp = scope.imports.get(name);
|
|
147
|
+
if (!imp) return void 0;
|
|
148
|
+
if (!imp.from.startsWith(".")) return "package";
|
|
149
|
+
if (imp.name === "default" || imp.name === "*") return unsupported();
|
|
150
|
+
const found = this.lookup(imp.name, this.scope(resolveFile(scope.file, imp.from)));
|
|
151
|
+
return found ?? unsupported();
|
|
152
|
+
}
|
|
153
|
+
print(node, scope, depth, opts = {}) {
|
|
154
|
+
const { ts } = this;
|
|
155
|
+
const K = ts.SyntaxKind;
|
|
156
|
+
switch (node.kind) {
|
|
157
|
+
case K.AnyKeyword:
|
|
158
|
+
case K.UnknownKeyword:
|
|
159
|
+
return "unknown";
|
|
160
|
+
case K.StringKeyword:
|
|
161
|
+
case K.NumberKeyword:
|
|
162
|
+
case K.BooleanKeyword:
|
|
163
|
+
case K.BigIntKeyword:
|
|
164
|
+
case K.SymbolKeyword:
|
|
165
|
+
case K.UndefinedKeyword:
|
|
166
|
+
case K.VoidKeyword:
|
|
167
|
+
case K.NeverKeyword:
|
|
168
|
+
return node.getText();
|
|
169
|
+
}
|
|
170
|
+
if (ts.isLiteralTypeNode(node)) {
|
|
171
|
+
const lit = node.literal;
|
|
172
|
+
if (ts.isStringLiteral(lit)) return JSON.stringify(lit.text);
|
|
173
|
+
if (lit.kind === K.NullKeyword || lit.kind === K.TrueKeyword || lit.kind === K.FalseKeyword || ts.isNumericLiteral(lit) || ts.isPrefixUnaryExpression(lit)) return lit.getText();
|
|
174
|
+
return unsupported();
|
|
175
|
+
}
|
|
176
|
+
if (ts.isParenthesizedTypeNode(node)) return this.print(node.type, scope, depth, opts);
|
|
177
|
+
if (ts.isUnionTypeNode(node)) {
|
|
178
|
+
const types = opts.nonNull ? node.types.filter((t) => t.kind !== K.UndefinedKeyword && !(ts.isLiteralTypeNode(t) && t.literal.kind === K.NullKeyword)) : node.types;
|
|
179
|
+
const parts = [...new Set(types.map((t) => this.print(t, scope, depth)))];
|
|
180
|
+
if (parts.includes("true") && parts.includes("false")) parts.splice(parts.indexOf("true"), 1, "boolean"), parts.splice(parts.indexOf("false"), 1);
|
|
181
|
+
return parts.join(" | ");
|
|
182
|
+
}
|
|
183
|
+
if (ts.isIntersectionTypeNode(node)) {
|
|
184
|
+
return node.types.map((t) => {
|
|
185
|
+
const s = this.print(t, scope, depth);
|
|
186
|
+
return ts.isUnionTypeNode(t) ? `(${s})` : s;
|
|
187
|
+
}).join(" & ");
|
|
188
|
+
}
|
|
189
|
+
if (depth > 6) return "unknown";
|
|
190
|
+
if (ts.isArrayTypeNode(node)) return `Array<${this.print(node.elementType, scope, depth + 1)}>`;
|
|
191
|
+
if (ts.isTypeOperatorNode(node) && node.operator === K.ReadonlyKeyword && (ts.isArrayTypeNode(node.type) || ts.isTupleTypeNode(node.type))) return this.print(node.type, scope, depth);
|
|
192
|
+
if (ts.isTupleTypeNode(node)) {
|
|
193
|
+
if (node.elements.some((e) => ts.isNamedTupleMember(e) || ts.isOptionalTypeNode(e) || ts.isRestTypeNode(e))) unsupported();
|
|
194
|
+
return `[${node.elements.map((e) => this.print(e, scope, depth + 1)).join(", ")}]`;
|
|
195
|
+
}
|
|
196
|
+
if (ts.isTypeLiteralNode(node)) return this.object(node.members, scope, depth, !!opts.top);
|
|
197
|
+
if (ts.isFunctionTypeNode(node)) return this.signature(node, scope, depth);
|
|
198
|
+
if (ts.isTypeReferenceNode(node)) return this.reference(node, scope, depth, opts);
|
|
199
|
+
return unsupported();
|
|
200
|
+
}
|
|
201
|
+
reference(node, scope, depth, opts) {
|
|
202
|
+
const { ts } = this;
|
|
203
|
+
if (!ts.isIdentifier(node.typeName)) return unsupported();
|
|
204
|
+
const name = node.typeName.text;
|
|
205
|
+
const args = node.typeArguments ?? [];
|
|
206
|
+
const found = this.lookup(name, scope);
|
|
207
|
+
if (found === "package") return "unknown";
|
|
208
|
+
if (found) {
|
|
209
|
+
if (args.length || found.decl.typeParameters) unsupported();
|
|
210
|
+
if (this.seen.has(found.decl)) return "unknown";
|
|
211
|
+
this.seen.add(found.decl);
|
|
212
|
+
try {
|
|
213
|
+
const decl = found.decl;
|
|
214
|
+
if (ts.isTypeAliasDeclaration(decl)) return this.print(decl.type, found.scope, depth, opts);
|
|
215
|
+
if (decl.heritageClauses?.length) unsupported();
|
|
216
|
+
return this.object(decl.members, found.scope, depth, !!opts.top);
|
|
217
|
+
} finally {
|
|
218
|
+
this.seen.delete(found.decl);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if ((name === "Array" || name === "ReadonlyArray") && args.length === 1) return `Array<${this.print(args[0], scope, depth + 1)}>`;
|
|
222
|
+
if (!GLOBAL.test(name)) return unsupported();
|
|
223
|
+
return args.length ? `${name}<${args.map((t) => this.print(t, scope, depth + 1)).join(", ")}>` : name;
|
|
224
|
+
}
|
|
225
|
+
object(members, scope, depth, top) {
|
|
226
|
+
const { ts } = this;
|
|
227
|
+
const fields = [];
|
|
228
|
+
const calls = [];
|
|
229
|
+
let index;
|
|
230
|
+
for (const m of members) {
|
|
231
|
+
if (ts.isPropertySignature(m) || ts.isMethodSignature(m)) {
|
|
232
|
+
const name = ts.isIdentifier(m.name) || ts.isStringLiteral(m.name) || ts.isNumericLiteral(m.name) ? m.name.text : unsupported();
|
|
233
|
+
if (top && name === "vitral") continue;
|
|
234
|
+
const optional = m.questionToken ? "?" : "";
|
|
235
|
+
const key = IDENT.test(name) ? name : JSON.stringify(name);
|
|
236
|
+
const type = ts.isMethodSignature(m) ? this.signature(m, scope, depth + 1) : m.type ? this.print(m.type, scope, depth + 1, { nonNull: !!optional }) : "unknown";
|
|
237
|
+
fields.push(`${key}${optional}: ${type}`);
|
|
238
|
+
} else if (ts.isIndexSignatureDeclaration(m)) {
|
|
239
|
+
const key = m.parameters[0]?.type;
|
|
240
|
+
if (!key || key.kind !== ts.SyntaxKind.StringKeyword) unsupported();
|
|
241
|
+
index = `[key: string]: ${this.print(m.type, scope, depth + 1)}`;
|
|
242
|
+
} else if (ts.isCallSignatureDeclaration(m)) {
|
|
243
|
+
calls.push(this.signature(m, scope, depth));
|
|
244
|
+
} else unsupported();
|
|
245
|
+
}
|
|
246
|
+
if (calls.length && !fields.length) return calls[0];
|
|
247
|
+
if (index) fields.push(index);
|
|
248
|
+
for (const c of calls) fields.push(c.replace(" =>", ":"));
|
|
249
|
+
return fields.length ? `{ ${fields.join("; ")} }` : "{}";
|
|
250
|
+
}
|
|
251
|
+
signature(sig, scope, depth) {
|
|
252
|
+
const { ts } = this;
|
|
253
|
+
if (sig.typeParameters?.length || !sig.type) unsupported();
|
|
254
|
+
const params = sig.parameters.map((p, i) => {
|
|
255
|
+
if (p.dotDotDotToken || ts.isIdentifier(p.name) && p.name.text === "this") unsupported();
|
|
256
|
+
const name = ts.isIdentifier(p.name) && IDENT.test(p.name.text) ? p.name.text : `a${i}`;
|
|
257
|
+
const type = p.type ? this.print(p.type, scope, depth + 1) : "unknown";
|
|
258
|
+
return `${name}: ${p.questionToken || p.initializer ? `${type} | undefined` : type}`;
|
|
259
|
+
});
|
|
260
|
+
return `(${params.join(", ")}) => ${this.print(sig.type, scope, depth + 1)}`;
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
function resolveFile(from, spec) {
|
|
264
|
+
const base = resolve(dirname(from), spec);
|
|
265
|
+
const stem = base.replace(/\.[cm]?jsx?$/, "");
|
|
266
|
+
const candidates = [base, ...[".ts", ".tsx", ".mts", ".cts", ".d.ts"].map((ext) => stem + ext), ...["index.ts", "index.tsx", "index.d.ts"].map((f) => join(base, f))];
|
|
267
|
+
const file = candidates.find((f) => /\.[cm]?tsx?$/.test(f) && existsSync(f));
|
|
268
|
+
return file ?? unsupported();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// src/vite/types.ts
|
|
272
|
+
function typesFile(name, props) {
|
|
273
|
+
return `// Props do remote ${JSON.stringify(name)}, geradas pelo vitral. N\xE3o edite.
|
|
274
|
+
export type Props = ${props}
|
|
275
|
+
`;
|
|
276
|
+
}
|
|
277
|
+
async function loadTs(root) {
|
|
278
|
+
const api = (ts) => typeof ts?.createProgram === "function" ? ts : null;
|
|
279
|
+
try {
|
|
280
|
+
const ts = api(createRequire2(join2(root, "package.json"))("typescript"));
|
|
281
|
+
if (ts) return ts;
|
|
282
|
+
} catch {
|
|
283
|
+
}
|
|
284
|
+
try {
|
|
285
|
+
return api((await import("typescript")).default);
|
|
286
|
+
} catch {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
async function extractProps(root, file) {
|
|
291
|
+
if (!/\.[cm]?[jt]sx?$/.test(file)) return null;
|
|
292
|
+
const ts = await loadTs(root);
|
|
293
|
+
if (!ts) return null;
|
|
294
|
+
const oxc = /\.[cm]?tsx?$/.test(file) ? await loadOxc(root) : null;
|
|
295
|
+
const fast = oxc && propsFromOxc(ts, oxc, file);
|
|
296
|
+
if (fast) return fast;
|
|
297
|
+
const configPath = ts.findConfigFile(root, ts.sys.fileExists);
|
|
298
|
+
const base = configPath ? ts.parseJsonConfigFileContent(ts.readConfigFile(configPath, ts.sys.readFile).config, ts.sys, root).options : {};
|
|
299
|
+
const options = {
|
|
300
|
+
target: ts.ScriptTarget.ES2022,
|
|
301
|
+
module: ts.ModuleKind.ESNext,
|
|
302
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
303
|
+
jsx: ts.JsxEmit.Preserve,
|
|
304
|
+
strict: true,
|
|
305
|
+
...base,
|
|
306
|
+
allowJs: true,
|
|
307
|
+
skipLibCheck: true,
|
|
308
|
+
noEmit: true
|
|
309
|
+
};
|
|
310
|
+
const program = ts.createProgram([file], options);
|
|
311
|
+
const checker = program.getTypeChecker();
|
|
312
|
+
const source = program.getSourceFile(file);
|
|
313
|
+
const moduleSymbol = source && checker.getSymbolAtLocation(source);
|
|
314
|
+
let symbol = moduleSymbol && checker.getExportsOfModule(moduleSymbol).find((s) => s.escapedName === "default");
|
|
315
|
+
if (!source || !symbol) return null;
|
|
316
|
+
if (symbol.flags & ts.SymbolFlags.Alias) symbol = checker.getAliasedSymbol(symbol);
|
|
317
|
+
const type = checker.getTypeOfSymbolAtLocation(symbol, source);
|
|
318
|
+
const props = propsOf(checker, type, source);
|
|
319
|
+
return props ? new Printer(ts, checker, program).print(props, 0, true) : "Record<string, unknown>";
|
|
320
|
+
}
|
|
321
|
+
function propsOf(checker, type, at) {
|
|
322
|
+
const param = (t, i) => {
|
|
323
|
+
const p = t.getCallSignatures()[0]?.parameters[i];
|
|
324
|
+
return p && checker.getTypeOfSymbolAtLocation(p, at);
|
|
325
|
+
};
|
|
326
|
+
const mount = type.getProperty("mount");
|
|
327
|
+
if (mount) return param(checker.getTypeOfSymbolAtLocation(mount, at), 1);
|
|
328
|
+
if (type.getCallSignatures().length) return param(type, 0);
|
|
329
|
+
const ctor = type.getConstructSignatures()[0];
|
|
330
|
+
const props = ctor?.getReturnType().getProperty("props");
|
|
331
|
+
return props && checker.getTypeOfSymbolAtLocation(props, at);
|
|
332
|
+
}
|
|
333
|
+
var Printer = class {
|
|
334
|
+
constructor(ts, checker, program) {
|
|
335
|
+
this.ts = ts;
|
|
336
|
+
this.checker = checker;
|
|
337
|
+
this.program = program;
|
|
338
|
+
this.seen = /* @__PURE__ */ new Set();
|
|
339
|
+
}
|
|
340
|
+
/** Declarado na lib padrão (Date, Map…): vale em qualquer projeto pelo nome. */
|
|
341
|
+
fromLib(type) {
|
|
342
|
+
const decl = (type.aliasSymbol ?? type.getSymbol())?.declarations?.[0];
|
|
343
|
+
return !!decl && this.program.isSourceFileDefaultLibrary(decl.getSourceFile());
|
|
344
|
+
}
|
|
345
|
+
fromPackage(type) {
|
|
346
|
+
const decl = (type.aliasSymbol ?? type.getSymbol())?.declarations?.[0];
|
|
347
|
+
return !!decl && /[/\\]node_modules[/\\]/.test(decl.getSourceFile().fileName);
|
|
348
|
+
}
|
|
349
|
+
print(type, depth, top = false) {
|
|
350
|
+
const { ts, checker } = this;
|
|
351
|
+
const f = type.flags;
|
|
352
|
+
if (f & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) return "unknown";
|
|
353
|
+
if (f & (ts.TypeFlags.StringLike | ts.TypeFlags.NumberLike | ts.TypeFlags.BooleanLike | ts.TypeFlags.BigIntLike | ts.TypeFlags.Null | ts.TypeFlags.Undefined | ts.TypeFlags.Void | ts.TypeFlags.Never | ts.TypeFlags.ESSymbolLike)) {
|
|
354
|
+
return checker.typeToString(type);
|
|
355
|
+
}
|
|
356
|
+
if (type.isUnion()) {
|
|
357
|
+
const parts = [...new Set(type.types.map((t) => this.print(t, depth)))];
|
|
358
|
+
if (parts.includes("true") && parts.includes("false")) parts.splice(parts.indexOf("true"), 1, "boolean"), parts.splice(parts.indexOf("false"), 1);
|
|
359
|
+
return parts.join(" | ");
|
|
360
|
+
}
|
|
361
|
+
if (type.isIntersection()) return type.types.map((t) => this.wrap(t, depth)).join(" & ");
|
|
362
|
+
if (depth > 6 || this.seen.has(type)) return "unknown";
|
|
363
|
+
if (checker.isArrayType(type)) return `Array<${this.print(checker.getTypeArguments(type)[0], depth + 1)}>`;
|
|
364
|
+
if (checker.isTupleType(type)) return `[${checker.getTypeArguments(type).map((t) => this.print(t, depth + 1)).join(", ")}]`;
|
|
365
|
+
if (this.fromLib(type)) {
|
|
366
|
+
const args = type.aliasTypeArguments ?? (type.objectFlags & ts.ObjectFlags.Reference ? checker.getTypeArguments(type) : []);
|
|
367
|
+
const name = (type.aliasSymbol ?? type.getSymbol()).name;
|
|
368
|
+
return args.length ? `${name}<${args.map((t) => this.print(t, depth + 1)).join(", ")}>` : checker.typeToString(type);
|
|
369
|
+
}
|
|
370
|
+
if (this.fromPackage(type)) return "unknown";
|
|
371
|
+
this.seen.add(type);
|
|
372
|
+
try {
|
|
373
|
+
const calls = type.getCallSignatures();
|
|
374
|
+
const props = checker.getPropertiesOfType(type).filter((p) => !(top && p.name === "vitral"));
|
|
375
|
+
if (calls.length && !props.length) return this.signature(calls[0], depth);
|
|
376
|
+
const fields = props.map((p) => {
|
|
377
|
+
const optional = p.flags & ts.SymbolFlags.Optional ? "?" : "";
|
|
378
|
+
const key = /^[A-Za-z_$][\w$]*$/.test(p.name) ? p.name : JSON.stringify(p.name);
|
|
379
|
+
const t = checker.getTypeOfSymbol(p);
|
|
380
|
+
return `${key}${optional}: ${this.print(optional ? checker.getNonNullableType(t) : t, depth + 1)}`;
|
|
381
|
+
});
|
|
382
|
+
const index = type.getStringIndexType();
|
|
383
|
+
if (index) fields.push(`[key: string]: ${this.print(index, depth + 1)}`);
|
|
384
|
+
for (const c of calls) fields.push(this.signature(c, depth).replace(" =>", ":"));
|
|
385
|
+
return fields.length ? `{ ${fields.join("; ")} }` : "{}";
|
|
386
|
+
} finally {
|
|
387
|
+
this.seen.delete(type);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
wrap(type, depth) {
|
|
391
|
+
const s = this.print(type, depth);
|
|
392
|
+
return type.isUnion() ? `(${s})` : s;
|
|
393
|
+
}
|
|
394
|
+
signature(sig, depth) {
|
|
395
|
+
const params = sig.parameters.map((p, i) => `${/^[A-Za-z_$][\w$]*$/.test(p.name) ? p.name : `a${i}`}: ${this.print(this.checker.getTypeOfSymbol(p), depth + 1)}`);
|
|
396
|
+
return `(${params.join(", ")}) => ${this.print(sig.getReturnType(), depth + 1)}`;
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
export {
|
|
401
|
+
typesFile,
|
|
402
|
+
loadTs,
|
|
403
|
+
extractProps
|
|
404
|
+
};
|