@tokamaq/build 0.0.0-stage → 0.1.0-dev.1
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,230 @@
|
|
|
1
|
+
import { runtimeExportNames, windowStateModule } from "@tokamaq/sdk/internal/build";
|
|
2
|
+
import { relative } from "node:path";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
import { normalizePath } from "vite";
|
|
5
|
+
import type MagicString from "magic-string";
|
|
6
|
+
import { inSource, type Element, type ElementMount } from "./compiler.ts";
|
|
7
|
+
|
|
8
|
+
type Render = ts.FunctionDeclaration | ts.FunctionExpression | ts.ArrowFunction;
|
|
9
|
+
type ComponentScope = { render: Render; name: string; symbol?: ts.Symbol };
|
|
10
|
+
type Lift = { call: ts.CallExpression; name: string; kind: "state" | "signal"; component?: ComponentScope };
|
|
11
|
+
type Mount = { node: Element; target: ts.Symbol; site: string };
|
|
12
|
+
type ResolveSymbol = (node: ts.Node) => ts.Symbol | undefined;
|
|
13
|
+
type Helper = "createSignal" | "state" | "view" | "component";
|
|
14
|
+
type CallHelper = (call: ts.CallExpression) => Helper | undefined;
|
|
15
|
+
|
|
16
|
+
const scope = "__tokamaqWindowState";
|
|
17
|
+
const instance = "__tokamaqStateInstance";
|
|
18
|
+
|
|
19
|
+
export function windowStateTransform(
|
|
20
|
+
root: string,
|
|
21
|
+
entry: string,
|
|
22
|
+
program: ts.Program,
|
|
23
|
+
erasedNodes: (file: ts.SourceFile) => (node: ts.Node) => boolean,
|
|
24
|
+
) {
|
|
25
|
+
const checker = program.getTypeChecker();
|
|
26
|
+
const original = (symbol?: ts.Symbol) =>
|
|
27
|
+
symbol && symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
|
|
28
|
+
const symbolAt: ResolveSymbol = (node) => original(checker.getSymbolAtLocation(node));
|
|
29
|
+
const files = program
|
|
30
|
+
.getSourceFiles()
|
|
31
|
+
.filter((file) => !file.isDeclarationFile && inSource(root, file.fileName));
|
|
32
|
+
const helpers = collectHelpers(files, checker, symbolAt, original);
|
|
33
|
+
const callHelper: CallHelper = ({ expression }) =>
|
|
34
|
+
helpers.get(symbolAt(ts.isPropertyAccessExpression(expression) ? expression.name : expression)!);
|
|
35
|
+
|
|
36
|
+
const publicModule = program.getSourceFile(entry);
|
|
37
|
+
const exports = publicModule && checker.getSymbolAtLocation(publicModule);
|
|
38
|
+
if (exports) rejectPublicState(checker, exports, original);
|
|
39
|
+
|
|
40
|
+
const compiled = new Map(
|
|
41
|
+
files.map((file) => [
|
|
42
|
+
file,
|
|
43
|
+
collectWindowFile(
|
|
44
|
+
file,
|
|
45
|
+
normalizePath(relative(root, file.fileName)),
|
|
46
|
+
symbolAt,
|
|
47
|
+
callHelper,
|
|
48
|
+
erasedNodes(file),
|
|
49
|
+
),
|
|
50
|
+
]),
|
|
51
|
+
);
|
|
52
|
+
const mounted = new Set<ts.Symbol>();
|
|
53
|
+
for (const { lifts } of compiled.values())
|
|
54
|
+
for (const { component } of lifts) if (component?.symbol) mounted.add(component.symbol);
|
|
55
|
+
|
|
56
|
+
return (file: ts.SourceFile, output: MagicString, isPart: (node: Element) => boolean) => {
|
|
57
|
+
const source = compiled.get(file);
|
|
58
|
+
if (!source) return;
|
|
59
|
+
const sites = source.mounts.filter(({ target }) => mounted.has(target));
|
|
60
|
+
if (!source.lifts.length && !sites.length) return;
|
|
61
|
+
emitState(file, output, source.path, source.lifts);
|
|
62
|
+
const partMounts = new Map<Element, ElementMount>();
|
|
63
|
+
for (const { node, site } of sites) {
|
|
64
|
+
const mount = { prefix: `${scope}.at(${JSON.stringify(site)}, () => `, suffix: ")" };
|
|
65
|
+
if (isPart(node)) {
|
|
66
|
+
partMounts.set(node, mount);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const child = ts.isJsxElement(node.parent) || ts.isJsxFragment(node.parent);
|
|
70
|
+
output.prependLeft(node.getStart(file), `${child ? "{" : ""}${mount.prefix}`);
|
|
71
|
+
output.appendLeft(node.end, `${mount.suffix}${child ? "}" : ""}`);
|
|
72
|
+
}
|
|
73
|
+
return { partMounts };
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function collectHelpers(
|
|
78
|
+
files: ts.SourceFile[],
|
|
79
|
+
checker: ts.TypeChecker,
|
|
80
|
+
symbolAt: ResolveSymbol,
|
|
81
|
+
original: (symbol: ts.Symbol) => ts.Symbol | undefined,
|
|
82
|
+
) {
|
|
83
|
+
const imports = new Map<string, readonly Helper[]>([
|
|
84
|
+
["solid-js", ["createSignal"]],
|
|
85
|
+
["@tokamaq/sdk/client", ["state", "view", "component"]],
|
|
86
|
+
]);
|
|
87
|
+
const helpers = new Map<ts.Symbol, Helper>();
|
|
88
|
+
for (const file of files) {
|
|
89
|
+
for (const statement of file.statements) {
|
|
90
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier)) continue;
|
|
91
|
+
const specifier = statement.moduleSpecifier;
|
|
92
|
+
const names = imports.get(specifier.text);
|
|
93
|
+
const module = names && symbolAt(specifier);
|
|
94
|
+
if (!module) continue;
|
|
95
|
+
for (const exported of checker.getExportsOfModule(module)) {
|
|
96
|
+
const name = names.find((candidate) => candidate === exported.name);
|
|
97
|
+
if (name) helpers.set(original(exported)!, name);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return helpers;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function rejectPublicState(
|
|
105
|
+
checker: ts.TypeChecker,
|
|
106
|
+
exports: ts.Symbol,
|
|
107
|
+
original: (symbol: ts.Symbol) => ts.Symbol | undefined,
|
|
108
|
+
) {
|
|
109
|
+
const publicNames = runtimeExportNames(checker, exports);
|
|
110
|
+
for (const exported of checker.getExportsOfModule(exports)) {
|
|
111
|
+
if (!publicNames.has(exported.name)) continue;
|
|
112
|
+
const symbol = original(exported);
|
|
113
|
+
const declaration = symbol?.valueDeclaration;
|
|
114
|
+
if (!declaration || !(symbol.flags & ts.SymbolFlags.Value)) continue;
|
|
115
|
+
const type = checker.getTypeOfSymbolAtLocation(symbol, declaration);
|
|
116
|
+
if (type.getProperties().some((property) => property.name.startsWith("__@windowStateBrand@")))
|
|
117
|
+
throw new Error(`Window state is private: do not export ${exported.name} from the plugin UI entry`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function collectWindowFile(
|
|
122
|
+
file: ts.SourceFile,
|
|
123
|
+
path: string,
|
|
124
|
+
symbolAt: ResolveSymbol,
|
|
125
|
+
helper: CallHelper,
|
|
126
|
+
erased: (node: ts.Node) => boolean,
|
|
127
|
+
) {
|
|
128
|
+
const lifts: Lift[] = [];
|
|
129
|
+
const mounts: Mount[] = [];
|
|
130
|
+
const ordinals = new Map<string, number>();
|
|
131
|
+
|
|
132
|
+
function visit(node: ts.Node, component?: ComponentScope, enclosing = "<module>") {
|
|
133
|
+
if (erased(node)) return;
|
|
134
|
+
if (ts.isFunctionLike(node)) {
|
|
135
|
+
component = componentScope(node, symbolAt, helper);
|
|
136
|
+
if (component) enclosing = component.name;
|
|
137
|
+
}
|
|
138
|
+
if (ts.isCallExpression(node)) {
|
|
139
|
+
const kind = helper(node);
|
|
140
|
+
const name = bindingName(node, file);
|
|
141
|
+
// Keep ordinary signals in helpers and callbacks on their usual Solid lifecycle.
|
|
142
|
+
// Render-local signals need lifting because unmounting would otherwise discard them.
|
|
143
|
+
if (kind === "state" || (kind === "createSignal" && component && name)) {
|
|
144
|
+
lifts.push({
|
|
145
|
+
call: node,
|
|
146
|
+
name: name ?? String(node.getStart(file)),
|
|
147
|
+
kind: kind === "state" ? "state" : "signal",
|
|
148
|
+
component,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node)) {
|
|
153
|
+
const tag = ts.isJsxElement(node) ? node.openingElement.tagName : node.tagName;
|
|
154
|
+
const target = symbolAt(tag);
|
|
155
|
+
if (target) {
|
|
156
|
+
// Use a per-tag ordinal within the render so sibling sites differ and
|
|
157
|
+
// unrelated edits or line shifts don't change their identity.
|
|
158
|
+
const name = `${enclosing}:${tag.getText(file)}`;
|
|
159
|
+
const n = (ordinals.get(name) ?? 0) + 1;
|
|
160
|
+
ordinals.set(name, n);
|
|
161
|
+
mounts.push({ node, target, site: `${path}:${name}:${n}` });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
ts.forEachChild(node, (child) => visit(child, component, enclosing));
|
|
165
|
+
}
|
|
166
|
+
visit(file);
|
|
167
|
+
return { path, lifts, mounts };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function componentScope(
|
|
171
|
+
render: ts.SignatureDeclaration,
|
|
172
|
+
symbolAt: ResolveSymbol,
|
|
173
|
+
helper: CallHelper,
|
|
174
|
+
): ComponentScope | undefined {
|
|
175
|
+
if (!(ts.isFunctionDeclaration(render) || ts.isArrowFunction(render) || ts.isFunctionExpression(render)))
|
|
176
|
+
return;
|
|
177
|
+
if (!render.body || !ts.isBlock(render.body)) return;
|
|
178
|
+
const parent = render.parent;
|
|
179
|
+
const declared =
|
|
180
|
+
ts.isVariableDeclaration(parent) && ts.isIdentifier(parent.name) ? parent.name : render.name;
|
|
181
|
+
// Solid's JSX treats capitalized names as components. Use the same convention
|
|
182
|
+
// to recognize plain renders without requiring them to use an SDK wrapper.
|
|
183
|
+
if (declared && /^[A-Z]/.test(declared.text))
|
|
184
|
+
return { render, name: declared.text, symbol: symbolAt(declared) };
|
|
185
|
+
if (!ts.isCallExpression(parent)) return;
|
|
186
|
+
// TKM-210's component render uses the same mount identity as a plain Solid render.
|
|
187
|
+
const kind = helper(parent);
|
|
188
|
+
const outer = parent.parent;
|
|
189
|
+
if (
|
|
190
|
+
(kind === "view" || kind === "component") &&
|
|
191
|
+
ts.isVariableDeclaration(outer) &&
|
|
192
|
+
ts.isIdentifier(outer.name)
|
|
193
|
+
)
|
|
194
|
+
return { render, name: outer.name.text, symbol: symbolAt(outer.name) };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function bindingName(call: ts.CallExpression, file: ts.SourceFile): string | undefined {
|
|
198
|
+
if (!ts.isVariableDeclaration(call.parent)) return;
|
|
199
|
+
const binding = call.parent.name;
|
|
200
|
+
if (ts.isIdentifier(binding)) return binding.text;
|
|
201
|
+
if (ts.isArrayBindingPattern(binding))
|
|
202
|
+
return binding.elements.find(ts.isBindingElement)?.name.getText(file);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function emitState(file: ts.SourceFile, output: MagicString, path: string, lifts: Lift[]) {
|
|
206
|
+
const initialized = new Set<ComponentScope>();
|
|
207
|
+
for (const { call, name, kind, component } of lifts) {
|
|
208
|
+
if (component && !initialized.has(component)) {
|
|
209
|
+
initialized.add(component);
|
|
210
|
+
const { render } = component;
|
|
211
|
+
const props = render.parameters[0]?.name;
|
|
212
|
+
const key = props && ts.isIdentifier(props) ? `, ${props.text}?.key` : "";
|
|
213
|
+
output.appendLeft(
|
|
214
|
+
render.body!.getStart(file) + 1,
|
|
215
|
+
`\nconst ${instance} = ${scope}.instance(${JSON.stringify(`${path}:${component.name}`)}${key});\n`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
const key = component ? `${instance} + ${JSON.stringify(":" + name)}` : JSON.stringify(`${path}:${name}`);
|
|
219
|
+
if (kind === "signal") {
|
|
220
|
+
output.prependLeft(call.getStart(file), `${scope}.signal(${key}, () => `);
|
|
221
|
+
output.appendLeft(call.end, ")");
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
const initial = call.arguments[0];
|
|
225
|
+
if (!initial) throw new Error(`state requires an initial value: ${file.fileName}`);
|
|
226
|
+
output.overwrite(call.expression.getStart(file), call.expression.end, `${scope}.state`);
|
|
227
|
+
output.appendLeft(initial.getStart(file), `${key}, `);
|
|
228
|
+
}
|
|
229
|
+
output.prepend(`import ${scope} from ${JSON.stringify(windowStateModule)};\n`);
|
|
230
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { windowStateModule } from "@tokamaq/sdk/internal/build";
|
|
2
|
+
import solid from "@solidjs/vite-plugin";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { dirname, resolve } from "node:path";
|
|
5
|
+
import type { AliasOptions, PluginOption } from "vite";
|
|
6
|
+
import { clientSurface } from "./client-surface.ts";
|
|
7
|
+
|
|
8
|
+
export function windowTestConfig(
|
|
9
|
+
root: string,
|
|
10
|
+
id: string,
|
|
11
|
+
alias: AliasOptions = [],
|
|
12
|
+
plugins: PluginOption[] = [],
|
|
13
|
+
) {
|
|
14
|
+
const require = createRequire(resolve(root, "package.json"));
|
|
15
|
+
const extras = Array.isArray(alias)
|
|
16
|
+
? alias
|
|
17
|
+
: Object.entries(alias).map(([find, replacement]) => ({ find, replacement }));
|
|
18
|
+
return {
|
|
19
|
+
root,
|
|
20
|
+
plugins: [
|
|
21
|
+
...plugins,
|
|
22
|
+
clientSurface(
|
|
23
|
+
() => root,
|
|
24
|
+
() => id,
|
|
25
|
+
),
|
|
26
|
+
solid({ refresh: false }),
|
|
27
|
+
],
|
|
28
|
+
resolve: {
|
|
29
|
+
alias: [
|
|
30
|
+
{
|
|
31
|
+
find: /^solid-js$/,
|
|
32
|
+
replacement: resolve(dirname(require.resolve("solid-js/package.json")), "dist/solid.dev.js"),
|
|
33
|
+
},
|
|
34
|
+
{ find: windowStateModule, replacement: resolve(root, "tests/window-state.ts") },
|
|
35
|
+
...extras,
|
|
36
|
+
],
|
|
37
|
+
dedupe: ["solid-js", "@solidjs/web", "@solidjs/signals", "@ui-sdk/core"],
|
|
38
|
+
},
|
|
39
|
+
optimizeDeps: { exclude: ["@ui-sdk/core"] },
|
|
40
|
+
test: {
|
|
41
|
+
environment: "jsdom",
|
|
42
|
+
setupFiles: [require.resolve("@tokamaq/ui/test-setup")],
|
|
43
|
+
css: false,
|
|
44
|
+
testTimeout: 60000,
|
|
45
|
+
include: ["tests/**/*.test.{ts,tsx}"],
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|