@lark.js/mvc 0.0.14 → 0.0.16

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/rspack.js CHANGED
@@ -27,12 +27,12 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
27
27
  mod
28
28
  ));
29
29
 
30
- // ../../node_modules/.pnpm/tsup@8.5.1_@swc+core@1.15.43_@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/esm_shims.js
30
+ // ../../node_modules/.pnpm/tsup@8.5.1_jiti@2.7.0_postcss@8.5.15_tsx@4.22.4_typescript@5.9.3_yaml@2.9.0/node_modules/tsup/assets/esm_shims.js
31
31
  import path from "path";
32
32
  import { fileURLToPath } from "url";
33
33
  var getFilename, __filename;
34
34
  var init_esm_shims = __esm({
35
- "../../node_modules/.pnpm/tsup@8.5.1_@swc+core@1.15.43_@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/esm_shims.js"() {
35
+ "../../node_modules/.pnpm/tsup@8.5.1_jiti@2.7.0_postcss@8.5.15_tsx@4.22.4_typescript@5.9.3_yaml@2.9.0/node_modules/tsup/assets/esm_shims.js"() {
36
36
  "use strict";
37
37
  getFilename = () => fileURLToPath(import.meta.url);
38
38
  __filename = /* @__PURE__ */ getFilename();
@@ -14735,12 +14735,7 @@ function convertArtSyntax(source, debug) {
14735
14735
  } else {
14736
14736
  cleanCode = code.trim();
14737
14737
  }
14738
- const converted = convertArtExpression(
14739
- cleanCode,
14740
- debug,
14741
- lineNo,
14742
- blockStack
14743
- );
14738
+ const converted = convertArtExpression(cleanCode, debug, lineNo, blockStack);
14744
14739
  result.push(converted);
14745
14740
  result.push(rest);
14746
14741
  }
@@ -14986,12 +14981,74 @@ var VOID_ELEMENTS = /* @__PURE__ */ new Set([
14986
14981
  function vdomEscapeStr(s) {
14987
14982
  return s.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\x1e/g, "\\x1e");
14988
14983
  }
14984
+ function vdomResolveAttrValueIIFE(rawValue, exprStore) {
14985
+ const stmts = [];
14986
+ let remaining = rawValue;
14987
+ while (remaining.length > 0) {
14988
+ const phIdx = remaining.indexOf("\0");
14989
+ const viIdx = remaining.indexOf("");
14990
+ let nextSpecial = -1;
14991
+ let specialType = null;
14992
+ if (phIdx >= 0 && (viIdx < 0 || phIdx <= viIdx)) {
14993
+ nextSpecial = phIdx;
14994
+ specialType = "ph";
14995
+ } else if (viIdx >= 0) {
14996
+ nextSpecial = viIdx;
14997
+ specialType = "vi";
14998
+ }
14999
+ if (nextSpecial === -1) {
15000
+ if (remaining) stmts.push(`$s+='${vdomEscapeStr(remaining)}'`);
15001
+ break;
15002
+ }
15003
+ if (nextSpecial > 0) {
15004
+ stmts.push(`$s+='${vdomEscapeStr(remaining.substring(0, nextSpecial))}'`);
15005
+ }
15006
+ if (specialType === "vi") {
15007
+ stmts.push(`$s+=$viewId`);
15008
+ remaining = remaining.substring(nextSpecial + 1);
15009
+ } else {
15010
+ const closeIdx = remaining.indexOf("\0", nextSpecial + 1);
15011
+ if (closeIdx === -1) {
15012
+ stmts.push(`$s+='${vdomEscapeStr(remaining.substring(nextSpecial))}'`);
15013
+ break;
15014
+ }
15015
+ const idx = parseInt(remaining.substring(nextSpecial + 1, closeIdx), 10);
15016
+ const expr = exprStore[idx];
15017
+ if (expr.op === "=" || expr.op === ":") {
15018
+ stmts.push(`$s+=$strSafe(${expr.content})`);
15019
+ } else if (expr.op === "!") {
15020
+ if (expr.content.startsWith("$encUri(") && expr.content.endsWith(")")) {
15021
+ stmts.push(`$s+=${expr.content}`);
15022
+ } else {
15023
+ stmts.push(`$s+=$strSafe(${expr.content})`);
15024
+ }
15025
+ } else if (expr.op === "@") {
15026
+ stmts.push(`$s+=$refFn($refAlt,${expr.content})`);
15027
+ } else {
15028
+ stmts.push(expr.content);
15029
+ }
15030
+ remaining = remaining.substring(closeIdx + 1);
15031
+ }
15032
+ }
15033
+ const body = stmts.join(";");
15034
+ return `(()=>{let $s='';${body};return $s;})()`;
15035
+ }
14989
15036
  function vdomResolveAttrValue(rawValue, exprStore) {
14990
15037
  const hasPlaceholders = rawValue.includes("\0");
14991
15038
  const hasViewId = rawValue.includes("");
14992
15039
  if (!hasPlaceholders && !hasViewId) {
14993
15040
  return `'${vdomEscapeStr(rawValue)}'`;
14994
15041
  }
15042
+ if (hasPlaceholders) {
15043
+ const codeBlockRegExp = /\x00(\d+)\x00/g;
15044
+ let m;
15045
+ while ((m = codeBlockRegExp.exec(rawValue)) !== null) {
15046
+ const idx = parseInt(m[1], 10);
15047
+ if (exprStore[idx] && exprStore[idx].op === "") {
15048
+ return vdomResolveAttrValueIIFE(rawValue, exprStore);
15049
+ }
15050
+ }
15051
+ }
14995
15052
  const segments = [];
14996
15053
  let remaining = rawValue;
14997
15054
  while (remaining.length > 0) {
@@ -15025,17 +15082,17 @@ function vdomResolveAttrValue(rawValue, exprStore) {
15025
15082
  const idx = parseInt(remaining.substring(nextSpecial + 1, closeIdx));
15026
15083
  const expr = exprStore[idx];
15027
15084
  if (expr.op === "=" || expr.op === ":") {
15028
- segments.push(`$n(${expr.content})`);
15085
+ segments.push(`$strSafe(${expr.content})`);
15029
15086
  } else if (expr.op === "!") {
15030
15087
  if (expr.content.startsWith("$encUri(") && expr.content.endsWith(")")) {
15031
15088
  segments.push(expr.content);
15032
15089
  } else {
15033
- segments.push(`$n(${expr.content})`);
15090
+ segments.push(`$strSafe(${expr.content})`);
15034
15091
  }
15035
15092
  } else if (expr.op === "@") {
15036
15093
  segments.push(`$refFn($refAlt,${expr.content})`);
15037
15094
  } else {
15038
- segments.push(`$n(${expr.content})`);
15095
+ segments.push(`$strSafe(${expr.content})`);
15039
15096
  }
15040
15097
  remaining = remaining.substring(closeIdx + 1);
15041
15098
  }
@@ -15077,14 +15134,7 @@ function compileToVDomFunction(source, debug, file) {
15077
15134
  });
15078
15135
  const rootVar = `$v${varCounter++}`;
15079
15136
  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
15137
  function allocVar() {
15087
- if (varCounter >= maxVars) return `$v${maxVars - 1}`;
15088
15138
  return `$v${varCounter++}`;
15089
15139
  }
15090
15140
  function emitNode(node, parentVar) {
@@ -15101,7 +15151,7 @@ function compileToVDomFunction(source, debug, file) {
15101
15151
  if (i % 2 === 0) {
15102
15152
  const trimmed = parts[i];
15103
15153
  if (trimmed.trim()) {
15104
- lines.push(`${parentVar}.push($c(0,'${vdomEscapeStr(trimmed)}'))`);
15154
+ lines.push(`${parentVar}.push($vdomCreate(0,'${vdomEscapeStr(trimmed)}'))`);
15105
15155
  }
15106
15156
  } else {
15107
15157
  const idx = parseInt(parts[i]);
@@ -15112,15 +15162,15 @@ function compileToVDomFunction(source, debug, file) {
15112
15162
  }
15113
15163
  function emitExpr(expr, parentVar) {
15114
15164
  if (expr.op === "=" || expr.op === ":") {
15115
- lines.push(`${parentVar}.push($c(0,$n(${expr.content})))`);
15165
+ lines.push(`${parentVar}.push($vdomCreate(0,$strSafe(${expr.content})))`);
15116
15166
  } else if (expr.op === "!") {
15117
15167
  if (expr.content.startsWith("$encUri(") && expr.content.endsWith(")")) {
15118
- lines.push(`${parentVar}.push($c(0,${expr.content}))`);
15168
+ lines.push(`${parentVar}.push($vdomCreate(0,${expr.content},1))`);
15119
15169
  } else {
15120
- lines.push(`${parentVar}.push($c(0,$n(${expr.content})))`);
15170
+ lines.push(`${parentVar}.push($vdomCreate(0,$strSafe(${expr.content}),1))`);
15121
15171
  }
15122
15172
  } else if (expr.op === "@") {
15123
- lines.push(`${parentVar}.push($c(0,$refFn($refAlt,${expr.content})))`);
15173
+ lines.push(`${parentVar}.push($vdomCreate(0,$refFn($refAlt,${expr.content})))`);
15124
15174
  } else if (expr.content) {
15125
15175
  lines.push(expr.content);
15126
15176
  }
@@ -15138,25 +15188,26 @@ function compileToVDomFunction(source, debug, file) {
15138
15188
  }
15139
15189
  const isVoid = VOID_ELEMENTS.has(tagName) && children.length === 0;
15140
15190
  const childrenArg = isVoid ? "1" : childVar;
15141
- lines.push(
15142
- `${parentVar}.push($c('${tagName}',${propsKey},${childrenArg}))`
15143
- );
15191
+ lines.push(`${parentVar}.push($vdomCreate('${tagName}',${propsKey},${childrenArg}))`);
15144
15192
  }
15145
15193
  for (const child of doc.children) {
15146
15194
  emitNode(child, rootVar);
15147
15195
  }
15148
- lines.push(`return $c($viewId,0,${rootVar})`);
15149
- const body = lines.join(";");
15196
+ lines.push(`return $vdomCreate($viewId,0,${rootVar})`);
15197
+ const varDeclStmts = [];
15198
+ for (let i = 1; i < varCounter; i++) varDeclStmts.push(`$v${i}`);
15199
+ const varDecl = varDeclStmts.length ? `let ${varDeclStmts.join(",")};` : "";
15200
+ const body = varDecl + lines.join(";");
15150
15201
  let funcBody = body;
15151
15202
  if (debug) {
15152
15203
  const filePart = file ? `\\r\\n\\tat file:${file}` : "";
15153
- funcBody = `let $dbgExpr,$dbgArt,$dbgLine;try{${body}}catch(ex){let msg='render view error:'+(ex.message||ex);msg+='${filePart}';throw msg;}`;
15204
+ funcBody = `let $dbgExpr,$dbgArt;try{${body}}catch(e){let msg='render error:'+(e.message||e);msg+='${filePart}';throw msg;}`;
15154
15205
  }
15155
15206
  const viewIdRegExp = new RegExp(String.fromCharCode(31), "g");
15156
15207
  funcBody = funcBody.replace(viewIdRegExp, `'+$viewId+'`);
15157
15208
  const refFallback = "if(!$refAlt)$refAlt=$data;";
15158
15209
  const fullSource = `${refFallback}let $splitter='\\x1e'{{VARS}};${funcBody}`;
15159
- return `($data,$viewId,$refAlt,$n,$refFn,$encUri,$encQuote)=>{${fullSource}}`;
15210
+ return `($data,$viewId,$refAlt,$strSafe,$refFn,$encUri,$encQuote)=>{${fullSource}}`;
15160
15211
  }
15161
15212
 
15162
15213
  // src/compiler/extract-global-vars.ts
@@ -15390,9 +15441,6 @@ var BUILTIN_GLOBALS = /* @__PURE__ */ new Set([
15390
15441
  // conversion, for error reporting. Only present in debug mode.
15391
15442
  // e.g. $dbgArt='{{=user.name}}'
15392
15443
  "$dbgArt",
15393
- // Debug: source line number — tracks the current line in the template
15394
- // source, for error reporting. Only present in debug mode.
15395
- "$dbgLine",
15396
15444
  // RefData alias — fallback reference lookup table.
15397
15445
  // Defaults to $data when no explicit $refAlt is provided.
15398
15446
  // Ensures $refFn() does not crash when @ operator is used without refData.
@@ -15476,19 +15524,14 @@ function compileToFunction(source, debug, file) {
15476
15524
  funcSource += source.substring(index, offset).replace(escapeSlashRegExp, "\\$&").replace(escapeBreakReturnRegExp, "\\n");
15477
15525
  index = offset + match.length;
15478
15526
  if (debug) {
15479
- let expr = source.substring(
15480
- index - match.length + 2 + (operate ? 1 : 0),
15481
- index - 2
15482
- );
15527
+ let expr = source.substring(index - match.length + 2 + (operate ? 1 : 0), index - 2);
15483
15528
  const x11 = String.fromCharCode(17);
15484
15529
  const artRegExp = new RegExp(`^'(\\d+)${x11}([^${x11}]+)${x11}'$`);
15485
15530
  const artM = expr.match(artRegExp);
15486
15531
  let art = "";
15487
- let line = -1;
15488
15532
  if (artM) {
15489
15533
  expr = expr.replace(artRegExp, "");
15490
15534
  art = artM[2];
15491
- line = parseInt(artM[1], 10);
15492
15535
  } else {
15493
15536
  expr = expr.replace(escapeSlashRegExp, "\\$&").replace(escapeBreakReturnRegExp, "\\n");
15494
15537
  }
@@ -15503,8 +15546,8 @@ function compileToFunction(source, debug, file) {
15503
15546
  }
15504
15547
  funcSource += `'+($dbgExpr='<%${operate + expr}%>',${content})+'`;
15505
15548
  } else if (content) {
15506
- if (line > -1) {
15507
- funcSource += `';$dbgLine=${line};$dbgArt='${art}';`;
15549
+ if (artM) {
15550
+ funcSource += `';$dbgArt='${art}';`;
15508
15551
  content = "";
15509
15552
  } else {
15510
15553
  funcSource += `';`;
@@ -15543,7 +15586,7 @@ function compileToFunction(source, debug, file) {
15543
15586
  funcSource = funcSource.replace(/\$out\+=''\+/g, "$out+=");
15544
15587
  if (debug) {
15545
15588
  const filePart = file ? `\\r\\n\\tat file:${file}` : "";
15546
- 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;}`;
15589
+ funcSource = `let $dbgExpr,$dbgArt;try{${funcSource}}catch(e){let msg='render error:'+(e.message||e);if($dbgArt)msg+='\\r\\n\\tsrc art:{{'+$dbgArt+'}};msg+='\\r\\n\\t'+($dbgArt?'translate to:':'expr:');msg+=$dbgExpr+'${filePart}';throw msg;}`;
15547
15590
  }
15548
15591
  const viewIdRegExp = new RegExp(String.fromCharCode(31), "g");
15549
15592
  funcSource = funcSource.replace(viewIdRegExp, `'+$viewId+'`);
@@ -15563,28 +15606,75 @@ async function compileTemplate(source, options = {}) {
15563
15606
  if (virtualDom) {
15564
15607
  const funcBody2 = compileToVDomFunction(finalSource, debug, file);
15565
15608
  const funcWithVars2 = funcBody2.replace("{{VARS}}", () => varDeclarations);
15566
- return `import { vdomCreate as __larkC } from "@lark.js/mvc";
15609
+ return `import { vdomCreate as __larkVdomCreate } from "@lark.js/mvc";
15567
15610
  import { strSafe as __larkStrSafe, encUri as __larkEncUri, encQuote as __larkEncQuote, refFn as __larkRefFn } from "@lark.js/mvc/runtime";
15568
- export default function(data, viewId, refData) {
15611
+ function __larkTemplate(data, viewId, refData) {
15569
15612
  let $data = data || {},
15570
15613
  $viewId = viewId || '',
15571
- $c = __larkC,
15572
- $n = __larkStrSafe;
15614
+ $vdomCreate = __larkVdomCreate,
15615
+ $strSafe = __larkStrSafe;
15573
15616
  return (${funcWithVars2})($data, $viewId, refData,
15574
- $n, __larkRefFn, __larkEncUri, __larkEncQuote
15617
+ $strSafe, __larkRefFn, __larkEncUri, __larkEncQuote
15575
15618
  );
15576
- }`;
15619
+ }
15620
+ export default __larkTemplate;`;
15577
15621
  }
15578
15622
  const funcBody = compileToFunction(finalSource, debug, file);
15579
15623
  const funcWithVars = funcBody.replace("{{VARS}}", () => varDeclarations);
15580
15624
  return `import { encHtml as __larkEncHtml, strSafe as __larkStrSafe, encUri as __larkEncUri, encQuote as __larkEncQuote, refFn as __larkRefFn } from "@lark.js/mvc/runtime";
15581
- export default function(data, viewId, refData) {
15625
+ function __larkTemplate(data, viewId, refData) {
15582
15626
  let $data = data || {},
15583
15627
  $viewId = viewId || '';
15584
15628
  return (${funcWithVars})($data, $viewId, refData,
15585
15629
  __larkEncHtml, __larkStrSafe, __larkEncUri, __larkRefFn, __larkEncQuote
15586
15630
  );
15587
- }`;
15631
+ }
15632
+ export default __larkTemplate;`;
15633
+ }
15634
+
15635
+ // src/hmr-inject.ts
15636
+ init_esm_shims();
15637
+ var TEMPLATE_VAR = "__larkTemplate";
15638
+ function getTemplateHmrSnippet(bundler) {
15639
+ if (bundler === "vite") {
15640
+ return `
15641
+ // \u2500\u2500 Lark template HMR (auto-injected by larkMvcPlugin \u2014 Vite) \u2500\u2500
15642
+ if (import.meta.hot) {
15643
+ import.meta.hot.dispose(function(__larkData) {
15644
+ __larkData.oldTemplate = ${TEMPLATE_VAR};
15645
+ });
15646
+ import.meta.hot.accept(function(__larkNewModule) {
15647
+ var __larkNew = __larkNewModule && __larkNewModule.default;
15648
+ var __larkOld = import.meta.hot.data && import.meta.hot.data.oldTemplate;
15649
+ if (__larkOld && __larkNew && __larkOld !== __larkNew) {
15650
+ import("@lark.js/mvc").then(function(m) {
15651
+ if (m && m.hotSwapByTemplate) m.hotSwapByTemplate(__larkOld, __larkNew);
15652
+ });
15653
+ }
15654
+ });
15655
+ }
15656
+ `;
15657
+ }
15658
+ return `
15659
+ // \u2500\u2500 Lark template HMR (auto-injected by larkMvcPlugin \u2014 ${bundler}) \u2500\u2500
15660
+ if (typeof module !== "undefined" && module.hot) {
15661
+ module.hot.dispose(function(__larkData) {
15662
+ __larkData.oldTemplate = ${TEMPLATE_VAR};
15663
+ });
15664
+ module.hot.accept(function() {
15665
+ var __larkNew = ${TEMPLATE_VAR};
15666
+ var __larkOld = module.hot && module.hot.data && module.hot.data.oldTemplate;
15667
+ if (__larkOld && __larkNew && __larkOld !== __larkNew) {
15668
+ import("@lark.js/mvc").then(function(m) {
15669
+ if (m && m.hotSwapByTemplate) m.hotSwapByTemplate(__larkOld, __larkNew);
15670
+ });
15671
+ }
15672
+ });
15673
+ }
15674
+ `;
15675
+ }
15676
+ function injectTemplateHmrSnippet(source, bundler) {
15677
+ return source + "\n" + getTemplateHmrSnippet(bundler);
15588
15678
  }
15589
15679
 
15590
15680
  // src/rspack.ts
@@ -15593,11 +15683,12 @@ async function larkMvcLoader(source) {
15593
15683
  const options = this.getOptions();
15594
15684
  const { debug = false, virtualDom = false } = options;
15595
15685
  const globalVars = await extractGlobalVars(source);
15596
- return await compileTemplate(source, {
15686
+ const compiled = await compileTemplate(source, {
15597
15687
  debug,
15598
15688
  globalVars,
15599
15689
  virtualDom
15600
15690
  });
15691
+ return injectTemplateHmrSnippet(compiled, "rspack");
15601
15692
  } catch (err) {
15602
15693
  console.error(err);
15603
15694
  return "";
package/dist/runtime.cjs CHANGED
@@ -60,10 +60,7 @@ var URI_ENT_MAP = {
60
60
  };
61
61
  var URI_ENT_REGEXP = /[!')(*]/g;
62
62
  function encodeURIExtra(v) {
63
- return encodeURIComponent(strSafe(v)).replace(
64
- URI_ENT_REGEXP,
65
- (m) => URI_ENT_MAP[m]
66
- );
63
+ return encodeURIComponent(strSafe(v)).replace(URI_ENT_REGEXP, (m) => URI_ENT_MAP[m]);
67
64
  }
68
65
  var QUOTE_ENT_REGEXP = /['"\\]/g;
69
66
  function encodeQuote(v) {
package/dist/runtime.js CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  encodeURIExtra,
5
5
  refFn,
6
6
  strSafe
7
- } from "./chunk-66OZBBSP.js";
7
+ } from "./chunk-OGPBFCIK.js";
8
8
 
9
9
  // src/runtime.ts
10
10
  var strSafe2 = strSafe;