@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,179 @@
|
|
|
1
|
+
import { pluginCompilerOptions } from "@tokamaq/sdk/internal/build";
|
|
2
|
+
import { inSource } from "./compiler.ts";
|
|
3
|
+
import { realpathSync } from "node:fs";
|
|
4
|
+
import { componentParts } from "./component-parts.ts";
|
|
5
|
+
import { clientEntry } from "./descriptor.ts";
|
|
6
|
+
/** Public client declarations come from the entry's symbol graph, never a
|
|
7
|
+
* runtime scan. This also intercepts calls inside the declaring plugin. */
|
|
8
|
+
import ts from "typescript";
|
|
9
|
+
import { normalizePath } from "vite";
|
|
10
|
+
import MagicString from "magic-string";
|
|
11
|
+
import type { Plugin } from "vite";
|
|
12
|
+
import { windowStateTransform } from "./window-state.ts";
|
|
13
|
+
|
|
14
|
+
type SourceEdit = [start: number, end: number, replacement: string];
|
|
15
|
+
|
|
16
|
+
function erasedNodes(source: ts.SourceFile, cuts: SourceEdit[]) {
|
|
17
|
+
return (node: ts.Node) => cuts.some(([start, end]) => node.getStart(source) >= start && node.end <= end);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function clientSurface(
|
|
21
|
+
root: () => string,
|
|
22
|
+
pluginId: () => string,
|
|
23
|
+
placement: (id: string) => SourceEdit[] = () => [],
|
|
24
|
+
catalog?: string,
|
|
25
|
+
): Plugin[] {
|
|
26
|
+
let directory: string;
|
|
27
|
+
let program: ts.Program;
|
|
28
|
+
let parts: ReturnType<typeof componentParts>;
|
|
29
|
+
let windowState: ReturnType<typeof windowStateTransform>;
|
|
30
|
+
const surface: Plugin = {
|
|
31
|
+
name: "tokamaq-client-surface",
|
|
32
|
+
enforce: "pre",
|
|
33
|
+
generateBundle: {
|
|
34
|
+
order: "post",
|
|
35
|
+
handler() {
|
|
36
|
+
parts?.publish((message) => this.warn(message), catalog);
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
transform(code, id) {
|
|
40
|
+
id = normalizePath(id);
|
|
41
|
+
// Vite uses real paths for module IDs, even when the plugin root is a symlink.
|
|
42
|
+
directory ??= normalizePath(realpathSync(root()));
|
|
43
|
+
if (!inSource(directory, id) || !/\.tsx?$/.test(id)) return;
|
|
44
|
+
if (!program) {
|
|
45
|
+
const entry = clientEntry(directory);
|
|
46
|
+
const configPath = ts.findConfigFile(directory, ts.sys.fileExists);
|
|
47
|
+
const config = configPath ? ts.readConfigFile(configPath, ts.sys.readFile).config : {};
|
|
48
|
+
const parsed = ts.parseJsonConfigFileContent(config, ts.sys, directory);
|
|
49
|
+
program = ts.createProgram(
|
|
50
|
+
[entry],
|
|
51
|
+
pluginCompilerOptions(directory, {
|
|
52
|
+
// Resolve package exports and read TSX even when the plugin has no tsconfig.
|
|
53
|
+
module: ts.ModuleKind.ESNext,
|
|
54
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
55
|
+
jsx: ts.JsxEmit.Preserve,
|
|
56
|
+
jsxImportSource: "@solidjs/web",
|
|
57
|
+
...parsed.options,
|
|
58
|
+
}),
|
|
59
|
+
);
|
|
60
|
+
// Both transforms share source coordinates. Window state only sees
|
|
61
|
+
// code retained in the window, so it cannot edit an erased handler.
|
|
62
|
+
windowState = windowStateTransform(directory, entry, program, (file) =>
|
|
63
|
+
erasedNodes(file, placement(normalizePath(file.fileName))),
|
|
64
|
+
);
|
|
65
|
+
parts = componentParts(program.getSourceFile(entry)!, program, pluginId(), directory);
|
|
66
|
+
}
|
|
67
|
+
const source =
|
|
68
|
+
program.getSourceFile(id) ??
|
|
69
|
+
ts.createSourceFile(
|
|
70
|
+
id,
|
|
71
|
+
code,
|
|
72
|
+
ts.ScriptTarget.Latest,
|
|
73
|
+
true,
|
|
74
|
+
id.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
|
|
75
|
+
);
|
|
76
|
+
const output = new MagicString(code);
|
|
77
|
+
const cuts = placement(id);
|
|
78
|
+
for (const [start, end, replacement] of cuts) output.overwrite(start, end, replacement);
|
|
79
|
+
// Both transforms use the original offsets. A part mounts its original
|
|
80
|
+
// tag lazily, so attach the state site to that tag inside its render callback.
|
|
81
|
+
const state = windowState(source, output, parts.has);
|
|
82
|
+
if (
|
|
83
|
+
parts.transform(source, output, state?.partMounts, erasedNodes(source, cuts)) ||
|
|
84
|
+
state ||
|
|
85
|
+
cuts.length
|
|
86
|
+
)
|
|
87
|
+
return {
|
|
88
|
+
code: output.toString(),
|
|
89
|
+
map: output.generateMap({ source: id, includeContent: cuts.length === 0, hires: true }),
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
return [surface, compiledParts()];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function compiledParts(): Plugin {
|
|
97
|
+
return {
|
|
98
|
+
name: "tokamaq-compiled-parts",
|
|
99
|
+
enforce: "post",
|
|
100
|
+
transform(code, id) {
|
|
101
|
+
if (!code.includes("__tokamaqScalarText(") && !code.includes("__tokamaqComponent(")) return;
|
|
102
|
+
// Solid already located the parent in the cloned template. Change the
|
|
103
|
+
// generated scalar insert so its effect owns a movable Text node.
|
|
104
|
+
const source = ts.createSourceFile(id, code, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS);
|
|
105
|
+
const inserts = new Set<string>();
|
|
106
|
+
const components = new Set<string>();
|
|
107
|
+
for (const statement of source.statements) {
|
|
108
|
+
if (
|
|
109
|
+
!ts.isImportDeclaration(statement) ||
|
|
110
|
+
statement.moduleSpecifier.getText(source).slice(1, -1) !== "@solidjs/web"
|
|
111
|
+
)
|
|
112
|
+
continue;
|
|
113
|
+
const imports = statement.importClause?.namedBindings;
|
|
114
|
+
if (imports && ts.isNamedImports(imports))
|
|
115
|
+
for (const binding of imports.elements)
|
|
116
|
+
switch ((binding.propertyName ?? binding.name).text) {
|
|
117
|
+
case "insert":
|
|
118
|
+
inserts.add(binding.name.text);
|
|
119
|
+
break;
|
|
120
|
+
case "createComponent":
|
|
121
|
+
components.add(binding.name.text);
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const output = new MagicString(code);
|
|
126
|
+
let uncompiled = false;
|
|
127
|
+
function visit(node: ts.Node) {
|
|
128
|
+
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
|
|
129
|
+
const name = node.expression.text;
|
|
130
|
+
if (name === "__tokamaqScalarText") uncompiled = true;
|
|
131
|
+
if (name === "__tokamaqComponent") {
|
|
132
|
+
const [call] = node.arguments;
|
|
133
|
+
if (
|
|
134
|
+
!call ||
|
|
135
|
+
!ts.isCallExpression(call) ||
|
|
136
|
+
!ts.isIdentifier(call.expression) ||
|
|
137
|
+
!components.has(call.expression.text)
|
|
138
|
+
)
|
|
139
|
+
throw new Error(`Solid did not compile a component part in ${id}`);
|
|
140
|
+
output.overwrite(node.getStart(source), call.expression.end, "__tokamaqContents.c");
|
|
141
|
+
output.remove(call.end, node.end);
|
|
142
|
+
ts.forEachChild(call, visit);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const body = inserts.has(name) && scalarTextBinding(node.arguments[1]);
|
|
146
|
+
if (body) {
|
|
147
|
+
const value = body.arguments[0]!;
|
|
148
|
+
output.overwrite(node.expression.getStart(source), node.expression.end, "__tokamaqText");
|
|
149
|
+
output.overwrite(body.getStart(source), value.getStart(source), "(");
|
|
150
|
+
output.overwrite(value.end, body.end, ")");
|
|
151
|
+
visit(value);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
ts.forEachChild(node, visit);
|
|
156
|
+
}
|
|
157
|
+
visit(source);
|
|
158
|
+
if (uncompiled) throw new Error(`Solid did not compile a native text binding in ${id}`);
|
|
159
|
+
if (!output.hasChanged()) return;
|
|
160
|
+
if (code.includes("__tokamaqScalarText("))
|
|
161
|
+
output.prepend('import { __renderNativeText as __tokamaqText } from "@tokamaq/sdk/client";\n');
|
|
162
|
+
return { code: output.toString(), map: output.generateMap({ source: id, hires: true }) };
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function scalarTextBinding(read: ts.Expression | undefined) {
|
|
168
|
+
if (!read || !ts.isArrowFunction(read)) return;
|
|
169
|
+
const first = ts.isBlock(read.body) && read.body.statements.length === 1 && read.body.statements[0];
|
|
170
|
+
let body = ts.isBlock(read.body) ? first && ts.isReturnStatement(first) && first.expression : read.body;
|
|
171
|
+
while (body && ts.isParenthesizedExpression(body)) body = body.expression;
|
|
172
|
+
if (
|
|
173
|
+
body &&
|
|
174
|
+
ts.isCallExpression(body) &&
|
|
175
|
+
ts.isIdentifier(body.expression) &&
|
|
176
|
+
body.expression.text === "__tokamaqScalarText"
|
|
177
|
+
)
|
|
178
|
+
return body;
|
|
179
|
+
}
|
package/src/compiler.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { isAbsolute, relative, resolve, sep } from "node:path";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
|
|
4
|
+
export function inSource(root: string, file: string) {
|
|
5
|
+
const local = relative(resolve(root, "src"), file);
|
|
6
|
+
return (
|
|
7
|
+
isAbsolute(file) && local !== "" && local !== ".." && !isAbsolute(local) && !local.startsWith(".." + sep)
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type Element = ts.JsxElement | ts.JsxSelfClosingElement;
|
|
12
|
+
export type ElementMount = { prefix: string; suffix: string };
|
|
13
|
+
|
|
14
|
+
export function unalias(checker: ts.TypeChecker, symbol: ts.Symbol) {
|
|
15
|
+
return symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function brand(type: ts.Type, name: string) {
|
|
19
|
+
return type.getProperties().find((p) => p.name.startsWith(`__@${name}@`));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function unwrap(expression: ts.Expression | undefined): ts.Expression | undefined {
|
|
23
|
+
while (
|
|
24
|
+
expression &&
|
|
25
|
+
(ts.isAsExpression(expression) ||
|
|
26
|
+
ts.isSatisfiesExpression(expression) ||
|
|
27
|
+
ts.isParenthesizedExpression(expression))
|
|
28
|
+
)
|
|
29
|
+
expression = expression.expression;
|
|
30
|
+
return expression;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function exportedInitializer(declaration: ts.Declaration) {
|
|
34
|
+
return unwrap(
|
|
35
|
+
ts.isVariableDeclaration(declaration)
|
|
36
|
+
? declaration.initializer
|
|
37
|
+
: ts.isExportAssignment(declaration)
|
|
38
|
+
? declaration.expression
|
|
39
|
+
: undefined,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function importModule(node: ts.Node | undefined) {
|
|
44
|
+
while (node && !ts.isImportDeclaration(node)) node = node.parent;
|
|
45
|
+
const specifier = node?.moduleSpecifier;
|
|
46
|
+
return specifier && ts.isStringLiteral(specifier) ? specifier.text : undefined;
|
|
47
|
+
}
|