@yannosay/sinth 1.12.4 → 1.12.5

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.
@@ -431,6 +431,7 @@ function compileFile(filePath, opts) {
431
431
  functionsJS: compiledFunctions,
432
432
  namespace: hash,
433
433
  declaredVars: ctx.declaredVars,
434
+ initLogic: file.initLogic,
434
435
  });
435
436
  const runtimeJS = typeof runtimeResult === 'string' ? runtimeResult : runtimeResult.page;
436
437
  const sharedJS = typeof runtimeResult === 'string' ? null : runtimeResult.shared;
@@ -1217,7 +1217,6 @@ function extractFunctionNames(js) {
1217
1217
  names.push(m[1]);
1218
1218
  return names;
1219
1219
  }
1220
- // sinth runtime
1221
1220
  function buildRuntime(opts) {
1222
1221
  const { varDecls, bodyHTML, logicBlocks, mixedBlocks, assignedVars, exprRegistry, functionsJS } = opts;
1223
1222
  const ns = opts.namespace ? "_" + opts.namespace : "";
@@ -1337,9 +1336,20 @@ function buildRuntime(opts) {
1337
1336
  const exprArrayJS = exprRegistry.length > 0
1338
1337
  ? `let __X${ns} = [${exprRegistry.map((js) => `function(_ctx){ return ${js}; }`).join(",")}];\n`
1339
1338
  : "";
1339
+ let initLogicJS = "";
1340
+ if (opts.initLogic && opts.initLogic.length > 0) {
1341
+ let logic = opts.initLogic.join("\n");
1342
+ if (opts.namespace && opts.declaredVars) {
1343
+ for (const v of opts.declaredVars) {
1344
+ const re = new RegExp(`\\b${v}\\b`, 'g');
1345
+ logic = logic.replace(re, opts.namespace + "_" + v);
1346
+ }
1347
+ }
1348
+ initLogicJS = logic + "\n";
1349
+ }
1340
1350
  let renderFunc = '';
1341
1351
  if (needsRender) {
1342
- renderFunc = `function ${renderFn}() {\n ${forSyncBlock}${renderBody.replace(/^/gm, " ")}\n}\n${renderFn}();`;
1352
+ renderFunc = `function ${renderFn}() {\n ${forSyncBlock}${renderBody.replace(/^/gm, " ")}\n}\n${initLogicJS}${renderFn}();`;
1343
1353
  }
1344
1354
  if (needsFullscreenSync) {
1345
1355
  renderFunc += `
@@ -24,6 +24,7 @@ class Parser {
24
24
  const scripts = [];
25
25
  const customEls = [];
26
26
  const varDecls = [];
27
+ const initLogic = [];
27
28
  this._varDecls = varDecls;
28
29
  if (this.check(types_1.TT.KW_PAGE)) {
29
30
  this.consume(types_1.TT.KW_PAGE);
@@ -66,7 +67,7 @@ class Parser {
66
67
  }
67
68
  else if (this.check(types_1.TT.KW_IF)) {
68
69
  const ifNode = this.parseIfBlock();
69
- uses.push(...this.flattenIfToUses(ifNode, scripts));
70
+ uses.push(...this.flattenIfToUses(ifNode, scripts, initLogic));
70
71
  }
71
72
  else if (this.check(types_1.TT.KW_FOR)) {
72
73
  const forNode = this.parseForLoop();
@@ -87,7 +88,6 @@ class Parser {
87
88
  const savedPos = this.pos;
88
89
  const identName = this.peek().value;
89
90
  const nextType = this.tokens[this.pos + 1]?.type;
90
- // detect accidental top-level expressions (guess == target" instead of "if guess == target")
91
91
  const isComparison = [types_1.TT.OP_EQEQ, types_1.TT.OP_NEQ, types_1.TT.OP_LT, types_1.TT.OP_GT, types_1.TT.OP_LTEQ, types_1.TT.OP_GTEQ].includes(nextType);
92
92
  const isLogicAndOr = nextType === types_1.TT.IDENT && (this.tokens[this.pos + 1]?.value === "and" || this.tokens[this.pos + 1]?.value === "or");
93
93
  if (isComparison || isLogicAndOr) {
@@ -110,9 +110,7 @@ class Parser {
110
110
  this.consume(types_1.TT.EQUALS);
111
111
  const expr = this.parseExpression();
112
112
  if (expr) {
113
- const js = `${identName} ${op} ${(0, expr_1.compileExprToJS)(expr)};
114
- sinthRender();`;
115
- scripts.push({ raw: js, attrs: {}, loc: this.peek().loc });
113
+ initLogic.push(`${identName} ${op} ${(0, expr_1.compileExprToJS)(expr)};`);
116
114
  }
117
115
  continue;
118
116
  }
@@ -140,13 +138,13 @@ sinthRender();`;
140
138
  throw new types_1.SinthError(`Unexpected token '${this.peek().value}' (${types_1.TT[this.peek().type]}) at top level`, this.peek().loc);
141
139
  }
142
140
  this._visited.clear();
143
- return { filePath: this.file, isPage, imports, meta, defs, functions, uses, styles, scripts, customEls, varDecls };
141
+ return { filePath: this.file, isPage, imports, meta, defs, functions, uses, styles, scripts, customEls, varDecls, initLogic };
144
142
  }
145
- flattenIfToUses(ifNode, scripts) {
143
+ flattenIfToUses(ifNode, scripts, initLogic) {
146
144
  const bodyHasComp = ifNode.body.some(c => c.kind === "use");
147
145
  const elseHasComp = (ifNode.elseBody ?? []).some(c => c.kind === "use");
148
146
  if (!bodyHasComp && !elseHasComp) {
149
- scripts.push({ raw: (0, expr_1.compileIfToJS)(ifNode), attrs: {}, loc: ifNode.loc });
147
+ initLogic.push((0, expr_1.compileIfToJS)(ifNode));
150
148
  return [];
151
149
  }
152
150
  const syntheticUse = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "1.12.4",
3
+ "version": "1.12.5",
4
4
  "description": "A declarative & reactive language that compiles to pure HTML, CSS, and JavaScript.",
5
5
  "author": "Yannosay Productions",
6
6
  "license": "AGPL-3.0",