@lark.js/mvc 0.0.10 → 0.0.12

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.
@@ -4,6 +4,9 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __getProtoOf = Object.getPrototypeOf;
6
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __esm = (fn, res) => function __init() {
8
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
9
+ };
7
10
  var __commonJS = (cb, mod) => function __require() {
8
11
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
9
12
  };
@@ -24,10 +27,23 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
24
27
  mod
25
28
  ));
26
29
 
30
+ // ../../node_modules/.pnpm/tsup@8.5.1_@swc+core@1.15.4_34a04ed47198e6dd24361ae1f6711a67/node_modules/tsup/assets/esm_shims.js
31
+ import path from "path";
32
+ import { fileURLToPath } from "url";
33
+ var getFilename, __filename;
34
+ var init_esm_shims = __esm({
35
+ "../../node_modules/.pnpm/tsup@8.5.1_@swc+core@1.15.4_34a04ed47198e6dd24361ae1f6711a67/node_modules/tsup/assets/esm_shims.js"() {
36
+ "use strict";
37
+ getFilename = () => fileURLToPath(import.meta.url);
38
+ __filename = /* @__PURE__ */ getFilename();
39
+ }
40
+ });
41
+
27
42
  // ../../node_modules/.pnpm/@babel+parser@7.29.7/node_modules/@babel/parser/lib/index.js
28
43
  var require_lib = __commonJS({
29
44
  "../../node_modules/.pnpm/@babel+parser@7.29.7/node_modules/@babel/parser/lib/index.js"(exports) {
30
45
  "use strict";
46
+ init_esm_shims();
31
47
  Object.defineProperty(exports, "__esModule", {
32
48
  value: true
33
49
  });
@@ -14512,7 +14528,7 @@ var require_lib = __commonJS({
14512
14528
  options = Object.assign({}, options);
14513
14529
  try {
14514
14530
  options.sourceType = "module";
14515
- const parser = getParser(options, input);
14531
+ const parser = getParser2(options, input);
14516
14532
  const ast = parser.parse();
14517
14533
  if (parser.sawUnambiguousESM) {
14518
14534
  return ast;
@@ -14520,7 +14536,7 @@ var require_lib = __commonJS({
14520
14536
  if (parser.ambiguousScriptDifferentAst) {
14521
14537
  try {
14522
14538
  options.sourceType = "script";
14523
- return getParser(options, input).parse();
14539
+ return getParser2(options, input).parse();
14524
14540
  } catch (_unused) {
14525
14541
  }
14526
14542
  } else {
@@ -14530,17 +14546,17 @@ var require_lib = __commonJS({
14530
14546
  } catch (moduleError) {
14531
14547
  try {
14532
14548
  options.sourceType = "script";
14533
- return getParser(options, input).parse();
14549
+ return getParser2(options, input).parse();
14534
14550
  } catch (_unused2) {
14535
14551
  }
14536
14552
  throw moduleError;
14537
14553
  }
14538
14554
  } else {
14539
- return getParser(options, input).parse();
14555
+ return getParser2(options, input).parse();
14540
14556
  }
14541
14557
  }
14542
14558
  function parseExpression(input, options) {
14543
- const parser = getParser(options, input);
14559
+ const parser = getParser2(options, input);
14544
14560
  if (parser.options.strictMode) {
14545
14561
  parser.state.strict = true;
14546
14562
  }
@@ -14554,7 +14570,7 @@ var require_lib = __commonJS({
14554
14570
  return tokenTypes2;
14555
14571
  }
14556
14572
  var tokTypes = generateExportedTokenTypes(tt);
14557
- function getParser(options, input) {
14573
+ function getParser2(options, input) {
14558
14574
  let cls = Parser;
14559
14575
  const pluginsMap = /* @__PURE__ */ new Map();
14560
14576
  if (options != null && options.plugins) {
@@ -14599,8 +14615,17 @@ var require_lib = __commonJS({
14599
14615
  }
14600
14616
  });
14601
14617
 
14618
+ // src/rspack.ts
14619
+ init_esm_shims();
14620
+
14602
14621
  // src/compiler.ts
14603
- var import_parser = __toESM(require_lib(), 1);
14622
+ init_esm_shims();
14623
+
14624
+ // src/compiler/compile-template.ts
14625
+ init_esm_shims();
14626
+
14627
+ // src/compiler/template-syntax.ts
14628
+ init_esm_shims();
14604
14629
  var SPLITTER = String.fromCharCode(30);
14605
14630
  var VIEW_ID_PLACEHOLDER = String.fromCharCode(31);
14606
14631
  function jsObjectToUrlParams(paramsStr) {
@@ -14721,7 +14746,7 @@ function convertArtSyntax(source, debug) {
14721
14746
  }
14722
14747
  if (blockStack.length > 0) {
14723
14748
  const unclosed = blockStack.map((b) => `"${b.ctrl}" at line ${b.line}`).join(", ");
14724
- throw new Error(`[@lark.js/mvc error] unclosed block(s): ${unclosed}`);
14749
+ throw new Error(`Unclosed block(s): ${unclosed}`);
14725
14750
  }
14726
14751
  return result.join("");
14727
14752
  }
@@ -14820,7 +14845,7 @@ function convertArtExpression(code, debug, lineNo, blockStack = []) {
14820
14845
  const object = tokens[0];
14821
14846
  if (tokens.length > 1 && tokens[1] !== "as") {
14822
14847
  throw new Error(
14823
- `[@lark.js/mvc error] bad forOf syntax: {{${code}}}. Expected "as" keyword, got "${tokens[1]}". Usage: {{forOf list as item [index]}}`
14848
+ `Bad forOf syntax: {{${code}}}. Expected "as" keyword, got "${tokens[1]}". Usage: {{forOf list as item [index]}}`
14824
14849
  );
14825
14850
  }
14826
14851
  const restTokens = tokens.slice(2);
@@ -14847,7 +14872,7 @@ function convertArtExpression(code, debug, lineNo, blockStack = []) {
14847
14872
  const object = tokens[0];
14848
14873
  if (tokens.length > 1 && tokens[1] !== "as") {
14849
14874
  throw new Error(
14850
- `[@lark.js/mvc error] bad forIn syntax: {{${code}}}. Expected "as" keyword, got "${tokens[1]}". Usage: {{forIn obj as val [key]}}`
14875
+ `Bad forIn syntax: {{${code}}}. Expected "as" keyword, got "${tokens[1]}". Usage: {{forIn obj as val [key]}}`
14851
14876
  );
14852
14877
  }
14853
14878
  const restTokens2 = tokens.slice(2);
@@ -14873,13 +14898,11 @@ function convertArtExpression(code, debug, lineNo, blockStack = []) {
14873
14898
  const expectedCtrl = keyword.substring(1);
14874
14899
  const last = blockStack.pop();
14875
14900
  if (!last) {
14876
- throw new Error(
14877
- `[@lark.js/mvc error] unexpected {{${code}}}: no matching open block`
14878
- );
14901
+ throw new Error(`Unexpected {{${code}}}: no matching open block`);
14879
14902
  }
14880
14903
  if (last.ctrl !== expectedCtrl) {
14881
14904
  throw new Error(
14882
- `[@lark.js/mvc error] unexpected {{${code}}}: expected {{/${last.ctrl}}} to close block opened at line ${last.line}`
14905
+ `Unexpected {{${code}}}: expected {{/${last.ctrl}}} to close block opened at line ${last.line}`
14883
14906
  );
14884
14907
  }
14885
14908
  return `${debugPrefix}<%}%>`;
@@ -14940,112 +14963,219 @@ function parseAsExpr(expr) {
14940
14963
  bad: false
14941
14964
  };
14942
14965
  }
14943
- function compileToFunction(source, debug, file) {
14944
- const matcher = /<%([@=!:])?([\s\S]*?)%>|$/g;
14945
- let index = 0;
14946
- let funcSource = `$out+='`;
14947
- let hasAtRule = false;
14948
- const escapeSlashRegExp = /\\|'/g;
14949
- const escapeBreakReturnRegExp = /\r|\n/g;
14950
- source.replace(matcher, (match, operate, content, offset) => {
14951
- funcSource += source.substring(index, offset).replace(escapeSlashRegExp, "\\$&").replace(escapeBreakReturnRegExp, "\\n");
14952
- index = offset + match.length;
14953
- if (debug) {
14954
- let expr = source.substring(
14955
- index - match.length + 2 + (operate ? 1 : 0),
14956
- index - 2
14957
- );
14958
- const x11 = String.fromCharCode(17);
14959
- const artRegExp = new RegExp(`^'(\\d+)${x11}([^${x11}]+)${x11}'$`);
14960
- const artM = expr.match(artRegExp);
14961
- let art = "";
14962
- let line = -1;
14963
- if (artM) {
14964
- expr = expr.replace(artRegExp, "");
14965
- art = artM[2];
14966
- line = parseInt(artM[1], 10);
14967
- } else {
14968
- expr = expr.replace(escapeSlashRegExp, "\\$&").replace(escapeBreakReturnRegExp, "\\n");
14966
+
14967
+ // src/compiler/compile-to-vdom-function.ts
14968
+ init_esm_shims();
14969
+ import { parseDocument } from "htmlparser2";
14970
+ var VOID_ELEMENTS = /* @__PURE__ */ new Set([
14971
+ "area",
14972
+ "base",
14973
+ "br",
14974
+ "col",
14975
+ "embed",
14976
+ "hr",
14977
+ "img",
14978
+ "input",
14979
+ "link",
14980
+ "meta",
14981
+ "param",
14982
+ "source",
14983
+ "track",
14984
+ "wbr"
14985
+ ]);
14986
+ function vdomEscapeStr(s) {
14987
+ return s.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\x1e/g, "\\x1e");
14988
+ }
14989
+ function vdomResolveAttrValue(rawValue, exprStore) {
14990
+ const hasPlaceholders = rawValue.includes("\0");
14991
+ const hasViewId = rawValue.includes("");
14992
+ if (!hasPlaceholders && !hasViewId) {
14993
+ return `'${vdomEscapeStr(rawValue)}'`;
14994
+ }
14995
+ const segments = [];
14996
+ let remaining = rawValue;
14997
+ while (remaining.length > 0) {
14998
+ const phIdx = remaining.indexOf("\0");
14999
+ const viIdx = remaining.indexOf("");
15000
+ let nextSpecial = -1;
15001
+ let specialType = null;
15002
+ if (phIdx >= 0 && (viIdx < 0 || phIdx <= viIdx)) {
15003
+ nextSpecial = phIdx;
15004
+ specialType = "ph";
15005
+ } else if (viIdx >= 0) {
15006
+ nextSpecial = viIdx;
15007
+ specialType = "vi";
15008
+ }
15009
+ if (nextSpecial === -1) {
15010
+ if (remaining) segments.push(`'${vdomEscapeStr(remaining)}'`);
15011
+ break;
15012
+ }
15013
+ if (nextSpecial > 0) {
15014
+ segments.push(`'${vdomEscapeStr(remaining.substring(0, nextSpecial))}'`);
15015
+ }
15016
+ if (specialType === "vi") {
15017
+ segments.push("$viewId");
15018
+ remaining = remaining.substring(nextSpecial + 1);
15019
+ } else {
15020
+ const closeIdx = remaining.indexOf("\0", nextSpecial + 1);
15021
+ if (closeIdx === -1) {
15022
+ segments.push(`'${vdomEscapeStr(remaining.substring(nextSpecial))}'`);
15023
+ break;
14969
15024
  }
14970
- if (operate === "@") {
14971
- hasAtRule = true;
14972
- funcSource += `'+($dbgExpr='<%${operate + expr}%>',$refFn($refAlt,${content}))+'`;
14973
- } else if (operate === "=" || operate === ":") {
14974
- funcSource += `'+($dbgExpr='<%${operate + expr}%>',$encHtml(${content}))+'`;
14975
- } else if (operate === "!") {
14976
- if (!content.startsWith("$encUri(") || !content.endsWith(")")) {
14977
- content = `$strSafe(${content})`;
14978
- }
14979
- funcSource += `'+($dbgExpr='<%${operate + expr}%>',${content})+'`;
14980
- } else if (content) {
14981
- if (line > -1) {
14982
- funcSource += `';$dbgLine=${line};$dbgArt='${art}';`;
14983
- content = "";
15025
+ const idx = parseInt(remaining.substring(nextSpecial + 1, closeIdx));
15026
+ const expr = exprStore[idx];
15027
+ if (expr.op === "=" || expr.op === ":") {
15028
+ segments.push(`$n(${expr.content})`);
15029
+ } else if (expr.op === "!") {
15030
+ if (expr.content.startsWith("$encUri(") && expr.content.endsWith(")")) {
15031
+ segments.push(expr.content);
14984
15032
  } else {
14985
- funcSource += `';`;
14986
- }
14987
- if (funcSource.endsWith(`+'';`)) {
14988
- funcSource = funcSource.substring(0, funcSource.length - 4) + ";";
15033
+ segments.push(`$n(${expr.content})`);
14989
15034
  }
14990
- if (expr) {
14991
- funcSource += `$dbgExpr='<%${expr}%>';`;
14992
- }
14993
- funcSource += content + `;$out+='`;
15035
+ } else if (expr.op === "@") {
15036
+ segments.push(`$refFn($refAlt,${expr.content})`);
15037
+ } else {
15038
+ segments.push(`$n(${expr.content})`);
14994
15039
  }
14995
- } else {
14996
- if (operate === "@") {
14997
- hasAtRule = true;
14998
- funcSource += `'+$refFn($refAlt,${content})+'`;
14999
- } else if (operate === "=" || operate === ":") {
15000
- funcSource += `'+$encHtml(${content})+'`;
15001
- } else if (operate === "!") {
15002
- if (!content.startsWith("$encUri(") || !content.endsWith(")")) {
15003
- content = `$strSafe(${content})`;
15004
- }
15005
- funcSource += `'+${content}+'`;
15006
- } else if (content) {
15007
- funcSource += `';`;
15008
- if (funcSource.endsWith(`+'';`)) {
15009
- funcSource = funcSource.substring(0, funcSource.length - 4) + ";";
15040
+ remaining = remaining.substring(closeIdx + 1);
15041
+ }
15042
+ }
15043
+ if (segments.length === 0) return "''";
15044
+ if (segments.length === 1) return segments[0];
15045
+ return segments.join("+");
15046
+ }
15047
+ function vdomBuildPropsFromAttribs(attribs, exprStore) {
15048
+ if (!attribs) return "null";
15049
+ const keys = Object.keys(attribs);
15050
+ if (keys.length === 0) return "null";
15051
+ const entries = [];
15052
+ for (const name of keys) {
15053
+ const value = attribs[name];
15054
+ const valueExpr = vdomResolveAttrValue(value, exprStore);
15055
+ entries.push(`'${vdomEscapeStr(name)}':${valueExpr}`);
15056
+ }
15057
+ return `{${entries.join(",")}}`;
15058
+ }
15059
+ function compileToVDomFunction(source, debug, file) {
15060
+ const lines = [];
15061
+ let varCounter = 0;
15062
+ let propsCounter = 0;
15063
+ const exprStore = [];
15064
+ const protectedSource = source.replace(
15065
+ /<%([@=!:])?([\s\S]*?)%>/g,
15066
+ (_, op, content) => {
15067
+ const idx = exprStore.length;
15068
+ exprStore.push({ op: op || "", content: (content || "").trim() });
15069
+ return `\0${idx}\0`;
15070
+ }
15071
+ );
15072
+ const doc = parseDocument(protectedSource, {
15073
+ recognizeSelfClosing: true,
15074
+ lowerCaseTags: false,
15075
+ lowerCaseAttributeNames: false,
15076
+ decodeEntities: false
15077
+ });
15078
+ const rootVar = `$v${varCounter++}`;
15079
+ lines.push(`let ${rootVar}=[]`);
15080
+ const maxVars = 30;
15081
+ const varDecls = [];
15082
+ for (let i = 1; i < maxVars; i++) {
15083
+ varDecls.push(`$v${i}`);
15084
+ }
15085
+ lines.push(`let ${varDecls.join(",")}`);
15086
+ function allocVar() {
15087
+ if (varCounter >= maxVars) return `$v${maxVars - 1}`;
15088
+ return `$v${varCounter++}`;
15089
+ }
15090
+ function emitNode(node, parentVar) {
15091
+ const type = node.type;
15092
+ if (type === "text") {
15093
+ emitText(node.data || "", parentVar);
15094
+ } else if (type === "tag" || type === "script" || type === "style") {
15095
+ emitElement(node, parentVar);
15096
+ }
15097
+ }
15098
+ function emitText(text, parentVar) {
15099
+ const parts = text.split(/\x00(\d+)\x00/);
15100
+ for (let i = 0; i < parts.length; i++) {
15101
+ if (i % 2 === 0) {
15102
+ const trimmed = parts[i];
15103
+ if (trimmed.trim()) {
15104
+ lines.push(`${parentVar}.push($c(0,'${vdomEscapeStr(trimmed)}'))`);
15010
15105
  }
15011
- funcSource += `${content};$out+='`;
15106
+ } else {
15107
+ const idx = parseInt(parts[i]);
15108
+ const expr = exprStore[idx];
15109
+ emitExpr(expr, parentVar);
15012
15110
  }
15013
15111
  }
15014
- return match;
15015
- });
15016
- funcSource += `';`;
15017
- funcSource = funcSource.replace(/\$out\+='';/g, "");
15018
- funcSource = funcSource.replace(/\$out\+=''\+/g, "$out+=");
15112
+ }
15113
+ function emitExpr(expr, parentVar) {
15114
+ if (expr.op === "=" || expr.op === ":") {
15115
+ lines.push(`${parentVar}.push($c(0,$n(${expr.content})))`);
15116
+ } else if (expr.op === "!") {
15117
+ if (expr.content.startsWith("$encUri(") && expr.content.endsWith(")")) {
15118
+ lines.push(`${parentVar}.push($c(0,${expr.content}))`);
15119
+ } else {
15120
+ lines.push(`${parentVar}.push($c(0,$n(${expr.content})))`);
15121
+ }
15122
+ } else if (expr.op === "@") {
15123
+ lines.push(`${parentVar}.push($c(0,$refFn($refAlt,${expr.content})))`);
15124
+ } else if (expr.content) {
15125
+ lines.push(expr.content);
15126
+ }
15127
+ }
15128
+ function emitElement(node, parentVar) {
15129
+ const tagName = node.name || "";
15130
+ const children = node.children || [];
15131
+ const childVar = allocVar();
15132
+ const propsKey = `__p${propsCounter++}`;
15133
+ const props = vdomBuildPropsFromAttribs(node.attribs, exprStore);
15134
+ lines.push(`let ${propsKey}=${props}`);
15135
+ lines.push(`${childVar}=[]`);
15136
+ for (const child of children) {
15137
+ emitNode(child, childVar);
15138
+ }
15139
+ const isVoid = VOID_ELEMENTS.has(tagName) && children.length === 0;
15140
+ const childrenArg = isVoid ? "1" : childVar;
15141
+ lines.push(
15142
+ `${parentVar}.push($c('${tagName}',${propsKey},${childrenArg}))`
15143
+ );
15144
+ }
15145
+ for (const child of doc.children) {
15146
+ emitNode(child, rootVar);
15147
+ }
15148
+ lines.push(`return $c($viewId,0,${rootVar})`);
15149
+ const body = lines.join(";");
15150
+ let funcBody = body;
15019
15151
  if (debug) {
15020
15152
  const filePart = file ? `\\r\\n\\tat file:${file}` : "";
15021
- 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;}`;
15153
+ funcBody = `let $dbgExpr,$dbgArt,$dbgLine;try{${body}}catch(ex){let msg='render view error:'+(ex.message||ex);msg+='${filePart}';throw msg;}`;
15022
15154
  }
15023
15155
  const viewIdRegExp = new RegExp(String.fromCharCode(31), "g");
15024
- funcSource = funcSource.replace(viewIdRegExp, `'+$viewId+'`);
15025
- void hasAtRule;
15156
+ funcBody = funcBody.replace(viewIdRegExp, `'+$viewId+'`);
15026
15157
  const refFallback = "if(!$refAlt)$refAlt=$data;";
15027
- const fullSource = `${refFallback}let $splitter='\\x1e',$tmp,$out=''{{VARS}};${funcSource}return $out`;
15028
- return `($data,$viewId,$refAlt,$encHtml,$strSafe,$encUri,$refFn,$encQuote)=>{${fullSource}}`;
15158
+ const fullSource = `${refFallback}let $splitter='\\x1e'{{VARS}};${funcBody}`;
15159
+ return `($data,$viewId,$refAlt,$n,$refFn,$encUri,$encQuote)=>{${fullSource}}`;
15029
15160
  }
15030
- function compileTemplate(source, options = {}) {
15031
- const { debug = false, globalVars = [], file } = options;
15032
- const { protectedSource, comments } = protectComments(source);
15033
- const converted = convertArtSyntax(protectedSource, debug);
15034
- const viewEventProcessed = processViewEvents(converted);
15035
- const finalSource = restoreComments(viewEventProcessed, comments);
15036
- const funcBody = compileToFunction(finalSource, debug, file);
15037
- const varDeclarations = globalVars.map((key) => `,${key}=$data.${key}`).join("");
15038
- const funcWithVars = funcBody.replace("{{VARS}}", () => varDeclarations);
15039
- return `import { encHtml as __larkEncHtml, strSafe as __larkStrSafe, encUri as __larkEncUri, encQuote as __larkEncQuote, refFn as __larkRefFn } from "@lark.js/mvc/runtime";
15040
- export default function(data, viewId, refData) {
15041
- let $data = data || {},
15042
- $viewId = viewId || '';
15043
- return (${funcWithVars})($data, $viewId, refData,
15044
- __larkEncHtml, __larkStrSafe, __larkEncUri, __larkRefFn, __larkEncQuote
15045
- );
15046
- }`;
15161
+
15162
+ // src/compiler/swc/extract-global-vars.ts
15163
+ init_esm_shims();
15164
+ var parseSyncFn = null;
15165
+ var parseSyncLoadAttempted = false;
15166
+ async function getParser() {
15167
+ if (!parseSyncLoadAttempted) {
15168
+ parseSyncLoadAttempted = true;
15169
+ try {
15170
+ const swc = await import("@swc/core");
15171
+ parseSyncFn = swc.parseSync;
15172
+ } catch (err) {
15173
+ console.error("failed to load @swc/core", err);
15174
+ }
15175
+ }
15176
+ return parseSyncFn;
15047
15177
  }
15048
- function extractGlobalVars(source) {
15178
+ async function extractGlobalVars(source) {
15049
15179
  const { protectedSource, comments: _comments } = protectComments(source);
15050
15180
  const viewEventProcessed = processViewEvents(protectedSource);
15051
15181
  const converted = convertArtSyntax(viewEventProcessed, false);
@@ -15074,12 +15204,15 @@ function extractGlobalVars(source) {
15074
15204
  );
15075
15205
  let fn = fnParts.join("");
15076
15206
  fn = `(function(){${fn}})`;
15207
+ const parseSync = await getParser();
15208
+ if (!parseSync) {
15209
+ return fallbackExtractVariables(source);
15210
+ }
15077
15211
  let ast;
15078
15212
  try {
15079
- ast = (0, import_parser.parse)(fn, {
15080
- sourceType: "script",
15081
- allowReturnOutsideFunction: true,
15082
- allowAwaitOutsideFunction: true
15213
+ ast = parseSync(fn, {
15214
+ syntax: "ecmascript",
15215
+ target: "es2022"
15083
15216
  });
15084
15217
  } catch {
15085
15218
  return fallbackExtractVariables(source);
@@ -15087,55 +15220,55 @@ function extractGlobalVars(source) {
15087
15220
  const globalExists = {};
15088
15221
  for (const name of BUILTIN_GLOBALS) globalExists[name] = 1;
15089
15222
  const globalVars = /* @__PURE__ */ Object.create(null);
15090
- const fnRange = [];
15091
- walkAst(ast, {
15223
+ const fnNodes = [];
15224
+ walkSwcAst(ast, {
15092
15225
  VariableDeclarator(node) {
15093
15226
  if (node.id.type === "Identifier") {
15094
- const name = node.id.name;
15227
+ const name = node.id.value;
15095
15228
  globalExists[name] = node.init ? 3 : 2;
15096
15229
  }
15097
15230
  },
15098
15231
  FunctionDeclaration(node) {
15099
- if (node.id) {
15100
- globalExists[node.id.name] = 3;
15232
+ if (node.identifier) {
15233
+ globalExists[node.identifier.value] = 3;
15101
15234
  }
15102
- fnRange.push(node);
15235
+ fnNodes.push(node);
15103
15236
  },
15104
15237
  FunctionExpression(node) {
15105
- fnRange.push(node);
15238
+ fnNodes.push(node);
15106
15239
  },
15107
15240
  ArrowFunctionExpression(node) {
15108
- fnRange.push(node);
15241
+ fnNodes.push(node);
15109
15242
  },
15110
15243
  CallExpression(node) {
15111
15244
  if (node.callee.type === "Identifier") {
15112
- globalExists[node.callee.name] = 1;
15245
+ globalExists[node.callee.value] = 1;
15113
15246
  }
15114
15247
  }
15115
15248
  });
15116
15249
  const functionParams = /* @__PURE__ */ Object.create(null);
15117
- for (const fnNode of fnRange) {
15118
- const params = "params" in fnNode ? fnNode.params : [];
15119
- for (const p of params) {
15120
- if (p.type === "Identifier") {
15121
- functionParams[p.name] = 1;
15122
- } else if (p.type === "AssignmentPattern" && p.left.type === "Identifier") {
15123
- functionParams[p.left.name] = 1;
15124
- } else if (p.type === "RestElement" && p.argument.type === "Identifier") {
15125
- functionParams[p.argument.name] = 1;
15250
+ for (const fnNode of fnNodes) {
15251
+ const patterns = getParamPatterns(fnNode);
15252
+ for (const pat of patterns) {
15253
+ if (pat.type === "Identifier") {
15254
+ functionParams[pat.value] = 1;
15255
+ } else if (pat.type === "AssignmentPattern" && pat.left.type === "Identifier") {
15256
+ functionParams[pat.left.value] = 1;
15257
+ } else if (pat.type === "RestElement" && pat.argument.type === "Identifier") {
15258
+ functionParams[pat.argument.value] = 1;
15126
15259
  }
15127
15260
  }
15128
15261
  }
15129
- walkAst(ast, {
15262
+ walkSwcAst(ast, {
15130
15263
  Identifier(node) {
15131
- const name = node.name;
15264
+ const name = node.value;
15132
15265
  if (globalExists[name]) return;
15133
15266
  if (functionParams[name]) return;
15134
15267
  globalVars[name] = 1;
15135
15268
  },
15136
15269
  AssignmentExpression(node) {
15137
15270
  if (node.left.type === "Identifier") {
15138
- const name = node.left.name;
15271
+ const name = node.left.value;
15139
15272
  if (!globalExists[name] || globalExists[name] === 1) {
15140
15273
  globalExists[name] = (globalExists[name] || 0) + 1;
15141
15274
  }
@@ -15144,6 +15277,12 @@ function extractGlobalVars(source) {
15144
15277
  });
15145
15278
  return Object.keys(globalVars);
15146
15279
  }
15280
+ function getParamPatterns(fnNode) {
15281
+ if (fnNode.type === "ArrowFunctionExpression") {
15282
+ return fnNode.params;
15283
+ }
15284
+ return fnNode.params.map((p) => p.pat);
15285
+ }
15147
15286
  function fallbackExtractVariables(source) {
15148
15287
  const vars = /* @__PURE__ */ new Set();
15149
15288
  const outputRegExp = /\{\{[:=!@]\s*([a-zA-Z_$][\w$]*)[^}]*\}\}/g;
@@ -15161,41 +15300,39 @@ function fallbackExtractVariables(source) {
15161
15300
  }
15162
15301
  return Array.from(vars).filter((v) => !BUILTIN_GLOBALS.has(v));
15163
15302
  }
15164
- function walkAst(ast, visitors) {
15303
+ function walkSwcAst(ast, visitors) {
15165
15304
  function visit(node) {
15166
15305
  const type = node.type;
15167
15306
  if (visitors[type]) {
15168
15307
  visitors[type](node);
15169
15308
  }
15170
- const bag = node;
15171
15309
  for (const key of Object.keys(node)) {
15172
- if (key === "type" || key === "start" || key === "end" || key === "loc" || key === "range")
15173
- continue;
15310
+ if (key === "type" || key === "span" || key === "ctxt") continue;
15174
15311
  if (type === "MemberExpression" && key === "property") {
15175
15312
  const me = node;
15176
- if (!me.computed) continue;
15313
+ if (me.property.type !== "Computed") continue;
15177
15314
  }
15178
- if (type === "ObjectProperty" && key === "key") {
15179
- const op = node;
15180
- if (!op.computed) continue;
15315
+ if (type === "KeyValueProperty" && key === "key") {
15316
+ const kv = node;
15317
+ if (kv.key.type !== "Computed") continue;
15181
15318
  }
15182
- if (type === "ObjectMethod" && key === "key") {
15183
- const om = node;
15184
- if (!om.computed) continue;
15319
+ if (type === "MethodProperty" && key === "key") {
15320
+ const mp = node;
15321
+ if (mp.key.type !== "Computed") continue;
15185
15322
  }
15186
- const child = bag[key];
15323
+ const child = Reflect.get(node, key);
15187
15324
  if (Array.isArray(child)) {
15188
15325
  for (const item of child) {
15189
- if (isAstNode(item)) visit(item);
15326
+ if (isSwcNode(item)) visit(item);
15190
15327
  }
15191
- } else if (isAstNode(child)) {
15328
+ } else if (isSwcNode(child)) {
15192
15329
  visit(child);
15193
15330
  }
15194
15331
  }
15195
15332
  }
15196
15333
  visit(ast);
15197
15334
  }
15198
- function isAstNode(v) {
15335
+ function isSwcNode(v) {
15199
15336
  return !!v && typeof v === "object" && typeof v.type === "string";
15200
15337
  }
15201
15338
  var BUILTIN_GLOBALS = /* @__PURE__ */ new Set([
@@ -15340,7 +15477,7 @@ var BUILTIN_GLOBALS = /* @__PURE__ */ new Set([
15340
15477
  "decodeURIComponent",
15341
15478
  "encodeURI",
15342
15479
  "decodeURI",
15343
- // Babel helpers
15480
+ // SWC helpers
15344
15481
  "arguments",
15345
15482
  "this",
15346
15483
  "require",
@@ -15348,7 +15485,490 @@ var BUILTIN_GLOBALS = /* @__PURE__ */ new Set([
15348
15485
  "Lark"
15349
15486
  ]);
15350
15487
 
15488
+ // src/compiler/extract-global-vars.ts
15489
+ init_esm_shims();
15490
+ var import_parser = __toESM(require_lib(), 1);
15491
+ async function extractGlobalVars2(source) {
15492
+ const { protectedSource, comments: _comments } = protectComments(source);
15493
+ const viewEventProcessed = processViewEvents(protectedSource);
15494
+ const converted = convertArtSyntax(viewEventProcessed, false);
15495
+ const template = restoreComments(converted, _comments);
15496
+ const templateCmdRegExp = /<%([@=!:])?([\s\S]*?)%>|$/g;
15497
+ const fnParts = [];
15498
+ const htmlStore = {};
15499
+ let htmlIndex = 0;
15500
+ let lastIndex = 0;
15501
+ const htmlKey = String.fromCharCode(5);
15502
+ template.replace(
15503
+ templateCmdRegExp,
15504
+ (match, operate, content, offset) => {
15505
+ const start = operate ? 3 : 2;
15506
+ const htmlText = template.substring(lastIndex, offset + start);
15507
+ const key = htmlKey + htmlIndex++ + htmlKey;
15508
+ htmlStore[key] = htmlText;
15509
+ lastIndex = offset + match.length - 2;
15510
+ if (operate && content.trim()) {
15511
+ fnParts.push(';"' + key + '";', "[" + content + "]");
15512
+ } else {
15513
+ fnParts.push(';"' + key + '";', content || "");
15514
+ }
15515
+ return match;
15516
+ }
15517
+ );
15518
+ let fn = fnParts.join("");
15519
+ fn = `(function(){${fn}})`;
15520
+ let ast;
15521
+ try {
15522
+ ast = (0, import_parser.parse)(fn, {
15523
+ sourceType: "script",
15524
+ allowReturnOutsideFunction: true,
15525
+ allowAwaitOutsideFunction: true
15526
+ });
15527
+ } catch {
15528
+ return fallbackExtractVariables2(source);
15529
+ }
15530
+ const globalExists = {};
15531
+ for (const name of BUILTIN_GLOBALS2) globalExists[name] = 1;
15532
+ const globalVars = /* @__PURE__ */ Object.create(null);
15533
+ const fnRange = [];
15534
+ walkAst(ast, {
15535
+ VariableDeclarator(node) {
15536
+ if (node.id.type === "Identifier") {
15537
+ const name = node.id.name;
15538
+ globalExists[name] = node.init ? 3 : 2;
15539
+ }
15540
+ },
15541
+ FunctionDeclaration(node) {
15542
+ if (node.id) {
15543
+ globalExists[node.id.name] = 3;
15544
+ }
15545
+ fnRange.push(node);
15546
+ },
15547
+ FunctionExpression(node) {
15548
+ fnRange.push(node);
15549
+ },
15550
+ ArrowFunctionExpression(node) {
15551
+ fnRange.push(node);
15552
+ },
15553
+ CallExpression(node) {
15554
+ if (node.callee.type === "Identifier") {
15555
+ globalExists[node.callee.name] = 1;
15556
+ }
15557
+ }
15558
+ });
15559
+ const functionParams = /* @__PURE__ */ Object.create(null);
15560
+ for (const fnNode of fnRange) {
15561
+ const params = "params" in fnNode ? fnNode.params : [];
15562
+ for (const p of params) {
15563
+ if (p.type === "Identifier") {
15564
+ functionParams[p.name] = 1;
15565
+ } else if (p.type === "AssignmentPattern" && p.left.type === "Identifier") {
15566
+ functionParams[p.left.name] = 1;
15567
+ } else if (p.type === "RestElement" && p.argument.type === "Identifier") {
15568
+ functionParams[p.argument.name] = 1;
15569
+ }
15570
+ }
15571
+ }
15572
+ walkAst(ast, {
15573
+ Identifier(node) {
15574
+ const name = node.name;
15575
+ if (globalExists[name]) return;
15576
+ if (functionParams[name]) return;
15577
+ globalVars[name] = 1;
15578
+ },
15579
+ AssignmentExpression(node) {
15580
+ if (node.left.type === "Identifier") {
15581
+ const name = node.left.name;
15582
+ if (!globalExists[name] || globalExists[name] === 1) {
15583
+ globalExists[name] = (globalExists[name] || 0) + 1;
15584
+ }
15585
+ }
15586
+ }
15587
+ });
15588
+ return Object.keys(globalVars);
15589
+ }
15590
+ function fallbackExtractVariables2(source) {
15591
+ const vars = /* @__PURE__ */ new Set();
15592
+ const outputRegExp = /\{\{[:=!@]\s*([a-zA-Z_$][\w$]*)[^}]*\}\}/g;
15593
+ let m;
15594
+ while ((m = outputRegExp.exec(source)) !== null) {
15595
+ vars.add(m[1]);
15596
+ }
15597
+ const eachRegExp = /\{\{forOf\s+([a-zA-Z_$][\w$]*)\s+as/g;
15598
+ while ((m = eachRegExp.exec(source)) !== null) {
15599
+ vars.add(m[1]);
15600
+ }
15601
+ const ifRegExp = /\{\{(?:else\s+)?if\s+([a-zA-Z_$][\w$]*)[^}]*\}\}/g;
15602
+ while ((m = ifRegExp.exec(source)) !== null) {
15603
+ vars.add(m[1]);
15604
+ }
15605
+ return Array.from(vars).filter((v) => !BUILTIN_GLOBALS2.has(v));
15606
+ }
15607
+ function walkAst(ast, visitors) {
15608
+ function visit(node) {
15609
+ const type = node.type;
15610
+ if (visitors[type]) {
15611
+ visitors[type](node);
15612
+ }
15613
+ for (const key of Object.keys(node)) {
15614
+ if (key === "type" || key === "start" || key === "end" || key === "loc" || key === "range")
15615
+ continue;
15616
+ if (type === "MemberExpression" && key === "property") {
15617
+ const me = node;
15618
+ if (!me.computed) continue;
15619
+ }
15620
+ if (type === "ObjectProperty" && key === "key") {
15621
+ const op = node;
15622
+ if (!op.computed) continue;
15623
+ }
15624
+ if (type === "ObjectMethod" && key === "key") {
15625
+ const om = node;
15626
+ if (!om.computed) continue;
15627
+ }
15628
+ const child = Reflect.get(node, key);
15629
+ if (Array.isArray(child)) {
15630
+ for (const item of child) {
15631
+ if (isAstNode(item)) visit(item);
15632
+ }
15633
+ } else if (isAstNode(child)) {
15634
+ visit(child);
15635
+ }
15636
+ }
15637
+ }
15638
+ visit(ast);
15639
+ }
15640
+ function isAstNode(v) {
15641
+ return !!v && typeof v === "object" && typeof v.type === "string";
15642
+ }
15643
+ var BUILTIN_GLOBALS2 = /* @__PURE__ */ new Set([
15644
+ // ─── Template runtime helpers (injected by compileToFunction) ───────
15645
+ //
15646
+ // These variables appear in the generated template function signature
15647
+ // or body. They must be excluded from extractGlobalVars() so that
15648
+ // they are not mistaken for user data variables and destructured from $data.
15649
+ // SPLITTER character constant (same as \x1e), used as namespace separator
15650
+ // for refData keys, event attribute encoding, and internal data structures.
15651
+ // Declared as: let $splitter='\x1e'
15652
+ "$splitter",
15653
+ // Data — the data object passed from Updater to the template function.
15654
+ // User variables are destructured from $data at the top of the function:
15655
+ // let {name, age} = $data;
15656
+ // This is the first parameter of the generated arrow function.
15657
+ "$data",
15658
+ // Null-safe toString: v => '' + (v == null ? '' : v)
15659
+ // Converts null/undefined to empty string, otherwise calls toString().
15660
+ // Wraps every {{!raw}} output to prevent "null" / "undefined" rendering.
15661
+ "$strSafe",
15662
+ // HTML entity encoder: v => $strSafe(v).replace(/[&<>"'`]/g, entityMap)
15663
+ // Encodes &, <, >, ", ', ` to HTML entities (&amp; &lt; etc.)
15664
+ // Applied to all {{=escaped}} and {{:binding}} outputs.
15665
+ "$encHtml",
15666
+ // HTML entity map — internal object used by $encHtml:
15667
+ // {'&':'amp','<':'gt','>':'gt','"':'#34','\'':'#39','`':'#96'}
15668
+ // Not a standalone function; referenced inside $encHtml's closure.
15669
+ "$entMap",
15670
+ // HTML entity RegExp — internal regexp used by $encHtml:
15671
+ // /[&<>"'`]/g
15672
+ "$entReg",
15673
+ // HTML entity replacer function — internal helper used by $encHtml:
15674
+ // m => '&' + $entMap[m] + ';'
15675
+ // Maps matched character to its entity string.
15676
+ "$entFn",
15677
+ // Output buffer — the string accumulator for rendered HTML.
15678
+ // All template output is appended via $out += '...'.
15679
+ // Declared as: let $out = ''
15680
+ "$out",
15681
+ // Reference lookup: (refData, value) => key
15682
+ // Finds or allocates a SPLITTER-prefixed key in refData for a given
15683
+ // object reference. Used by {{@ref}} operator for passing object
15684
+ // references to child views via v-lark attributes.
15685
+ "$refFn",
15686
+ // URI encoder: v => encodeURIComponent($strSafe(v)).replace(/[!')(*]/g, extraMap)
15687
+ // Extends encodeURIComponent with encoding of ! ' ( ) *.
15688
+ // Applied to values in @event URL parameters and {{!uri}} contexts.
15689
+ "$encUri",
15690
+ // URI encode map — internal object used by $encUri:
15691
+ // {'!':'%21','\'':'%27','(':'%28',')':'%29','*':'%2A'}
15692
+ "$uriMap",
15693
+ // URI encode replacer — internal helper used by $encUri:
15694
+ // m => $uriMap[m]
15695
+ "$uriFn",
15696
+ // URI encode regexp — internal regexp used by $encUri:
15697
+ // /[!')(*]/g
15698
+ "$uriReg",
15699
+ // Quote encoder: v => $strSafe(v).replace(/['"\\]/g, '\\$&')
15700
+ // Escapes quotes and backslashes for safe embedding in HTML attribute
15701
+ // values (e.g. data-json='...').
15702
+ "$encQuote",
15703
+ // Quote encode regexp — internal regexp used by $encQuote:
15704
+ // /['"\\]/g
15705
+ "$qReg",
15706
+ // View ID — the unique identifier of the owning View instance.
15707
+ // Injected into @event attribute values at render time so that
15708
+ // EventDelegator can dispatch events to the correct View handler.
15709
+ // The \x1f placeholder in compiled output is replaced with '+$viewId+'.
15710
+ "$viewId",
15711
+ // Debug: current expression text — stores the template expression being
15712
+ // evaluated, for error reporting. Only present in debug mode.
15713
+ // e.g. $dbgExpr='<%=user.name%>'
15714
+ "$dbgExpr",
15715
+ // Debug: original art syntax — stores the {{}} template syntax before
15716
+ // conversion, for error reporting. Only present in debug mode.
15717
+ // e.g. $dbgArt='{{=user.name}}'
15718
+ "$dbgArt",
15719
+ // Debug: source line number — tracks the current line in the template
15720
+ // source, for error reporting. Only present in debug mode.
15721
+ "$dbgLine",
15722
+ // RefData alias — fallback reference lookup table.
15723
+ // Defaults to $data when no explicit $refAlt is provided.
15724
+ // Ensures $refFn() does not crash when @ operator is used without refData.
15725
+ "$refAlt",
15726
+ // Temporary variable — used by the compiler for intermediate
15727
+ // expression results in generated code (e.g. loop variables,
15728
+ // conditional branches). Declared as: let $tmp
15729
+ "$tmp",
15730
+ // JS literals
15731
+ "undefined",
15732
+ "null",
15733
+ "true",
15734
+ "false",
15735
+ "NaN",
15736
+ "Infinity",
15737
+ // JS built-in globals
15738
+ "window",
15739
+ "self",
15740
+ "globalThis",
15741
+ "document",
15742
+ "console",
15743
+ "JSON",
15744
+ "Math",
15745
+ "Intl",
15746
+ "Promise",
15747
+ "Symbol",
15748
+ "Number",
15749
+ "String",
15750
+ "Boolean",
15751
+ "Array",
15752
+ "Object",
15753
+ "Date",
15754
+ "RegExp",
15755
+ "Error",
15756
+ "TypeError",
15757
+ "RangeError",
15758
+ "SyntaxError",
15759
+ "Map",
15760
+ "Set",
15761
+ "WeakMap",
15762
+ "WeakSet",
15763
+ "Proxy",
15764
+ "Reflect",
15765
+ "ArrayBuffer",
15766
+ "DataView",
15767
+ "Float32Array",
15768
+ "Float64Array",
15769
+ "Int8Array",
15770
+ "Int16Array",
15771
+ "Int32Array",
15772
+ "Uint8Array",
15773
+ "Uint16Array",
15774
+ "Uint32Array",
15775
+ "Uint8ClampedArray",
15776
+ // Functions
15777
+ "parseInt",
15778
+ "parseFloat",
15779
+ "isNaN",
15780
+ "isFinite",
15781
+ "encodeURIComponent",
15782
+ "decodeURIComponent",
15783
+ "encodeURI",
15784
+ "decodeURI",
15785
+ // Babel helpers
15786
+ "arguments",
15787
+ "this",
15788
+ "require",
15789
+ // Lark framework
15790
+ "Lark"
15791
+ ]);
15792
+
15793
+ // src/compiler/compile-template.ts
15794
+ function compileToFunction(source, debug, file) {
15795
+ const matcher = /<%([@=!:])?([\s\S]*?)%>|$/g;
15796
+ let index = 0;
15797
+ let funcSource = `$out+='`;
15798
+ let hasAtRule = false;
15799
+ const escapeSlashRegExp = /\\|'/g;
15800
+ const escapeBreakReturnRegExp = /\r|\n/g;
15801
+ source.replace(matcher, (match, operate, content, offset) => {
15802
+ funcSource += source.substring(index, offset).replace(escapeSlashRegExp, "\\$&").replace(escapeBreakReturnRegExp, "\\n");
15803
+ index = offset + match.length;
15804
+ if (debug) {
15805
+ let expr = source.substring(
15806
+ index - match.length + 2 + (operate ? 1 : 0),
15807
+ index - 2
15808
+ );
15809
+ const x11 = String.fromCharCode(17);
15810
+ const artRegExp = new RegExp(`^'(\\d+)${x11}([^${x11}]+)${x11}'$`);
15811
+ const artM = expr.match(artRegExp);
15812
+ let art = "";
15813
+ let line = -1;
15814
+ if (artM) {
15815
+ expr = expr.replace(artRegExp, "");
15816
+ art = artM[2];
15817
+ line = parseInt(artM[1], 10);
15818
+ } else {
15819
+ expr = expr.replace(escapeSlashRegExp, "\\$&").replace(escapeBreakReturnRegExp, "\\n");
15820
+ }
15821
+ if (operate === "@") {
15822
+ hasAtRule = true;
15823
+ funcSource += `'+($dbgExpr='<%${operate + expr}%>',$refFn($refAlt,${content}))+'`;
15824
+ } else if (operate === "=" || operate === ":") {
15825
+ funcSource += `'+($dbgExpr='<%${operate + expr}%>',$encHtml(${content}))+'`;
15826
+ } else if (operate === "!") {
15827
+ if (!content.startsWith("$encUri(") || !content.endsWith(")")) {
15828
+ content = `$strSafe(${content})`;
15829
+ }
15830
+ funcSource += `'+($dbgExpr='<%${operate + expr}%>',${content})+'`;
15831
+ } else if (content) {
15832
+ if (line > -1) {
15833
+ funcSource += `';$dbgLine=${line};$dbgArt='${art}';`;
15834
+ content = "";
15835
+ } else {
15836
+ funcSource += `';`;
15837
+ }
15838
+ if (funcSource.endsWith(`+'';`)) {
15839
+ funcSource = funcSource.substring(0, funcSource.length - 4) + ";";
15840
+ }
15841
+ if (expr) {
15842
+ funcSource += `$dbgExpr='<%${expr}%>';`;
15843
+ }
15844
+ funcSource += content + `;$out+='`;
15845
+ }
15846
+ } else {
15847
+ if (operate === "@") {
15848
+ hasAtRule = true;
15849
+ funcSource += `'+$refFn($refAlt,${content})+'`;
15850
+ } else if (operate === "=" || operate === ":") {
15851
+ funcSource += `'+$encHtml(${content})+'`;
15852
+ } else if (operate === "!") {
15853
+ if (!content.startsWith("$encUri(") || !content.endsWith(")")) {
15854
+ content = `$strSafe(${content})`;
15855
+ }
15856
+ funcSource += `'+${content}+'`;
15857
+ } else if (content) {
15858
+ funcSource += `';`;
15859
+ if (funcSource.endsWith(`+'';`)) {
15860
+ funcSource = funcSource.substring(0, funcSource.length - 4) + ";";
15861
+ }
15862
+ funcSource += `${content};$out+='`;
15863
+ }
15864
+ }
15865
+ return match;
15866
+ });
15867
+ funcSource += `';`;
15868
+ funcSource = funcSource.replace(/\$out\+='';/g, "");
15869
+ funcSource = funcSource.replace(/\$out\+=''\+/g, "$out+=");
15870
+ if (debug) {
15871
+ const filePart = file ? `\\r\\n\\tat file:${file}` : "";
15872
+ 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;}`;
15873
+ }
15874
+ const viewIdRegExp = new RegExp(String.fromCharCode(31), "g");
15875
+ funcSource = funcSource.replace(viewIdRegExp, `'+$viewId+'`);
15876
+ void hasAtRule;
15877
+ const refFallback = "if(!$refAlt)$refAlt=$data;";
15878
+ const fullSource = `${refFallback}let $splitter='\\x1e',$tmp,$out=''{{VARS}};${funcSource}return $out`;
15879
+ return `($data,$viewId,$refAlt,$encHtml,$strSafe,$encUri,$refFn,$encQuote)=>{${fullSource}}`;
15880
+ }
15881
+ async function compileTemplate(source, options = {}) {
15882
+ const { debug = false, file, virtualDom = false, useSwc = false } = options;
15883
+ const globalVars = options.globalVars ?? await (useSwc ? extractGlobalVars(source) : extractGlobalVars2(source));
15884
+ const { protectedSource, comments } = protectComments(source);
15885
+ const converted = convertArtSyntax(protectedSource, debug);
15886
+ const viewEventProcessed = processViewEvents(converted);
15887
+ const finalSource = restoreComments(viewEventProcessed, comments);
15888
+ const varDeclarations = globalVars.map((key) => `,${key}=$data.${key}`).join("");
15889
+ if (virtualDom) {
15890
+ const funcBody2 = compileToVDomFunction(finalSource, debug, file);
15891
+ const funcWithVars2 = funcBody2.replace("{{VARS}}", () => varDeclarations);
15892
+ return `import { vdomCreate as __larkC } from "@lark.js/mvc";
15893
+ import { strSafe as __larkStrSafe, encUri as __larkEncUri, encQuote as __larkEncQuote, refFn as __larkRefFn } from "@lark.js/mvc/runtime";
15894
+ export default function(data, viewId, refData) {
15895
+ let $data = data || {},
15896
+ $viewId = viewId || '',
15897
+ $c = __larkC,
15898
+ $n = __larkStrSafe;
15899
+ return (${funcWithVars2})($data, $viewId, refData,
15900
+ $n, __larkRefFn, __larkEncUri, __larkEncQuote
15901
+ );
15902
+ }`;
15903
+ }
15904
+ const funcBody = compileToFunction(finalSource, debug, file);
15905
+ const funcWithVars = funcBody.replace("{{VARS}}", () => varDeclarations);
15906
+ return `import { encHtml as __larkEncHtml, strSafe as __larkStrSafe, encUri as __larkEncUri, encQuote as __larkEncQuote, refFn as __larkRefFn } from "@lark.js/mvc/runtime";
15907
+ export default function(data, viewId, refData) {
15908
+ let $data = data || {},
15909
+ $viewId = viewId || '';
15910
+ return (${funcWithVars})($data, $viewId, refData,
15911
+ __larkEncHtml, __larkStrSafe, __larkEncUri, __larkRefFn, __larkEncQuote
15912
+ );
15913
+ }`;
15914
+ }
15915
+
15916
+ // src/rspack.ts
15917
+ async function larkMvcLoader(source) {
15918
+ try {
15919
+ const options = this.getOptions();
15920
+ const { debug = false, virtualDom = false, useSwc = false } = options;
15921
+ const globalVars = await extractGlobalVars2(source);
15922
+ return await compileTemplate(source, {
15923
+ debug,
15924
+ globalVars,
15925
+ virtualDom,
15926
+ useSwc
15927
+ });
15928
+ } catch (err) {
15929
+ console.error(err);
15930
+ return "";
15931
+ }
15932
+ }
15933
+ var LarkMvcPlugin = class {
15934
+ options;
15935
+ constructor(options = {}) {
15936
+ this.options = {
15937
+ debug: false,
15938
+ virtualDom: false,
15939
+ useSwc: false,
15940
+ test: /\.html$/,
15941
+ exclude: /node_modules/,
15942
+ ...options
15943
+ };
15944
+ }
15945
+ /**
15946
+ * Rspack plugin entry point.
15947
+ * Called by rspack when the plugin is applied.
15948
+ */
15949
+ apply(compiler) {
15950
+ const { debug, virtualDom, useSwc, test, exclude } = this.options;
15951
+ compiler.options.module = compiler.options.module || {};
15952
+ compiler.options.module.rules = compiler.options.module.rules || [];
15953
+ compiler.options.module.rules.push({
15954
+ test,
15955
+ exclude,
15956
+ use: [
15957
+ {
15958
+ // Resolve the loader path (this file).
15959
+ // Deprecated implementation
15960
+ // isCjs() ? __filename : fileURLToPath(import.meta.url);
15961
+ // __filename is provided by tsup's ESM shim (shims: true) in ESM output,
15962
+ // and is a native CJS global in CJS output.
15963
+ loader: __filename,
15964
+ options: { debug, virtualDom, useSwc }
15965
+ }
15966
+ ]
15967
+ });
15968
+ }
15969
+ };
15351
15970
  export {
15352
- compileTemplate,
15353
- extractGlobalVars
15971
+ LarkMvcPlugin,
15972
+ larkMvcLoader as default,
15973
+ larkMvcLoader
15354
15974
  };