@yannosay/sinth 4.1.0 → 5.1.0
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/CHANGELOG.md +83 -0
- package/DOCS.md +1359 -0
- package/README.md +83 -74
- package/bin/sinth.js +2 -1
- package/dist/cli/args.d.ts +11 -0
- package/dist/cli/args.js +74 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +437 -0
- package/dist/cli/templates.d.ts +10 -0
- package/dist/cli/templates.js +193 -0
- package/dist/cli/ui.d.ts +28 -0
- package/dist/cli/ui.js +61 -0
- package/dist/cli/update-check.d.ts +2 -0
- package/dist/cli/update-check.js +124 -0
- package/dist/compiler/ast.d.ts +315 -0
- package/dist/compiler/ast.js +4 -0
- package/dist/compiler/codegen.d.ts +143 -0
- package/dist/compiler/codegen.js +1326 -0
- package/dist/compiler/diagnostics.d.ts +36 -0
- package/dist/compiler/diagnostics.js +142 -0
- package/dist/compiler/elements.d.ts +14 -0
- package/dist/compiler/elements.js +78 -0
- package/dist/compiler/globals.d.ts +2 -0
- package/dist/compiler/globals.js +27 -0
- package/dist/compiler/html.d.ts +37 -0
- package/dist/compiler/html.js +152 -0
- package/dist/compiler/lexer.d.ts +42 -0
- package/dist/compiler/lexer.js +404 -0
- package/dist/compiler/parser.d.ts +95 -0
- package/dist/compiler/parser.js +1374 -0
- package/dist/compiler/prerender.d.ts +72 -0
- package/dist/compiler/prerender.js +910 -0
- package/dist/compiler/script-analysis.d.ts +6 -0
- package/dist/compiler/script-analysis.js +106 -0
- package/dist/compiler/style.d.ts +39 -0
- package/dist/compiler/style.js +463 -0
- package/dist/deploy/cloudflare.d.ts +9 -0
- package/dist/deploy/cloudflare.js +111 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +23 -0
- package/dist/project/build.d.ts +31 -0
- package/dist/project/build.js +425 -0
- package/dist/project/config.d.ts +33 -0
- package/dist/project/config.js +142 -0
- package/dist/project/project.d.ts +74 -0
- package/dist/project/project.js +535 -0
- package/dist/runtime/index.d.ts +68 -0
- package/dist/runtime/index.js +912 -0
- package/dist/server/client.d.ts +1 -0
- package/dist/server/client.js +112 -0
- package/dist/server/dev.d.ts +8 -0
- package/dist/server/dev.js +244 -0
- package/dist/server/preview.d.ts +6 -0
- package/dist/server/preview.js +106 -0
- package/dist/server/static.d.ts +10 -0
- package/dist/server/static.js +136 -0
- package/lib/ui/index.sinth +436 -0
- package/package.json +33 -15
- package/schema/sinth.config.schema.json +54 -0
- package/dist/browser/compile.js +0 -63
- package/dist/cli.js +0 -509
- package/dist/core/builtins.js +0 -117
- package/dist/core/cli-compiler.js +0 -705
- package/dist/core/compiler.js +0 -1489
- package/dist/core/expr.js +0 -129
- package/dist/core/head-builder.js +0 -130
- package/dist/core/lexer.js +0 -410
- package/dist/core/parser.js +0 -1646
- package/dist/core/runtime/custom-element.js +0 -81
- package/dist/core/runtime/functions.js +0 -99
- package/dist/core/runtime/helpers.js +0 -347
- package/dist/core/runtime/render.js +0 -136
- package/dist/core/style-processor.js +0 -317
- package/dist/core/types.js +0 -93
- package/dist/resolver.js +0 -175
- package/dist/server.js +0 -174
- package/dist/update-check.js +0 -109
- package/dist/utils.js +0 -80
|
@@ -0,0 +1,910 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Prerenderer = void 0;
|
|
4
|
+
exports.toText = toText;
|
|
5
|
+
exports.prerenderPage = prerenderPage;
|
|
6
|
+
exports.evaluatePaths = evaluatePaths;
|
|
7
|
+
const elements_1 = require("./elements");
|
|
8
|
+
const html_1 = require("./html");
|
|
9
|
+
const lexer_1 = require("./lexer");
|
|
10
|
+
const parser_1 = require("./parser");
|
|
11
|
+
const style_1 = require("./style");
|
|
12
|
+
const UNKNOWN = Symbol("unknown");
|
|
13
|
+
class Cell {
|
|
14
|
+
value;
|
|
15
|
+
constructor(value) {
|
|
16
|
+
this.value = value;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
class Env {
|
|
20
|
+
parent;
|
|
21
|
+
vars = new Map();
|
|
22
|
+
constructor(parent) {
|
|
23
|
+
this.parent = parent;
|
|
24
|
+
}
|
|
25
|
+
define(name, value) {
|
|
26
|
+
this.vars.set(name, new Cell(value));
|
|
27
|
+
}
|
|
28
|
+
find(name) {
|
|
29
|
+
return this.vars.get(name) ?? this.parent?.find(name);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
class ReturnSignal {
|
|
33
|
+
value;
|
|
34
|
+
constructor(value) {
|
|
35
|
+
this.value = value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
class BreakSignal {
|
|
39
|
+
}
|
|
40
|
+
class ContinueSignal {
|
|
41
|
+
}
|
|
42
|
+
const BLOCKED_PROPS = new Set(["constructor", "__proto__", "prototype", "__defineGetter__", "__defineSetter__"]);
|
|
43
|
+
const MAX_STEPS = 2_000_000;
|
|
44
|
+
class Prerenderer {
|
|
45
|
+
host;
|
|
46
|
+
complete = true;
|
|
47
|
+
steps = 0;
|
|
48
|
+
moduleEnvs = new Map();
|
|
49
|
+
componentTables = new Map();
|
|
50
|
+
depth = 0;
|
|
51
|
+
constructor(host) {
|
|
52
|
+
this.host = host;
|
|
53
|
+
}
|
|
54
|
+
renderPage(file, params) {
|
|
55
|
+
const env = this.moduleEnv(file, params);
|
|
56
|
+
return this.renderNodes(file.body, { env: new Env(env), file });
|
|
57
|
+
}
|
|
58
|
+
/** Evaluates metadata in the page's module scope. Values that need the browser come back as `undefined` with `known: false`. */
|
|
59
|
+
evalMeta(file, params) {
|
|
60
|
+
const env = this.moduleEnv(file, params);
|
|
61
|
+
return file.meta.filter(m => m.key !== "paths").map(m => {
|
|
62
|
+
let value;
|
|
63
|
+
try {
|
|
64
|
+
value = this.eval(m.value, env, file);
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
value = UNKNOWN;
|
|
68
|
+
}
|
|
69
|
+
return { key: m.key, value: value === UNKNOWN ? undefined : value, known: value !== UNKNOWN, loc: m.loc };
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/** Evaluates `paths` for a dynamic route. Route params are unknown at this point. */
|
|
73
|
+
evalPaths(file, paramNames) {
|
|
74
|
+
const entry = file.meta.find(m => m.key === "paths");
|
|
75
|
+
if (!entry)
|
|
76
|
+
return { value: undefined, known: true };
|
|
77
|
+
const unknownParams = {};
|
|
78
|
+
const env = this.moduleEnv(file, unknownParams, paramNames);
|
|
79
|
+
let value;
|
|
80
|
+
try {
|
|
81
|
+
value = this.eval(entry.value, env, file);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
value = UNKNOWN;
|
|
85
|
+
}
|
|
86
|
+
return { value: value === UNKNOWN ? undefined : value, known: value !== UNKNOWN };
|
|
87
|
+
}
|
|
88
|
+
tick() {
|
|
89
|
+
if (++this.steps > MAX_STEPS)
|
|
90
|
+
throw new Error("Prerender step limit exceeded");
|
|
91
|
+
}
|
|
92
|
+
moduleEnv(file, params, unknownParams = []) {
|
|
93
|
+
const cached = this.moduleEnvs.get(file.path);
|
|
94
|
+
if (cached)
|
|
95
|
+
return cached;
|
|
96
|
+
const env = new Env(null);
|
|
97
|
+
this.moduleEnvs.set(file.path, env);
|
|
98
|
+
for (const name of unknownParams)
|
|
99
|
+
env.define(name, UNKNOWN);
|
|
100
|
+
if (params)
|
|
101
|
+
for (const [k, v] of Object.entries(params))
|
|
102
|
+
env.define(k, v);
|
|
103
|
+
for (const imp of this.host.getImports(file)) {
|
|
104
|
+
const d = imp.decl;
|
|
105
|
+
if (imp.kind === "json") {
|
|
106
|
+
if (d.defaultName)
|
|
107
|
+
env.define(d.defaultName, imp.json);
|
|
108
|
+
for (const s of d.named)
|
|
109
|
+
env.define(s.alias, imp.json?.[s.name]);
|
|
110
|
+
}
|
|
111
|
+
else if (imp.kind === "sinth" && imp.exports) {
|
|
112
|
+
const other = this.host.getFile(imp.specifier);
|
|
113
|
+
const otherEnv = this.moduleEnv(other);
|
|
114
|
+
for (const s of d.named) {
|
|
115
|
+
const cell = otherEnv.find(s.name);
|
|
116
|
+
if (cell)
|
|
117
|
+
env.define(s.alias, cell.value);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
for (const name of [d.defaultName, d.namespace, ...d.named.map(s => s.alias)])
|
|
122
|
+
if (name)
|
|
123
|
+
env.define(name, UNKNOWN);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
for (const ext of file.externs)
|
|
127
|
+
env.define(ext.name, UNKNOWN);
|
|
128
|
+
for (const f of file.functions)
|
|
129
|
+
env.define(f.name, this.makeFunction(f, env, file));
|
|
130
|
+
if (file.scripts.length)
|
|
131
|
+
this.complete = false;
|
|
132
|
+
for (const v of file.vars) {
|
|
133
|
+
try {
|
|
134
|
+
this.declare(v, env, file);
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
env.define(v.name, UNKNOWN);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return env;
|
|
141
|
+
}
|
|
142
|
+
declare(v, env, file) {
|
|
143
|
+
if (v.kind === "computed" && v.init) {
|
|
144
|
+
const init = v.init;
|
|
145
|
+
env.define(v.name, UNKNOWN);
|
|
146
|
+
const cell = env.find(v.name);
|
|
147
|
+
Object.defineProperty(cell, "value", { get: () => this.eval(init, env, file), configurable: true });
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
env.define(v.name, v.init ? this.eval(v.init, env, file) : defaultValue(v));
|
|
151
|
+
}
|
|
152
|
+
makeFunction(f, env, file) {
|
|
153
|
+
if (f.view)
|
|
154
|
+
return UNKNOWN;
|
|
155
|
+
return (...args) => {
|
|
156
|
+
const local = new Env(env);
|
|
157
|
+
f.params.forEach((p, i) => {
|
|
158
|
+
let v = args[i];
|
|
159
|
+
if (v === undefined && p.defaultValue)
|
|
160
|
+
v = this.eval(p.defaultValue, local, file);
|
|
161
|
+
local.define(p.name, v);
|
|
162
|
+
});
|
|
163
|
+
try {
|
|
164
|
+
this.execAll(f.body, local, file);
|
|
165
|
+
}
|
|
166
|
+
catch (sig) {
|
|
167
|
+
if (sig instanceof ReturnSignal)
|
|
168
|
+
return sig.value;
|
|
169
|
+
throw sig;
|
|
170
|
+
}
|
|
171
|
+
return undefined;
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
execAll(stmts, env, file) {
|
|
175
|
+
for (const s of stmts)
|
|
176
|
+
this.exec(s, env, file);
|
|
177
|
+
}
|
|
178
|
+
exec(s, env, file) {
|
|
179
|
+
this.tick();
|
|
180
|
+
switch (s.k) {
|
|
181
|
+
case "expr":
|
|
182
|
+
this.eval(s.expr, env, file);
|
|
183
|
+
return;
|
|
184
|
+
case "var":
|
|
185
|
+
this.declare(s.decl, env, file);
|
|
186
|
+
return;
|
|
187
|
+
case "if": {
|
|
188
|
+
const t = this.eval(s.test, env, file);
|
|
189
|
+
if (t === UNKNOWN)
|
|
190
|
+
throw new ReturnSignal(UNKNOWN);
|
|
191
|
+
if (t)
|
|
192
|
+
this.execAll(s.then, new Env(env), file);
|
|
193
|
+
else if (s.else)
|
|
194
|
+
this.execAll(s.else, new Env(env), file);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
case "for": {
|
|
198
|
+
const list = this.eval(s.iter, env, file);
|
|
199
|
+
if (list === UNKNOWN)
|
|
200
|
+
throw new ReturnSignal(UNKNOWN);
|
|
201
|
+
for (const [item, index, key] of iterate(list)) {
|
|
202
|
+
const inner = new Env(env);
|
|
203
|
+
[item, index, key].slice(0, s.names.length).forEach((v, i) => inner.define(s.names[i], v));
|
|
204
|
+
if (s.filter && !this.eval(s.filter, inner, file))
|
|
205
|
+
continue;
|
|
206
|
+
try {
|
|
207
|
+
this.execAll(s.body, inner, file);
|
|
208
|
+
}
|
|
209
|
+
catch (sig) {
|
|
210
|
+
if (sig instanceof BreakSignal)
|
|
211
|
+
break;
|
|
212
|
+
if (sig instanceof ContinueSignal)
|
|
213
|
+
continue;
|
|
214
|
+
throw sig;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
case "while": {
|
|
220
|
+
for (;;) {
|
|
221
|
+
const t = this.eval(s.test, env, file);
|
|
222
|
+
if (t === UNKNOWN)
|
|
223
|
+
throw new ReturnSignal(UNKNOWN);
|
|
224
|
+
if (!t)
|
|
225
|
+
break;
|
|
226
|
+
try {
|
|
227
|
+
this.execAll(s.body, new Env(env), file);
|
|
228
|
+
}
|
|
229
|
+
catch (sig) {
|
|
230
|
+
if (sig instanceof BreakSignal)
|
|
231
|
+
break;
|
|
232
|
+
if (sig instanceof ContinueSignal)
|
|
233
|
+
continue;
|
|
234
|
+
throw sig;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
case "return":
|
|
240
|
+
throw new ReturnSignal(s.value ? this.eval(s.value, env, file) : undefined);
|
|
241
|
+
case "break":
|
|
242
|
+
throw new BreakSignal();
|
|
243
|
+
case "continue":
|
|
244
|
+
throw new ContinueSignal();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
eval(e, env, file) {
|
|
248
|
+
this.tick();
|
|
249
|
+
switch (e.k) {
|
|
250
|
+
case "lit":
|
|
251
|
+
return e.value;
|
|
252
|
+
case "tpl": {
|
|
253
|
+
let out = "";
|
|
254
|
+
for (const p of e.parts) {
|
|
255
|
+
if (typeof p === "string")
|
|
256
|
+
out += p;
|
|
257
|
+
else {
|
|
258
|
+
const v = this.eval(p, env, file);
|
|
259
|
+
if (v === UNKNOWN)
|
|
260
|
+
return UNKNOWN;
|
|
261
|
+
out += toText(v);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return out;
|
|
265
|
+
}
|
|
266
|
+
case "id": {
|
|
267
|
+
const cell = env.find(e.name);
|
|
268
|
+
if (cell)
|
|
269
|
+
return cell.value;
|
|
270
|
+
return SAFE_GLOBALS[e.name] ?? UNKNOWN;
|
|
271
|
+
}
|
|
272
|
+
case "member": {
|
|
273
|
+
const obj = this.eval(e.object, env, file);
|
|
274
|
+
if (obj === UNKNOWN)
|
|
275
|
+
return UNKNOWN;
|
|
276
|
+
if (obj === null || obj === undefined)
|
|
277
|
+
return e.optional ? undefined : UNKNOWN;
|
|
278
|
+
return getProp(obj, e.prop);
|
|
279
|
+
}
|
|
280
|
+
case "index": {
|
|
281
|
+
const obj = this.eval(e.object, env, file);
|
|
282
|
+
const key = this.eval(e.index, env, file);
|
|
283
|
+
if (obj === UNKNOWN || key === UNKNOWN)
|
|
284
|
+
return UNKNOWN;
|
|
285
|
+
if (obj === null || obj === undefined)
|
|
286
|
+
return e.optional ? undefined : UNKNOWN;
|
|
287
|
+
return getProp(obj, String(key));
|
|
288
|
+
}
|
|
289
|
+
case "call": {
|
|
290
|
+
let thisArg;
|
|
291
|
+
let fn;
|
|
292
|
+
if (e.callee.k === "member") {
|
|
293
|
+
thisArg = this.eval(e.callee.object, env, file);
|
|
294
|
+
if (thisArg === UNKNOWN)
|
|
295
|
+
return UNKNOWN;
|
|
296
|
+
if (thisArg === null || thisArg === undefined)
|
|
297
|
+
return e.optional || e.callee.optional ? undefined : UNKNOWN;
|
|
298
|
+
fn = getProp(thisArg, e.callee.prop);
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
fn = this.eval(e.callee, env, file);
|
|
302
|
+
}
|
|
303
|
+
if (typeof fn !== "function")
|
|
304
|
+
return UNKNOWN;
|
|
305
|
+
const args = [];
|
|
306
|
+
for (const a of e.args) {
|
|
307
|
+
const v = this.eval(a.value, env, file);
|
|
308
|
+
if (v === UNKNOWN)
|
|
309
|
+
return UNKNOWN;
|
|
310
|
+
if (a.spread && Array.isArray(v))
|
|
311
|
+
args.push(...v);
|
|
312
|
+
else
|
|
313
|
+
args.push(v);
|
|
314
|
+
}
|
|
315
|
+
if (!isSafeCallable(fn, thisArg))
|
|
316
|
+
return UNKNOWN;
|
|
317
|
+
try {
|
|
318
|
+
const result = fn.apply(thisArg, args);
|
|
319
|
+
return result instanceof Promise ? UNKNOWN : result;
|
|
320
|
+
}
|
|
321
|
+
catch (err) {
|
|
322
|
+
if (err instanceof ReturnSignal)
|
|
323
|
+
return err.value;
|
|
324
|
+
return UNKNOWN;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
case "new": {
|
|
328
|
+
if (e.callee.k === "id" && e.callee.name === "Date") {
|
|
329
|
+
const args = e.args.map(a => this.eval(a.value, env, file));
|
|
330
|
+
if (args.includes(UNKNOWN))
|
|
331
|
+
return UNKNOWN;
|
|
332
|
+
return new Date(...args);
|
|
333
|
+
}
|
|
334
|
+
return UNKNOWN;
|
|
335
|
+
}
|
|
336
|
+
case "unary": {
|
|
337
|
+
if (e.op === "await")
|
|
338
|
+
return UNKNOWN;
|
|
339
|
+
const v = this.eval(e.arg, env, file);
|
|
340
|
+
if (v === UNKNOWN)
|
|
341
|
+
return UNKNOWN;
|
|
342
|
+
switch (e.op) {
|
|
343
|
+
case "!": return !v;
|
|
344
|
+
case "-": return -v;
|
|
345
|
+
case "+": return +v;
|
|
346
|
+
case "~": return ~v;
|
|
347
|
+
case "typeof": return typeof v;
|
|
348
|
+
}
|
|
349
|
+
return UNKNOWN;
|
|
350
|
+
}
|
|
351
|
+
case "binary": {
|
|
352
|
+
const l = this.eval(e.left, env, file);
|
|
353
|
+
if (l === UNKNOWN)
|
|
354
|
+
return UNKNOWN;
|
|
355
|
+
if (e.op === "&&")
|
|
356
|
+
return l ? this.eval(e.right, env, file) : l;
|
|
357
|
+
if (e.op === "||")
|
|
358
|
+
return l ? l : this.eval(e.right, env, file);
|
|
359
|
+
if (e.op === "??")
|
|
360
|
+
return l ?? this.eval(e.right, env, file);
|
|
361
|
+
const r = this.eval(e.right, env, file);
|
|
362
|
+
if (r === UNKNOWN)
|
|
363
|
+
return UNKNOWN;
|
|
364
|
+
return binary(e.op, l, r);
|
|
365
|
+
}
|
|
366
|
+
case "cond": {
|
|
367
|
+
const t = this.eval(e.test, env, file);
|
|
368
|
+
if (t === UNKNOWN)
|
|
369
|
+
return UNKNOWN;
|
|
370
|
+
return t ? this.eval(e.then, env, file) : this.eval(e.else, env, file);
|
|
371
|
+
}
|
|
372
|
+
case "assign": {
|
|
373
|
+
const value = this.eval(e.value, env, file);
|
|
374
|
+
if (e.target.k === "id") {
|
|
375
|
+
const cell = env.find(e.target.name);
|
|
376
|
+
if (!cell)
|
|
377
|
+
return UNKNOWN;
|
|
378
|
+
const cur = cell.value;
|
|
379
|
+
cell.value = e.op === "=" ? value : value === UNKNOWN || cur === UNKNOWN ? UNKNOWN : binary(e.op.slice(0, -1), cur, value);
|
|
380
|
+
return cell.value;
|
|
381
|
+
}
|
|
382
|
+
if (e.target.k === "member" || e.target.k === "index") {
|
|
383
|
+
const obj = this.eval(e.target.object, env, file);
|
|
384
|
+
const key = e.target.k === "member" ? e.target.prop : this.eval(e.target.index, env, file);
|
|
385
|
+
if (obj && typeof obj === "object" && key !== UNKNOWN && !BLOCKED_PROPS.has(String(key))) {
|
|
386
|
+
const rec = obj;
|
|
387
|
+
rec[String(key)] = e.op === "=" ? value : binary(e.op.slice(0, -1), rec[String(key)], value);
|
|
388
|
+
return rec[String(key)];
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return UNKNOWN;
|
|
392
|
+
}
|
|
393
|
+
case "update": {
|
|
394
|
+
if (e.target.k !== "id")
|
|
395
|
+
return UNKNOWN;
|
|
396
|
+
const cell = env.find(e.target.name);
|
|
397
|
+
if (!cell || typeof cell.value !== "number")
|
|
398
|
+
return UNKNOWN;
|
|
399
|
+
const old = cell.value;
|
|
400
|
+
cell.value = old + 1;
|
|
401
|
+
return e.prefix ? cell.value : old;
|
|
402
|
+
}
|
|
403
|
+
case "array": {
|
|
404
|
+
const out = [];
|
|
405
|
+
for (const i of e.items) {
|
|
406
|
+
const v = this.eval(i.value, env, file);
|
|
407
|
+
if (v === UNKNOWN)
|
|
408
|
+
return UNKNOWN;
|
|
409
|
+
if (i.spread && Array.isArray(v))
|
|
410
|
+
out.push(...v);
|
|
411
|
+
else
|
|
412
|
+
out.push(v);
|
|
413
|
+
}
|
|
414
|
+
return out;
|
|
415
|
+
}
|
|
416
|
+
case "object": {
|
|
417
|
+
const out = {};
|
|
418
|
+
for (const p of e.props) {
|
|
419
|
+
const v = this.eval(p.value, env, file);
|
|
420
|
+
if (v === UNKNOWN)
|
|
421
|
+
return UNKNOWN;
|
|
422
|
+
if (p.spread)
|
|
423
|
+
Object.assign(out, v);
|
|
424
|
+
else {
|
|
425
|
+
const key = p.computedKey ? String(this.eval(p.computedKey, env, file)) : p.key;
|
|
426
|
+
if (!BLOCKED_PROPS.has(key))
|
|
427
|
+
out[key] = v;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return out;
|
|
431
|
+
}
|
|
432
|
+
case "arrow": {
|
|
433
|
+
const body = e.body;
|
|
434
|
+
const params = e.params;
|
|
435
|
+
if (e.async)
|
|
436
|
+
return UNKNOWN;
|
|
437
|
+
return (...args) => {
|
|
438
|
+
const local = new Env(env);
|
|
439
|
+
params.forEach((p, i) => local.define(p.name, args[i] === undefined && p.defaultValue ? this.eval(p.defaultValue, local, file) : args[i]));
|
|
440
|
+
if (!Array.isArray(body))
|
|
441
|
+
return this.eval(body, local, file);
|
|
442
|
+
try {
|
|
443
|
+
this.execAll(body, local, file);
|
|
444
|
+
}
|
|
445
|
+
catch (sig) {
|
|
446
|
+
if (sig instanceof ReturnSignal)
|
|
447
|
+
return sig.value;
|
|
448
|
+
throw sig;
|
|
449
|
+
}
|
|
450
|
+
return undefined;
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
case "range":
|
|
454
|
+
return UNKNOWN;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
components(file) {
|
|
458
|
+
const cached = this.componentTables.get(file.path);
|
|
459
|
+
if (cached)
|
|
460
|
+
return cached;
|
|
461
|
+
const table = new Map();
|
|
462
|
+
this.componentTables.set(file.path, table);
|
|
463
|
+
for (const imp of this.host.getImports(file)) {
|
|
464
|
+
if (imp.kind !== "sinth" || !imp.exports)
|
|
465
|
+
continue;
|
|
466
|
+
const other = this.host.getFile(imp.specifier);
|
|
467
|
+
const d = imp.decl;
|
|
468
|
+
const byName = (n) => {
|
|
469
|
+
const decl = other.components.find(c => c.name === n);
|
|
470
|
+
return decl ? { decl, file: other } : undefined;
|
|
471
|
+
};
|
|
472
|
+
if (d.sideEffect || (!d.named.length && !d.defaultName && !d.namespace)) {
|
|
473
|
+
for (const c of other.components)
|
|
474
|
+
table.set(c.name, { decl: c, file: other });
|
|
475
|
+
}
|
|
476
|
+
if (d.defaultName && imp.exports.defaultComponent) {
|
|
477
|
+
const ref = byName(imp.exports.defaultComponent);
|
|
478
|
+
if (ref)
|
|
479
|
+
table.set(d.defaultName, ref);
|
|
480
|
+
}
|
|
481
|
+
if (d.namespace)
|
|
482
|
+
for (const c of other.components)
|
|
483
|
+
table.set(`${d.namespace}.${c.name}`, { decl: c, file: other });
|
|
484
|
+
for (const s of d.named) {
|
|
485
|
+
const ref = byName(s.name);
|
|
486
|
+
if (ref)
|
|
487
|
+
table.set(s.alias, ref);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
for (const c of file.components)
|
|
491
|
+
table.set(c.name, { decl: c, file });
|
|
492
|
+
return table;
|
|
493
|
+
}
|
|
494
|
+
renderNodes(nodes, scope) {
|
|
495
|
+
let html = "";
|
|
496
|
+
for (const n of nodes)
|
|
497
|
+
html += this.renderNode(n, scope);
|
|
498
|
+
return html;
|
|
499
|
+
}
|
|
500
|
+
renderNode(node, scope) {
|
|
501
|
+
this.tick();
|
|
502
|
+
switch (node.k) {
|
|
503
|
+
case "text": {
|
|
504
|
+
if (node.expr.k === "call" && node.expr.callee.k === "id") {
|
|
505
|
+
const fn = scope.file.functions.find(f => f.name === node.expr.callee.name && f.view);
|
|
506
|
+
if (fn && fn.view)
|
|
507
|
+
return this.renderUiFunction(fn, node.expr.args.map(a => this.eval(a.value, scope.env, scope.file)), scope);
|
|
508
|
+
}
|
|
509
|
+
const v = this.eval(node.expr, scope.env, scope.file);
|
|
510
|
+
if (v === UNKNOWN) {
|
|
511
|
+
this.complete = false;
|
|
512
|
+
return "";
|
|
513
|
+
}
|
|
514
|
+
return (0, html_1.escapeHtml)(toText(v));
|
|
515
|
+
}
|
|
516
|
+
case "element":
|
|
517
|
+
return this.renderElement(node, scope);
|
|
518
|
+
case "if": {
|
|
519
|
+
for (const br of node.branches) {
|
|
520
|
+
const t = br.test ? this.eval(br.test, scope.env, scope.file) : true;
|
|
521
|
+
if (t === UNKNOWN) {
|
|
522
|
+
this.complete = false;
|
|
523
|
+
return "";
|
|
524
|
+
}
|
|
525
|
+
if (t)
|
|
526
|
+
return this.renderNodes(br.body, { ...scope, env: new Env(scope.env) });
|
|
527
|
+
}
|
|
528
|
+
return "";
|
|
529
|
+
}
|
|
530
|
+
case "for": {
|
|
531
|
+
const list = this.eval(node.iter, scope.env, scope.file);
|
|
532
|
+
if (list === UNKNOWN) {
|
|
533
|
+
this.complete = false;
|
|
534
|
+
return "";
|
|
535
|
+
}
|
|
536
|
+
let html = "";
|
|
537
|
+
let count = 0;
|
|
538
|
+
for (const [item, index, key] of iterate(list)) {
|
|
539
|
+
const env = new Env(scope.env);
|
|
540
|
+
[item, count, key].slice(0, node.names.length).forEach((v, i) => env.define(node.names[i], v));
|
|
541
|
+
void index;
|
|
542
|
+
if (node.filter) {
|
|
543
|
+
const keep = this.eval(node.filter, env, scope.file);
|
|
544
|
+
if (keep === UNKNOWN)
|
|
545
|
+
this.complete = false;
|
|
546
|
+
if (!keep || keep === UNKNOWN)
|
|
547
|
+
continue;
|
|
548
|
+
}
|
|
549
|
+
count++;
|
|
550
|
+
html += this.renderNodes(node.body, { ...scope, env });
|
|
551
|
+
}
|
|
552
|
+
if (count === 0 && node.empty)
|
|
553
|
+
html += this.renderNodes(node.empty, scope);
|
|
554
|
+
return html;
|
|
555
|
+
}
|
|
556
|
+
case "slot": {
|
|
557
|
+
const fill = scope.slots?.[node.name];
|
|
558
|
+
return fill ? fill() : this.renderNodes(node.fallback, scope);
|
|
559
|
+
}
|
|
560
|
+
case "stmt":
|
|
561
|
+
try {
|
|
562
|
+
this.exec(node.stmt, scope.env, scope.file);
|
|
563
|
+
}
|
|
564
|
+
catch {
|
|
565
|
+
this.complete = false;
|
|
566
|
+
}
|
|
567
|
+
return "";
|
|
568
|
+
case "var":
|
|
569
|
+
this.declare(node.decl, scope.env, scope.file);
|
|
570
|
+
return "";
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
renderUiFunction(fn, args, scope) {
|
|
574
|
+
const env = new Env(this.moduleEnv(scope.file));
|
|
575
|
+
fn.params.forEach((p, i) => env.define(p.name, args[i] === undefined && p.defaultValue ? this.eval(p.defaultValue, env, scope.file) : args[i]));
|
|
576
|
+
for (const s of fn.body)
|
|
577
|
+
if (s.k === "var")
|
|
578
|
+
this.declare(s.decl, env, scope.file);
|
|
579
|
+
return this.renderNodes(fn.view ?? [], { ...scope, env });
|
|
580
|
+
}
|
|
581
|
+
attrValue(a, scope) {
|
|
582
|
+
if (a.value === null)
|
|
583
|
+
return true;
|
|
584
|
+
return this.eval(a.value, scope.env, scope.file);
|
|
585
|
+
}
|
|
586
|
+
renderElement(node, scope) {
|
|
587
|
+
const comp = scope.compName === node.name && elements_1.ELEMENTS[node.name] ? undefined : this.components(scope.file).get(node.name);
|
|
588
|
+
if (comp)
|
|
589
|
+
return this.renderComponent(node, comp, scope);
|
|
590
|
+
if (node.name === "Fragment")
|
|
591
|
+
return this.renderNodes(node.children, scope);
|
|
592
|
+
if (node.name === "RawHTML" || node.name === "Html") {
|
|
593
|
+
const c = node.attrs.find(a => a.name === "content" || a.name === "html");
|
|
594
|
+
const v = c ? this.attrValue(c, scope) : "";
|
|
595
|
+
if (v === UNKNOWN) {
|
|
596
|
+
this.complete = false;
|
|
597
|
+
return "";
|
|
598
|
+
}
|
|
599
|
+
return toText(v);
|
|
600
|
+
}
|
|
601
|
+
const info = elements_1.ELEMENTS[node.name];
|
|
602
|
+
if (!info)
|
|
603
|
+
return "";
|
|
604
|
+
let tag = info.tag;
|
|
605
|
+
const attrs = new Map(Object.entries(info.attrs ?? {}));
|
|
606
|
+
const classes = [];
|
|
607
|
+
if (info.className)
|
|
608
|
+
classes.push(info.className);
|
|
609
|
+
if (scope.scopeClass)
|
|
610
|
+
classes.push(scope.scopeClass);
|
|
611
|
+
const styles = [];
|
|
612
|
+
let inner;
|
|
613
|
+
for (const a of node.attrs) {
|
|
614
|
+
if (a.handler || a.name === "ref" || a.name === "key" || a.name === "...")
|
|
615
|
+
continue;
|
|
616
|
+
if (a.name === "level" && node.name === "Heading") {
|
|
617
|
+
const lv = this.attrValue(a, scope);
|
|
618
|
+
if (typeof lv === "number")
|
|
619
|
+
tag = `h${Math.min(6, Math.max(1, lv))}`;
|
|
620
|
+
continue;
|
|
621
|
+
}
|
|
622
|
+
const v = this.attrValue(a, scope);
|
|
623
|
+
if (v === UNKNOWN) {
|
|
624
|
+
this.complete = false;
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
switch (a.name) {
|
|
628
|
+
case "class":
|
|
629
|
+
classes.push(classValue(v));
|
|
630
|
+
continue;
|
|
631
|
+
case "style":
|
|
632
|
+
if (v && typeof v === "object") {
|
|
633
|
+
for (const [k, sv] of Object.entries(v))
|
|
634
|
+
if (sv !== null && sv !== undefined && sv !== false)
|
|
635
|
+
styles.push(`${kebab(k)}: ${String(sv)}`);
|
|
636
|
+
}
|
|
637
|
+
else if (typeof v === "string")
|
|
638
|
+
styles.push(v);
|
|
639
|
+
continue;
|
|
640
|
+
case "model":
|
|
641
|
+
case "bind":
|
|
642
|
+
if (attrs.get("type") === "checkbox" || node.name === "Checkbox") {
|
|
643
|
+
if (v === true)
|
|
644
|
+
attrs.set("checked", "");
|
|
645
|
+
}
|
|
646
|
+
else if (tag === "textarea")
|
|
647
|
+
inner = (0, html_1.escapeHtml)(toText(v));
|
|
648
|
+
else if (tag !== "select")
|
|
649
|
+
attrs.set("value", toText(v));
|
|
650
|
+
continue;
|
|
651
|
+
case "hide":
|
|
652
|
+
if (v)
|
|
653
|
+
styles.push("display: none");
|
|
654
|
+
continue;
|
|
655
|
+
case "show":
|
|
656
|
+
if (!v)
|
|
657
|
+
styles.push("display: none");
|
|
658
|
+
continue;
|
|
659
|
+
case "delay":
|
|
660
|
+
styles.push("display: none");
|
|
661
|
+
continue;
|
|
662
|
+
case "html":
|
|
663
|
+
inner = toText(v);
|
|
664
|
+
continue;
|
|
665
|
+
default:
|
|
666
|
+
break;
|
|
667
|
+
}
|
|
668
|
+
if (elements_1.INLINE_STYLE_PROPS.has(a.name) && !((a.name === "width" || a.name === "height") && ["img", "video", "canvas", "iframe", "svg", "input", "rect", "embed", "object", "source"].includes(tag))) {
|
|
669
|
+
if (v !== null && v !== undefined && v !== false)
|
|
670
|
+
styles.push(`${kebab(a.name)}: ${String(v)}`);
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
if (v === null || v === undefined || v === false)
|
|
674
|
+
continue;
|
|
675
|
+
const name = (0, elements_1.attrNameToHtml)(a.name);
|
|
676
|
+
if (/^on/i.test(name))
|
|
677
|
+
continue;
|
|
678
|
+
const str = v === true ? "" : typeof v === "object" ? JSON.stringify(v) : String(v);
|
|
679
|
+
attrs.set(name, /^(href|src|action|formaction|poster)$/.test(name) && /^\s*(javascript|vbscript):/i.test(str) ? "about:blank#blocked" : str);
|
|
680
|
+
}
|
|
681
|
+
const cls = classes.filter(Boolean).join(" ");
|
|
682
|
+
if (cls)
|
|
683
|
+
attrs.set("class", cls);
|
|
684
|
+
if (styles.length)
|
|
685
|
+
attrs.set("style", [attrs.get("style"), ...styles].filter(Boolean).join("; "));
|
|
686
|
+
const attrStr = [...attrs].map(([k, v]) => (v === "" && elements_1.BOOLEAN_ATTRS.has(k) ? ` ${k}` : ` ${k}="${(0, html_1.escapeAttr)(v)}"`)).join("");
|
|
687
|
+
if (elements_1.VOID_TAGS.has(tag))
|
|
688
|
+
return `<${tag}${attrStr}>`;
|
|
689
|
+
const children = inner ?? this.renderNodes(node.children, scope);
|
|
690
|
+
return `<${tag}${attrStr}>${children}</${tag}>`;
|
|
691
|
+
}
|
|
692
|
+
renderComponent(node, ref, scope) {
|
|
693
|
+
if (++this.depth > 64) {
|
|
694
|
+
this.depth--;
|
|
695
|
+
throw new Error(`Component nesting is deeper than 64 levels at '${node.name}' (recursive component without a stop condition?)`);
|
|
696
|
+
}
|
|
697
|
+
try {
|
|
698
|
+
const decl = ref.decl;
|
|
699
|
+
const moduleEnv = this.moduleEnv(ref.file);
|
|
700
|
+
const env = new Env(moduleEnv);
|
|
701
|
+
const fallthrough = [];
|
|
702
|
+
for (const p of decl.params) {
|
|
703
|
+
const a = node.attrs.find(x => x.name === p.name);
|
|
704
|
+
if (a && !a.handler)
|
|
705
|
+
env.define(p.name, this.attrValue(a, scope));
|
|
706
|
+
else if (a?.handler)
|
|
707
|
+
env.define(p.name, UNKNOWN);
|
|
708
|
+
else
|
|
709
|
+
env.define(p.name, p.defaultValue ? this.eval(p.defaultValue, env, ref.file) : undefined);
|
|
710
|
+
}
|
|
711
|
+
for (const a of node.attrs)
|
|
712
|
+
if (!decl.params.some(p => p.name === a.name) && !a.handler)
|
|
713
|
+
fallthrough.push(a);
|
|
714
|
+
for (const v of decl.vars)
|
|
715
|
+
this.declare(v, env, ref.file);
|
|
716
|
+
if (decl.scripts.length)
|
|
717
|
+
this.complete = false;
|
|
718
|
+
const slots = {};
|
|
719
|
+
if (node.children.length)
|
|
720
|
+
slots.default = () => this.renderNodes(node.children, scope);
|
|
721
|
+
for (const sf of node.slots)
|
|
722
|
+
slots[sf.name] = () => this.renderNodes(sf.children, scope);
|
|
723
|
+
const scopeClass = `s-${(0, html_1.hashString)(ref.file.path.replace(/\\/g, "/") + "#" + decl.name).slice(0, 7)}`;
|
|
724
|
+
const inner = { env, file: ref.file, scopeClass, slots, compName: decl.name };
|
|
725
|
+
let html = this.renderNodes(decl.body, inner);
|
|
726
|
+
const vars = this.styleVars(decl, ref.file, env);
|
|
727
|
+
const extraClass = fallthrough.filter(a => a.name === "class").map(a => classValue(this.attrValue(a, scope))).join(" ");
|
|
728
|
+
const extraAttrs = fallthrough
|
|
729
|
+
.filter(a => a.name !== "class" && a.name !== "style" && a.name !== "model" && a.name !== "bind" && a.name !== "ref")
|
|
730
|
+
.map(a => [(0, elements_1.attrNameToHtml)(a.name), this.attrValue(a, scope)])
|
|
731
|
+
.filter(([, v]) => v !== UNKNOWN && v !== null && v !== undefined && v !== false);
|
|
732
|
+
if (vars || extraClass || extraAttrs.length) {
|
|
733
|
+
html = html.replace(/^<([a-zA-Z][\w-]*)((?:\s[^>]*)?)>/, (_m, tag, rest) => {
|
|
734
|
+
let attrs = rest;
|
|
735
|
+
if (extraClass)
|
|
736
|
+
attrs = /\sclass="/.test(attrs) ? attrs.replace(/\sclass="([^"]*)"/, (_c, c) => ` class="${c} ${(0, html_1.escapeAttr)(extraClass)}"`) : `${attrs} class="${(0, html_1.escapeAttr)(extraClass)}"`;
|
|
737
|
+
if (vars)
|
|
738
|
+
attrs = /\sstyle="/.test(attrs) ? attrs.replace(/\sstyle="([^"]*)"/, (_c, c) => ` style="${c}; ${(0, html_1.escapeAttr)(vars)}"`) : `${attrs} style="${(0, html_1.escapeAttr)(vars)}"`;
|
|
739
|
+
for (const [k, v] of extraAttrs)
|
|
740
|
+
if (!new RegExp(`\\s${k}=`).test(attrs))
|
|
741
|
+
attrs += v === true ? ` ${k}` : ` ${k}="${(0, html_1.escapeAttr)(String(v))}"`;
|
|
742
|
+
return `<${tag}${attrs}>`;
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
return html;
|
|
746
|
+
}
|
|
747
|
+
finally {
|
|
748
|
+
this.depth--;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
styleVars(decl, file, env) {
|
|
752
|
+
const names = new Set([...decl.params.map(p => p.name), ...decl.vars.map(v => v.name)]);
|
|
753
|
+
const parts = [];
|
|
754
|
+
for (const s of decl.styles) {
|
|
755
|
+
if (s.global)
|
|
756
|
+
continue;
|
|
757
|
+
const varPrefix = `--sv-${(0, html_1.hashString)(file.path + decl.name).slice(0, 5)}-`;
|
|
758
|
+
try {
|
|
759
|
+
const res = (0, style_1.compileStyle)(s.raw, s.lang === "scss" ? "css" : s.lang, { reactiveNames: names, varPrefix, loc: s.loc, sinthSyntax: s.lang === "sinth" });
|
|
760
|
+
for (const e of res.expressions) {
|
|
761
|
+
const expr = parser_1.Parser.fromTokens((0, lexer_1.tokenize)(e.source, file.path, s.loc), file.path).parseExpr();
|
|
762
|
+
const v = this.eval(expr, env, file);
|
|
763
|
+
if (v !== UNKNOWN && v !== null && v !== undefined)
|
|
764
|
+
parts.push(`${e.cssVar}: ${toText(v)}`);
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
catch {
|
|
768
|
+
continue;
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
return parts.join("; ");
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
exports.Prerenderer = Prerenderer;
|
|
775
|
+
function defaultValue(v) {
|
|
776
|
+
const t = v.type;
|
|
777
|
+
if (!t || t.nullable)
|
|
778
|
+
return null;
|
|
779
|
+
if (t.array)
|
|
780
|
+
return [];
|
|
781
|
+
if (t.base === "str")
|
|
782
|
+
return "";
|
|
783
|
+
if (t.base === "num" || t.base === "int")
|
|
784
|
+
return 0;
|
|
785
|
+
if (t.base === "bool")
|
|
786
|
+
return false;
|
|
787
|
+
return null;
|
|
788
|
+
}
|
|
789
|
+
function binary(op, l, r) {
|
|
790
|
+
const a = l;
|
|
791
|
+
const b = r;
|
|
792
|
+
switch (op) {
|
|
793
|
+
case "+": return l + r;
|
|
794
|
+
case "-": return a - b;
|
|
795
|
+
case "*": return a * b;
|
|
796
|
+
case "/": return a / b;
|
|
797
|
+
case "%": return a % b;
|
|
798
|
+
case "**": return a ** b;
|
|
799
|
+
case "==": return l === r;
|
|
800
|
+
case "!=": return l !== r;
|
|
801
|
+
case "<": return a < b;
|
|
802
|
+
case ">": return a > b;
|
|
803
|
+
case "<=": return a <= b;
|
|
804
|
+
case ">=": return a >= b;
|
|
805
|
+
default: return UNKNOWN;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
function getProp(obj, prop) {
|
|
809
|
+
if (BLOCKED_PROPS.has(prop))
|
|
810
|
+
return UNKNOWN;
|
|
811
|
+
if (typeof obj === "string" || typeof obj === "number" || typeof obj === "boolean") {
|
|
812
|
+
const v = Object(obj)[prop];
|
|
813
|
+
return v === undefined ? undefined : v;
|
|
814
|
+
}
|
|
815
|
+
if (obj && (typeof obj === "object" || typeof obj === "function"))
|
|
816
|
+
return obj[prop];
|
|
817
|
+
return UNKNOWN;
|
|
818
|
+
}
|
|
819
|
+
const SAFE_GLOBALS = {
|
|
820
|
+
Math, JSON, Number, String, Boolean, Array, Object, Date, parseInt, parseFloat, isNaN, isFinite,
|
|
821
|
+
encodeURIComponent, decodeURIComponent, encodeURI, decodeURI, Infinity, NaN,
|
|
822
|
+
range: (a, b, step = 1) => {
|
|
823
|
+
const s = b === undefined ? 0 : a;
|
|
824
|
+
const e = b === undefined ? a : b;
|
|
825
|
+
const out = [];
|
|
826
|
+
if (step > 0)
|
|
827
|
+
for (let i = s; i < e && out.length < 100000; i += step)
|
|
828
|
+
out.push(i);
|
|
829
|
+
else if (step < 0)
|
|
830
|
+
for (let i = s; i > e && out.length < 100000; i += step)
|
|
831
|
+
out.push(i);
|
|
832
|
+
return out;
|
|
833
|
+
},
|
|
834
|
+
};
|
|
835
|
+
const UNSAFE_FUNCTIONS = new Set([Function, Object.defineProperty, Object.defineProperties, Object.setPrototypeOf, Object.assign]);
|
|
836
|
+
function isSafeCallable(fn, thisArg) {
|
|
837
|
+
if (UNSAFE_FUNCTIONS.has(fn))
|
|
838
|
+
return false;
|
|
839
|
+
if (fn === Function.prototype.call || fn === Function.prototype.apply || fn === Function.prototype.bind)
|
|
840
|
+
return false;
|
|
841
|
+
void thisArg;
|
|
842
|
+
return true;
|
|
843
|
+
}
|
|
844
|
+
function iterate(value) {
|
|
845
|
+
if (value === null || value === undefined || value === false)
|
|
846
|
+
return [];
|
|
847
|
+
if (typeof value === "number")
|
|
848
|
+
return Array.from({ length: Math.max(0, Math.floor(value)) }, (_, i) => [i, i, i]);
|
|
849
|
+
if (Array.isArray(value))
|
|
850
|
+
return value.map((v, i) => [v, i, i]);
|
|
851
|
+
if (value instanceof Map)
|
|
852
|
+
return Array.from(value.entries()).map(([k, v], i) => [v, i, k]);
|
|
853
|
+
if (typeof value === "string")
|
|
854
|
+
return Array.from(value).map((v, i) => [v, i, i]);
|
|
855
|
+
if (typeof value === "object")
|
|
856
|
+
return Object.entries(value).map(([k, v], i) => [v, i, k]);
|
|
857
|
+
return [];
|
|
858
|
+
}
|
|
859
|
+
function toText(value) {
|
|
860
|
+
if (value === null || value === undefined || value === false || value === UNKNOWN)
|
|
861
|
+
return "";
|
|
862
|
+
if (typeof value === "string")
|
|
863
|
+
return value;
|
|
864
|
+
if (typeof value === "number")
|
|
865
|
+
return String(value);
|
|
866
|
+
if (Array.isArray(value))
|
|
867
|
+
return value.map(toText).join(", ");
|
|
868
|
+
if (value instanceof Date)
|
|
869
|
+
return value.toLocaleString("en-US");
|
|
870
|
+
if (typeof value === "object") {
|
|
871
|
+
try {
|
|
872
|
+
return JSON.stringify(value);
|
|
873
|
+
}
|
|
874
|
+
catch {
|
|
875
|
+
return "";
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
return String(value);
|
|
879
|
+
}
|
|
880
|
+
function classValue(value) {
|
|
881
|
+
if (!value || value === UNKNOWN)
|
|
882
|
+
return "";
|
|
883
|
+
if (typeof value === "string")
|
|
884
|
+
return value;
|
|
885
|
+
if (Array.isArray(value))
|
|
886
|
+
return value.map(classValue).filter(Boolean).join(" ");
|
|
887
|
+
if (typeof value === "object")
|
|
888
|
+
return Object.entries(value).filter(([, on]) => !!on).map(([k]) => k).join(" ");
|
|
889
|
+
return String(value);
|
|
890
|
+
}
|
|
891
|
+
function kebab(name) {
|
|
892
|
+
if (name.startsWith("--"))
|
|
893
|
+
return name;
|
|
894
|
+
return name.replace(/^(Webkit|Moz|Ms)/, m => "-" + m.toLowerCase()).replace(/[A-Z]/g, m => "-" + m.toLowerCase());
|
|
895
|
+
}
|
|
896
|
+
function prerenderPage(file, host, params) {
|
|
897
|
+
const p = new Prerenderer(host);
|
|
898
|
+
let html;
|
|
899
|
+
try {
|
|
900
|
+
html = p.renderPage(file, params);
|
|
901
|
+
}
|
|
902
|
+
catch {
|
|
903
|
+
html = undefined;
|
|
904
|
+
}
|
|
905
|
+
const complete = html !== undefined && p.complete;
|
|
906
|
+
return { html: html ?? "", complete, meta: p.evalMeta(file, params) };
|
|
907
|
+
}
|
|
908
|
+
function evaluatePaths(file, host, paramNames) {
|
|
909
|
+
return new Prerenderer(host).evalPaths(file, paramNames);
|
|
910
|
+
}
|