@tokamaq/build 0.0.0-stage → 0.1.0-dev.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 +21 -0
- package/README.md +346 -2
- package/package.json +71 -4
- package/src/artifact-bundle.ts +86 -0
- package/src/auth-dependencies.ts +65 -0
- package/src/build.ts +73 -0
- package/src/client-surface.ts +179 -0
- package/src/compiler.ts +47 -0
- package/src/component-parts.ts +928 -0
- package/src/contributions.ts +534 -0
- package/src/css.ts +25 -0
- package/src/descriptor.ts +8 -0
- package/src/exports.ts +79 -0
- package/src/hook-targets.ts +46 -0
- package/src/part-props.ts +172 -0
- package/src/plugin-boundary.ts +48 -0
- package/src/ui-build-config.ts +48 -0
- package/src/vite.ts +284 -0
- package/src/window-state.ts +230 -0
- package/src/window-test-config.ts +48 -0
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
import {
|
|
2
|
+
eventNamespace,
|
|
3
|
+
flattenAttributes,
|
|
4
|
+
isSearchAttribute,
|
|
5
|
+
pluginCompilerOptions,
|
|
6
|
+
reservedAttribute,
|
|
7
|
+
type ScreenSearch,
|
|
8
|
+
unnamedTargetWarning,
|
|
9
|
+
} from "@tokamaq/sdk/internal/build";
|
|
10
|
+
import { parsePluginSpecifier } from "@tokamaq/sdk/internal/server-build";
|
|
11
|
+
import { componentParts, problem } from "./component-parts.ts";
|
|
12
|
+
import { brand, exportedInitializer, importModule, inSource, unalias, unwrap } from "./compiler.ts";
|
|
13
|
+
import { clientEntry } from "./descriptor.ts";
|
|
14
|
+
/** Use the compiler's export/alias graph, as backend exports do. Keep TS 5.9.3:
|
|
15
|
+
* 6.0.3 is the newest JS API release; 7.x has no JS API. Upgrading belongs to
|
|
16
|
+
* a separate tooling change, not the plugin wire-format implementation. */
|
|
17
|
+
import { resolve } from "node:path";
|
|
18
|
+
import ts from "typescript";
|
|
19
|
+
|
|
20
|
+
export function uiContributions(root: string, plugin: string, warn: (message: string) => void) {
|
|
21
|
+
const entry = clientEntry(root);
|
|
22
|
+
const configPath = ts.findConfigFile(root, ts.sys.fileExists);
|
|
23
|
+
const configResult = configPath ? ts.readConfigFile(configPath, ts.sys.readFile) : { config: {} };
|
|
24
|
+
if (configResult.error)
|
|
25
|
+
throw new Error(ts.flattenDiagnosticMessageText(configResult.error.messageText, "\n"));
|
|
26
|
+
const config = configResult.config;
|
|
27
|
+
const parsed = ts.parseJsonConfigFileContent(config, ts.sys, root);
|
|
28
|
+
const program = ts.createProgram([entry], {
|
|
29
|
+
...pluginCompilerOptions(root, {
|
|
30
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
31
|
+
jsx: ts.JsxEmit.Preserve,
|
|
32
|
+
jsxImportSource: "@solidjs/web",
|
|
33
|
+
...parsed.options,
|
|
34
|
+
}),
|
|
35
|
+
noEmit: false,
|
|
36
|
+
noEmitOnError: false,
|
|
37
|
+
declaration: false,
|
|
38
|
+
emitDeclarationOnly: false,
|
|
39
|
+
sourceMap: false,
|
|
40
|
+
module: ts.ModuleKind.ESNext,
|
|
41
|
+
});
|
|
42
|
+
const checker = program.getTypeChecker();
|
|
43
|
+
const source = program.getSourceFile(entry);
|
|
44
|
+
if (!source) throw new Error(`Missing plugin UI entry ${entry}`);
|
|
45
|
+
const runtimeNames = emittedExports(program, checker);
|
|
46
|
+
const runtime = runtimeNames(source);
|
|
47
|
+
const module = checker.getSymbolAtLocation(source);
|
|
48
|
+
const contributes: Record<string, { id: string; [key: string]: string | number }[]> = {};
|
|
49
|
+
const owners = new Map<string, ReturnType<typeof componentParts>>();
|
|
50
|
+
const edits = new Map<string, [number, number, string][]>();
|
|
51
|
+
function lookup(owner: string, search: ScreenSearch, action: string) {
|
|
52
|
+
let parts = owners.get(owner);
|
|
53
|
+
if (!parts) {
|
|
54
|
+
const folder = resolve(root, "..", owner);
|
|
55
|
+
const file = program.getSourceFile(clientEntry(folder));
|
|
56
|
+
if (!file) return;
|
|
57
|
+
owners.set(owner, (parts = componentParts(file, program, owner, folder)));
|
|
58
|
+
}
|
|
59
|
+
return parts.resolve(search, action);
|
|
60
|
+
}
|
|
61
|
+
const checked = new Map<ts.SourceFile, readonly ts.Diagnostic[]>();
|
|
62
|
+
function checkAction(call: ts.CallExpression) {
|
|
63
|
+
const file = call.getSourceFile();
|
|
64
|
+
let diagnostics = checked.get(file);
|
|
65
|
+
if (!diagnostics) checked.set(file, (diagnostics = program.getSemanticDiagnostics(file)));
|
|
66
|
+
const diagnostic = diagnostics.find(
|
|
67
|
+
(d) => d.start !== undefined && d.start >= call.getStart(file) && d.start < call.end,
|
|
68
|
+
);
|
|
69
|
+
if (diagnostic) throw problem(call, ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
|
|
70
|
+
}
|
|
71
|
+
const unnamed = new Set<string>();
|
|
72
|
+
const exports = (module ? checker.getExportsOfModule(module) : []).flatMap((exported) => {
|
|
73
|
+
if (!runtime.has(exported.name)) return [];
|
|
74
|
+
const symbol = unalias(checker, exported);
|
|
75
|
+
const declaration = symbol.valueDeclaration;
|
|
76
|
+
if (!declaration || !inSource(root, declaration.getSourceFile().fileName)) return [];
|
|
77
|
+
return [{ exported, declaration, type: checker.getTypeOfSymbolAtLocation(symbol, declaration) }];
|
|
78
|
+
});
|
|
79
|
+
const requested = new Map<ts.CallExpression, Map<boolean, string>>();
|
|
80
|
+
for (const { declaration, type } of exports) {
|
|
81
|
+
const action = brand(type, "componentActionBrand");
|
|
82
|
+
if (!action) continue;
|
|
83
|
+
const kind = checker.getTypeOfSymbolAtLocation(action, declaration);
|
|
84
|
+
const init = exportedInitializer(declaration);
|
|
85
|
+
const reference = init && ts.isCallExpression(init) && unwrap(init.arguments[0]);
|
|
86
|
+
const call = reference && targetSearch(reference, checker);
|
|
87
|
+
if (!call || !kind.isStringLiteral()) continue;
|
|
88
|
+
const actions = requested.get(call) ?? new Map<boolean, string>();
|
|
89
|
+
actions.set(kind.value === "attach", kind.value);
|
|
90
|
+
requested.set(call, actions);
|
|
91
|
+
}
|
|
92
|
+
const searches = new Map<ts.CallExpression, Map<boolean, Record<string, string | number>>>();
|
|
93
|
+
for (const file of program.getSourceFiles()) {
|
|
94
|
+
if (file.isDeclarationFile || !inSource(root, file.fileName)) continue;
|
|
95
|
+
function visit(node: ts.Node) {
|
|
96
|
+
if (ts.isImportDeclaration(node) || ts.isTypeNode(node)) return;
|
|
97
|
+
if (ts.isCallExpression(node)) {
|
|
98
|
+
const owner = findOwner(node.expression, checker);
|
|
99
|
+
if (owner) {
|
|
100
|
+
const search = searchArguments(node);
|
|
101
|
+
const targets = new Map<boolean, Record<string, string | number>>();
|
|
102
|
+
for (const [attach, action] of requested.get(node) ?? ([[false, "placement"]] as const)) {
|
|
103
|
+
const result = lookup(owner, search, action);
|
|
104
|
+
if (!result) throw problem(node, `Cannot read screen parts for plugin "${owner}"`);
|
|
105
|
+
if (!result.element) throw problem(node, result.reason);
|
|
106
|
+
const { target, fingerprint } = result.element;
|
|
107
|
+
targets.set(attach, {
|
|
108
|
+
name: search.name,
|
|
109
|
+
...(search.in !== undefined ? { in: search.in } : {}),
|
|
110
|
+
...(search.nth !== undefined ? { nth: search.nth } : {}),
|
|
111
|
+
// Manifest options are scalar, so ARIA entries keep their JSX names.
|
|
112
|
+
...Object.fromEntries(flattenAttributes(search)),
|
|
113
|
+
owner,
|
|
114
|
+
target,
|
|
115
|
+
...(fingerprint ? { fingerprint } : {}),
|
|
116
|
+
});
|
|
117
|
+
if (result.unnamed && !unnamed.has(target)) {
|
|
118
|
+
unnamed.add(target);
|
|
119
|
+
warn(unnamedTargetWarning(plugin, search, target));
|
|
120
|
+
}
|
|
121
|
+
// Only a search by position depends on telling the siblings apart.
|
|
122
|
+
if (result.ambiguous && search.nth !== undefined)
|
|
123
|
+
warn(
|
|
124
|
+
`Plugin "${plugin}" targets ${target}: ambiguous positional target; ask the owner for a data-part or id`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
searches.set(node, targets);
|
|
128
|
+
const element = targets.get(false);
|
|
129
|
+
const render = targets.get(true);
|
|
130
|
+
const target = element?.target ?? render?.target;
|
|
131
|
+
const addresses = { target, ...(element && render ? { renderTarget: render.target } : {}) };
|
|
132
|
+
const cuts = edits.get(file.fileName) ?? [];
|
|
133
|
+
cuts.push([node.getStart(), node.end, `(${JSON.stringify(addresses)})`]);
|
|
134
|
+
edits.set(file.fileName, cuts);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (
|
|
139
|
+
(ts.isPropertyAccessExpression(node) ||
|
|
140
|
+
ts.isElementAccessExpression(node) ||
|
|
141
|
+
ts.isIdentifier(node)) &&
|
|
142
|
+
findOwner(node, checker)
|
|
143
|
+
)
|
|
144
|
+
throw problem(
|
|
145
|
+
node,
|
|
146
|
+
"Call find directly from its public client import so its arguments can be checked",
|
|
147
|
+
);
|
|
148
|
+
ts.forEachChild(node, visit);
|
|
149
|
+
}
|
|
150
|
+
visit(file);
|
|
151
|
+
}
|
|
152
|
+
for (const { exported, declaration, type } of exports) {
|
|
153
|
+
if (brand(type, "viewBrand")) {
|
|
154
|
+
(contributes.view ??= []).push({ id: exported.name, ...pageOptions(declaration, checker) });
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const action = brand(type, "componentActionBrand");
|
|
158
|
+
if (action) {
|
|
159
|
+
const kind = checker.getTypeOfSymbolAtLocation(action, declaration);
|
|
160
|
+
if (!kind.isStringLiteral()) throw problem(declaration, "Declare a single component action");
|
|
161
|
+
const values = componentActionOptions(declaration, kind.value, checker, plugin, searches, checkAction);
|
|
162
|
+
(contributes["component-action"] ??= []).push({ id: exported.name, ...values });
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return { contributes, edits };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function emittedExports(program: ts.Program, checker: ts.TypeChecker) {
|
|
170
|
+
// Inspect TS's emitted module syntax before bundler dead-code elimination.
|
|
171
|
+
// This excludes type-only stars/aliases throughout re-export chains while
|
|
172
|
+
// preserving declared exports after a top-level throw (never execute code).
|
|
173
|
+
const emittedNames = new Map<ts.SourceFile, Set<string>>();
|
|
174
|
+
function runtimeNames(file: ts.SourceFile): Set<string> {
|
|
175
|
+
const cached = emittedNames.get(file);
|
|
176
|
+
if (cached) return cached;
|
|
177
|
+
const names = new Set<string>();
|
|
178
|
+
emittedNames.set(file, names);
|
|
179
|
+
let js = "";
|
|
180
|
+
program.emit(file, (name, text) => {
|
|
181
|
+
if (/\.[cm]?jsx?$/.test(name)) js = text;
|
|
182
|
+
});
|
|
183
|
+
const emitted = ts.createSourceFile(file.fileName, js, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS);
|
|
184
|
+
function binding(name: ts.BindingName) {
|
|
185
|
+
if (ts.isIdentifier(name)) names.add(name.text);
|
|
186
|
+
else for (const element of name.elements) if (ts.isBindingElement(element)) binding(element.name);
|
|
187
|
+
}
|
|
188
|
+
for (const statement of emitted.statements) {
|
|
189
|
+
if (ts.isExportDeclaration(statement)) addReexports(statement, file, names, runtimeNames, checker);
|
|
190
|
+
else addDeclaration(statement, names, binding);
|
|
191
|
+
}
|
|
192
|
+
return names;
|
|
193
|
+
}
|
|
194
|
+
return runtimeNames;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function pageOptions(declaration: ts.Declaration, checker: ts.TypeChecker): Record<string, string> {
|
|
198
|
+
const init = exportedInitializer(declaration);
|
|
199
|
+
const options = init && ts.isCallExpression(init) ? unwrap(init.arguments[0]) : undefined;
|
|
200
|
+
const fail = (): never => {
|
|
201
|
+
throw problem(options ?? declaration, "Use a literal view route and optional title and icon");
|
|
202
|
+
};
|
|
203
|
+
if (!options || !ts.isObjectLiteralExpression(options)) return fail();
|
|
204
|
+
const values: Record<string, string> = {};
|
|
205
|
+
for (const property of options.properties) {
|
|
206
|
+
if (!ts.isPropertyAssignment(property) && !ts.isShorthandPropertyAssignment(property)) return fail();
|
|
207
|
+
const name = property.name;
|
|
208
|
+
const key = ts.isIdentifier(name) || ts.isStringLiteral(name) ? name.text : fail();
|
|
209
|
+
if (!["route", "title", "icon"].includes(key) || key in values) return fail();
|
|
210
|
+
values[key] = pageOptionValue(property, key, checker, fail);
|
|
211
|
+
}
|
|
212
|
+
if (values.route === undefined) return fail();
|
|
213
|
+
validateRoute(values.route, fail);
|
|
214
|
+
if (values.icon !== undefined) validateIcon(values.icon, fail);
|
|
215
|
+
return values;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function pageOptionValue(
|
|
219
|
+
property: ts.PropertyAssignment | ts.ShorthandPropertyAssignment,
|
|
220
|
+
key: string,
|
|
221
|
+
checker: ts.TypeChecker,
|
|
222
|
+
fail: () => never,
|
|
223
|
+
): string {
|
|
224
|
+
if (key === "route") {
|
|
225
|
+
if (ts.isPropertyAssignment(property) && ts.isStringLiteralLike(property.initializer))
|
|
226
|
+
return property.initializer.text;
|
|
227
|
+
return fail();
|
|
228
|
+
}
|
|
229
|
+
if (ts.isPropertyAssignment(property)) return constantString(property.initializer, checker, fail);
|
|
230
|
+
const value = checker.getShorthandAssignmentValueSymbol(property);
|
|
231
|
+
const type = value && checker.getTypeOfSymbolAtLocation(value, property.name);
|
|
232
|
+
return type?.isStringLiteral() ? type.value : fail();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function constantString(expression: ts.Expression, checker: ts.TypeChecker, fail: () => never): string {
|
|
236
|
+
const value = unwrap(expression)!;
|
|
237
|
+
if (ts.isStringLiteralLike(value)) return value.text;
|
|
238
|
+
const type = checker.getTypeAtLocation(value);
|
|
239
|
+
return type.isStringLiteral() ? type.value : fail();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function validateIcon(icon: string, fail: () => never): void {
|
|
243
|
+
if (!icon || icon.length > 8192 || !/^[MmLlHhVvCcSsQqTtAaZz0-9eE.,\s+-]+$/.test(icon)) fail();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function validateRoute(route: string, fail: () => never): void {
|
|
247
|
+
if (!route.startsWith("/") || route.startsWith("//") || /[\\\x00-\x20\x7f?#]/.test(route)) fail();
|
|
248
|
+
const names = new Set<string>();
|
|
249
|
+
const segments = route.slice(1).split("/");
|
|
250
|
+
for (const [index, part] of segments.entries()) {
|
|
251
|
+
if (!part.startsWith(":") && !part.startsWith("*")) continue;
|
|
252
|
+
const name = part.slice(1);
|
|
253
|
+
if (
|
|
254
|
+
!/^[A-Za-z][A-Za-z0-9_]*$/.test(name) ||
|
|
255
|
+
names.has(name) ||
|
|
256
|
+
(part.startsWith("*") && index !== segments.length - 1)
|
|
257
|
+
)
|
|
258
|
+
fail();
|
|
259
|
+
names.add(name);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function addReexports(
|
|
264
|
+
statement: ts.ExportDeclaration,
|
|
265
|
+
file: ts.SourceFile,
|
|
266
|
+
names: Set<string>,
|
|
267
|
+
runtimeNames: (file: ts.SourceFile) => Set<string>,
|
|
268
|
+
checker: ts.TypeChecker,
|
|
269
|
+
) {
|
|
270
|
+
if (statement.exportClause) {
|
|
271
|
+
if (ts.isNamedExports(statement.exportClause))
|
|
272
|
+
for (const item of statement.exportClause.elements) names.add(item.name.text);
|
|
273
|
+
else names.add(statement.exportClause.name.text);
|
|
274
|
+
} else if (statement.moduleSpecifier && ts.isStringLiteral(statement.moduleSpecifier)) {
|
|
275
|
+
const specifier = statement.moduleSpecifier.text;
|
|
276
|
+
const original = file.statements.find(
|
|
277
|
+
(node): node is ts.ExportDeclaration =>
|
|
278
|
+
ts.isExportDeclaration(node) &&
|
|
279
|
+
!!node.moduleSpecifier &&
|
|
280
|
+
ts.isStringLiteral(node.moduleSpecifier) &&
|
|
281
|
+
node.moduleSpecifier.text === specifier,
|
|
282
|
+
);
|
|
283
|
+
const target = original?.moduleSpecifier && checker.getSymbolAtLocation(original.moduleSpecifier);
|
|
284
|
+
const source = target?.declarations?.find(ts.isSourceFile);
|
|
285
|
+
if (source) for (const name of runtimeNames(source)) if (name !== "default") names.add(name);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function addDeclaration(
|
|
290
|
+
statement: ts.Statement,
|
|
291
|
+
names: Set<string>,
|
|
292
|
+
binding: (name: ts.BindingName) => void,
|
|
293
|
+
) {
|
|
294
|
+
if (ts.isExportAssignment(statement)) names.add("default");
|
|
295
|
+
else if (
|
|
296
|
+
ts.canHaveModifiers(statement) &&
|
|
297
|
+
ts.getModifiers(statement)?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)
|
|
298
|
+
) {
|
|
299
|
+
if (ts.getModifiers(statement)?.some((m) => m.kind === ts.SyntaxKind.DefaultKeyword))
|
|
300
|
+
names.add("default");
|
|
301
|
+
else if (ts.isVariableStatement(statement))
|
|
302
|
+
for (const declaration of statement.declarationList.declarations) binding(declaration.name);
|
|
303
|
+
else if ((ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement)) && statement.name)
|
|
304
|
+
names.add(statement.name.text);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function componentActionOptions(
|
|
309
|
+
declaration: ts.Declaration,
|
|
310
|
+
action: string,
|
|
311
|
+
checker: ts.TypeChecker,
|
|
312
|
+
plugin: string,
|
|
313
|
+
searches: ReadonlyMap<ts.CallExpression, ReadonlyMap<boolean, Record<string, string | number>>>,
|
|
314
|
+
checkAction: (call: ts.CallExpression) => void,
|
|
315
|
+
): Record<string, string | number> {
|
|
316
|
+
const init = exportedInitializer(declaration);
|
|
317
|
+
if (!init || !ts.isCallExpression(init) || !init.arguments[0])
|
|
318
|
+
throw problem(declaration, "Export a component action declared with an action helper");
|
|
319
|
+
const reference = unwrap(init.arguments[0])!;
|
|
320
|
+
const call = targetSearch(reference, checker);
|
|
321
|
+
const found = call && searches.get(call)?.get(action === "attach");
|
|
322
|
+
if (!found || findOwner(call.expression, checker) === plugin)
|
|
323
|
+
throw problem(reference, "Use find from another plugin's public client import");
|
|
324
|
+
if (
|
|
325
|
+
!brand(
|
|
326
|
+
checker.getTypeAtLocation(reference),
|
|
327
|
+
action === "attach" ? "renderFunctionBrand" : "componentPartBrand",
|
|
328
|
+
)
|
|
329
|
+
)
|
|
330
|
+
throw problem(reference, "Build the owning plugin first to generate its find types");
|
|
331
|
+
const options = unwrap(init.arguments[action === "hide" ? 1 : 2]);
|
|
332
|
+
const values: Record<string, string | number> = { action, ...found };
|
|
333
|
+
if (options) {
|
|
334
|
+
if (!ts.isObjectLiteralExpression(options))
|
|
335
|
+
throw problem(options, "Use literal component action options");
|
|
336
|
+
for (const prop of options.properties) {
|
|
337
|
+
if (!(ts.isPropertyAssignment(prop) || ts.isShorthandPropertyAssignment(prop)))
|
|
338
|
+
throw problem(prop, "Use a literal order or a live when accessor");
|
|
339
|
+
if (prop.name.getText() === "when") continue;
|
|
340
|
+
const value = ts.isPropertyAssignment(prop) ? prop.initializer : undefined;
|
|
341
|
+
if (prop.name.getText() === "order" && value && /^-?\d+(?:\.\d+)?$/.test(value.getText())) {
|
|
342
|
+
values.order = Number(value.getText());
|
|
343
|
+
} else throw problem(prop, "Use a literal order or a live when accessor");
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
if (action === "attrs" || action === "layer") {
|
|
347
|
+
const { callable, nativeEvents } = callableAttributes(reference, checker);
|
|
348
|
+
checkAttributeLiterals(init.arguments[1], action, checker, callable, nativeEvents);
|
|
349
|
+
if (callable.size) values.callableAttributes = JSON.stringify([...callable]);
|
|
350
|
+
}
|
|
351
|
+
checkAction(init);
|
|
352
|
+
return values;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function callableAttributes(reference: ts.Expression, checker: ts.TypeChecker) {
|
|
356
|
+
const property = brand(checker.getTypeAtLocation(reference), "elementAttributesBrand");
|
|
357
|
+
const callable = new Set<string>();
|
|
358
|
+
let nativeEvents: Set<string> | undefined;
|
|
359
|
+
if (property) {
|
|
360
|
+
const type = checker.getNonNullableType(checker.getTypeOfSymbolAtLocation(property, reference));
|
|
361
|
+
for (const member of type.isUnion() ? type.types : [type]) {
|
|
362
|
+
const native = !!brand(member, "nativeAttributesBrand");
|
|
363
|
+
if (native) nativeEvents ??= new Set();
|
|
364
|
+
for (const prop of member.getProperties()) {
|
|
365
|
+
const value = checker.getTypeOfSymbolAtLocation(prop, reference);
|
|
366
|
+
if (!(value.isUnion() ? value.types : [value]).some((part) => part.getCallSignatures().length))
|
|
367
|
+
continue;
|
|
368
|
+
if (native) nativeEvents!.add(prop.name.toLowerCase());
|
|
369
|
+
else if (!reservedAttribute(prop.name)) callable.add(prop.name);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return { callable, nativeEvents };
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function checkAttributeLiterals(
|
|
377
|
+
expression: ts.Expression | undefined,
|
|
378
|
+
action: "attrs" | "layer",
|
|
379
|
+
checker: ts.TypeChecker,
|
|
380
|
+
callable: ReadonlySet<string>,
|
|
381
|
+
nativeEvents?: ReadonlySet<string>,
|
|
382
|
+
) {
|
|
383
|
+
let read: ts.Node | undefined = unwrap(expression);
|
|
384
|
+
if (read && ts.isIdentifier(read)) {
|
|
385
|
+
const declaration = checker.getSymbolAtLocation(read)?.valueDeclaration;
|
|
386
|
+
if (declaration && ts.isVariableDeclaration(declaration)) read = unwrap(declaration.initializer);
|
|
387
|
+
else if (declaration && ts.isFunctionDeclaration(declaration)) read = declaration;
|
|
388
|
+
}
|
|
389
|
+
if (!read) return;
|
|
390
|
+
function check(name: ts.Node, original = false) {
|
|
391
|
+
if (ts.isComputedPropertyName(name)) {
|
|
392
|
+
const literal = unwrap(name.expression);
|
|
393
|
+
if (!literal || !ts.isStringLiteralLike(literal)) return;
|
|
394
|
+
name = literal;
|
|
395
|
+
}
|
|
396
|
+
const key = ts.isStringLiteralLike(name) || ts.isIdentifier(name) ? name.text : name.getText();
|
|
397
|
+
if (original && key === "children") return;
|
|
398
|
+
if (
|
|
399
|
+
reservedAttribute(key) ||
|
|
400
|
+
callable.has(key) ||
|
|
401
|
+
(nativeEvents && (eventNamespace(key) || nativeEvents.has(key.toLowerCase())))
|
|
402
|
+
)
|
|
403
|
+
throw problem(name, `Cannot change attribute "${key}" with ${original ? "Original" : "attrs"}`);
|
|
404
|
+
}
|
|
405
|
+
function returned(expression: ts.Expression | undefined) {
|
|
406
|
+
const value = unwrap(expression);
|
|
407
|
+
if (!value) return;
|
|
408
|
+
if (ts.isObjectLiteralExpression(value)) {
|
|
409
|
+
for (const prop of value.properties) {
|
|
410
|
+
if (ts.isSpreadAssignment(prop)) returned(prop.expression);
|
|
411
|
+
else if (prop.name) check(prop.name);
|
|
412
|
+
}
|
|
413
|
+
} else if (ts.isConditionalExpression(value)) {
|
|
414
|
+
returned(value.whenTrue);
|
|
415
|
+
returned(value.whenFalse);
|
|
416
|
+
} else if (
|
|
417
|
+
ts.isBinaryExpression(value) &&
|
|
418
|
+
(value.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken ||
|
|
419
|
+
value.operatorToken.kind === ts.SyntaxKind.BarBarToken ||
|
|
420
|
+
value.operatorToken.kind === ts.SyntaxKind.QuestionQuestionToken)
|
|
421
|
+
) {
|
|
422
|
+
returned(value.left);
|
|
423
|
+
returned(value.right);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (action === "attrs") {
|
|
427
|
+
if (!(ts.isArrowFunction(read) || ts.isFunctionExpression(read) || ts.isFunctionDeclaration(read)))
|
|
428
|
+
return;
|
|
429
|
+
const body = read.body;
|
|
430
|
+
if (!body) return;
|
|
431
|
+
const visit = (node: ts.Node) => {
|
|
432
|
+
if (ts.isReturnStatement(node)) returned(node.expression);
|
|
433
|
+
else if (!ts.isFunctionLike(node)) ts.forEachChild(node, visit);
|
|
434
|
+
};
|
|
435
|
+
if (ts.isBlock(body)) visit(body);
|
|
436
|
+
else returned(body);
|
|
437
|
+
} else {
|
|
438
|
+
const visit = (node: ts.Node) => {
|
|
439
|
+
if (
|
|
440
|
+
(ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) &&
|
|
441
|
+
!/^[a-z][\w-]*$/.test(node.tagName.getText()) &&
|
|
442
|
+
brand(checker.getTypeAtLocation(node.tagName), "originalElementBrand")
|
|
443
|
+
) {
|
|
444
|
+
for (const prop of node.attributes.properties) if (ts.isJsxAttribute(prop)) check(prop.name, true);
|
|
445
|
+
}
|
|
446
|
+
ts.forEachChild(node, visit);
|
|
447
|
+
};
|
|
448
|
+
visit(read);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function targetSearch(target: ts.Expression, checker: ts.TypeChecker) {
|
|
453
|
+
if (ts.isCallExpression(target)) return target;
|
|
454
|
+
const declaration = ts.isIdentifier(target) && checker.getSymbolAtLocation(target)?.valueDeclaration;
|
|
455
|
+
if (
|
|
456
|
+
!declaration ||
|
|
457
|
+
!ts.isVariableDeclaration(declaration) ||
|
|
458
|
+
!(declaration.parent.flags & ts.NodeFlags.Const)
|
|
459
|
+
)
|
|
460
|
+
return;
|
|
461
|
+
const value = unwrap(declaration.initializer);
|
|
462
|
+
if (value && ts.isCallExpression(value)) return value;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function findOwner(expression: ts.Expression, checker: ts.TypeChecker) {
|
|
466
|
+
let path = unwrap(expression)!;
|
|
467
|
+
let member: string | undefined;
|
|
468
|
+
if (ts.isPropertyAccessExpression(path)) {
|
|
469
|
+
member = path.name.text;
|
|
470
|
+
path = path.expression;
|
|
471
|
+
} else if (ts.isElementAccessExpression(path) && ts.isStringLiteralLike(path.argumentExpression)) {
|
|
472
|
+
member = path.argumentExpression.text;
|
|
473
|
+
path = path.expression;
|
|
474
|
+
}
|
|
475
|
+
if (!ts.isIdentifier(path) || (member !== undefined && member !== "find")) return;
|
|
476
|
+
const binding = checker
|
|
477
|
+
.getSymbolAtLocation(path)
|
|
478
|
+
?.declarations?.find((node) => ts.isImportSpecifier(node) || ts.isNamespaceImport(node));
|
|
479
|
+
if (!binding) return;
|
|
480
|
+
if (ts.isImportSpecifier(binding)) {
|
|
481
|
+
if (member !== undefined || (binding.propertyName ?? binding.name).text !== "find") return;
|
|
482
|
+
} else if (member !== "find") return;
|
|
483
|
+
const specifier = importModule(binding);
|
|
484
|
+
const reference = specifier && parsePluginSpecifier(specifier);
|
|
485
|
+
if (reference && reference.side === "client") return reference.id;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function searchArguments(call: ts.CallExpression): ScreenSearch {
|
|
489
|
+
const name = unwrap(call.arguments[0]);
|
|
490
|
+
const options = unwrap(call.arguments[1]);
|
|
491
|
+
const fail = (node: ts.Node = call): never => {
|
|
492
|
+
throw problem(
|
|
493
|
+
node,
|
|
494
|
+
"Use a literal screen element name and literal in, type, href, role or aria filters, or a positive integer nth",
|
|
495
|
+
);
|
|
496
|
+
};
|
|
497
|
+
if (!name || !ts.isStringLiteralLike(name) || call.arguments.length > 2) return fail();
|
|
498
|
+
const search: ScreenSearch = { name: name.text };
|
|
499
|
+
if (options) {
|
|
500
|
+
if (!ts.isObjectLiteralExpression(options)) return fail(options);
|
|
501
|
+
function literalProperty(property: ts.ObjectLiteralElementLike) {
|
|
502
|
+
if (!ts.isPropertyAssignment(property)) return fail(property);
|
|
503
|
+
const { name } = property;
|
|
504
|
+
if (!ts.isIdentifier(name) && !ts.isStringLiteral(name)) return fail(name);
|
|
505
|
+
return [name.text, unwrap(property.initializer)!] as const;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
const seen = new Set<string>();
|
|
509
|
+
for (const property of options.properties) {
|
|
510
|
+
const [key, initializer] = literalProperty(property);
|
|
511
|
+
if (seen.has(key)) return fail(property);
|
|
512
|
+
seen.add(key);
|
|
513
|
+
if (key === "in" && ts.isStringLiteralLike(initializer)) search.in = initializer.text;
|
|
514
|
+
else if (
|
|
515
|
+
key === "nth" &&
|
|
516
|
+
ts.isNumericLiteral(initializer) &&
|
|
517
|
+
Number.isSafeInteger(Number(initializer.text)) &&
|
|
518
|
+
Number(initializer.text) > 0
|
|
519
|
+
)
|
|
520
|
+
search.nth = Number(initializer.text);
|
|
521
|
+
else if (isSearchAttribute(key) && ts.isStringLiteralLike(initializer)) search[key] = initializer.text;
|
|
522
|
+
else if (key === "aria" && ts.isObjectLiteralExpression(initializer)) {
|
|
523
|
+
const values = new Map<string, string>();
|
|
524
|
+
for (const entry of initializer.properties) {
|
|
525
|
+
const [name, value] = literalProperty(entry);
|
|
526
|
+
if (!name || values.has(name) || !ts.isStringLiteralLike(value)) return fail(entry);
|
|
527
|
+
values.set(name, value.text);
|
|
528
|
+
}
|
|
529
|
+
search.aria = Object.fromEntries(values);
|
|
530
|
+
} else return fail(property);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return search;
|
|
534
|
+
}
|
package/src/css.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Build-time Tailwind compilation. */
|
|
2
|
+
import { compile } from "@tailwindcss/node";
|
|
3
|
+
import { Scanner } from "@tailwindcss/oxide";
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import { dirname } from "node:path";
|
|
6
|
+
|
|
7
|
+
export async function compileCss(path: string): Promise<string> {
|
|
8
|
+
const compiler = await compile(await readFile(path, "utf8"), {
|
|
9
|
+
base: dirname(path),
|
|
10
|
+
from: path,
|
|
11
|
+
onDependency() {},
|
|
12
|
+
});
|
|
13
|
+
const automatic =
|
|
14
|
+
compiler.root === "none"
|
|
15
|
+
? []
|
|
16
|
+
: [
|
|
17
|
+
{
|
|
18
|
+
...(compiler.root ?? { base: dirname(path), pattern: "**/*" }),
|
|
19
|
+
negated: false,
|
|
20
|
+
},
|
|
21
|
+
];
|
|
22
|
+
// The maintained scanner owns extraction, ignores and @source semantics.
|
|
23
|
+
const scanner = new Scanner({ sources: [...automatic, ...compiler.sources] });
|
|
24
|
+
return compiler.build(scanner.scan());
|
|
25
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Static package conventions. Never import or evaluate an authored descriptor. */
|
|
2
|
+
import { basename, resolve } from "node:path";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
export { clientEntry, readDescriptor } from "@tokamaq/sdk/internal/build";
|
|
5
|
+
|
|
6
|
+
export function serverModule(root: string, module: string): string {
|
|
7
|
+
return existsSync(resolve(root, "src/server")) ? basename(module) : module;
|
|
8
|
+
}
|
package/src/exports.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Visibility,
|
|
3
|
+
definitionInfo,
|
|
4
|
+
isServerModule,
|
|
5
|
+
pluginCompilerOptions,
|
|
6
|
+
runtimeExportNames,
|
|
7
|
+
serverConfigPath,
|
|
8
|
+
serverDirectory,
|
|
9
|
+
} from "@tokamaq/sdk/internal/build";
|
|
10
|
+
import { serverModule } from "./descriptor.ts";
|
|
11
|
+
/** Resolve public aliases with the compiler so re-exports never execute plugin code. */
|
|
12
|
+
import ts from "typescript";
|
|
13
|
+
import { relative, resolve, sep } from "node:path";
|
|
14
|
+
|
|
15
|
+
export function exportedFunctions(root: string, entry: string | undefined) {
|
|
16
|
+
const empty = { publicFunctions: [] as string[], pluginFunctions: [] as string[] };
|
|
17
|
+
if (entry === undefined) return empty;
|
|
18
|
+
const path = resolve(root, entry);
|
|
19
|
+
const fail = (message: string): never => {
|
|
20
|
+
throw new Error(`Invalid plugin exports ${entry}: ${message}`);
|
|
21
|
+
};
|
|
22
|
+
if (!ts.sys.fileExists(path)) fail("file does not exist");
|
|
23
|
+
const configPath = serverConfigPath(serverDirectory(root), root);
|
|
24
|
+
const config = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
25
|
+
if (config.error) fail(ts.flattenDiagnosticMessageText(config.error.messageText, "\n"));
|
|
26
|
+
const parsed = ts.parseJsonConfigFileContent(config.config, ts.sys, resolve(configPath, ".."));
|
|
27
|
+
const program = ts.createProgram([path, resolve(serverDirectory(root), "_generated/api.ts")], {
|
|
28
|
+
...pluginCompilerOptions(root, parsed.options),
|
|
29
|
+
noEmit: true,
|
|
30
|
+
});
|
|
31
|
+
const diagnostics = ts.getPreEmitDiagnostics(program);
|
|
32
|
+
if (diagnostics.length)
|
|
33
|
+
fail(diagnostics.map((d) => ts.flattenDiagnosticMessageText(d.messageText, "\n")).join("\n"));
|
|
34
|
+
const checker = program.getTypeChecker();
|
|
35
|
+
const source = program.getSourceFile(path)!;
|
|
36
|
+
const module = checker.getSymbolAtLocation(source);
|
|
37
|
+
if (!module) fail("expected a module re-exporting server functions");
|
|
38
|
+
const original = (symbol: ts.Symbol): ts.Symbol =>
|
|
39
|
+
symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
|
|
40
|
+
const names = serverFunctionNames(root, program, checker, original);
|
|
41
|
+
const runtime = runtimeExportNames(checker, module!);
|
|
42
|
+
const result = new Set<string>();
|
|
43
|
+
const pluginOnly = new Set<string>();
|
|
44
|
+
for (const exported of checker.getExportsOfModule(module!)) {
|
|
45
|
+
if (!runtime.has(exported.getName())) continue;
|
|
46
|
+
const target = original(exported);
|
|
47
|
+
const targets = names.get(target);
|
|
48
|
+
if (!targets) continue;
|
|
49
|
+
const info = definitionInfo(checker, target, source);
|
|
50
|
+
if (!info?.visibility || info.visibility === Visibility.Internal)
|
|
51
|
+
fail(`internal function ${exported.getName()} cannot be exported`);
|
|
52
|
+
for (const name of targets!) (info?.visibility === Visibility.PluginOnly ? pluginOnly : result).add(name);
|
|
53
|
+
}
|
|
54
|
+
return { publicFunctions: [...result].sort(), pluginFunctions: [...pluginOnly].sort() };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function serverFunctionNames(
|
|
58
|
+
root: string,
|
|
59
|
+
program: ts.Program,
|
|
60
|
+
checker: ts.TypeChecker,
|
|
61
|
+
original: (symbol: ts.Symbol) => ts.Symbol,
|
|
62
|
+
) {
|
|
63
|
+
const names = new Map<ts.Symbol, string[]>();
|
|
64
|
+
for (const file of program.getSourceFiles()) {
|
|
65
|
+
const local = relative(serverDirectory(root), file.fileName).split(sep).join("/");
|
|
66
|
+
if (!isServerModule(serverDirectory(root), local)) continue;
|
|
67
|
+
const symbol = checker.getSymbolAtLocation(file);
|
|
68
|
+
if (!symbol) continue;
|
|
69
|
+
for (const exported of checker.getExportsOfModule(symbol)) {
|
|
70
|
+
const target = original(exported);
|
|
71
|
+
const definition = definitionInfo(checker, target, file);
|
|
72
|
+
if (!definition || definition.kind === "httpAction") continue;
|
|
73
|
+
const list = names.get(target) ?? [];
|
|
74
|
+
list.push(`${serverModule(root, local.slice(0, -3))}:${exported.getName()}`);
|
|
75
|
+
names.set(target, list);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return names;
|
|
79
|
+
}
|