@lark.js/mvc 0.0.9 → 0.0.11

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/vite.cjs CHANGED
@@ -5,6 +5,9 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __esm = (fn, res) => function __init() {
9
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
10
+ };
8
11
  var __commonJS = (cb, mod) => function __require() {
9
12
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
13
  };
@@ -30,10 +33,18 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
33
  ));
31
34
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
35
 
36
+ // ../../node_modules/.pnpm/tsup@8.5.1_@swc+core@1.15.41_@swc+helpers@0.5.23__jiti@2.7.0_postcss@8.5.15_tsx@4.22.4_typescript@5.9.3_yaml@2.9.0/node_modules/tsup/assets/cjs_shims.js
37
+ var init_cjs_shims = __esm({
38
+ "../../node_modules/.pnpm/tsup@8.5.1_@swc+core@1.15.41_@swc+helpers@0.5.23__jiti@2.7.0_postcss@8.5.15_tsx@4.22.4_typescript@5.9.3_yaml@2.9.0/node_modules/tsup/assets/cjs_shims.js"() {
39
+ "use strict";
40
+ }
41
+ });
42
+
33
43
  // ../../node_modules/.pnpm/@babel+parser@7.29.7/node_modules/@babel/parser/lib/index.js
34
44
  var require_lib = __commonJS({
35
45
  "../../node_modules/.pnpm/@babel+parser@7.29.7/node_modules/@babel/parser/lib/index.js"(exports2) {
36
46
  "use strict";
47
+ init_cjs_shims();
37
48
  Object.defineProperty(exports2, "__esModule", {
38
49
  value: true
39
50
  });
@@ -14518,7 +14529,7 @@ var require_lib = __commonJS({
14518
14529
  options = Object.assign({}, options);
14519
14530
  try {
14520
14531
  options.sourceType = "module";
14521
- const parser = getParser(options, input);
14532
+ const parser = getParser2(options, input);
14522
14533
  const ast = parser.parse();
14523
14534
  if (parser.sawUnambiguousESM) {
14524
14535
  return ast;
@@ -14526,7 +14537,7 @@ var require_lib = __commonJS({
14526
14537
  if (parser.ambiguousScriptDifferentAst) {
14527
14538
  try {
14528
14539
  options.sourceType = "script";
14529
- return getParser(options, input).parse();
14540
+ return getParser2(options, input).parse();
14530
14541
  } catch (_unused) {
14531
14542
  }
14532
14543
  } else {
@@ -14536,17 +14547,17 @@ var require_lib = __commonJS({
14536
14547
  } catch (moduleError) {
14537
14548
  try {
14538
14549
  options.sourceType = "script";
14539
- return getParser(options, input).parse();
14550
+ return getParser2(options, input).parse();
14540
14551
  } catch (_unused2) {
14541
14552
  }
14542
14553
  throw moduleError;
14543
14554
  }
14544
14555
  } else {
14545
- return getParser(options, input).parse();
14556
+ return getParser2(options, input).parse();
14546
14557
  }
14547
14558
  }
14548
14559
  function parseExpression(input, options) {
14549
- const parser = getParser(options, input);
14560
+ const parser = getParser2(options, input);
14550
14561
  if (parser.options.strictMode) {
14551
14562
  parser.state.strict = true;
14552
14563
  }
@@ -14560,7 +14571,7 @@ var require_lib = __commonJS({
14560
14571
  return tokenTypes2;
14561
14572
  }
14562
14573
  var tokTypes = generateExportedTokenTypes(tt);
14563
- function getParser(options, input) {
14574
+ function getParser2(options, input) {
14564
14575
  let cls = Parser;
14565
14576
  const pluginsMap = /* @__PURE__ */ new Map();
14566
14577
  if (options != null && options.plugins) {
@@ -14612,11 +14623,18 @@ __export(vite_exports, {
14612
14623
  larkMvcPlugin: () => larkMvcPlugin
14613
14624
  });
14614
14625
  module.exports = __toCommonJS(vite_exports);
14626
+ init_cjs_shims();
14615
14627
  var import_path = __toESM(require("path"), 1);
14616
14628
  var import_fs = __toESM(require("fs"), 1);
14617
14629
 
14618
14630
  // src/compiler.ts
14619
- var import_parser = __toESM(require_lib(), 1);
14631
+ init_cjs_shims();
14632
+
14633
+ // src/compiler/compile-template.ts
14634
+ init_cjs_shims();
14635
+
14636
+ // src/compiler/template-syntax.ts
14637
+ init_cjs_shims();
14620
14638
  var SPLITTER = String.fromCharCode(30);
14621
14639
  var VIEW_ID_PLACEHOLDER = String.fromCharCode(31);
14622
14640
  function jsObjectToUrlParams(paramsStr) {
@@ -14737,7 +14755,7 @@ function convertArtSyntax(source, debug) {
14737
14755
  }
14738
14756
  if (blockStack.length > 0) {
14739
14757
  const unclosed = blockStack.map((b) => `"${b.ctrl}" at line ${b.line}`).join(", ");
14740
- throw new Error(`[@lark.js/mvc error] unclosed block(s): ${unclosed}`);
14758
+ throw new Error(`Unclosed block(s): ${unclosed}`);
14741
14759
  }
14742
14760
  return result.join("");
14743
14761
  }
@@ -14836,7 +14854,7 @@ function convertArtExpression(code, debug, lineNo, blockStack = []) {
14836
14854
  const object = tokens[0];
14837
14855
  if (tokens.length > 1 && tokens[1] !== "as") {
14838
14856
  throw new Error(
14839
- `[@lark.js/mvc error] bad forOf syntax: {{${code}}}. Expected "as" keyword, got "${tokens[1]}". Usage: {{forOf list as item [index]}}`
14857
+ `Bad forOf syntax: {{${code}}}. Expected "as" keyword, got "${tokens[1]}". Usage: {{forOf list as item [index]}}`
14840
14858
  );
14841
14859
  }
14842
14860
  const restTokens = tokens.slice(2);
@@ -14863,7 +14881,7 @@ function convertArtExpression(code, debug, lineNo, blockStack = []) {
14863
14881
  const object = tokens[0];
14864
14882
  if (tokens.length > 1 && tokens[1] !== "as") {
14865
14883
  throw new Error(
14866
- `[@lark.js/mvc error] bad forIn syntax: {{${code}}}. Expected "as" keyword, got "${tokens[1]}". Usage: {{for-in obj as val [key]}}`
14884
+ `Bad forIn syntax: {{${code}}}. Expected "as" keyword, got "${tokens[1]}". Usage: {{forIn obj as val [key]}}`
14867
14885
  );
14868
14886
  }
14869
14887
  const restTokens2 = tokens.slice(2);
@@ -14889,13 +14907,11 @@ function convertArtExpression(code, debug, lineNo, blockStack = []) {
14889
14907
  const expectedCtrl = keyword.substring(1);
14890
14908
  const last = blockStack.pop();
14891
14909
  if (!last) {
14892
- throw new Error(
14893
- `[@lark.js/mvc error] unexpected {{${code}}}: no matching open block`
14894
- );
14910
+ throw new Error(`Unexpected {{${code}}}: no matching open block`);
14895
14911
  }
14896
14912
  if (last.ctrl !== expectedCtrl) {
14897
14913
  throw new Error(
14898
- `[@lark.js/mvc error] unexpected {{${code}}}: expected {{/${last.ctrl}}} to close block opened at line ${last.line}`
14914
+ `Unexpected {{${code}}}: expected {{/${last.ctrl}}} to close block opened at line ${last.line}`
14899
14915
  );
14900
14916
  }
14901
14917
  return `${debugPrefix}<%}%>`;
@@ -14956,112 +14972,532 @@ function parseAsExpr(expr) {
14956
14972
  bad: false
14957
14973
  };
14958
14974
  }
14959
- function compileToFunction(source, debug, file) {
14960
- const matcher = /<%([@=!:])?([\s\S]*?)%>|$/g;
14961
- let index = 0;
14962
- let funcSource = `$out+='`;
14963
- let hasAtRule = false;
14964
- const escapeSlashRegExp = /\\|'/g;
14965
- const escapeBreakReturnRegExp = /\r|\n/g;
14966
- source.replace(matcher, (match, operate, content, offset) => {
14967
- funcSource += source.substring(index, offset).replace(escapeSlashRegExp, "\\$&").replace(escapeBreakReturnRegExp, "\\n");
14968
- index = offset + match.length;
14969
- if (debug) {
14970
- let expr = source.substring(
14971
- index - match.length + 2 + (operate ? 1 : 0),
14972
- index - 2
14973
- );
14974
- const x11 = String.fromCharCode(17);
14975
- const artRegExp = new RegExp(`^'(\\d+)${x11}([^${x11}]+)${x11}'$`);
14976
- const artM = expr.match(artRegExp);
14977
- let art = "";
14978
- let line = -1;
14979
- if (artM) {
14980
- expr = expr.replace(artRegExp, "");
14981
- art = artM[2];
14982
- line = parseInt(artM[1], 10);
14983
- } else {
14984
- expr = expr.replace(escapeSlashRegExp, "\\$&").replace(escapeBreakReturnRegExp, "\\n");
14975
+
14976
+ // src/compiler/compile-to-vdom-function.ts
14977
+ init_cjs_shims();
14978
+ var import_htmlparser2 = require("htmlparser2");
14979
+ var VOID_ELEMENTS = /* @__PURE__ */ new Set([
14980
+ "area",
14981
+ "base",
14982
+ "br",
14983
+ "col",
14984
+ "embed",
14985
+ "hr",
14986
+ "img",
14987
+ "input",
14988
+ "link",
14989
+ "meta",
14990
+ "param",
14991
+ "source",
14992
+ "track",
14993
+ "wbr"
14994
+ ]);
14995
+ function vdomEscapeStr(s) {
14996
+ return s.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\x1e/g, "\\x1e");
14997
+ }
14998
+ function vdomResolveAttrValue(rawValue, exprStore) {
14999
+ const hasPlaceholders = rawValue.includes("\0");
15000
+ const hasViewId = rawValue.includes("");
15001
+ if (!hasPlaceholders && !hasViewId) {
15002
+ return `'${vdomEscapeStr(rawValue)}'`;
15003
+ }
15004
+ const segments = [];
15005
+ let remaining = rawValue;
15006
+ while (remaining.length > 0) {
15007
+ const phIdx = remaining.indexOf("\0");
15008
+ const viIdx = remaining.indexOf("");
15009
+ let nextSpecial = -1;
15010
+ let specialType = null;
15011
+ if (phIdx >= 0 && (viIdx < 0 || phIdx <= viIdx)) {
15012
+ nextSpecial = phIdx;
15013
+ specialType = "ph";
15014
+ } else if (viIdx >= 0) {
15015
+ nextSpecial = viIdx;
15016
+ specialType = "vi";
15017
+ }
15018
+ if (nextSpecial === -1) {
15019
+ if (remaining) segments.push(`'${vdomEscapeStr(remaining)}'`);
15020
+ break;
15021
+ }
15022
+ if (nextSpecial > 0) {
15023
+ segments.push(`'${vdomEscapeStr(remaining.substring(0, nextSpecial))}'`);
15024
+ }
15025
+ if (specialType === "vi") {
15026
+ segments.push("$viewId");
15027
+ remaining = remaining.substring(nextSpecial + 1);
15028
+ } else {
15029
+ const closeIdx = remaining.indexOf("\0", nextSpecial + 1);
15030
+ if (closeIdx === -1) {
15031
+ segments.push(`'${vdomEscapeStr(remaining.substring(nextSpecial))}'`);
15032
+ break;
14985
15033
  }
14986
- if (operate === "@") {
14987
- hasAtRule = true;
14988
- funcSource += `'+($dbgExpr='<%${operate + expr}%>',$refFn($refAlt,${content}))+'`;
14989
- } else if (operate === "=" || operate === ":") {
14990
- funcSource += `'+($dbgExpr='<%${operate + expr}%>',$encHtml(${content}))+'`;
14991
- } else if (operate === "!") {
14992
- if (!content.startsWith("$encUri(") || !content.endsWith(")")) {
14993
- content = `$strSafe(${content})`;
14994
- }
14995
- funcSource += `'+($dbgExpr='<%${operate + expr}%>',${content})+'`;
14996
- } else if (content) {
14997
- if (line > -1) {
14998
- funcSource += `';$dbgLine=${line};$dbgArt='${art}';`;
14999
- content = "";
15034
+ const idx = parseInt(remaining.substring(nextSpecial + 1, closeIdx));
15035
+ const expr = exprStore[idx];
15036
+ if (expr.op === "=" || expr.op === ":") {
15037
+ segments.push(`$n(${expr.content})`);
15038
+ } else if (expr.op === "!") {
15039
+ if (expr.content.startsWith("$encUri(") && expr.content.endsWith(")")) {
15040
+ segments.push(expr.content);
15000
15041
  } else {
15001
- funcSource += `';`;
15002
- }
15003
- if (funcSource.endsWith(`+'';`)) {
15004
- funcSource = funcSource.substring(0, funcSource.length - 4) + ";";
15005
- }
15006
- if (expr) {
15007
- funcSource += `$dbgExpr='<%${expr}%>';`;
15042
+ segments.push(`$n(${expr.content})`);
15008
15043
  }
15009
- funcSource += content + `;$out+='`;
15044
+ } else if (expr.op === "@") {
15045
+ segments.push(`$refFn($refAlt,${expr.content})`);
15046
+ } else {
15047
+ segments.push(`$n(${expr.content})`);
15010
15048
  }
15011
- } else {
15012
- if (operate === "@") {
15013
- hasAtRule = true;
15014
- funcSource += `'+$refFn($refAlt,${content})+'`;
15015
- } else if (operate === "=" || operate === ":") {
15016
- funcSource += `'+$encHtml(${content})+'`;
15017
- } else if (operate === "!") {
15018
- if (!content.startsWith("$encUri(") || !content.endsWith(")")) {
15019
- content = `$strSafe(${content})`;
15020
- }
15021
- funcSource += `'+${content}+'`;
15022
- } else if (content) {
15023
- funcSource += `';`;
15024
- if (funcSource.endsWith(`+'';`)) {
15025
- funcSource = funcSource.substring(0, funcSource.length - 4) + ";";
15049
+ remaining = remaining.substring(closeIdx + 1);
15050
+ }
15051
+ }
15052
+ if (segments.length === 0) return "''";
15053
+ if (segments.length === 1) return segments[0];
15054
+ return segments.join("+");
15055
+ }
15056
+ function vdomBuildPropsFromAttribs(attribs, exprStore) {
15057
+ if (!attribs) return "null";
15058
+ const keys = Object.keys(attribs);
15059
+ if (keys.length === 0) return "null";
15060
+ const entries = [];
15061
+ for (const name of keys) {
15062
+ const value = attribs[name];
15063
+ const valueExpr = vdomResolveAttrValue(value, exprStore);
15064
+ entries.push(`'${vdomEscapeStr(name)}':${valueExpr}`);
15065
+ }
15066
+ return `{${entries.join(",")}}`;
15067
+ }
15068
+ function compileToVDomFunction(source, debug, file) {
15069
+ const lines = [];
15070
+ let varCounter = 0;
15071
+ let propsCounter = 0;
15072
+ const exprStore = [];
15073
+ const protectedSource = source.replace(
15074
+ /<%([@=!:])?([\s\S]*?)%>/g,
15075
+ (_, op, content) => {
15076
+ const idx = exprStore.length;
15077
+ exprStore.push({ op: op || "", content: (content || "").trim() });
15078
+ return `\0${idx}\0`;
15079
+ }
15080
+ );
15081
+ const doc = (0, import_htmlparser2.parseDocument)(protectedSource, {
15082
+ recognizeSelfClosing: true,
15083
+ lowerCaseTags: false,
15084
+ lowerCaseAttributeNames: false,
15085
+ decodeEntities: false
15086
+ });
15087
+ const rootVar = `$v${varCounter++}`;
15088
+ lines.push(`let ${rootVar}=[]`);
15089
+ const maxVars = 30;
15090
+ const varDecls = [];
15091
+ for (let i = 1; i < maxVars; i++) {
15092
+ varDecls.push(`$v${i}`);
15093
+ }
15094
+ lines.push(`let ${varDecls.join(",")}`);
15095
+ function allocVar() {
15096
+ if (varCounter >= maxVars) return `$v${maxVars - 1}`;
15097
+ return `$v${varCounter++}`;
15098
+ }
15099
+ function emitNode(node, parentVar) {
15100
+ const type = node.type;
15101
+ if (type === "text") {
15102
+ emitText(node.data || "", parentVar);
15103
+ } else if (type === "tag" || type === "script" || type === "style") {
15104
+ emitElement(node, parentVar);
15105
+ }
15106
+ }
15107
+ function emitText(text, parentVar) {
15108
+ const parts = text.split(/\x00(\d+)\x00/);
15109
+ for (let i = 0; i < parts.length; i++) {
15110
+ if (i % 2 === 0) {
15111
+ const trimmed = parts[i];
15112
+ if (trimmed.trim()) {
15113
+ lines.push(`${parentVar}.push($c(0,'${vdomEscapeStr(trimmed)}'))`);
15026
15114
  }
15027
- funcSource += `${content};$out+='`;
15115
+ } else {
15116
+ const idx = parseInt(parts[i]);
15117
+ const expr = exprStore[idx];
15118
+ emitExpr(expr, parentVar);
15028
15119
  }
15029
15120
  }
15030
- return match;
15031
- });
15032
- funcSource += `';`;
15033
- funcSource = funcSource.replace(/\$out\+='';/g, "");
15034
- funcSource = funcSource.replace(/\$out\+=''\+/g, "$out+=");
15121
+ }
15122
+ function emitExpr(expr, parentVar) {
15123
+ if (expr.op === "=" || expr.op === ":") {
15124
+ lines.push(`${parentVar}.push($c(0,$n(${expr.content})))`);
15125
+ } else if (expr.op === "!") {
15126
+ if (expr.content.startsWith("$encUri(") && expr.content.endsWith(")")) {
15127
+ lines.push(`${parentVar}.push($c(0,${expr.content}))`);
15128
+ } else {
15129
+ lines.push(`${parentVar}.push($c(0,$n(${expr.content})))`);
15130
+ }
15131
+ } else if (expr.op === "@") {
15132
+ lines.push(`${parentVar}.push($c(0,$refFn($refAlt,${expr.content})))`);
15133
+ } else if (expr.content) {
15134
+ lines.push(expr.content);
15135
+ }
15136
+ }
15137
+ function emitElement(node, parentVar) {
15138
+ const tagName = node.name || "";
15139
+ const children = node.children || [];
15140
+ const childVar = allocVar();
15141
+ const propsKey = `__p${propsCounter++}`;
15142
+ const props = vdomBuildPropsFromAttribs(node.attribs, exprStore);
15143
+ lines.push(`let ${propsKey}=${props}`);
15144
+ lines.push(`${childVar}=[]`);
15145
+ for (const child of children) {
15146
+ emitNode(child, childVar);
15147
+ }
15148
+ const isVoid = VOID_ELEMENTS.has(tagName) && children.length === 0;
15149
+ const childrenArg = isVoid ? "1" : childVar;
15150
+ lines.push(
15151
+ `${parentVar}.push($c('${tagName}',${propsKey},${childrenArg}))`
15152
+ );
15153
+ }
15154
+ for (const child of doc.children) {
15155
+ emitNode(child, rootVar);
15156
+ }
15157
+ lines.push(`return $c($viewId,0,${rootVar})`);
15158
+ const body = lines.join(";");
15159
+ let funcBody = body;
15035
15160
  if (debug) {
15036
15161
  const filePart = file ? `\\r\\n\\tat file:${file}` : "";
15037
- funcSource = `let $dbgExpr,$dbgArt,$dbgLine;try{${funcSource}}catch(ex){let msg='render view error:'+(ex.message||ex);if($dbgArt)msg+='\\r\\n\\tsrc art:{{'+$dbgArt+'}}\\r\\n\\tat line:'+$dbgLine;msg+='\\r\\n\\t'+($dbgArt?'translate to:':'expr:');msg+=$dbgExpr+'${filePart}';throw msg;}`;
15162
+ funcBody = `let $dbgExpr,$dbgArt,$dbgLine;try{${body}}catch(ex){let msg='render view error:'+(ex.message||ex);msg+='${filePart}';throw msg;}`;
15038
15163
  }
15039
15164
  const viewIdRegExp = new RegExp(String.fromCharCode(31), "g");
15040
- funcSource = funcSource.replace(viewIdRegExp, `'+$viewId+'`);
15041
- void hasAtRule;
15165
+ funcBody = funcBody.replace(viewIdRegExp, `'+$viewId+'`);
15042
15166
  const refFallback = "if(!$refAlt)$refAlt=$data;";
15043
- const fullSource = `${refFallback}let $splitter='\\x1e',$tmp,$out=''{{VARS}};${funcSource}return $out`;
15044
- return `($data,$viewId,$refAlt,$encHtml,$strSafe,$encUri,$refFn,$encQuote)=>{${fullSource}}`;
15167
+ const fullSource = `${refFallback}let $splitter='\\x1e'{{VARS}};${funcBody}`;
15168
+ return `($data,$viewId,$refAlt,$n,$refFn,$encUri,$encQuote)=>{${fullSource}}`;
15045
15169
  }
15046
- function compileTemplate(source, options = {}) {
15047
- const { debug = false, globalVars = [], file } = options;
15048
- const { protectedSource, comments } = protectComments(source);
15049
- const converted = convertArtSyntax(protectedSource, debug);
15050
- const viewEventProcessed = processViewEvents(converted);
15051
- const finalSource = restoreComments(viewEventProcessed, comments);
15052
- const funcBody = compileToFunction(finalSource, debug, file);
15053
- const varDeclarations = globalVars.map((key) => `,${key}=$data.${key}`).join("");
15054
- const funcWithVars = funcBody.replace("{{VARS}}", () => varDeclarations);
15055
- return `import { encHtml as __larkEncHtml, strSafe as __larkStrSafe, encUri as __larkEncUri, encQuote as __larkEncQuote, refFn as __larkRefFn } from "@lark.js/mvc/runtime";
15056
- export default function(data, viewId, refData) {
15057
- let $data = data || {},
15058
- $viewId = viewId || '';
15059
- return (${funcWithVars})($data, $viewId, refData,
15060
- __larkEncHtml, __larkStrSafe, __larkEncUri, __larkRefFn, __larkEncQuote
15170
+
15171
+ // src/compiler/swc/extract-global-vars.ts
15172
+ init_cjs_shims();
15173
+ var parseSyncFn = null;
15174
+ var parseSyncLoadAttempted = false;
15175
+ async function getParser() {
15176
+ if (!parseSyncLoadAttempted) {
15177
+ parseSyncLoadAttempted = true;
15178
+ try {
15179
+ const swc = await import("@swc/core");
15180
+ parseSyncFn = swc.parseSync;
15181
+ } catch (err) {
15182
+ console.error("failed to load @swc/core", err);
15183
+ }
15184
+ }
15185
+ return parseSyncFn;
15186
+ }
15187
+ async function extractGlobalVars(source) {
15188
+ const { protectedSource, comments: _comments } = protectComments(source);
15189
+ const viewEventProcessed = processViewEvents(protectedSource);
15190
+ const converted = convertArtSyntax(viewEventProcessed, false);
15191
+ const template = restoreComments(converted, _comments);
15192
+ const templateCmdRegExp = /<%([@=!:])?([\s\S]*?)%>|$/g;
15193
+ const fnParts = [];
15194
+ const htmlStore = {};
15195
+ let htmlIndex = 0;
15196
+ let lastIndex = 0;
15197
+ const htmlKey = String.fromCharCode(5);
15198
+ template.replace(
15199
+ templateCmdRegExp,
15200
+ (match, operate, content, offset) => {
15201
+ const start = operate ? 3 : 2;
15202
+ const htmlText = template.substring(lastIndex, offset + start);
15203
+ const key = htmlKey + htmlIndex++ + htmlKey;
15204
+ htmlStore[key] = htmlText;
15205
+ lastIndex = offset + match.length - 2;
15206
+ if (operate && content.trim()) {
15207
+ fnParts.push(';"' + key + '";', "[" + content + "]");
15208
+ } else {
15209
+ fnParts.push(';"' + key + '";', content || "");
15210
+ }
15211
+ return match;
15212
+ }
15061
15213
  );
15062
- }`;
15214
+ let fn = fnParts.join("");
15215
+ fn = `(function(){${fn}})`;
15216
+ const parseSync = await getParser();
15217
+ if (!parseSync) {
15218
+ return fallbackExtractVariables(source);
15219
+ }
15220
+ let ast;
15221
+ try {
15222
+ ast = parseSync(fn, {
15223
+ syntax: "ecmascript",
15224
+ target: "es2022"
15225
+ });
15226
+ } catch {
15227
+ return fallbackExtractVariables(source);
15228
+ }
15229
+ const globalExists = {};
15230
+ for (const name of BUILTIN_GLOBALS) globalExists[name] = 1;
15231
+ const globalVars = /* @__PURE__ */ Object.create(null);
15232
+ const fnNodes = [];
15233
+ walkSwcAst(ast, {
15234
+ VariableDeclarator(node) {
15235
+ if (node.id.type === "Identifier") {
15236
+ const name = node.id.value;
15237
+ globalExists[name] = node.init ? 3 : 2;
15238
+ }
15239
+ },
15240
+ FunctionDeclaration(node) {
15241
+ if (node.identifier) {
15242
+ globalExists[node.identifier.value] = 3;
15243
+ }
15244
+ fnNodes.push(node);
15245
+ },
15246
+ FunctionExpression(node) {
15247
+ fnNodes.push(node);
15248
+ },
15249
+ ArrowFunctionExpression(node) {
15250
+ fnNodes.push(node);
15251
+ },
15252
+ CallExpression(node) {
15253
+ if (node.callee.type === "Identifier") {
15254
+ globalExists[node.callee.value] = 1;
15255
+ }
15256
+ }
15257
+ });
15258
+ const functionParams = /* @__PURE__ */ Object.create(null);
15259
+ for (const fnNode of fnNodes) {
15260
+ const patterns = getParamPatterns(fnNode);
15261
+ for (const pat of patterns) {
15262
+ if (pat.type === "Identifier") {
15263
+ functionParams[pat.value] = 1;
15264
+ } else if (pat.type === "AssignmentPattern" && pat.left.type === "Identifier") {
15265
+ functionParams[pat.left.value] = 1;
15266
+ } else if (pat.type === "RestElement" && pat.argument.type === "Identifier") {
15267
+ functionParams[pat.argument.value] = 1;
15268
+ }
15269
+ }
15270
+ }
15271
+ walkSwcAst(ast, {
15272
+ Identifier(node) {
15273
+ const name = node.value;
15274
+ if (globalExists[name]) return;
15275
+ if (functionParams[name]) return;
15276
+ globalVars[name] = 1;
15277
+ },
15278
+ AssignmentExpression(node) {
15279
+ if (node.left.type === "Identifier") {
15280
+ const name = node.left.value;
15281
+ if (!globalExists[name] || globalExists[name] === 1) {
15282
+ globalExists[name] = (globalExists[name] || 0) + 1;
15283
+ }
15284
+ }
15285
+ }
15286
+ });
15287
+ return Object.keys(globalVars);
15288
+ }
15289
+ function getParamPatterns(fnNode) {
15290
+ if (fnNode.type === "ArrowFunctionExpression") {
15291
+ return fnNode.params;
15292
+ }
15293
+ return fnNode.params.map((p) => p.pat);
15294
+ }
15295
+ function fallbackExtractVariables(source) {
15296
+ const vars = /* @__PURE__ */ new Set();
15297
+ const outputRegExp = /\{\{[:=!@]\s*([a-zA-Z_$][\w$]*)[^}]*\}\}/g;
15298
+ let m;
15299
+ while ((m = outputRegExp.exec(source)) !== null) {
15300
+ vars.add(m[1]);
15301
+ }
15302
+ const eachRegExp = /\{\{forOf\s+([a-zA-Z_$][\w$]*)\s+as/g;
15303
+ while ((m = eachRegExp.exec(source)) !== null) {
15304
+ vars.add(m[1]);
15305
+ }
15306
+ const ifRegExp = /\{\{(?:else\s+)?if\s+([a-zA-Z_$][\w$]*)[^}]*\}\}/g;
15307
+ while ((m = ifRegExp.exec(source)) !== null) {
15308
+ vars.add(m[1]);
15309
+ }
15310
+ return Array.from(vars).filter((v) => !BUILTIN_GLOBALS.has(v));
15311
+ }
15312
+ function walkSwcAst(ast, visitors) {
15313
+ function visit(node) {
15314
+ const type = node.type;
15315
+ if (visitors[type]) {
15316
+ visitors[type](node);
15317
+ }
15318
+ for (const key of Object.keys(node)) {
15319
+ if (key === "type" || key === "span" || key === "ctxt") continue;
15320
+ if (type === "MemberExpression" && key === "property") {
15321
+ const me = node;
15322
+ if (me.property.type !== "Computed") continue;
15323
+ }
15324
+ if (type === "KeyValueProperty" && key === "key") {
15325
+ const kv = node;
15326
+ if (kv.key.type !== "Computed") continue;
15327
+ }
15328
+ if (type === "MethodProperty" && key === "key") {
15329
+ const mp = node;
15330
+ if (mp.key.type !== "Computed") continue;
15331
+ }
15332
+ const child = Reflect.get(node, key);
15333
+ if (Array.isArray(child)) {
15334
+ for (const item of child) {
15335
+ if (isSwcNode(item)) visit(item);
15336
+ }
15337
+ } else if (isSwcNode(child)) {
15338
+ visit(child);
15339
+ }
15340
+ }
15341
+ }
15342
+ visit(ast);
15343
+ }
15344
+ function isSwcNode(v) {
15345
+ return !!v && typeof v === "object" && typeof v.type === "string";
15063
15346
  }
15064
- function extractGlobalVars(source) {
15347
+ var BUILTIN_GLOBALS = /* @__PURE__ */ new Set([
15348
+ // ─── Template runtime helpers (injected by compileToFunction) ───────
15349
+ //
15350
+ // These variables appear in the generated template function signature
15351
+ // or body. They must be excluded from extractGlobalVars() so that
15352
+ // they are not mistaken for user data variables and destructured from $data.
15353
+ // SPLITTER character constant (same as \x1e), used as namespace separator
15354
+ // for refData keys, event attribute encoding, and internal data structures.
15355
+ // Declared as: let $splitter='\x1e'
15356
+ "$splitter",
15357
+ // Data — the data object passed from Updater to the template function.
15358
+ // User variables are destructured from $data at the top of the function:
15359
+ // let {name, age} = $data;
15360
+ // This is the first parameter of the generated arrow function.
15361
+ "$data",
15362
+ // Null-safe toString: v => '' + (v == null ? '' : v)
15363
+ // Converts null/undefined to empty string, otherwise calls toString().
15364
+ // Wraps every {{!raw}} output to prevent "null" / "undefined" rendering.
15365
+ "$strSafe",
15366
+ // HTML entity encoder: v => $strSafe(v).replace(/[&<>"'`]/g, entityMap)
15367
+ // Encodes &, <, >, ", ', ` to HTML entities (&amp; &lt; etc.)
15368
+ // Applied to all {{=escaped}} and {{:binding}} outputs.
15369
+ "$encHtml",
15370
+ // HTML entity map — internal object used by $encHtml:
15371
+ // {'&':'amp','<':'gt','>':'gt','"':'#34','\'':'#39','`':'#96'}
15372
+ // Not a standalone function; referenced inside $encHtml's closure.
15373
+ "$entMap",
15374
+ // HTML entity RegExp — internal regexp used by $encHtml:
15375
+ // /[&<>"'`]/g
15376
+ "$entReg",
15377
+ // HTML entity replacer function — internal helper used by $encHtml:
15378
+ // m => '&' + $entMap[m] + ';'
15379
+ // Maps matched character to its entity string.
15380
+ "$entFn",
15381
+ // Output buffer — the string accumulator for rendered HTML.
15382
+ // All template output is appended via $out += '...'.
15383
+ // Declared as: let $out = ''
15384
+ "$out",
15385
+ // Reference lookup: (refData, value) => key
15386
+ // Finds or allocates a SPLITTER-prefixed key in refData for a given
15387
+ // object reference. Used by {{@ref}} operator for passing object
15388
+ // references to child views via v-lark attributes.
15389
+ "$refFn",
15390
+ // URI encoder: v => encodeURIComponent($strSafe(v)).replace(/[!')(*]/g, extraMap)
15391
+ // Extends encodeURIComponent with encoding of ! ' ( ) *.
15392
+ // Applied to values in @event URL parameters and {{!uri}} contexts.
15393
+ "$encUri",
15394
+ // URI encode map — internal object used by $encUri:
15395
+ // {'!':'%21','\'':'%27','(':'%28',')':'%29','*':'%2A'}
15396
+ "$uriMap",
15397
+ // URI encode replacer — internal helper used by $encUri:
15398
+ // m => $uriMap[m]
15399
+ "$uriFn",
15400
+ // URI encode regexp — internal regexp used by $encUri:
15401
+ // /[!')(*]/g
15402
+ "$uriReg",
15403
+ // Quote encoder: v => $strSafe(v).replace(/['"\\]/g, '\\$&')
15404
+ // Escapes quotes and backslashes for safe embedding in HTML attribute
15405
+ // values (e.g. data-json='...').
15406
+ "$encQuote",
15407
+ // Quote encode regexp — internal regexp used by $encQuote:
15408
+ // /['"\\]/g
15409
+ "$qReg",
15410
+ // View ID — the unique identifier of the owning View instance.
15411
+ // Injected into @event attribute values at render time so that
15412
+ // EventDelegator can dispatch events to the correct View handler.
15413
+ // The \x1f placeholder in compiled output is replaced with '+$viewId+'.
15414
+ "$viewId",
15415
+ // Debug: current expression text — stores the template expression being
15416
+ // evaluated, for error reporting. Only present in debug mode.
15417
+ // e.g. $dbgExpr='<%=user.name%>'
15418
+ "$dbgExpr",
15419
+ // Debug: original art syntax — stores the {{}} template syntax before
15420
+ // conversion, for error reporting. Only present in debug mode.
15421
+ // e.g. $dbgArt='{{=user.name}}'
15422
+ "$dbgArt",
15423
+ // Debug: source line number — tracks the current line in the template
15424
+ // source, for error reporting. Only present in debug mode.
15425
+ "$dbgLine",
15426
+ // RefData alias — fallback reference lookup table.
15427
+ // Defaults to $data when no explicit $refAlt is provided.
15428
+ // Ensures $refFn() does not crash when @ operator is used without refData.
15429
+ "$refAlt",
15430
+ // Temporary variable — used by the compiler for intermediate
15431
+ // expression results in generated code (e.g. loop variables,
15432
+ // conditional branches). Declared as: let $tmp
15433
+ "$tmp",
15434
+ // JS literals
15435
+ "undefined",
15436
+ "null",
15437
+ "true",
15438
+ "false",
15439
+ "NaN",
15440
+ "Infinity",
15441
+ // JS built-in globals
15442
+ "window",
15443
+ "self",
15444
+ "globalThis",
15445
+ "document",
15446
+ "console",
15447
+ "JSON",
15448
+ "Math",
15449
+ "Intl",
15450
+ "Promise",
15451
+ "Symbol",
15452
+ "Number",
15453
+ "String",
15454
+ "Boolean",
15455
+ "Array",
15456
+ "Object",
15457
+ "Date",
15458
+ "RegExp",
15459
+ "Error",
15460
+ "TypeError",
15461
+ "RangeError",
15462
+ "SyntaxError",
15463
+ "Map",
15464
+ "Set",
15465
+ "WeakMap",
15466
+ "WeakSet",
15467
+ "Proxy",
15468
+ "Reflect",
15469
+ "ArrayBuffer",
15470
+ "DataView",
15471
+ "Float32Array",
15472
+ "Float64Array",
15473
+ "Int8Array",
15474
+ "Int16Array",
15475
+ "Int32Array",
15476
+ "Uint8Array",
15477
+ "Uint16Array",
15478
+ "Uint32Array",
15479
+ "Uint8ClampedArray",
15480
+ // Functions
15481
+ "parseInt",
15482
+ "parseFloat",
15483
+ "isNaN",
15484
+ "isFinite",
15485
+ "encodeURIComponent",
15486
+ "decodeURIComponent",
15487
+ "encodeURI",
15488
+ "decodeURI",
15489
+ // SWC helpers
15490
+ "arguments",
15491
+ "this",
15492
+ "require",
15493
+ // Lark framework
15494
+ "Lark"
15495
+ ]);
15496
+
15497
+ // src/compiler/extract-global-vars.ts
15498
+ init_cjs_shims();
15499
+ var import_parser = __toESM(require_lib(), 1);
15500
+ async function extractGlobalVars2(source) {
15065
15501
  const { protectedSource, comments: _comments } = protectComments(source);
15066
15502
  const viewEventProcessed = processViewEvents(protectedSource);
15067
15503
  const converted = convertArtSyntax(viewEventProcessed, false);
@@ -15098,9 +15534,10 @@ function extractGlobalVars(source) {
15098
15534
  allowAwaitOutsideFunction: true
15099
15535
  });
15100
15536
  } catch {
15101
- return fallbackExtractVariables(source);
15537
+ return fallbackExtractVariables2(source);
15102
15538
  }
15103
- const globalExists = { ...BUILTIN_GLOBALS };
15539
+ const globalExists = {};
15540
+ for (const name of BUILTIN_GLOBALS2) globalExists[name] = 1;
15104
15541
  const globalVars = /* @__PURE__ */ Object.create(null);
15105
15542
  const fnRange = [];
15106
15543
  walkAst(ast, {
@@ -15159,7 +15596,7 @@ function extractGlobalVars(source) {
15159
15596
  });
15160
15597
  return Object.keys(globalVars);
15161
15598
  }
15162
- function fallbackExtractVariables(source) {
15599
+ function fallbackExtractVariables2(source) {
15163
15600
  const vars = /* @__PURE__ */ new Set();
15164
15601
  const outputRegExp = /\{\{[:=!@]\s*([a-zA-Z_$][\w$]*)[^}]*\}\}/g;
15165
15602
  let m;
@@ -15174,7 +15611,7 @@ function fallbackExtractVariables(source) {
15174
15611
  while ((m = ifRegExp.exec(source)) !== null) {
15175
15612
  vars.add(m[1]);
15176
15613
  }
15177
- return Array.from(vars).filter((v) => !BUILTIN_GLOBAL_SET.has(v));
15614
+ return Array.from(vars).filter((v) => !BUILTIN_GLOBALS2.has(v));
15178
15615
  }
15179
15616
  function walkAst(ast, visitors) {
15180
15617
  function visit(node) {
@@ -15182,7 +15619,6 @@ function walkAst(ast, visitors) {
15182
15619
  if (visitors[type]) {
15183
15620
  visitors[type](node);
15184
15621
  }
15185
- const bag = node;
15186
15622
  for (const key of Object.keys(node)) {
15187
15623
  if (key === "type" || key === "start" || key === "end" || key === "loc" || key === "range")
15188
15624
  continue;
@@ -15198,7 +15634,7 @@ function walkAst(ast, visitors) {
15198
15634
  const om = node;
15199
15635
  if (!om.computed) continue;
15200
15636
  }
15201
- const child = bag[key];
15637
+ const child = Reflect.get(node, key);
15202
15638
  if (Array.isArray(child)) {
15203
15639
  for (const item of child) {
15204
15640
  if (isAstNode(item)) visit(item);
@@ -15213,7 +15649,7 @@ function walkAst(ast, visitors) {
15213
15649
  function isAstNode(v) {
15214
15650
  return !!v && typeof v === "object" && typeof v.type === "string";
15215
15651
  }
15216
- var BUILTIN_GLOBALS = {
15652
+ var BUILTIN_GLOBALS2 = /* @__PURE__ */ new Set([
15217
15653
  // ─── Template runtime helpers (injected by compileToFunction) ───────
15218
15654
  //
15219
15655
  // These variables appear in the generated template function signature
@@ -15222,152 +15658,274 @@ var BUILTIN_GLOBALS = {
15222
15658
  // SPLITTER character constant (same as \x1e), used as namespace separator
15223
15659
  // for refData keys, event attribute encoding, and internal data structures.
15224
15660
  // Declared as: let $splitter='\x1e'
15225
- $splitter: 1,
15661
+ "$splitter",
15226
15662
  // Data — the data object passed from Updater to the template function.
15227
15663
  // User variables are destructured from $data at the top of the function:
15228
15664
  // let {name, age} = $data;
15229
15665
  // This is the first parameter of the generated arrow function.
15230
- $data: 1,
15666
+ "$data",
15231
15667
  // Null-safe toString: v => '' + (v == null ? '' : v)
15232
15668
  // Converts null/undefined to empty string, otherwise calls toString().
15233
15669
  // Wraps every {{!raw}} output to prevent "null" / "undefined" rendering.
15234
- $strSafe: 1,
15670
+ "$strSafe",
15235
15671
  // HTML entity encoder: v => $strSafe(v).replace(/[&<>"'`]/g, entityMap)
15236
15672
  // Encodes &, <, >, ", ', ` to HTML entities (&amp; &lt; etc.)
15237
15673
  // Applied to all {{=escaped}} and {{:binding}} outputs.
15238
- $encHtml: 1,
15674
+ "$encHtml",
15239
15675
  // HTML entity map — internal object used by $encHtml:
15240
15676
  // {'&':'amp','<':'gt','>':'gt','"':'#34','\'':'#39','`':'#96'}
15241
15677
  // Not a standalone function; referenced inside $encHtml's closure.
15242
- $entMap: 1,
15678
+ "$entMap",
15243
15679
  // HTML entity RegExp — internal regexp used by $encHtml:
15244
15680
  // /[&<>"'`]/g
15245
- $entReg: 1,
15681
+ "$entReg",
15246
15682
  // HTML entity replacer function — internal helper used by $encHtml:
15247
15683
  // m => '&' + $entMap[m] + ';'
15248
15684
  // Maps matched character to its entity string.
15249
- $entFn: 1,
15685
+ "$entFn",
15250
15686
  // Output buffer — the string accumulator for rendered HTML.
15251
15687
  // All template output is appended via $out += '...'.
15252
15688
  // Declared as: let $out = ''
15253
- $out: 1,
15689
+ "$out",
15254
15690
  // Reference lookup: (refData, value) => key
15255
15691
  // Finds or allocates a SPLITTER-prefixed key in refData for a given
15256
15692
  // object reference. Used by {{@ref}} operator for passing object
15257
15693
  // references to child views via v-lark attributes.
15258
- $refFn: 1,
15694
+ "$refFn",
15259
15695
  // URI encoder: v => encodeURIComponent($strSafe(v)).replace(/[!')(*]/g, extraMap)
15260
15696
  // Extends encodeURIComponent with encoding of ! ' ( ) *.
15261
15697
  // Applied to values in @event URL parameters and {{!uri}} contexts.
15262
- $encUri: 1,
15698
+ "$encUri",
15263
15699
  // URI encode map — internal object used by $encUri:
15264
15700
  // {'!':'%21','\'':'%27','(':'%28',')':'%29','*':'%2A'}
15265
- $uriMap: 1,
15701
+ "$uriMap",
15266
15702
  // URI encode replacer — internal helper used by $encUri:
15267
15703
  // m => $uriMap[m]
15268
- $uriFn: 1,
15704
+ "$uriFn",
15269
15705
  // URI encode regexp — internal regexp used by $encUri:
15270
15706
  // /[!')(*]/g
15271
- $uriReg: 1,
15707
+ "$uriReg",
15272
15708
  // Quote encoder: v => $strSafe(v).replace(/['"\\]/g, '\\$&')
15273
15709
  // Escapes quotes and backslashes for safe embedding in HTML attribute
15274
15710
  // values (e.g. data-json='...').
15275
- $encQuote: 1,
15711
+ "$encQuote",
15276
15712
  // Quote encode regexp — internal regexp used by $encQuote:
15277
15713
  // /['"\\]/g
15278
- $qReg: 1,
15714
+ "$qReg",
15279
15715
  // View ID — the unique identifier of the owning View instance.
15280
15716
  // Injected into @event attribute values at render time so that
15281
15717
  // EventDelegator can dispatch events to the correct View handler.
15282
15718
  // The \x1f placeholder in compiled output is replaced with '+$viewId+'.
15283
- $viewId: 1,
15719
+ "$viewId",
15284
15720
  // Debug: current expression text — stores the template expression being
15285
15721
  // evaluated, for error reporting. Only present in debug mode.
15286
15722
  // e.g. $dbgExpr='<%=user.name%>'
15287
- $dbgExpr: 1,
15723
+ "$dbgExpr",
15288
15724
  // Debug: original art syntax — stores the {{}} template syntax before
15289
15725
  // conversion, for error reporting. Only present in debug mode.
15290
15726
  // e.g. $dbgArt='{{=user.name}}'
15291
- $dbgArt: 1,
15727
+ "$dbgArt",
15292
15728
  // Debug: source line number — tracks the current line in the template
15293
15729
  // source, for error reporting. Only present in debug mode.
15294
- $dbgLine: 1,
15730
+ "$dbgLine",
15295
15731
  // RefData alias — fallback reference lookup table.
15296
15732
  // Defaults to $data when no explicit $refAlt is provided.
15297
15733
  // Ensures $refFn() does not crash when @ operator is used without refData.
15298
- $refAlt: 1,
15734
+ "$refAlt",
15299
15735
  // Temporary variable — used by the compiler for intermediate
15300
15736
  // expression results in generated code (e.g. loop variables,
15301
15737
  // conditional branches). Declared as: let $tmp
15302
- $tmp: 1,
15738
+ "$tmp",
15303
15739
  // JS literals
15304
- undefined: 1,
15305
- null: 1,
15306
- true: 1,
15307
- false: 1,
15308
- NaN: 1,
15309
- Infinity: 1,
15740
+ "undefined",
15741
+ "null",
15742
+ "true",
15743
+ "false",
15744
+ "NaN",
15745
+ "Infinity",
15310
15746
  // JS built-in globals
15311
- window: 1,
15312
- self: 1,
15313
- globalThis: 1,
15314
- document: 1,
15315
- console: 1,
15316
- JSON: 1,
15317
- Math: 1,
15318
- Intl: 1,
15319
- Promise: 1,
15320
- Symbol: 1,
15321
- Number: 1,
15322
- String: 1,
15323
- Boolean: 1,
15324
- Array: 1,
15325
- Object: 1,
15326
- Date: 1,
15327
- RegExp: 1,
15328
- Error: 1,
15329
- TypeError: 1,
15330
- RangeError: 1,
15331
- SyntaxError: 1,
15332
- Map: 1,
15333
- Set: 1,
15334
- WeakMap: 1,
15335
- WeakSet: 1,
15336
- Proxy: 1,
15337
- Reflect: 1,
15338
- ArrayBuffer: 1,
15339
- DataView: 1,
15340
- Float32Array: 1,
15341
- Float64Array: 1,
15342
- Int8Array: 1,
15343
- Int16Array: 1,
15344
- Int32Array: 1,
15345
- Uint8Array: 1,
15346
- Uint16Array: 1,
15347
- Uint32Array: 1,
15348
- Uint8ClampedArray: 1,
15747
+ "window",
15748
+ "self",
15749
+ "globalThis",
15750
+ "document",
15751
+ "console",
15752
+ "JSON",
15753
+ "Math",
15754
+ "Intl",
15755
+ "Promise",
15756
+ "Symbol",
15757
+ "Number",
15758
+ "String",
15759
+ "Boolean",
15760
+ "Array",
15761
+ "Object",
15762
+ "Date",
15763
+ "RegExp",
15764
+ "Error",
15765
+ "TypeError",
15766
+ "RangeError",
15767
+ "SyntaxError",
15768
+ "Map",
15769
+ "Set",
15770
+ "WeakMap",
15771
+ "WeakSet",
15772
+ "Proxy",
15773
+ "Reflect",
15774
+ "ArrayBuffer",
15775
+ "DataView",
15776
+ "Float32Array",
15777
+ "Float64Array",
15778
+ "Int8Array",
15779
+ "Int16Array",
15780
+ "Int32Array",
15781
+ "Uint8Array",
15782
+ "Uint16Array",
15783
+ "Uint32Array",
15784
+ "Uint8ClampedArray",
15349
15785
  // Functions
15350
- parseInt: 1,
15351
- parseFloat: 1,
15352
- isNaN: 1,
15353
- isFinite: 1,
15354
- encodeURIComponent: 1,
15355
- decodeURIComponent: 1,
15356
- encodeURI: 1,
15357
- decodeURI: 1,
15786
+ "parseInt",
15787
+ "parseFloat",
15788
+ "isNaN",
15789
+ "isFinite",
15790
+ "encodeURIComponent",
15791
+ "decodeURIComponent",
15792
+ "encodeURI",
15793
+ "decodeURI",
15358
15794
  // Babel helpers
15359
- arguments: 1,
15360
- this: 1,
15361
- require: 1,
15795
+ "arguments",
15796
+ "this",
15797
+ "require",
15362
15798
  // Lark framework
15363
- Lark: 1
15364
- };
15365
- var BUILTIN_GLOBAL_SET = new Set(Object.keys(BUILTIN_GLOBALS));
15799
+ "Lark"
15800
+ ]);
15801
+
15802
+ // src/compiler/compile-template.ts
15803
+ function compileToFunction(source, debug, file) {
15804
+ const matcher = /<%([@=!:])?([\s\S]*?)%>|$/g;
15805
+ let index = 0;
15806
+ let funcSource = `$out+='`;
15807
+ let hasAtRule = false;
15808
+ const escapeSlashRegExp = /\\|'/g;
15809
+ const escapeBreakReturnRegExp = /\r|\n/g;
15810
+ source.replace(matcher, (match, operate, content, offset) => {
15811
+ funcSource += source.substring(index, offset).replace(escapeSlashRegExp, "\\$&").replace(escapeBreakReturnRegExp, "\\n");
15812
+ index = offset + match.length;
15813
+ if (debug) {
15814
+ let expr = source.substring(
15815
+ index - match.length + 2 + (operate ? 1 : 0),
15816
+ index - 2
15817
+ );
15818
+ const x11 = String.fromCharCode(17);
15819
+ const artRegExp = new RegExp(`^'(\\d+)${x11}([^${x11}]+)${x11}'$`);
15820
+ const artM = expr.match(artRegExp);
15821
+ let art = "";
15822
+ let line = -1;
15823
+ if (artM) {
15824
+ expr = expr.replace(artRegExp, "");
15825
+ art = artM[2];
15826
+ line = parseInt(artM[1], 10);
15827
+ } else {
15828
+ expr = expr.replace(escapeSlashRegExp, "\\$&").replace(escapeBreakReturnRegExp, "\\n");
15829
+ }
15830
+ if (operate === "@") {
15831
+ hasAtRule = true;
15832
+ funcSource += `'+($dbgExpr='<%${operate + expr}%>',$refFn($refAlt,${content}))+'`;
15833
+ } else if (operate === "=" || operate === ":") {
15834
+ funcSource += `'+($dbgExpr='<%${operate + expr}%>',$encHtml(${content}))+'`;
15835
+ } else if (operate === "!") {
15836
+ if (!content.startsWith("$encUri(") || !content.endsWith(")")) {
15837
+ content = `$strSafe(${content})`;
15838
+ }
15839
+ funcSource += `'+($dbgExpr='<%${operate + expr}%>',${content})+'`;
15840
+ } else if (content) {
15841
+ if (line > -1) {
15842
+ funcSource += `';$dbgLine=${line};$dbgArt='${art}';`;
15843
+ content = "";
15844
+ } else {
15845
+ funcSource += `';`;
15846
+ }
15847
+ if (funcSource.endsWith(`+'';`)) {
15848
+ funcSource = funcSource.substring(0, funcSource.length - 4) + ";";
15849
+ }
15850
+ if (expr) {
15851
+ funcSource += `$dbgExpr='<%${expr}%>';`;
15852
+ }
15853
+ funcSource += content + `;$out+='`;
15854
+ }
15855
+ } else {
15856
+ if (operate === "@") {
15857
+ hasAtRule = true;
15858
+ funcSource += `'+$refFn($refAlt,${content})+'`;
15859
+ } else if (operate === "=" || operate === ":") {
15860
+ funcSource += `'+$encHtml(${content})+'`;
15861
+ } else if (operate === "!") {
15862
+ if (!content.startsWith("$encUri(") || !content.endsWith(")")) {
15863
+ content = `$strSafe(${content})`;
15864
+ }
15865
+ funcSource += `'+${content}+'`;
15866
+ } else if (content) {
15867
+ funcSource += `';`;
15868
+ if (funcSource.endsWith(`+'';`)) {
15869
+ funcSource = funcSource.substring(0, funcSource.length - 4) + ";";
15870
+ }
15871
+ funcSource += `${content};$out+='`;
15872
+ }
15873
+ }
15874
+ return match;
15875
+ });
15876
+ funcSource += `';`;
15877
+ funcSource = funcSource.replace(/\$out\+='';/g, "");
15878
+ funcSource = funcSource.replace(/\$out\+=''\+/g, "$out+=");
15879
+ if (debug) {
15880
+ const filePart = file ? `\\r\\n\\tat file:${file}` : "";
15881
+ funcSource = `let $dbgExpr,$dbgArt,$dbgLine;try{${funcSource}}catch(ex){let msg='render view error:'+(ex.message||ex);if($dbgArt)msg+='\\r\\n\\tsrc art:{{'+$dbgArt+'}}\\r\\n\\tat line:'+$dbgLine;msg+='\\r\\n\\t'+($dbgArt?'translate to:':'expr:');msg+=$dbgExpr+'${filePart}';throw msg;}`;
15882
+ }
15883
+ const viewIdRegExp = new RegExp(String.fromCharCode(31), "g");
15884
+ funcSource = funcSource.replace(viewIdRegExp, `'+$viewId+'`);
15885
+ void hasAtRule;
15886
+ const refFallback = "if(!$refAlt)$refAlt=$data;";
15887
+ const fullSource = `${refFallback}let $splitter='\\x1e',$tmp,$out=''{{VARS}};${funcSource}return $out`;
15888
+ return `($data,$viewId,$refAlt,$encHtml,$strSafe,$encUri,$refFn,$encQuote)=>{${fullSource}}`;
15889
+ }
15890
+ async function compileTemplate(source, options = {}) {
15891
+ const { debug = false, file, virtualDom = false, useSwc = false } = options;
15892
+ const globalVars = options.globalVars ?? await (useSwc ? extractGlobalVars(source) : extractGlobalVars2(source));
15893
+ const { protectedSource, comments } = protectComments(source);
15894
+ const converted = convertArtSyntax(protectedSource, debug);
15895
+ const viewEventProcessed = processViewEvents(converted);
15896
+ const finalSource = restoreComments(viewEventProcessed, comments);
15897
+ const varDeclarations = globalVars.map((key) => `,${key}=$data.${key}`).join("");
15898
+ if (virtualDom) {
15899
+ const funcBody2 = compileToVDomFunction(finalSource, debug, file);
15900
+ const funcWithVars2 = funcBody2.replace("{{VARS}}", () => varDeclarations);
15901
+ return `import { vdomCreate as __larkC } from "@lark.js/mvc";
15902
+ import { strSafe as __larkStrSafe, encUri as __larkEncUri, encQuote as __larkEncQuote, refFn as __larkRefFn } from "@lark.js/mvc/runtime";
15903
+ export default function(data, viewId, refData) {
15904
+ let $data = data || {},
15905
+ $viewId = viewId || '',
15906
+ $c = __larkC,
15907
+ $n = __larkStrSafe;
15908
+ return (${funcWithVars2})($data, $viewId, refData,
15909
+ $n, __larkRefFn, __larkEncUri, __larkEncQuote
15910
+ );
15911
+ }`;
15912
+ }
15913
+ const funcBody = compileToFunction(finalSource, debug, file);
15914
+ const funcWithVars = funcBody.replace("{{VARS}}", () => varDeclarations);
15915
+ return `import { encHtml as __larkEncHtml, strSafe as __larkStrSafe, encUri as __larkEncUri, encQuote as __larkEncQuote, refFn as __larkRefFn } from "@lark.js/mvc/runtime";
15916
+ export default function(data, viewId, refData) {
15917
+ let $data = data || {},
15918
+ $viewId = viewId || '';
15919
+ return (${funcWithVars})($data, $viewId, refData,
15920
+ __larkEncHtml, __larkStrSafe, __larkEncUri, __larkRefFn, __larkEncQuote
15921
+ );
15922
+ }`;
15923
+ }
15366
15924
 
15367
15925
  // src/vite.ts
15368
15926
  var LARK_TEMPLATE_SUFFIX = "?lark-template";
15369
15927
  function larkMvcPlugin(options = {}) {
15370
- const { debug = false } = options;
15928
+ const { debug = false, virtualDom = false, useSwc = false } = options;
15371
15929
  return {
15372
15930
  name: "lark-template",
15373
15931
  enforce: "pre",
@@ -15377,12 +15935,12 @@ function larkMvcPlugin(options = {}) {
15377
15935
  }
15378
15936
  return void 0;
15379
15937
  },
15380
- load(id) {
15938
+ async load(id) {
15381
15939
  if (id.endsWith(LARK_TEMPLATE_SUFFIX)) {
15382
15940
  const filePath = id.slice(0, -LARK_TEMPLATE_SUFFIX.length);
15383
15941
  const raw = import_fs.default.readFileSync(filePath, "utf-8");
15384
- const globalVars = extractGlobalVars(raw);
15385
- return compileTemplate(raw, { debug, globalVars });
15942
+ const globalVars = await extractGlobalVars2(raw);
15943
+ return compileTemplate(raw, { debug, globalVars, virtualDom, useSwc });
15386
15944
  }
15387
15945
  return void 0;
15388
15946
  }