@yannosay/sinth 1.11.6 → 1.12.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/bin/sinth.js +1 -4
- package/dist/browser/compile.js +62 -0
- package/dist/cli.js +469 -0
- package/dist/core/builtins.js +117 -0
- package/dist/core/cli-compiler.js +553 -0
- package/dist/core/compiler.js +1344 -0
- package/dist/core/expr.js +122 -0
- package/dist/core/head-builder.js +130 -0
- package/dist/core/lexer.js +414 -0
- package/dist/core/parser.js +1672 -0
- package/dist/core/runtime/custom-element.js +81 -0
- package/dist/core/runtime/functions.js +99 -0
- package/{src/core/runtime/helpers.ts → dist/core/runtime/helpers.js} +24 -34
- package/{src/core/runtime/render.ts → dist/core/runtime/render.js} +48 -63
- package/dist/core/style-processor.js +318 -0
- package/dist/core/types.js +93 -0
- package/dist/resolver.js +175 -0
- package/dist/server.js +168 -0
- package/dist/update-check.js +101 -0
- package/dist/utils.js +73 -0
- package/package.json +12 -8
- package/src/browser/compile.ts +0 -72
- package/src/cli.ts +0 -339
- package/src/core/builtins.ts +0 -117
- package/src/core/cli-compiler.ts +0 -514
- package/src/core/compiler.ts +0 -1351
- package/src/core/expr.ts +0 -113
- package/src/core/head-builder.ts +0 -86
- package/src/core/lexer.ts +0 -276
- package/src/core/parser.ts +0 -1533
- package/src/core/runtime/custom-element.ts +0 -84
- package/src/core/runtime/functions.ts +0 -87
- package/src/core/style-processor.ts +0 -299
- package/src/core/types.ts +0 -197
- package/src/resolver.ts +0 -150
- package/src/server.ts +0 -129
- package/src/utils.ts +0 -77
- package/tsconfig.json +0 -27
package/src/core/cli-compiler.ts
DELETED
|
@@ -1,514 +0,0 @@
|
|
|
1
|
-
import * as path from "path";
|
|
2
|
-
import * as fs from "fs";
|
|
3
|
-
import { Loc, Literal, Expression, Child, Attr, CompUse, IfBlock, ForLoop, RemoveStmt, ReturnStmt, StyleBlock, CompDef, ParamDecl, VarDeclaration, SinthFile, CompileCtx, MixedBlockEntry, SinthError, SinthWarning, TT, AssignStmt, MetaEntry } from "./types.ts";
|
|
4
|
-
import { Parser } from "./parser.ts";
|
|
5
|
-
import { fnv1a, camelToKebab, esc, escAttr, litToString, tagNameToPascal, interpolateAttr, renderText } from "../utils.ts";
|
|
6
|
-
import { compileExprToJS, compileIfToJS, bodyToJS } from "./expr.ts";
|
|
7
|
-
import { FunctionDef } from "./types.ts";
|
|
8
|
-
import { parseFile, resolveImports, ResolverConfig, ResolvedImports } from "../resolver.ts";
|
|
9
|
-
import { compileFunctionDef } from "./runtime/functions.ts";
|
|
10
|
-
import { generateHelpers } from "./runtime/helpers.ts";
|
|
11
|
-
import { buildRenderBody } from "./runtime/render.ts";
|
|
12
|
-
import { BUILTIN_MAP, VOID_TAGS, BuiltinInfo } from "./builtins.ts";
|
|
13
|
-
import { processStyleBlock } from "./style-processor.ts";
|
|
14
|
-
import { buildHeadData, renderHead, HeadData } from "./head-builder.ts";
|
|
15
|
-
import { compileCustomElement } from "./runtime/custom-element.ts";
|
|
16
|
-
import { eventAttrName, INLINE_STYLE_PROPS, resolveBuiltinTag, registerExpr, renderAttr, renderChild, renderIfBlock, renderCompUse, expandUserComp, collectScripts, extractFunctionNames, buildRuntime } from "./compiler.ts";
|
|
17
|
-
|
|
18
|
-
export interface CompileOptions {
|
|
19
|
-
projectRoot: string;
|
|
20
|
-
outDir: string;
|
|
21
|
-
libraryPaths: string[];
|
|
22
|
-
minify: boolean;
|
|
23
|
-
checkOnly: boolean;
|
|
24
|
-
sharedRuntime: boolean;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function compileFile(filePath: string, opts: CompileOptions): { html: string; shared?: string } | null {
|
|
28
|
-
const absPath = path.resolve(filePath);
|
|
29
|
-
const file = parseFile(absPath);
|
|
30
|
-
const cfg: ResolverConfig = { projectRoot: opts.projectRoot, libraryPaths: opts.libraryPaths };
|
|
31
|
-
const { allDefs, customEls, cssLinks, jsLinks } = resolveImports(file, cfg);
|
|
32
|
-
const hash = fnv1a(absPath);
|
|
33
|
-
const allVarDecls: VarDeclaration[] = file.varDecls;
|
|
34
|
-
const functionDefs: FunctionDef[] = file.functions;
|
|
35
|
-
|
|
36
|
-
const fnLocalVarNames = new Set<string>();
|
|
37
|
-
const collectFnVars = (child: Child): void => {
|
|
38
|
-
if (child.kind === "var") {
|
|
39
|
-
fnLocalVarNames.add((child as VarDeclaration).name);
|
|
40
|
-
} else if (child.kind === "if") {
|
|
41
|
-
(child as IfBlock).body.forEach(collectFnVars);
|
|
42
|
-
(child as IfBlock).elseBody?.forEach(collectFnVars);
|
|
43
|
-
} else if (child.kind === "for") {
|
|
44
|
-
(child as ForLoop).body.forEach(collectFnVars);
|
|
45
|
-
} else if (child.kind === "use") {
|
|
46
|
-
(child as CompUse).children.forEach(collectFnVars);
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
for (const fn of file.functions) {
|
|
50
|
-
fn.body.forEach(collectFnVars);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const pageVarDecls = allVarDecls.filter(v => !fnLocalVarNames.has(v.name));
|
|
54
|
-
const declaredVars = new Set(pageVarDecls.map(v => v.name));
|
|
55
|
-
const declaredFuncs = new Set(file.functions.map(f => f.name));
|
|
56
|
-
|
|
57
|
-
function collectExprVars(expr: Expression | undefined, vars: Set<string>): void {
|
|
58
|
-
if (!expr) return;
|
|
59
|
-
if (expr.kind === "variable" && expr.name && !declaredFuncs.has(expr.name)) vars.add(expr.name);
|
|
60
|
-
if (expr.kind === "assign" && expr.target) vars.add(expr.target);
|
|
61
|
-
if (expr.left) collectExprVars(expr.left, vars);
|
|
62
|
-
if (expr.right) collectExprVars(expr.right, vars);
|
|
63
|
-
if (expr.operand) collectExprVars(expr.operand, vars);
|
|
64
|
-
if (expr.object) collectExprVars(expr.object, vars);
|
|
65
|
-
if (expr.key) collectExprVars(expr.key, vars);
|
|
66
|
-
if (expr.args) expr.args.forEach(a => collectExprVars(a, vars));
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function collectChildVars(child: Child, vars: Set<string>): void {
|
|
70
|
-
if (child.kind === "expr" || child.kind === "assign_stmt" || child.kind === "return") {
|
|
71
|
-
collectExprVars(child.expression, vars);
|
|
72
|
-
}
|
|
73
|
-
if (child.kind === "if") {
|
|
74
|
-
collectExprVars((child as IfBlock).condition, vars);
|
|
75
|
-
(child as IfBlock).body.forEach(c => collectChildVars(c, vars));
|
|
76
|
-
(child as IfBlock).elseBody?.forEach(c => collectChildVars(c, vars));
|
|
77
|
-
}
|
|
78
|
-
if (child.kind === "for") {
|
|
79
|
-
(child as ForLoop).body.forEach(c => collectChildVars(c, vars));
|
|
80
|
-
}
|
|
81
|
-
if (child.kind === "use") {
|
|
82
|
-
(child as CompUse).attrs.forEach(a => {
|
|
83
|
-
if (a.name === "id") return;
|
|
84
|
-
if (a.value?.kind === "str") {
|
|
85
|
-
const v = a.value.value;
|
|
86
|
-
if (v.startsWith("__EXPR__") || v.startsWith("__MULTI_EXPR__")) {
|
|
87
|
-
try {
|
|
88
|
-
const exprs: Expression[] = v.startsWith("__MULTI_EXPR__")
|
|
89
|
-
? JSON.parse(v.substring("__MULTI_EXPR__".length))
|
|
90
|
-
: [JSON.parse(v.substring(8))];
|
|
91
|
-
exprs.forEach(e => collectExprVars(e, vars));
|
|
92
|
-
} catch {}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
(child as CompUse).children.forEach(c => collectChildVars(c, vars));
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
const pageVars = new Set<string>();
|
|
100
|
-
file.uses.forEach(u => collectChildVars(u, pageVars));
|
|
101
|
-
|
|
102
|
-
const pageLoopVars = new Set<string>();
|
|
103
|
-
const gatherLoopVars = (child: Child): void => {
|
|
104
|
-
if (child.kind === "for") {
|
|
105
|
-
const fl = child as ForLoop;
|
|
106
|
-
if (fl.itemVar) pageLoopVars.add(fl.itemVar);
|
|
107
|
-
if (fl.keyVar) pageLoopVars.add(fl.keyVar);
|
|
108
|
-
if (fl.indexVar) pageLoopVars.add(fl.indexVar);
|
|
109
|
-
fl.body.forEach(gatherLoopVars);
|
|
110
|
-
} else if (child.kind === "if") {
|
|
111
|
-
(child as IfBlock).body.forEach(gatherLoopVars);
|
|
112
|
-
(child as IfBlock).elseBody?.forEach(gatherLoopVars);
|
|
113
|
-
} else if (child.kind === "use") {
|
|
114
|
-
(child as CompUse).children.forEach(gatherLoopVars);
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
file.uses.forEach(gatherLoopVars);
|
|
118
|
-
|
|
119
|
-
for (const v of pageVars) {
|
|
120
|
-
if (pageLoopVars.has(v)) continue;
|
|
121
|
-
const rootVar = v.split('.')[0];
|
|
122
|
-
if (declaredVars.has(rootVar) || fnLocalVarNames.has(rootVar)) continue;
|
|
123
|
-
let foundInFn = false;
|
|
124
|
-
for (const fn of file.functions) {
|
|
125
|
-
if (fn.params.some(p => p.name === rootVar)) { foundInFn = true; break; }
|
|
126
|
-
const fnLoopVars = new Set<string>();
|
|
127
|
-
const gatherFnLoopVars = (child: Child): void => {
|
|
128
|
-
if (child.kind === "for") {
|
|
129
|
-
const fl = child as ForLoop;
|
|
130
|
-
if (fl.itemVar) fnLoopVars.add(fl.itemVar);
|
|
131
|
-
if (fl.keyVar) fnLoopVars.add(fl.keyVar);
|
|
132
|
-
if (fl.indexVar) fnLoopVars.add(fl.indexVar);
|
|
133
|
-
fl.body.forEach(gatherFnLoopVars);
|
|
134
|
-
} else if (child.kind === "if") {
|
|
135
|
-
(child as IfBlock).body.forEach(gatherFnLoopVars);
|
|
136
|
-
(child as IfBlock).elseBody?.forEach(gatherFnLoopVars);
|
|
137
|
-
} else if (child.kind === "use") {
|
|
138
|
-
(child as CompUse).children.forEach(gatherFnLoopVars);
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
fn.body.forEach(gatherFnLoopVars);
|
|
142
|
-
if (fnLoopVars.has(rootVar)) { foundInFn = true; break; }
|
|
143
|
-
}
|
|
144
|
-
if (!foundInFn) {
|
|
145
|
-
throw new SinthError(
|
|
146
|
-
`Variable '${rootVar}' is used but never declared. Add 'var str ${rootVar}' or 'var int ${rootVar}' before using it.`
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
for (const fn of file.functions) {
|
|
151
|
-
const fnParamNames = new Set(fn.params.map(p => p.name));
|
|
152
|
-
const fnBodyVars = new Set<string>();
|
|
153
|
-
fn.body.forEach(c => collectChildVars(c, fnBodyVars));
|
|
154
|
-
|
|
155
|
-
const fnLoopVars = new Set<string>();
|
|
156
|
-
const gatherFnLoopVars = (child: Child): void => {
|
|
157
|
-
if (child.kind === "for") {
|
|
158
|
-
const fl = child as ForLoop;
|
|
159
|
-
if (fl.itemVar) fnLoopVars.add(fl.itemVar);
|
|
160
|
-
if (fl.keyVar) fnLoopVars.add(fl.keyVar);
|
|
161
|
-
if (fl.indexVar) fnLoopVars.add(fl.indexVar);
|
|
162
|
-
fl.body.forEach(gatherFnLoopVars);
|
|
163
|
-
} else if (child.kind === "if") {
|
|
164
|
-
(child as IfBlock).body.forEach(gatherFnLoopVars);
|
|
165
|
-
(child as IfBlock).elseBody?.forEach(gatherFnLoopVars);
|
|
166
|
-
} else if (child.kind === "use") {
|
|
167
|
-
(child as CompUse).children.forEach(gatherFnLoopVars);
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
fn.body.forEach(gatherFnLoopVars);
|
|
171
|
-
|
|
172
|
-
const fnLocalDeclared = new Set<string>();
|
|
173
|
-
const collectFnLocalDeclared = (child: Child): void => {
|
|
174
|
-
if (child.kind === "var") {
|
|
175
|
-
fnLocalDeclared.add((child as VarDeclaration).name);
|
|
176
|
-
} else if (child.kind === "if") {
|
|
177
|
-
(child as IfBlock).body.forEach(collectFnLocalDeclared);
|
|
178
|
-
(child as IfBlock).elseBody?.forEach(collectFnLocalDeclared);
|
|
179
|
-
} else if (child.kind === "for") {
|
|
180
|
-
(child as ForLoop).body.forEach(collectFnLocalDeclared);
|
|
181
|
-
} else if (child.kind === "use") {
|
|
182
|
-
(child as CompUse).children.forEach(collectFnLocalDeclared);
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
fn.body.forEach(collectFnLocalDeclared);
|
|
186
|
-
|
|
187
|
-
for (const v of fnBodyVars) {
|
|
188
|
-
if (fnLoopVars.has(v)) continue;
|
|
189
|
-
const rootVar = v.split('.')[0];
|
|
190
|
-
if (!fnParamNames.has(rootVar) && !declaredVars.has(rootVar) && !declaredFuncs.has(rootVar) && !fnLocalDeclared.has(rootVar)) {
|
|
191
|
-
throw new SinthError(
|
|
192
|
-
`Variable '${rootVar}' used in function '${fn.name}' is not declared. It must be a parameter or a page-level variable.`
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
for (const child of fn.body) {
|
|
197
|
-
checkTypeInChild(child, allVarDecls, functionDefs);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
for (const use of file.uses) {
|
|
201
|
-
checkTypeInChild(use, allVarDecls, functionDefs);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function inferType(expr: Expression, varDecls: VarDeclaration[], functionDefs: FunctionDef[]): string | null {
|
|
205
|
-
if (expr.kind === "literal" && expr.value) {
|
|
206
|
-
if (expr.value.kind === "str") return "str";
|
|
207
|
-
if (expr.value.kind === "num") return "int";
|
|
208
|
-
if (expr.value.kind === "bool") return "bool";
|
|
209
|
-
return null;
|
|
210
|
-
}
|
|
211
|
-
if (expr.kind === "variable" && expr.name) {
|
|
212
|
-
const vd = varDecls.find(v => v.name === expr.name);
|
|
213
|
-
if (vd) return vd.varType === "str[]" ? "str[]" : vd.varType;
|
|
214
|
-
return null;
|
|
215
|
-
}
|
|
216
|
-
if (expr.kind === "call" && expr.callee?.kind === "variable" && expr.callee.name) {
|
|
217
|
-
const fd = functionDefs.find(f => f.name === expr.callee!.name);
|
|
218
|
-
if (fd?.returnType) return fd.returnType;
|
|
219
|
-
return null;
|
|
220
|
-
}
|
|
221
|
-
if (expr.kind === "binary" && expr.op === "+") return "str";
|
|
222
|
-
if (expr.kind === "unary" && expr.op === "not") return "bool";
|
|
223
|
-
return null;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function checkCallArgs(fnDef: FunctionDef, args: Expression[], loc: Loc, varDecls: VarDeclaration[], functionDefs: FunctionDef[]): void {
|
|
227
|
-
for (let i = 0; i < fnDef.params.length && i < args.length; i++) {
|
|
228
|
-
const param = fnDef.params[i];
|
|
229
|
-
const arg = args[i];
|
|
230
|
-
if (!param.paramType) continue;
|
|
231
|
-
const argType = inferType(arg, varDecls, functionDefs);
|
|
232
|
-
if (argType && argType !== param.paramType) {
|
|
233
|
-
if (param.paramType === "int" && argType === "str" && arg.kind === "literal" && arg.value?.kind === "str") {
|
|
234
|
-
if (!isNaN(Number(arg.value.value))) continue;
|
|
235
|
-
}
|
|
236
|
-
throw new SinthError(
|
|
237
|
-
`Function '${fnDef.name}' parameter '${param.name}' expects '${param.paramType}' but got '${argType}'`,
|
|
238
|
-
loc
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
function checkTypeInExpr(expr: Expression | undefined, varDecls: VarDeclaration[], functionDefs: FunctionDef[]): void {
|
|
245
|
-
if (!expr) return;
|
|
246
|
-
if (expr.kind === "call" && expr.callee?.kind === "variable" && expr.callee.name) {
|
|
247
|
-
const fnDef = functionDefs.find(f => f.name === expr.callee!.name);
|
|
248
|
-
if (fnDef) checkCallArgs(fnDef, expr.args ?? [], fnDef.loc, varDecls, functionDefs);
|
|
249
|
-
}
|
|
250
|
-
if (expr.left) checkTypeInExpr(expr.left, varDecls, functionDefs);
|
|
251
|
-
if (expr.right) checkTypeInExpr(expr.right, varDecls, functionDefs);
|
|
252
|
-
if (expr.operand) checkTypeInExpr(expr.operand, varDecls, functionDefs);
|
|
253
|
-
if (expr.args) expr.args.forEach(a => checkTypeInExpr(a, varDecls, functionDefs));
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
function checkTypeInChild(child: Child, varDecls: VarDeclaration[], functionDefs: FunctionDef[]): void {
|
|
257
|
-
if (child.kind === "expr" && child.expression) {
|
|
258
|
-
checkTypeInExpr(child.expression, varDecls, functionDefs);
|
|
259
|
-
}
|
|
260
|
-
if (child.kind === "assign_stmt" && child.expression) {
|
|
261
|
-
checkTypeInExpr(child.expression, varDecls, functionDefs);
|
|
262
|
-
}
|
|
263
|
-
if (child.kind === "return" && (child as ReturnStmt).expression) {
|
|
264
|
-
checkTypeInExpr((child as ReturnStmt).expression, varDecls, functionDefs);
|
|
265
|
-
}
|
|
266
|
-
if (child.kind === "if") {
|
|
267
|
-
const ib = child as IfBlock;
|
|
268
|
-
checkTypeInExpr(ib.condition, varDecls, functionDefs);
|
|
269
|
-
ib.body.forEach(c => checkTypeInChild(c, varDecls, functionDefs));
|
|
270
|
-
ib.elseBody?.forEach(c => checkTypeInChild(c, varDecls, functionDefs));
|
|
271
|
-
}
|
|
272
|
-
if (child.kind === "for") {
|
|
273
|
-
(child as ForLoop).body.forEach(c => checkTypeInChild(c, varDecls, functionDefs));
|
|
274
|
-
}
|
|
275
|
-
if (child.kind === "use") {
|
|
276
|
-
const u = child as CompUse;
|
|
277
|
-
u.attrs.forEach(a => {
|
|
278
|
-
if (a.value?.kind === "str" && a.value.value.startsWith("__EXPR__")) {
|
|
279
|
-
try {
|
|
280
|
-
const expr: Expression = JSON.parse(a.value.value.substring(8));
|
|
281
|
-
checkTypeInExpr(expr, varDecls, functionDefs);
|
|
282
|
-
} catch {}
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
u.children.forEach(c => checkTypeInChild(c, varDecls, functionDefs));
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const enableDiffing = file.meta.some(m =>
|
|
290
|
-
m.key === "domdiffing" &&
|
|
291
|
-
(m.value.kind === "bool" ? m.value.value === true : (m.value.kind === "str" && m.value.value === "true"))
|
|
292
|
-
);
|
|
293
|
-
const topLevelLogicIfs: string[] = [];
|
|
294
|
-
file.scripts = file.scripts.filter(s => {
|
|
295
|
-
const trimmed = s.raw.trim();
|
|
296
|
-
if (/^if\s*\(/.test(trimmed) || /^\{/.test(trimmed)) {
|
|
297
|
-
topLevelLogicIfs.push(trimmed);
|
|
298
|
-
return false;
|
|
299
|
-
}
|
|
300
|
-
return true;
|
|
301
|
-
});
|
|
302
|
-
|
|
303
|
-
const ctx: CompileCtx = {
|
|
304
|
-
allDefs, functionDefs, customEls, cssLinks, jsLinks,
|
|
305
|
-
scopeHash: hash,
|
|
306
|
-
pageFile: absPath,
|
|
307
|
-
extraCSS: [],
|
|
308
|
-
mixedBlocks: [],
|
|
309
|
-
mixedCounter: 0,
|
|
310
|
-
logicBlocks: [...topLevelLogicIfs],
|
|
311
|
-
ifIdCounter: 0,
|
|
312
|
-
exprRegistry: [],
|
|
313
|
-
exprMap: new Map(),
|
|
314
|
-
varDecls: pageVarDecls,
|
|
315
|
-
actionButtons: [],
|
|
316
|
-
diffingEnabled: enableDiffing,
|
|
317
|
-
declaredVars: declaredVars,
|
|
318
|
-
namespace: hash,
|
|
319
|
-
};
|
|
320
|
-
|
|
321
|
-
if (!file.isPage) {
|
|
322
|
-
const body = file.uses.map(u => renderCompUse(u, ctx, new Map(), 0)).join("\n");
|
|
323
|
-
const pageCSS = file.styles.map(s => processStyleBlock(s, hash)).join("\n");
|
|
324
|
-
const allCSS = [pageCSS, ...ctx.extraCSS].join("\n");
|
|
325
|
-
const h = `${body}\n<style>\n${allCSS}\n</style>`;
|
|
326
|
-
return { html: h };
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
const headData = buildHeadData(file.meta);
|
|
330
|
-
const bodyHTML = file.uses.map(u => renderCompUse(u, ctx, new Map(), 0)).join("\n");
|
|
331
|
-
const pageCSS = file.styles.map(s => processStyleBlock(s, hash, new Map())).join("\n");
|
|
332
|
-
const scopedCSS = [pageCSS, ...ctx.extraCSS].filter(c => c.trim()).join("\n");
|
|
333
|
-
const { componentScripts, pageScripts } = collectScripts(file, allDefs);
|
|
334
|
-
|
|
335
|
-
// collect all assigned variables for default-value warnings
|
|
336
|
-
const assignedVars = new Set<string>();
|
|
337
|
-
for (const s of file.scripts) {
|
|
338
|
-
for (const m of s.raw.matchAll(/\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?:[+\-]?=)/g)) assignedVars.add(m[1]);
|
|
339
|
-
}
|
|
340
|
-
for (const v of file.varDecls) { if (v.value) assignedVars.add(v.name); }
|
|
341
|
-
|
|
342
|
-
// warn about reserved browser globals
|
|
343
|
-
const RESERVED_GLOBALS = new Set(["name", "location", "history", "status", "closed", "length", "top", "self", "parent", "frames", "origin"]);
|
|
344
|
-
for (const v of file.varDecls) {
|
|
345
|
-
if (RESERVED_GLOBALS.has(v.name)) {
|
|
346
|
-
SinthWarning.emit(`Variable '${v.name}' shadows a reserved browser global. This may cause unexpected behavior. Consider renaming.`, v.loc);
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
// compile function definitions to JS
|
|
351
|
-
const compiledFunctions = functionDefs.map(f => compileFunctionDef(f, ctx)).join("\n");
|
|
352
|
-
|
|
353
|
-
// companion JS file (needs same name as .sinth, adds support for JS libraries)
|
|
354
|
-
const companionJS = (() => {
|
|
355
|
-
const base = absPath.replace(/\.sinth$/, ".js");
|
|
356
|
-
return fs.existsSync(base) ? path.basename(base) : undefined;
|
|
357
|
-
})();
|
|
358
|
-
|
|
359
|
-
const relativeCssLinks = cssLinks.map(css => {
|
|
360
|
-
const rel = path.relative(path.dirname(absPath), css).replace(/\\/g, "/");
|
|
361
|
-
return rel.startsWith(".") ? rel : "./" + rel;
|
|
362
|
-
});
|
|
363
|
-
const relativeJsLinks = jsLinks.map(js => ({
|
|
364
|
-
...js,
|
|
365
|
-
src: (() => {
|
|
366
|
-
const rel = path.relative(path.dirname(absPath), js.src).replace(/\\/g, "/");
|
|
367
|
-
return rel.startsWith(".") ? rel : "./" + rel;
|
|
368
|
-
})(),
|
|
369
|
-
}));
|
|
370
|
-
|
|
371
|
-
const runtimeResult = buildRuntime({
|
|
372
|
-
varDecls: pageVarDecls,
|
|
373
|
-
bodyHTML,
|
|
374
|
-
logicBlocks: ctx.logicBlocks,
|
|
375
|
-
mixedBlocks: ctx.mixedBlocks,
|
|
376
|
-
assignedVars,
|
|
377
|
-
exprRegistry: ctx.exprRegistry,
|
|
378
|
-
sharedRuntime: opts.sharedRuntime,
|
|
379
|
-
functionsJS: compiledFunctions,
|
|
380
|
-
namespace: hash,
|
|
381
|
-
});
|
|
382
|
-
const runtimeJS = typeof runtimeResult === 'string' ? runtimeResult : runtimeResult.page;
|
|
383
|
-
const sharedJS = typeof runtimeResult === 'string' ? null : runtimeResult.shared;
|
|
384
|
-
let finalRuntimeJS = runtimeJS;
|
|
385
|
-
if (enableDiffing) {
|
|
386
|
-
finalRuntimeJS = runtimeJS.replace('sinthRender();',
|
|
387
|
-
'var _bump=function(v){_bump._v[v]=(_bump._v[v]||0)+1;};' +
|
|
388
|
-
'_bump._v={};' +
|
|
389
|
-
'window._bump=_bump;' +
|
|
390
|
-
'var _exprCache=new WeakMap();' +
|
|
391
|
-
'var _origSinthExpr=sinthExpr;' +
|
|
392
|
-
'sinthExpr=function(el){' +
|
|
393
|
-
'var deps=el.dataset.sinthDeps;' +
|
|
394
|
-
'if(deps!=null){' +
|
|
395
|
-
'var list=deps.split(",").filter(Boolean);' +
|
|
396
|
-
'if(list.length){' +
|
|
397
|
-
'var h="";' +
|
|
398
|
-
'for(var i=0;i<list.length;i++){var v=list[i].trim();h+=v+":"+(_bump._v[v]||0)+";";}' +
|
|
399
|
-
'var old=_exprCache.get(el);' +
|
|
400
|
-
'if(old===h)return;' +
|
|
401
|
-
'_exprCache.set(el,h);' +
|
|
402
|
-
'}else{' +
|
|
403
|
-
'if(_exprCache.has(el))return;' +
|
|
404
|
-
'_exprCache.set(el,1);' +
|
|
405
|
-
'}' +
|
|
406
|
-
'}' +
|
|
407
|
-
'_origSinthExpr(el);' +
|
|
408
|
-
'};' +
|
|
409
|
-
'sinthRender();'
|
|
410
|
-
);
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
const head = renderHead(headData, relativeCssLinks, relativeJsLinks, scopedCSS, companionJS);
|
|
415
|
-
const scriptTags: string[] = [];
|
|
416
|
-
if (componentScripts.length > 0) {
|
|
417
|
-
scriptTags.push(`<script>\n${componentScripts.join("\n\n")}\n</script>`);
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
for (const s of pageScripts) {
|
|
421
|
-
const extra = Object.entries(s.attrs)
|
|
422
|
-
.map(([k, v]) => v === "true" ? k : `${k}="${escAttr(v)}"`)
|
|
423
|
-
.join(" ");
|
|
424
|
-
const globalised = s.raw.replace(/\b(let|const)\b/g, "let");
|
|
425
|
-
scriptTags.push(`<script${extra ? " " + extra : ""}>\n${globalised}\n</script>`);
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
const sharedRuntimeTag = (() => {
|
|
430
|
-
if (!opts.sharedRuntime || !sharedJS) return "";
|
|
431
|
-
|
|
432
|
-
const relPath = path.relative(opts.projectRoot, absPath);
|
|
433
|
-
const htmlOutputDir = path.dirname(relPath.replace(/\.sinth$/, '.html'));
|
|
434
|
-
const relativeRuntimePath = path.relative(htmlOutputDir, '.').replace(/\\/g, '/');
|
|
435
|
-
const runtimeSrc = relativeRuntimePath ? `${relativeRuntimePath}/sinth-runtime.js` : './sinth-runtime.js';
|
|
436
|
-
|
|
437
|
-
return `<script src="${runtimeSrc}"></script>`;
|
|
438
|
-
})();
|
|
439
|
-
|
|
440
|
-
let actionInitScript = '';
|
|
441
|
-
if (ctx.actionButtons.length > 0) {
|
|
442
|
-
actionInitScript = '<script>' +
|
|
443
|
-
ctx.actionButtons.map(b =>
|
|
444
|
-
`setTimeout(function(){ var el=document.querySelector('[data-sinth-queue-id="${b.qid}"]'); if(el)el.style.display=''; },${b.delayMs});`
|
|
445
|
-
).join('') +
|
|
446
|
-
'</script>';
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
const html = [
|
|
450
|
-
"<!DOCTYPE html>",
|
|
451
|
-
`<html lang="${escAttr(headData.lang)}">`,
|
|
452
|
-
head,
|
|
453
|
-
`<body data-s="${hash}">`,
|
|
454
|
-
bodyHTML,
|
|
455
|
-
actionInitScript,
|
|
456
|
-
sharedRuntimeTag,
|
|
457
|
-
finalRuntimeJS.trim() ? `<script>\n${finalRuntimeJS}\n</script>` : "",
|
|
458
|
-
scriptTags.join("\n"),
|
|
459
|
-
"</body>",
|
|
460
|
-
"</html>",
|
|
461
|
-
].filter(Boolean).join("\n");
|
|
462
|
-
|
|
463
|
-
for (const cel of file.customEls) {
|
|
464
|
-
if (cel.exportTag) {
|
|
465
|
-
try {
|
|
466
|
-
const compJs = compileCustomElement(cel, opts, hash, ctx);
|
|
467
|
-
const relPath = path.relative(opts.projectRoot, path.dirname(absPath));
|
|
468
|
-
const outPath = path.join(opts.outDir, relPath, cel.exportTag + ".js");
|
|
469
|
-
if (!opts.checkOnly) {
|
|
470
|
-
fs.mkdirSync(path.dirname(outPath), { recursive: true });
|
|
471
|
-
fs.writeFileSync(outPath, compJs);
|
|
472
|
-
process.stdout.write(` \x1b[32m✓\x1b[0m ${cel.exportTag}.js (custom element)\n`);
|
|
473
|
-
}
|
|
474
|
-
} catch (e: unknown) {
|
|
475
|
-
SinthWarning.emit(`Failed to compile custom element '${cel.exportTag}': ${(e as Error).message}`, cel.loc);
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
const finalHTML = opts.minify ? minifyHTML(html) : html;
|
|
481
|
-
return sharedJS ? { html: finalHTML, shared: sharedJS } : { html: finalHTML };
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
export function minifyHTML(html: string): string {
|
|
487
|
-
return html.replace(/>\s+</g, "><").replace(/\n\s*\n/g, "\n").trim();
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
// file discovery & asset copy
|
|
491
|
-
|
|
492
|
-
export function findSinthPages(dir: string, outDir?: string): string[] {
|
|
493
|
-
const results: string[] = [];
|
|
494
|
-
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
495
|
-
if (entry.name.startsWith(".") || entry.name === "node_modules") continue;
|
|
496
|
-
const full = path.join(dir, entry.name);
|
|
497
|
-
if (outDir && path.resolve(full) === path.resolve(outDir)) continue;
|
|
498
|
-
if (entry.isDirectory()) results.push(...findSinthPages(full, outDir));
|
|
499
|
-
else if (entry.name.endsWith(".sinth")) results.push(full);
|
|
500
|
-
}
|
|
501
|
-
return results;
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
export function copyDir(src: string, dest: string): void {
|
|
505
|
-
if (!fs.existsSync(src)) return;
|
|
506
|
-
fs.mkdirSync(dest, { recursive: true });
|
|
507
|
-
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
508
|
-
const sp = path.join(src, entry.name);
|
|
509
|
-
const dp = path.join(dest, entry.name);
|
|
510
|
-
if (entry.isDirectory()) copyDir(sp, dp);
|
|
511
|
-
else fs.copyFileSync(sp, dp);
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
|