@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/README.md +324 -215
- package/dist/{chunk-66OZBBSP.js → chunk-OGPBFCIK.js} +1 -4
- package/dist/client.d.cts +63 -0
- package/dist/client.d.ts +63 -0
- package/dist/compiler.cjs +93 -48
- package/dist/compiler.js +93 -48
- package/dist/devtool.cjs +885 -1465
- package/dist/devtool.js +885 -1462
- package/dist/index.cjs +1785 -2195
- package/dist/index.d.cts +568 -1287
- package/dist/index.d.ts +568 -1287
- package/dist/index.js +1764 -2187
- package/dist/rspack.cjs +142 -51
- package/dist/rspack.js +142 -51
- package/dist/runtime.cjs +1 -4
- package/dist/runtime.js +1 -1
- package/dist/vite.cjs +298 -52
- package/dist/vite.js +298 -52
- package/dist/webpack.cjs +142 -51
- package/dist/webpack.js +142 -51
- package/package.json +3 -4
package/dist/vite.cjs
CHANGED
|
@@ -33,9 +33,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
33
33
|
));
|
|
34
34
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
35
35
|
|
|
36
|
-
// ../../node_modules/.pnpm/tsup@8.5.
|
|
36
|
+
// ../../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/cjs_shims.js
|
|
37
37
|
var init_cjs_shims = __esm({
|
|
38
|
-
"../../node_modules/.pnpm/tsup@8.5.
|
|
38
|
+
"../../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/cjs_shims.js"() {
|
|
39
39
|
"use strict";
|
|
40
40
|
}
|
|
41
41
|
});
|
|
@@ -14747,12 +14747,7 @@ function convertArtSyntax(source, debug) {
|
|
|
14747
14747
|
} else {
|
|
14748
14748
|
cleanCode = code.trim();
|
|
14749
14749
|
}
|
|
14750
|
-
const converted = convertArtExpression(
|
|
14751
|
-
cleanCode,
|
|
14752
|
-
debug,
|
|
14753
|
-
lineNo,
|
|
14754
|
-
blockStack
|
|
14755
|
-
);
|
|
14750
|
+
const converted = convertArtExpression(cleanCode, debug, lineNo, blockStack);
|
|
14756
14751
|
result.push(converted);
|
|
14757
14752
|
result.push(rest);
|
|
14758
14753
|
}
|
|
@@ -14998,12 +14993,74 @@ var VOID_ELEMENTS = /* @__PURE__ */ new Set([
|
|
|
14998
14993
|
function vdomEscapeStr(s) {
|
|
14999
14994
|
return s.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\x1e/g, "\\x1e");
|
|
15000
14995
|
}
|
|
14996
|
+
function vdomResolveAttrValueIIFE(rawValue, exprStore) {
|
|
14997
|
+
const stmts = [];
|
|
14998
|
+
let remaining = rawValue;
|
|
14999
|
+
while (remaining.length > 0) {
|
|
15000
|
+
const phIdx = remaining.indexOf("\0");
|
|
15001
|
+
const viIdx = remaining.indexOf("");
|
|
15002
|
+
let nextSpecial = -1;
|
|
15003
|
+
let specialType = null;
|
|
15004
|
+
if (phIdx >= 0 && (viIdx < 0 || phIdx <= viIdx)) {
|
|
15005
|
+
nextSpecial = phIdx;
|
|
15006
|
+
specialType = "ph";
|
|
15007
|
+
} else if (viIdx >= 0) {
|
|
15008
|
+
nextSpecial = viIdx;
|
|
15009
|
+
specialType = "vi";
|
|
15010
|
+
}
|
|
15011
|
+
if (nextSpecial === -1) {
|
|
15012
|
+
if (remaining) stmts.push(`$s+='${vdomEscapeStr(remaining)}'`);
|
|
15013
|
+
break;
|
|
15014
|
+
}
|
|
15015
|
+
if (nextSpecial > 0) {
|
|
15016
|
+
stmts.push(`$s+='${vdomEscapeStr(remaining.substring(0, nextSpecial))}'`);
|
|
15017
|
+
}
|
|
15018
|
+
if (specialType === "vi") {
|
|
15019
|
+
stmts.push(`$s+=$viewId`);
|
|
15020
|
+
remaining = remaining.substring(nextSpecial + 1);
|
|
15021
|
+
} else {
|
|
15022
|
+
const closeIdx = remaining.indexOf("\0", nextSpecial + 1);
|
|
15023
|
+
if (closeIdx === -1) {
|
|
15024
|
+
stmts.push(`$s+='${vdomEscapeStr(remaining.substring(nextSpecial))}'`);
|
|
15025
|
+
break;
|
|
15026
|
+
}
|
|
15027
|
+
const idx = parseInt(remaining.substring(nextSpecial + 1, closeIdx), 10);
|
|
15028
|
+
const expr = exprStore[idx];
|
|
15029
|
+
if (expr.op === "=" || expr.op === ":") {
|
|
15030
|
+
stmts.push(`$s+=$strSafe(${expr.content})`);
|
|
15031
|
+
} else if (expr.op === "!") {
|
|
15032
|
+
if (expr.content.startsWith("$encUri(") && expr.content.endsWith(")")) {
|
|
15033
|
+
stmts.push(`$s+=${expr.content}`);
|
|
15034
|
+
} else {
|
|
15035
|
+
stmts.push(`$s+=$strSafe(${expr.content})`);
|
|
15036
|
+
}
|
|
15037
|
+
} else if (expr.op === "@") {
|
|
15038
|
+
stmts.push(`$s+=$refFn($refAlt,${expr.content})`);
|
|
15039
|
+
} else {
|
|
15040
|
+
stmts.push(expr.content);
|
|
15041
|
+
}
|
|
15042
|
+
remaining = remaining.substring(closeIdx + 1);
|
|
15043
|
+
}
|
|
15044
|
+
}
|
|
15045
|
+
const body = stmts.join(";");
|
|
15046
|
+
return `(()=>{let $s='';${body};return $s;})()`;
|
|
15047
|
+
}
|
|
15001
15048
|
function vdomResolveAttrValue(rawValue, exprStore) {
|
|
15002
15049
|
const hasPlaceholders = rawValue.includes("\0");
|
|
15003
15050
|
const hasViewId = rawValue.includes("");
|
|
15004
15051
|
if (!hasPlaceholders && !hasViewId) {
|
|
15005
15052
|
return `'${vdomEscapeStr(rawValue)}'`;
|
|
15006
15053
|
}
|
|
15054
|
+
if (hasPlaceholders) {
|
|
15055
|
+
const codeBlockRegExp = /\x00(\d+)\x00/g;
|
|
15056
|
+
let m;
|
|
15057
|
+
while ((m = codeBlockRegExp.exec(rawValue)) !== null) {
|
|
15058
|
+
const idx = parseInt(m[1], 10);
|
|
15059
|
+
if (exprStore[idx] && exprStore[idx].op === "") {
|
|
15060
|
+
return vdomResolveAttrValueIIFE(rawValue, exprStore);
|
|
15061
|
+
}
|
|
15062
|
+
}
|
|
15063
|
+
}
|
|
15007
15064
|
const segments = [];
|
|
15008
15065
|
let remaining = rawValue;
|
|
15009
15066
|
while (remaining.length > 0) {
|
|
@@ -15037,17 +15094,17 @@ function vdomResolveAttrValue(rawValue, exprStore) {
|
|
|
15037
15094
|
const idx = parseInt(remaining.substring(nextSpecial + 1, closeIdx));
|
|
15038
15095
|
const expr = exprStore[idx];
|
|
15039
15096
|
if (expr.op === "=" || expr.op === ":") {
|
|
15040
|
-
segments.push(`$
|
|
15097
|
+
segments.push(`$strSafe(${expr.content})`);
|
|
15041
15098
|
} else if (expr.op === "!") {
|
|
15042
15099
|
if (expr.content.startsWith("$encUri(") && expr.content.endsWith(")")) {
|
|
15043
15100
|
segments.push(expr.content);
|
|
15044
15101
|
} else {
|
|
15045
|
-
segments.push(`$
|
|
15102
|
+
segments.push(`$strSafe(${expr.content})`);
|
|
15046
15103
|
}
|
|
15047
15104
|
} else if (expr.op === "@") {
|
|
15048
15105
|
segments.push(`$refFn($refAlt,${expr.content})`);
|
|
15049
15106
|
} else {
|
|
15050
|
-
segments.push(`$
|
|
15107
|
+
segments.push(`$strSafe(${expr.content})`);
|
|
15051
15108
|
}
|
|
15052
15109
|
remaining = remaining.substring(closeIdx + 1);
|
|
15053
15110
|
}
|
|
@@ -15089,14 +15146,7 @@ function compileToVDomFunction(source, debug, file) {
|
|
|
15089
15146
|
});
|
|
15090
15147
|
const rootVar = `$v${varCounter++}`;
|
|
15091
15148
|
lines.push(`let ${rootVar}=[]`);
|
|
15092
|
-
const maxVars = 30;
|
|
15093
|
-
const varDecls = [];
|
|
15094
|
-
for (let i = 1; i < maxVars; i++) {
|
|
15095
|
-
varDecls.push(`$v${i}`);
|
|
15096
|
-
}
|
|
15097
|
-
lines.push(`let ${varDecls.join(",")}`);
|
|
15098
15149
|
function allocVar() {
|
|
15099
|
-
if (varCounter >= maxVars) return `$v${maxVars - 1}`;
|
|
15100
15150
|
return `$v${varCounter++}`;
|
|
15101
15151
|
}
|
|
15102
15152
|
function emitNode(node, parentVar) {
|
|
@@ -15113,7 +15163,7 @@ function compileToVDomFunction(source, debug, file) {
|
|
|
15113
15163
|
if (i % 2 === 0) {
|
|
15114
15164
|
const trimmed = parts[i];
|
|
15115
15165
|
if (trimmed.trim()) {
|
|
15116
|
-
lines.push(`${parentVar}.push($
|
|
15166
|
+
lines.push(`${parentVar}.push($vdomCreate(0,'${vdomEscapeStr(trimmed)}'))`);
|
|
15117
15167
|
}
|
|
15118
15168
|
} else {
|
|
15119
15169
|
const idx = parseInt(parts[i]);
|
|
@@ -15124,15 +15174,15 @@ function compileToVDomFunction(source, debug, file) {
|
|
|
15124
15174
|
}
|
|
15125
15175
|
function emitExpr(expr, parentVar) {
|
|
15126
15176
|
if (expr.op === "=" || expr.op === ":") {
|
|
15127
|
-
lines.push(`${parentVar}.push($
|
|
15177
|
+
lines.push(`${parentVar}.push($vdomCreate(0,$strSafe(${expr.content})))`);
|
|
15128
15178
|
} else if (expr.op === "!") {
|
|
15129
15179
|
if (expr.content.startsWith("$encUri(") && expr.content.endsWith(")")) {
|
|
15130
|
-
lines.push(`${parentVar}.push($
|
|
15180
|
+
lines.push(`${parentVar}.push($vdomCreate(0,${expr.content},1))`);
|
|
15131
15181
|
} else {
|
|
15132
|
-
lines.push(`${parentVar}.push($
|
|
15182
|
+
lines.push(`${parentVar}.push($vdomCreate(0,$strSafe(${expr.content}),1))`);
|
|
15133
15183
|
}
|
|
15134
15184
|
} else if (expr.op === "@") {
|
|
15135
|
-
lines.push(`${parentVar}.push($
|
|
15185
|
+
lines.push(`${parentVar}.push($vdomCreate(0,$refFn($refAlt,${expr.content})))`);
|
|
15136
15186
|
} else if (expr.content) {
|
|
15137
15187
|
lines.push(expr.content);
|
|
15138
15188
|
}
|
|
@@ -15150,25 +15200,26 @@ function compileToVDomFunction(source, debug, file) {
|
|
|
15150
15200
|
}
|
|
15151
15201
|
const isVoid = VOID_ELEMENTS.has(tagName) && children.length === 0;
|
|
15152
15202
|
const childrenArg = isVoid ? "1" : childVar;
|
|
15153
|
-
lines.push(
|
|
15154
|
-
`${parentVar}.push($c('${tagName}',${propsKey},${childrenArg}))`
|
|
15155
|
-
);
|
|
15203
|
+
lines.push(`${parentVar}.push($vdomCreate('${tagName}',${propsKey},${childrenArg}))`);
|
|
15156
15204
|
}
|
|
15157
15205
|
for (const child of doc.children) {
|
|
15158
15206
|
emitNode(child, rootVar);
|
|
15159
15207
|
}
|
|
15160
|
-
lines.push(`return $
|
|
15161
|
-
const
|
|
15208
|
+
lines.push(`return $vdomCreate($viewId,0,${rootVar})`);
|
|
15209
|
+
const varDeclStmts = [];
|
|
15210
|
+
for (let i = 1; i < varCounter; i++) varDeclStmts.push(`$v${i}`);
|
|
15211
|
+
const varDecl = varDeclStmts.length ? `let ${varDeclStmts.join(",")};` : "";
|
|
15212
|
+
const body = varDecl + lines.join(";");
|
|
15162
15213
|
let funcBody = body;
|
|
15163
15214
|
if (debug) {
|
|
15164
15215
|
const filePart = file ? `\\r\\n\\tat file:${file}` : "";
|
|
15165
|
-
funcBody = `let $dbgExpr,$dbgArt
|
|
15216
|
+
funcBody = `let $dbgExpr,$dbgArt;try{${body}}catch(e){let msg='render error:'+(e.message||e);msg+='${filePart}';throw msg;}`;
|
|
15166
15217
|
}
|
|
15167
15218
|
const viewIdRegExp = new RegExp(String.fromCharCode(31), "g");
|
|
15168
15219
|
funcBody = funcBody.replace(viewIdRegExp, `'+$viewId+'`);
|
|
15169
15220
|
const refFallback = "if(!$refAlt)$refAlt=$data;";
|
|
15170
15221
|
const fullSource = `${refFallback}let $splitter='\\x1e'{{VARS}};${funcBody}`;
|
|
15171
|
-
return `($data,$viewId,$refAlt,$
|
|
15222
|
+
return `($data,$viewId,$refAlt,$strSafe,$refFn,$encUri,$encQuote)=>{${fullSource}}`;
|
|
15172
15223
|
}
|
|
15173
15224
|
|
|
15174
15225
|
// src/compiler/extract-global-vars.ts
|
|
@@ -15402,9 +15453,6 @@ var BUILTIN_GLOBALS = /* @__PURE__ */ new Set([
|
|
|
15402
15453
|
// conversion, for error reporting. Only present in debug mode.
|
|
15403
15454
|
// e.g. $dbgArt='{{=user.name}}'
|
|
15404
15455
|
"$dbgArt",
|
|
15405
|
-
// Debug: source line number — tracks the current line in the template
|
|
15406
|
-
// source, for error reporting. Only present in debug mode.
|
|
15407
|
-
"$dbgLine",
|
|
15408
15456
|
// RefData alias — fallback reference lookup table.
|
|
15409
15457
|
// Defaults to $data when no explicit $refAlt is provided.
|
|
15410
15458
|
// Ensures $refFn() does not crash when @ operator is used without refData.
|
|
@@ -15488,19 +15536,14 @@ function compileToFunction(source, debug, file) {
|
|
|
15488
15536
|
funcSource += source.substring(index, offset).replace(escapeSlashRegExp, "\\$&").replace(escapeBreakReturnRegExp, "\\n");
|
|
15489
15537
|
index = offset + match.length;
|
|
15490
15538
|
if (debug) {
|
|
15491
|
-
let expr = source.substring(
|
|
15492
|
-
index - match.length + 2 + (operate ? 1 : 0),
|
|
15493
|
-
index - 2
|
|
15494
|
-
);
|
|
15539
|
+
let expr = source.substring(index - match.length + 2 + (operate ? 1 : 0), index - 2);
|
|
15495
15540
|
const x11 = String.fromCharCode(17);
|
|
15496
15541
|
const artRegExp = new RegExp(`^'(\\d+)${x11}([^${x11}]+)${x11}'$`);
|
|
15497
15542
|
const artM = expr.match(artRegExp);
|
|
15498
15543
|
let art = "";
|
|
15499
|
-
let line = -1;
|
|
15500
15544
|
if (artM) {
|
|
15501
15545
|
expr = expr.replace(artRegExp, "");
|
|
15502
15546
|
art = artM[2];
|
|
15503
|
-
line = parseInt(artM[1], 10);
|
|
15504
15547
|
} else {
|
|
15505
15548
|
expr = expr.replace(escapeSlashRegExp, "\\$&").replace(escapeBreakReturnRegExp, "\\n");
|
|
15506
15549
|
}
|
|
@@ -15515,8 +15558,8 @@ function compileToFunction(source, debug, file) {
|
|
|
15515
15558
|
}
|
|
15516
15559
|
funcSource += `'+($dbgExpr='<%${operate + expr}%>',${content})+'`;
|
|
15517
15560
|
} else if (content) {
|
|
15518
|
-
if (
|
|
15519
|
-
funcSource += `';$
|
|
15561
|
+
if (artM) {
|
|
15562
|
+
funcSource += `';$dbgArt='${art}';`;
|
|
15520
15563
|
content = "";
|
|
15521
15564
|
} else {
|
|
15522
15565
|
funcSource += `';`;
|
|
@@ -15555,7 +15598,7 @@ function compileToFunction(source, debug, file) {
|
|
|
15555
15598
|
funcSource = funcSource.replace(/\$out\+=''\+/g, "$out+=");
|
|
15556
15599
|
if (debug) {
|
|
15557
15600
|
const filePart = file ? `\\r\\n\\tat file:${file}` : "";
|
|
15558
|
-
funcSource = `let $dbgExpr,$dbgArt
|
|
15601
|
+
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;}`;
|
|
15559
15602
|
}
|
|
15560
15603
|
const viewIdRegExp = new RegExp(String.fromCharCode(31), "g");
|
|
15561
15604
|
funcSource = funcSource.replace(viewIdRegExp, `'+$viewId+'`);
|
|
@@ -15575,28 +15618,205 @@ async function compileTemplate(source, options = {}) {
|
|
|
15575
15618
|
if (virtualDom) {
|
|
15576
15619
|
const funcBody2 = compileToVDomFunction(finalSource, debug, file);
|
|
15577
15620
|
const funcWithVars2 = funcBody2.replace("{{VARS}}", () => varDeclarations);
|
|
15578
|
-
return `import { vdomCreate as
|
|
15621
|
+
return `import { vdomCreate as __larkVdomCreate } from "@lark.js/mvc";
|
|
15579
15622
|
import { strSafe as __larkStrSafe, encUri as __larkEncUri, encQuote as __larkEncQuote, refFn as __larkRefFn } from "@lark.js/mvc/runtime";
|
|
15580
|
-
|
|
15623
|
+
function __larkTemplate(data, viewId, refData) {
|
|
15581
15624
|
let $data = data || {},
|
|
15582
15625
|
$viewId = viewId || '',
|
|
15583
|
-
$
|
|
15584
|
-
$
|
|
15626
|
+
$vdomCreate = __larkVdomCreate,
|
|
15627
|
+
$strSafe = __larkStrSafe;
|
|
15585
15628
|
return (${funcWithVars2})($data, $viewId, refData,
|
|
15586
|
-
$
|
|
15629
|
+
$strSafe, __larkRefFn, __larkEncUri, __larkEncQuote
|
|
15587
15630
|
);
|
|
15588
|
-
}
|
|
15631
|
+
}
|
|
15632
|
+
export default __larkTemplate;`;
|
|
15589
15633
|
}
|
|
15590
15634
|
const funcBody = compileToFunction(finalSource, debug, file);
|
|
15591
15635
|
const funcWithVars = funcBody.replace("{{VARS}}", () => varDeclarations);
|
|
15592
15636
|
return `import { encHtml as __larkEncHtml, strSafe as __larkStrSafe, encUri as __larkEncUri, encQuote as __larkEncQuote, refFn as __larkRefFn } from "@lark.js/mvc/runtime";
|
|
15593
|
-
|
|
15637
|
+
function __larkTemplate(data, viewId, refData) {
|
|
15594
15638
|
let $data = data || {},
|
|
15595
15639
|
$viewId = viewId || '';
|
|
15596
15640
|
return (${funcWithVars})($data, $viewId, refData,
|
|
15597
15641
|
__larkEncHtml, __larkStrSafe, __larkEncUri, __larkRefFn, __larkEncQuote
|
|
15598
15642
|
);
|
|
15599
|
-
}
|
|
15643
|
+
}
|
|
15644
|
+
export default __larkTemplate;`;
|
|
15645
|
+
}
|
|
15646
|
+
|
|
15647
|
+
// src/hmr-inject.ts
|
|
15648
|
+
init_cjs_shims();
|
|
15649
|
+
var TEMPLATE_VAR = "__larkTemplate";
|
|
15650
|
+
function getTemplateHmrSnippet(bundler) {
|
|
15651
|
+
if (bundler === "vite") {
|
|
15652
|
+
return `
|
|
15653
|
+
// \u2500\u2500 Lark template HMR (auto-injected by larkMvcPlugin \u2014 Vite) \u2500\u2500
|
|
15654
|
+
if (import.meta.hot) {
|
|
15655
|
+
import.meta.hot.dispose(function(__larkData) {
|
|
15656
|
+
__larkData.oldTemplate = ${TEMPLATE_VAR};
|
|
15657
|
+
});
|
|
15658
|
+
import.meta.hot.accept(function(__larkNewModule) {
|
|
15659
|
+
var __larkNew = __larkNewModule && __larkNewModule.default;
|
|
15660
|
+
var __larkOld = import.meta.hot.data && import.meta.hot.data.oldTemplate;
|
|
15661
|
+
if (__larkOld && __larkNew && __larkOld !== __larkNew) {
|
|
15662
|
+
import("@lark.js/mvc").then(function(m) {
|
|
15663
|
+
if (m && m.hotSwapByTemplate) m.hotSwapByTemplate(__larkOld, __larkNew);
|
|
15664
|
+
});
|
|
15665
|
+
}
|
|
15666
|
+
});
|
|
15667
|
+
}
|
|
15668
|
+
`;
|
|
15669
|
+
}
|
|
15670
|
+
return `
|
|
15671
|
+
// \u2500\u2500 Lark template HMR (auto-injected by larkMvcPlugin \u2014 ${bundler}) \u2500\u2500
|
|
15672
|
+
if (typeof module !== "undefined" && module.hot) {
|
|
15673
|
+
module.hot.dispose(function(__larkData) {
|
|
15674
|
+
__larkData.oldTemplate = ${TEMPLATE_VAR};
|
|
15675
|
+
});
|
|
15676
|
+
module.hot.accept(function() {
|
|
15677
|
+
var __larkNew = ${TEMPLATE_VAR};
|
|
15678
|
+
var __larkOld = module.hot && module.hot.data && module.hot.data.oldTemplate;
|
|
15679
|
+
if (__larkOld && __larkNew && __larkOld !== __larkNew) {
|
|
15680
|
+
import("@lark.js/mvc").then(function(m) {
|
|
15681
|
+
if (m && m.hotSwapByTemplate) m.hotSwapByTemplate(__larkOld, __larkNew);
|
|
15682
|
+
});
|
|
15683
|
+
}
|
|
15684
|
+
});
|
|
15685
|
+
}
|
|
15686
|
+
`;
|
|
15687
|
+
}
|
|
15688
|
+
function injectTemplateHmrSnippet(source, bundler) {
|
|
15689
|
+
return source + "\n" + getTemplateHmrSnippet(bundler);
|
|
15690
|
+
}
|
|
15691
|
+
function getViewHmrSnippet(bundler) {
|
|
15692
|
+
const VIEW_VAR = "__larkViewDefault";
|
|
15693
|
+
if (bundler === "vite") {
|
|
15694
|
+
return `
|
|
15695
|
+
// \u2500\u2500 Lark view class HMR (auto-injected by larkMvcPlugin \u2014 Vite) \u2500\u2500
|
|
15696
|
+
if (import.meta.hot) {
|
|
15697
|
+
import.meta.hot.dispose(function(__larkData) {
|
|
15698
|
+
__larkData.oldClass = ${VIEW_VAR};
|
|
15699
|
+
});
|
|
15700
|
+
import.meta.hot.accept(function(__larkNewModule) {
|
|
15701
|
+
var __larkNew = __larkNewModule && __larkNewModule.default;
|
|
15702
|
+
var __larkOld = import.meta.hot.data && import.meta.hot.data.oldClass;
|
|
15703
|
+
if (__larkOld && __larkNew && __larkOld !== __larkNew) {
|
|
15704
|
+
import("@lark.js/mvc").then(function(m) {
|
|
15705
|
+
if (m && m.hotSwapByView) m.hotSwapByView(__larkOld, __larkNew);
|
|
15706
|
+
});
|
|
15707
|
+
}
|
|
15708
|
+
});
|
|
15709
|
+
}
|
|
15710
|
+
`;
|
|
15711
|
+
}
|
|
15712
|
+
return `
|
|
15713
|
+
// \u2500\u2500 Lark view class HMR (auto-injected by larkMvcPlugin \u2014 ${bundler}) \u2500\u2500
|
|
15714
|
+
if (typeof module !== "undefined" && module.hot) {
|
|
15715
|
+
module.hot.dispose(function(__larkData) {
|
|
15716
|
+
__larkData.oldClass = ${VIEW_VAR};
|
|
15717
|
+
});
|
|
15718
|
+
module.hot.accept(function() {
|
|
15719
|
+
var __larkNew = ${VIEW_VAR};
|
|
15720
|
+
var __larkOld = module.hot && module.hot.data && module.hot.data.oldClass;
|
|
15721
|
+
if (__larkOld && __larkNew && __larkOld !== __larkNew) {
|
|
15722
|
+
import("@lark.js/mvc").then(function(m) {
|
|
15723
|
+
if (m && m.hotSwapByView) m.hotSwapByView(__larkOld, __larkNew);
|
|
15724
|
+
});
|
|
15725
|
+
}
|
|
15726
|
+
});
|
|
15727
|
+
}
|
|
15728
|
+
`;
|
|
15729
|
+
}
|
|
15730
|
+
var HTML_IMPORT_RE = /import\s+(?:template\s+from\s+|.*from\s+)?["'][^"']+\.html["']/;
|
|
15731
|
+
function importsHtmlTemplate(source) {
|
|
15732
|
+
return HTML_IMPORT_RE.test(source);
|
|
15733
|
+
}
|
|
15734
|
+
function injectViewHmr(source, bundler) {
|
|
15735
|
+
if (!importsHtmlTemplate(source)) return source;
|
|
15736
|
+
const exportDefaultRe = /export\s+default\s+/;
|
|
15737
|
+
const match = exportDefaultRe.exec(source);
|
|
15738
|
+
if (!match) return source;
|
|
15739
|
+
const startIdx = match.index + match[0].length;
|
|
15740
|
+
const endIdx = findExpressionEnd(source, startIdx);
|
|
15741
|
+
if (endIdx === -1) return source;
|
|
15742
|
+
const expression = source.substring(startIdx, endIdx);
|
|
15743
|
+
const before = source.substring(0, match.index);
|
|
15744
|
+
const after = source.substring(endIdx);
|
|
15745
|
+
const VIEW_VAR = "__larkViewDefault";
|
|
15746
|
+
const transformed = before + `const ${VIEW_VAR} = ${expression};
|
|
15747
|
+
export default ${VIEW_VAR};` + after + "\n" + getViewHmrSnippet(bundler);
|
|
15748
|
+
return transformed;
|
|
15749
|
+
}
|
|
15750
|
+
function findExpressionEnd(source, startIdx) {
|
|
15751
|
+
let depth = 0;
|
|
15752
|
+
let inString = null;
|
|
15753
|
+
let inTemplate = false;
|
|
15754
|
+
let inLineComment = false;
|
|
15755
|
+
let inBlockComment = false;
|
|
15756
|
+
for (let i = startIdx; i < source.length; i++) {
|
|
15757
|
+
const ch = source[i];
|
|
15758
|
+
const next = source[i + 1];
|
|
15759
|
+
if (inLineComment) {
|
|
15760
|
+
if (ch === "\n") inLineComment = false;
|
|
15761
|
+
continue;
|
|
15762
|
+
}
|
|
15763
|
+
if (inBlockComment) {
|
|
15764
|
+
if (ch === "*" && next === "/") {
|
|
15765
|
+
inBlockComment = false;
|
|
15766
|
+
i++;
|
|
15767
|
+
}
|
|
15768
|
+
continue;
|
|
15769
|
+
}
|
|
15770
|
+
if (inString) {
|
|
15771
|
+
if (ch === "\\") {
|
|
15772
|
+
i++;
|
|
15773
|
+
continue;
|
|
15774
|
+
}
|
|
15775
|
+
if (ch === inString) inString = null;
|
|
15776
|
+
continue;
|
|
15777
|
+
}
|
|
15778
|
+
if (inTemplate) {
|
|
15779
|
+
if (ch === "\\") {
|
|
15780
|
+
i++;
|
|
15781
|
+
continue;
|
|
15782
|
+
}
|
|
15783
|
+
if (ch === "`") inTemplate = false;
|
|
15784
|
+
continue;
|
|
15785
|
+
}
|
|
15786
|
+
if (ch === "/" && next === "/") {
|
|
15787
|
+
inLineComment = true;
|
|
15788
|
+
i++;
|
|
15789
|
+
continue;
|
|
15790
|
+
}
|
|
15791
|
+
if (ch === "/" && next === "*") {
|
|
15792
|
+
inBlockComment = true;
|
|
15793
|
+
i++;
|
|
15794
|
+
continue;
|
|
15795
|
+
}
|
|
15796
|
+
if (ch === '"' || ch === "'") {
|
|
15797
|
+
inString = ch;
|
|
15798
|
+
continue;
|
|
15799
|
+
}
|
|
15800
|
+
if (ch === "`") {
|
|
15801
|
+
inTemplate = true;
|
|
15802
|
+
continue;
|
|
15803
|
+
}
|
|
15804
|
+
if (ch === "(" || ch === "{" || ch === "[") {
|
|
15805
|
+
depth++;
|
|
15806
|
+
continue;
|
|
15807
|
+
}
|
|
15808
|
+
if (ch === ")" || ch === "}" || ch === "]") {
|
|
15809
|
+
depth--;
|
|
15810
|
+
if (depth === 0) {
|
|
15811
|
+
return i + 1;
|
|
15812
|
+
}
|
|
15813
|
+
continue;
|
|
15814
|
+
}
|
|
15815
|
+
if (depth === 0 && ch === ";") {
|
|
15816
|
+
return i;
|
|
15817
|
+
}
|
|
15818
|
+
}
|
|
15819
|
+
return -1;
|
|
15600
15820
|
}
|
|
15601
15821
|
|
|
15602
15822
|
// src/vite.ts
|
|
@@ -15645,9 +15865,30 @@ function larkMvcPlugin(options = {}) {
|
|
|
15645
15865
|
if (!(0, import_fs.existsSync)(filePath)) return void 0;
|
|
15646
15866
|
const raw = (0, import_fs.readFileSync)(filePath, "utf-8");
|
|
15647
15867
|
const globalVars = await extractGlobalVars(raw);
|
|
15648
|
-
|
|
15868
|
+
const compiled = await compileTemplate(raw, {
|
|
15869
|
+
debug,
|
|
15870
|
+
globalVars,
|
|
15871
|
+
virtualDom
|
|
15872
|
+
});
|
|
15873
|
+
return { code: injectTemplateHmrSnippet(compiled, "vite"), map: null };
|
|
15649
15874
|
}
|
|
15650
15875
|
return void 0;
|
|
15876
|
+
},
|
|
15877
|
+
/**
|
|
15878
|
+
* Transform hook: inject view-class HMR into .ts files that import .html.
|
|
15879
|
+
*
|
|
15880
|
+
* When a .ts view file changes, the auto-injected HMR snippet captures
|
|
15881
|
+
* the old View setup function (via dispose) and the new one (via accept),
|
|
15882
|
+
* then calls `hotSwapByView(old, new)` to hot-swap all mounted views
|
|
15883
|
+
* — preserving state.
|
|
15884
|
+
*/
|
|
15885
|
+
transform(code, id) {
|
|
15886
|
+
if (!/\.[tj]s$/.test(id)) return void 0;
|
|
15887
|
+
if (id.includes("node_modules")) return void 0;
|
|
15888
|
+
if (!importsHtmlTemplate(code)) return void 0;
|
|
15889
|
+
const transformed = injectViewHmr(code, "vite");
|
|
15890
|
+
if (transformed === code) return void 0;
|
|
15891
|
+
return { code: transformed, map: null };
|
|
15651
15892
|
}
|
|
15652
15893
|
};
|
|
15653
15894
|
}
|
|
@@ -15668,7 +15909,12 @@ function larkMvcPluginLegacy(options = {}) {
|
|
|
15668
15909
|
const filePath = id.slice(0, -LARK_TEMPLATE_SUFFIX.length);
|
|
15669
15910
|
const raw = (0, import_fs.readFileSync)(filePath, "utf-8");
|
|
15670
15911
|
const globalVars = await extractGlobalVars(raw);
|
|
15671
|
-
|
|
15912
|
+
const compiled = await compileTemplate(raw, {
|
|
15913
|
+
debug,
|
|
15914
|
+
globalVars,
|
|
15915
|
+
virtualDom
|
|
15916
|
+
});
|
|
15917
|
+
return { code: compiled, map: null };
|
|
15672
15918
|
}
|
|
15673
15919
|
return void 0;
|
|
15674
15920
|
}
|