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