@yannosay/sinth 1.12.2 → 1.12.4
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/dist/core/cli-compiler.js +1 -1
- package/dist/core/compiler.js +56 -16
- package/dist/core/runtime/helpers.js +31 -15
- package/package.json +1 -1
|
@@ -52,7 +52,7 @@ function compileFile(filePath, opts) {
|
|
|
52
52
|
const file = (0, resolver_1.parseFile)(absPath);
|
|
53
53
|
const cfg = { projectRoot: opts.projectRoot, libraryPaths: opts.libraryPaths };
|
|
54
54
|
const { allDefs, customEls, cssLinks, jsLinks } = (0, resolver_1.resolveImports)(file, cfg);
|
|
55
|
-
const hash = (0, utils_1.fnv1a)(absPath);
|
|
55
|
+
const hash = "_" + (0, utils_1.fnv1a)(absPath);
|
|
56
56
|
const allVarDecls = file.varDecls;
|
|
57
57
|
const functionDefs = file.functions;
|
|
58
58
|
const fnLocalVarNames = new Set();
|
package/dist/core/compiler.js
CHANGED
|
@@ -104,10 +104,10 @@ function inlineFunctionBody(fnDef, args, ctx) {
|
|
|
104
104
|
for (const c of fnDef.body) {
|
|
105
105
|
const substituted = subChild(c);
|
|
106
106
|
if (substituted.kind === "assign_stmt") {
|
|
107
|
-
statements.push((0, expr_1.compileExprToJS)(substituted.expression, ctx.loopVars, ctx.namespace, ctx.declaredVars)
|
|
107
|
+
statements.push((0, expr_1.compileExprToJS)(substituted.expression, ctx.loopVars, ctx.namespace, ctx.declaredVars));
|
|
108
108
|
}
|
|
109
109
|
else if (substituted.kind === "expr") {
|
|
110
|
-
statements.push((0, expr_1.compileExprToJS)(substituted.expression, ctx.loopVars, ctx.namespace, ctx.declaredVars)
|
|
110
|
+
statements.push((0, expr_1.compileExprToJS)(substituted.expression, ctx.loopVars, ctx.namespace, ctx.declaredVars));
|
|
111
111
|
}
|
|
112
112
|
else if (substituted.kind === "if") {
|
|
113
113
|
statements.push((0, expr_1.compileIfToJS)(substituted, ctx.loopVars, ctx.namespace, ctx.declaredVars));
|
|
@@ -127,13 +127,13 @@ function inlineFunctionBody(fnDef, args, ctx) {
|
|
|
127
127
|
const bodyStmts = fl.body.map((bc) => {
|
|
128
128
|
const s = subChild(bc);
|
|
129
129
|
if (s.kind === "assign_stmt")
|
|
130
|
-
return (0, expr_1.compileExprToJS)(s.expression, innerLoopVars, ctx.namespace, ctx.declaredVars)
|
|
130
|
+
return (0, expr_1.compileExprToJS)(s.expression, innerLoopVars, ctx.namespace, ctx.declaredVars);
|
|
131
131
|
if (s.kind === "if")
|
|
132
132
|
return (0, expr_1.compileIfToJS)(s, innerLoopVars, ctx.namespace, ctx.declaredVars);
|
|
133
133
|
if (s.kind === "expr")
|
|
134
|
-
return (0, expr_1.compileExprToJS)(s.expression, innerLoopVars, ctx.namespace, ctx.declaredVars)
|
|
134
|
+
return (0, expr_1.compileExprToJS)(s.expression, innerLoopVars, ctx.namespace, ctx.declaredVars);
|
|
135
135
|
return "";
|
|
136
|
-
}).filter(Boolean).join(" ");
|
|
136
|
+
}).filter(Boolean).join("; ");
|
|
137
137
|
const nsArrayVar = (ctx.namespace && ctx.declaredVars && ctx.declaredVars.has(fl.arrayVar)) ? ctx.namespace + "_" + fl.arrayVar : fl.arrayVar;
|
|
138
138
|
if (fl.indexVar) {
|
|
139
139
|
const alreadyDeclared = fnDef.body.some((bc) => bc.kind === "var" && bc.name === fl.indexVar);
|
|
@@ -194,7 +194,7 @@ function inlineFunctionBody(fnDef, args, ctx) {
|
|
|
194
194
|
}
|
|
195
195
|
if (statements.length === 0)
|
|
196
196
|
return null;
|
|
197
|
-
return statements.join(" ");
|
|
197
|
+
return statements.join("; ");
|
|
198
198
|
}
|
|
199
199
|
function resolveBuiltinTag(name, attrs) {
|
|
200
200
|
if (name === "Heading") {
|
|
@@ -353,7 +353,13 @@ function renderAttr(attr, paramMap, ctx) {
|
|
|
353
353
|
return `${name}="${value.value}"`;
|
|
354
354
|
let raw = value.value;
|
|
355
355
|
if (raw.startsWith("__ACTION_JS__")) {
|
|
356
|
-
|
|
356
|
+
let js = raw.substring("__ACTION_JS__".length).replace(/;\s*$/, '');
|
|
357
|
+
if (ctx.namespace && ctx.declaredVars) {
|
|
358
|
+
for (const v of ctx.declaredVars) {
|
|
359
|
+
const regex = new RegExp(`\\b${v}\\b`, 'g');
|
|
360
|
+
js = js.replace(regex, ctx.namespace + '_' + v);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
357
363
|
const ev = eventAttrName(name);
|
|
358
364
|
if (ev)
|
|
359
365
|
return `${ev}="(function(){ ${js.replace(/"/g, """)}; ${renderFn}(); })()"`;
|
|
@@ -363,7 +369,14 @@ function renderAttr(attr, paramMap, ctx) {
|
|
|
363
369
|
const exprJson = raw.substring("__MULTI_EXPR__".length);
|
|
364
370
|
try {
|
|
365
371
|
const exprs = JSON.parse(exprJson);
|
|
366
|
-
|
|
372
|
+
let jsExprs = exprs.map(e => (0, expr_1.compileExprToJS)(e, undefined, ctx.namespace, ctx.declaredVars)).join("; ");
|
|
373
|
+
jsExprs = jsExprs.replace(/;\s*$/, '');
|
|
374
|
+
if (ctx.namespace && ctx.declaredVars) {
|
|
375
|
+
for (const v of ctx.declaredVars) {
|
|
376
|
+
const regex = new RegExp(`\\b${v}\\b`, 'g');
|
|
377
|
+
jsExprs = jsExprs.replace(regex, ctx.namespace + '_' + v);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
367
380
|
const ev = eventAttrName(name);
|
|
368
381
|
let bumps = "";
|
|
369
382
|
if (ctx.diffingEnabled) {
|
|
@@ -392,11 +405,18 @@ function renderAttr(attr, paramMap, ctx) {
|
|
|
392
405
|
if (fnDef) {
|
|
393
406
|
const inlinedJS = inlineFunctionBody(fnDef, expr.args ?? [], ctx);
|
|
394
407
|
if (inlinedJS) {
|
|
395
|
-
|
|
408
|
+
const handlerJS = inlinedJS.replace(/;\s*$/, '');
|
|
409
|
+
return `${ev}="(function(){ ${handlerJS.replace(/"/g, """)}; ${renderFn}(); })()"`;
|
|
396
410
|
}
|
|
397
411
|
}
|
|
398
412
|
}
|
|
399
413
|
let jsExpr = (0, expr_1.compileExprToJS)(expr, ctx?.loopVars, ctx.namespace, ctx.declaredVars);
|
|
414
|
+
if (ctx.namespace && ctx.declaredVars && !ctx.scopePrefix) {
|
|
415
|
+
for (const v of ctx.declaredVars) {
|
|
416
|
+
const regex = new RegExp(`\\b${v}\\b`, 'g');
|
|
417
|
+
jsExpr = jsExpr.replace(regex, ctx.namespace + '_' + v);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
400
420
|
if (ctx.scopePrefix && ctx.scopeVar) {
|
|
401
421
|
if (expr.kind === "variable" && expr.name && ctx.varDecls?.some(v => v.name === expr.name)) {
|
|
402
422
|
jsExpr = `${ctx.scopePrefix}${expr.name}`;
|
|
@@ -414,7 +434,7 @@ function renderAttr(attr, paramMap, ctx) {
|
|
|
414
434
|
bump = `;window._bump&&window._bump('${expr.target.replace(/'/g, "\\'")}')`;
|
|
415
435
|
}
|
|
416
436
|
if (ev) {
|
|
417
|
-
const handlerBody = `${jsExpr}${bump}; ${renderCall}`;
|
|
437
|
+
const handlerBody = `${jsExpr.replace(/;\s*$/, '')}${bump}; ${renderCall}`;
|
|
418
438
|
if (ctx.scopePrefix) {
|
|
419
439
|
return emitCEAction(ctx, ev, handlerBody);
|
|
420
440
|
}
|
|
@@ -729,7 +749,26 @@ function renderIfBlock(ifBlock, ctx, params, depth) {
|
|
|
729
749
|
const bodyHTML = ifBlock.body.map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
730
750
|
const elseHTML = (ifBlock.elseBody ?? []).map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
731
751
|
let replaceAttr = "";
|
|
732
|
-
|
|
752
|
+
const hasStatefulComp = ifBlock.body.some(c => {
|
|
753
|
+
if (c.kind !== "use")
|
|
754
|
+
return false;
|
|
755
|
+
const u = c;
|
|
756
|
+
if (u.name === "__IF_ROOT__")
|
|
757
|
+
return false;
|
|
758
|
+
if (ctx.customEls.has(u.name))
|
|
759
|
+
return true;
|
|
760
|
+
return u.attrs.some(a => a.name === "bind" || a.name === "model" || a.name === "fullscreenSync");
|
|
761
|
+
}) || (ifBlock.elseBody ?? []).some(c => {
|
|
762
|
+
if (c.kind !== "use")
|
|
763
|
+
return false;
|
|
764
|
+
const u = c;
|
|
765
|
+
if (u.name === "__IF_ROOT__")
|
|
766
|
+
return false;
|
|
767
|
+
if (ctx.customEls.has(u.name))
|
|
768
|
+
return true;
|
|
769
|
+
return u.attrs.some(a => a.name === "bind" || a.name === "model" || a.name === "fullscreenSync");
|
|
770
|
+
});
|
|
771
|
+
let persistAttr = (ifBlock.persist || hasStatefulComp) ? ` data-sinth-if-persist="true"` : "";
|
|
733
772
|
const firstComp = ifBlock.body.find(c => c.kind === "use");
|
|
734
773
|
if (firstComp) {
|
|
735
774
|
const idAttr = firstComp.attrs.find(a => a.name === "id");
|
|
@@ -1184,14 +1223,15 @@ function buildRuntime(opts) {
|
|
|
1184
1223
|
const ns = opts.namespace ? "_" + opts.namespace : "";
|
|
1185
1224
|
const renderFn = "sinthRender" + ns;
|
|
1186
1225
|
const needsMixed = mixedBlocks.length > 0;
|
|
1187
|
-
const
|
|
1226
|
+
const needsDynamicAttrs = bodyHTML.includes("data-sinth-style") || bodyHTML.includes("data-sinth-value") || bodyHTML.includes("data-sinth-step") || bodyHTML.includes("data-sinth-checked");
|
|
1227
|
+
const needsExpr = bodyHTML.includes("sinth-expr") || needsMixed || needsDynamicAttrs;
|
|
1188
1228
|
const needsIf = bodyHTML.includes("data-sinth-if") || needsMixed;
|
|
1189
1229
|
const needsFor = bodyHTML.includes("data-sinth-for") || bodyHTML.includes("data-sinth-for-expr");
|
|
1190
1230
|
const needsDelay = bodyHTML.includes("data-sinth-delay") || bodyHTML.includes("data-sinth-delay-expr-id") || mixedBlocks.some(mb => mb.ifHTML.includes("data-sinth-delay") || mb.ifHTML.includes("data-sinth-delay-expr-id") || mb.elseHTML.includes("data-sinth-delay") || mb.elseHTML.includes("data-sinth-delay-expr-id"));
|
|
1191
1231
|
const needsLogic = logicBlocks.length > 0;
|
|
1192
1232
|
const needsFullscreen = bodyHTML.includes("data-sinth-fullscreen");
|
|
1193
1233
|
const needsFullscreenSync = bodyHTML.includes("data-sinth-fullscreen-sync");
|
|
1194
|
-
const needsRender = needsExpr || needsIf || needsFor || needsMixed || needsLogic || bodyHTML.includes("data-sinth-hide") || needsFullscreen || needsFullscreenSync || needsDelay;
|
|
1234
|
+
const needsRender = needsExpr || needsIf || needsFor || needsMixed || needsLogic || bodyHTML.includes("data-sinth-hide") || needsFullscreen || needsFullscreenSync || needsDelay || needsDynamicAttrs;
|
|
1195
1235
|
const varLines = varDecls.map(v => {
|
|
1196
1236
|
const vn = ns ? ns.slice(1) + "_" + v.name : v.name;
|
|
1197
1237
|
if (!v.value) {
|
|
@@ -1251,7 +1291,7 @@ function buildRuntime(opts) {
|
|
|
1251
1291
|
const memoVarDecls = [...memoVars].map(m => `var ${m};`).join("\n");
|
|
1252
1292
|
if (!needsRender) {
|
|
1253
1293
|
const out = [varLines, memoVarDecls].filter(Boolean).join("\n");
|
|
1254
|
-
return out ? `// Sinth
|
|
1294
|
+
return out ? `// Sinth Compiled Runtime\n${out}` : "";
|
|
1255
1295
|
}
|
|
1256
1296
|
const helpers = (0, helpers_1.generateHelpers)({ needsExpr, needsIf, needsFor, needsDelay, needsMixed, ns });
|
|
1257
1297
|
const exprVarUpdates = varDecls
|
|
@@ -1330,11 +1370,11 @@ ${memoVarDecls}
|
|
|
1330
1370
|
${varLines}
|
|
1331
1371
|
${renderFunc}`;
|
|
1332
1372
|
if (opts.sharedRuntime && helpers.trim()) {
|
|
1333
|
-
const sharedCode = `// Sinth
|
|
1373
|
+
const sharedCode = `// Sinth Shared Runtime
|
|
1334
1374
|
${helpers}`;
|
|
1335
1375
|
return { page: pageCode, shared: sharedCode };
|
|
1336
1376
|
}
|
|
1337
|
-
return `// Sinth
|
|
1377
|
+
return `// Sinth Compiled Runtime
|
|
1338
1378
|
${forDataJS}
|
|
1339
1379
|
${functionsJS ? functionsJS + "\n" : ""}${helpers}
|
|
1340
1380
|
${exprArrayJS}
|
|
@@ -40,6 +40,14 @@ function sinthReplaceInsert(t, anchor, ifId, replaceId) {
|
|
|
40
40
|
_rpParent = _rp.parentNode;
|
|
41
41
|
_rpNext = _rp.nextSibling;
|
|
42
42
|
_rp.parentNode.removeChild(_rp);
|
|
43
|
+
let src = _rp.previousSibling;
|
|
44
|
+
while (src && src !== _rp.parentNode.firstChild) {
|
|
45
|
+
if (src.dataset && src.dataset.sinthIfAnchor) {
|
|
46
|
+
anchor._sinthReplaceSourceIfId = src.dataset.sinthIfAnchor;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
src = src.previousSibling;
|
|
50
|
+
}
|
|
43
51
|
anchor._sinthReplaced = _rp;
|
|
44
52
|
anchor._sinthReplacedParent = _rpParent;
|
|
45
53
|
anchor._sinthReplacedNext = _rpNext;
|
|
@@ -117,7 +125,9 @@ function sinthDelayExpr(el, _ctx) {
|
|
|
117
125
|
helpers += `
|
|
118
126
|
function sinthIfBlock(t) {
|
|
119
127
|
let ifId = t.dataset.sinthIfId;
|
|
120
|
-
let
|
|
128
|
+
let replaceId = t.dataset.sinthIfReplace || null;
|
|
129
|
+
let anchorKey = replaceId ? '__replace__' + replaceId : ifId;
|
|
130
|
+
let anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + anchorKey + '"]');
|
|
121
131
|
let condFn = ${X}[t.dataset.sinthIfExpr];
|
|
122
132
|
let cond = condFn ? condFn() : false;
|
|
123
133
|
if (cond) {
|
|
@@ -133,10 +143,10 @@ function sinthIfBlock(t) {
|
|
|
133
143
|
if (t.dataset.sinthIfDelayHide === 'false' && t.dataset.sinthIfDelay && _hasContent) {
|
|
134
144
|
let dms = parseInt(t.dataset.sinthIfDelay) || 0;
|
|
135
145
|
setTimeout(function() {
|
|
136
|
-
sinthReplaceInsert(t, anchor,
|
|
146
|
+
sinthReplaceInsert(t, anchor, anchorKey, replaceId);
|
|
137
147
|
}, dms);
|
|
138
148
|
} else {
|
|
139
|
-
anchor = sinthReplaceInsert(t, anchor,
|
|
149
|
+
anchor = sinthReplaceInsert(t, anchor, anchorKey, replaceId);
|
|
140
150
|
}
|
|
141
151
|
} else {
|
|
142
152
|
let runElse = function() {
|
|
@@ -150,6 +160,7 @@ function sinthIfBlock(t) {
|
|
|
150
160
|
}
|
|
151
161
|
if (anchor) {
|
|
152
162
|
if (anchor._sinthReplaced) {
|
|
163
|
+
var restored = anchor._sinthReplaced;
|
|
153
164
|
let insFirst = anchor._sinthInsertedFirst;
|
|
154
165
|
let insLast = anchor._sinthInsertedLast;
|
|
155
166
|
let rpParent = anchor._sinthReplacedParent;
|
|
@@ -164,22 +175,27 @@ function sinthIfBlock(t) {
|
|
|
164
175
|
if (insLast) insLast.remove();
|
|
165
176
|
}
|
|
166
177
|
if (rpParent && rpNext) {
|
|
167
|
-
rpParent.insertBefore(
|
|
178
|
+
rpParent.insertBefore(restored, rpNext);
|
|
168
179
|
} else if (rpParent) {
|
|
169
|
-
rpParent.appendChild(
|
|
180
|
+
rpParent.appendChild(restored);
|
|
170
181
|
}
|
|
171
|
-
if (
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
sinthDelay(anchor._sinthReplaced);
|
|
182
|
+
if (restored.id) {
|
|
183
|
+
var allDup = t.parentNode.querySelectorAll('[id="' + restored.id + '"]');
|
|
184
|
+
for (var di = 0; di < allDup.length; di++) {
|
|
185
|
+
if (allDup[di] !== restored) allDup[di].remove();
|
|
176
186
|
}
|
|
177
|
-
anchor._sinthReplaced = null;
|
|
178
|
-
anchor._sinthReplacedParent = null;
|
|
179
|
-
anchor._sinthReplacedNext = null;
|
|
180
|
-
anchor._sinthInsertedFirst = null;
|
|
181
|
-
anchor._sinthInsertedLast = null;
|
|
182
187
|
}
|
|
188
|
+
restored.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
189
|
+
if (restored.dataset.sinthDelay) {
|
|
190
|
+
delete restored.dataset.sinthDelayDone;
|
|
191
|
+
sinthDelay(restored);
|
|
192
|
+
}
|
|
193
|
+
anchor._sinthReplaced = null;
|
|
194
|
+
anchor._sinthReplacedParent = null;
|
|
195
|
+
anchor._sinthReplacedNext = null;
|
|
196
|
+
anchor._sinthInsertedFirst = null;
|
|
197
|
+
anchor._sinthInsertedLast = null;
|
|
198
|
+
anchor._sinthReplaceSourceIfId = null;
|
|
183
199
|
} else {
|
|
184
200
|
let cur2 = anchor.nextSibling;
|
|
185
201
|
while (cur2 && cur2 !== t) { let nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
|
package/package.json
CHANGED