@mpcore/simulator 0.3.0 → 0.3.2

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/index.d.mts CHANGED
@@ -1268,6 +1268,18 @@ declare function createComponentInstance(options: CreateComponentInstanceOptions
1268
1268
  declare function runComponentLifecycle(instance: HeadlessComponentInstance, lifecycleName: 'attached' | 'created' | 'detached' | 'ready'): void;
1269
1269
  declare function runComponentPageLifetime(instance: HeadlessComponentInstance, lifetimeName: 'hide' | 'resize' | 'show', payload?: unknown): void;
1270
1270
  //#endregion
1271
+ //#region src/view/templateRuntime.d.ts
1272
+ interface TemplateNodeLike {
1273
+ attribs?: Record<string, string>;
1274
+ children?: TemplateNodeLike[];
1275
+ name?: string;
1276
+ type?: string;
1277
+ }
1278
+ interface TemplateRenderState<T extends TemplateNodeLike> {
1279
+ definitions: Map<string, T>;
1280
+ stack: string[];
1281
+ }
1282
+ //#endregion
1271
1283
  //#region src/browser/render/types.d.ts
1272
1284
  interface DomNodeLike$5 {
1273
1285
  attribs?: Record<string, string>;
@@ -1301,6 +1313,7 @@ interface BrowserSlotContent {
1301
1313
  ownerFilePath: string;
1302
1314
  ownerJsonPath: string;
1303
1315
  scope: BrowserRenderScope;
1316
+ templateRenderState: TemplateRenderState<DomNodeLike$5>;
1304
1317
  }
1305
1318
  interface BrowserRenderedPageTree {
1306
1319
  root: DomNodeLike$5;
@@ -1386,6 +1399,7 @@ interface RuntimeSlotContent {
1386
1399
  ownerFilePath: string;
1387
1400
  ownerJsonPath: string;
1388
1401
  scope: RuntimeRenderScope;
1402
+ templateRenderState: TemplateRenderState<DomNodeLike$4>;
1389
1403
  }
1390
1404
  interface RuntimeRenderedPageTree {
1391
1405
  root: DomNodeLike$4;
package/dist/index.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  import { dirname, join, normalize, posix } from "pathe";
2
2
  import fs from "node:fs";
3
3
  import path from "node:path";
4
+ import { parseExpression } from "@babel/parser";
4
5
  import { WEVU_NATIVE_INSTANCE_KEY } from "@weapp-core/constants";
5
6
  import { parseDocument } from "htmlparser2";
6
- import { parseExpression } from "@babel/parser";
7
7
  import process from "node:process";
8
8
  import vm from "node:vm";
9
9
  //#region src/host/globals.ts
@@ -1241,11 +1241,15 @@ function resolveInitialProperties(definition, properties) {
1241
1241
  //#region src/runtime/componentInstance/index.ts
1242
1242
  function createComponentInstance(options) {
1243
1243
  const definition = normalizeComponentDefinition(options.definition);
1244
+ const properties = resolveInitialProperties(definition, options.properties ?? {});
1244
1245
  const instance = {
1245
1246
  __definition__: definition,
1246
- data: resolveInitialData$2(definition),
1247
+ data: {
1248
+ ...resolveInitialData$2(definition),
1249
+ ...properties
1250
+ },
1247
1251
  __propertySnapshots__: {},
1248
- properties: resolveInitialProperties(definition, options.properties ?? {}),
1252
+ properties,
1249
1253
  setData(patch, callback) {
1250
1254
  const changedKeys = Object.keys(patch);
1251
1255
  for (const [key, value] of Object.entries(patch)) assignByPath$1(instance.data, key, value);
@@ -1280,119 +1284,6 @@ function runComponentPageLifetime(instance, lifetimeName, payload) {
1280
1284
  handler.call(instance, payload);
1281
1285
  }
1282
1286
  //#endregion
1283
- //#region src/view/eventBinding.ts
1284
- const LEGACY_EVENT_NAME_RE = /^\w+$/;
1285
- const EVENT_BINDING_PREFIXES = [
1286
- {
1287
- prefix: "bind:",
1288
- priority: 0,
1289
- stopAfter: false
1290
- },
1291
- {
1292
- prefix: "catch:",
1293
- priority: 0,
1294
- stopAfter: true
1295
- },
1296
- {
1297
- prefix: "bind",
1298
- priority: 1,
1299
- stopAfter: false
1300
- },
1301
- {
1302
- prefix: "catch",
1303
- priority: 1,
1304
- stopAfter: true
1305
- }
1306
- ];
1307
- function parseEventBinding(attributeName, method) {
1308
- const definition = EVENT_BINDING_PREFIXES.find((item) => attributeName.startsWith(item.prefix));
1309
- if (!definition) return null;
1310
- const eventName = attributeName.slice(definition.prefix.length);
1311
- if (!eventName || !definition.prefix.endsWith(":") && !LEGACY_EVENT_NAME_RE.test(eventName)) return null;
1312
- return {
1313
- eventName,
1314
- method,
1315
- priority: definition.priority,
1316
- stopAfter: definition.stopAfter
1317
- };
1318
- }
1319
- function collectMiniProgramEventBindings(attributes = {}) {
1320
- const bindings = /* @__PURE__ */ new Map();
1321
- const priorities = /* @__PURE__ */ new Map();
1322
- for (const [attributeName, method] of Object.entries(attributes)) {
1323
- const candidate = parseEventBinding(attributeName, method);
1324
- if (!candidate || candidate.priority < (priorities.get(candidate.eventName) ?? -1)) continue;
1325
- bindings.set(candidate.eventName, {
1326
- method: candidate.method,
1327
- stopAfter: candidate.stopAfter
1328
- });
1329
- priorities.set(candidate.eventName, candidate.priority);
1330
- }
1331
- return bindings;
1332
- }
1333
- function resolveMiniProgramEventBinding(attributes, eventName) {
1334
- return collectMiniProgramEventBindings(attributes).get(eventName) ?? null;
1335
- }
1336
- //#endregion
1337
- //#region src/view/selectorQueryScope.ts
1338
- const MPCORE_COMPONENT_SCOPE_ID = "__mpcoreComponentScopeId";
1339
- function isScopeCandidate(value) {
1340
- return typeof value === "object" && value !== null || typeof value === "function";
1341
- }
1342
- function resolveScopeCandidates(scope) {
1343
- const candidates = [];
1344
- const visited = /* @__PURE__ */ new Set();
1345
- const queue = [scope];
1346
- while (queue.length > 0) {
1347
- const candidate = queue.shift();
1348
- if (!isScopeCandidate(candidate) || visited.has(candidate)) continue;
1349
- visited.add(candidate);
1350
- candidates.push(candidate);
1351
- queue.push(candidate.$, candidate.$el, candidate.$state, candidate[WEVU_NATIVE_INSTANCE_KEY]);
1352
- }
1353
- return candidates;
1354
- }
1355
- function resolveSelectorQueryNativeScope(scope, page, components) {
1356
- if (!scope) return;
1357
- const nativeScopes = [page, ...components];
1358
- const nativeScopeSet = new Set(nativeScopes);
1359
- for (const candidate of resolveScopeCandidates(scope)) {
1360
- if (nativeScopeSet.has(candidate)) return candidate;
1361
- const scopeId = candidate[MPCORE_COMPONENT_SCOPE_ID];
1362
- if (typeof scopeId !== "string" || !scopeId) continue;
1363
- const currentScope = nativeScopes.find((nativeScope) => nativeScope[MPCORE_COMPONENT_SCOPE_ID] === scopeId);
1364
- if (currentScope) return currentScope;
1365
- }
1366
- return null;
1367
- }
1368
- function setSelectorQueryScopeId(scope, scopeId) {
1369
- Object.defineProperty(scope, MPCORE_COMPONENT_SCOPE_ID, {
1370
- configurable: true,
1371
- enumerable: false,
1372
- value: scopeId,
1373
- writable: false
1374
- });
1375
- }
1376
- function resolveSelectorQueryScopeId(scope) {
1377
- if (!scope) return null;
1378
- for (const candidate of resolveScopeCandidates(scope)) {
1379
- const scopeId = candidate[MPCORE_COMPONENT_SCOPE_ID];
1380
- if (typeof scopeId === "string" && scopeId) return scopeId;
1381
- }
1382
- return null;
1383
- }
1384
- function resolveSelectorQueryScopeSnapshot(scope, snapshots) {
1385
- if (!scope) return null;
1386
- for (const candidate of resolveScopeCandidates(scope)) {
1387
- const snapshot = snapshots.get(candidate);
1388
- if (snapshot) return {
1389
- nativeScope: candidate,
1390
- snapshot
1391
- };
1392
- }
1393
- return null;
1394
- }
1395
- //#endregion
1396
1287
  //#region src/view/templateExpression.ts
1397
1288
  const expressionCache = /* @__PURE__ */ new Map();
1398
1289
  const BLOCKED_MEMBER_KEYS = /* @__PURE__ */ new Set([
@@ -1400,7 +1291,7 @@ const BLOCKED_MEMBER_KEYS = /* @__PURE__ */ new Set([
1400
1291
  "constructor",
1401
1292
  "prototype"
1402
1293
  ]);
1403
- const NUMERIC_DOT_PATH_RE = /\.(\d+)(?=[.[\s]|$)/g;
1294
+ const NUMERIC_DOT_PATH_RE = /\.(\d+)(?=[.[\s,}\]]|$)/g;
1404
1295
  function isMustacheOnly$2(value) {
1405
1296
  const trimmed = value.trim();
1406
1297
  return trimmed.startsWith("{{") && trimmed.endsWith("}}") && !trimmed.includes("{{", 2);
@@ -1532,6 +1423,160 @@ function resolveTemplateExpression(source, expression) {
1532
1423
  return evaluateExpressionNode(parseTemplateExpression(normalized), source);
1533
1424
  }
1534
1425
  //#endregion
1426
+ //#region src/view/templateRuntime.ts
1427
+ function isTagNode$2(node) {
1428
+ return node.type === "tag" && typeof node.name === "string";
1429
+ }
1430
+ function readMustacheExpression(value) {
1431
+ const trimmed = value.trim();
1432
+ return trimmed.startsWith("{{") && trimmed.endsWith("}}") ? trimmed.slice(2, -2).trim() : trimmed;
1433
+ }
1434
+ function createTemplateRenderState(root) {
1435
+ const definitions = /* @__PURE__ */ new Map();
1436
+ const visit = (node) => {
1437
+ if (isTagNode$2(node) && node.name === "template") {
1438
+ const name = node.attribs?.name?.trim();
1439
+ if (name) definitions.set(name, node);
1440
+ }
1441
+ for (const child of node.children ?? []) visit(child);
1442
+ };
1443
+ visit(root);
1444
+ return {
1445
+ definitions,
1446
+ stack: []
1447
+ };
1448
+ }
1449
+ function isTemplateDefinition(node) {
1450
+ return node.name === "template" && Boolean(node.attribs?.name?.trim());
1451
+ }
1452
+ function resolveTemplateCall(node, source) {
1453
+ if (node.name !== "template" || isTemplateDefinition(node)) return;
1454
+ const isValue = node.attribs?.is?.trim();
1455
+ if (!isValue) return;
1456
+ const name = isValue.includes("{{") ? resolveTemplateExpression(source, readMustacheExpression(isValue)) : isValue;
1457
+ return typeof name === "string" && name ? name : void 0;
1458
+ }
1459
+ function resolveTemplateData(node, source) {
1460
+ const dataValue = node.attribs?.data?.trim();
1461
+ if (!dataValue) return {};
1462
+ const expression = readMustacheExpression(dataValue);
1463
+ const resolved = resolveTemplateExpression(source, expression) ?? resolveTemplateExpression(source, `{${expression}}`);
1464
+ return resolved && typeof resolved === "object" ? resolved : {};
1465
+ }
1466
+ //#endregion
1467
+ //#region src/view/eventBinding.ts
1468
+ const LEGACY_EVENT_NAME_RE = /^\w+$/;
1469
+ const EVENT_BINDING_PREFIXES = [
1470
+ {
1471
+ prefix: "bind:",
1472
+ priority: 0,
1473
+ stopAfter: false
1474
+ },
1475
+ {
1476
+ prefix: "catch:",
1477
+ priority: 0,
1478
+ stopAfter: true
1479
+ },
1480
+ {
1481
+ prefix: "bind",
1482
+ priority: 1,
1483
+ stopAfter: false
1484
+ },
1485
+ {
1486
+ prefix: "catch",
1487
+ priority: 1,
1488
+ stopAfter: true
1489
+ }
1490
+ ];
1491
+ function parseEventBinding(attributeName, method) {
1492
+ const definition = EVENT_BINDING_PREFIXES.find((item) => attributeName.startsWith(item.prefix));
1493
+ if (!definition) return null;
1494
+ const eventName = attributeName.slice(definition.prefix.length);
1495
+ if (!eventName || !definition.prefix.endsWith(":") && !LEGACY_EVENT_NAME_RE.test(eventName)) return null;
1496
+ return {
1497
+ eventName,
1498
+ method,
1499
+ priority: definition.priority,
1500
+ stopAfter: definition.stopAfter
1501
+ };
1502
+ }
1503
+ function collectMiniProgramEventBindings(attributes = {}) {
1504
+ const bindings = /* @__PURE__ */ new Map();
1505
+ const priorities = /* @__PURE__ */ new Map();
1506
+ for (const [attributeName, method] of Object.entries(attributes)) {
1507
+ const candidate = parseEventBinding(attributeName, method);
1508
+ if (!candidate || candidate.priority < (priorities.get(candidate.eventName) ?? -1)) continue;
1509
+ bindings.set(candidate.eventName, {
1510
+ method: candidate.method,
1511
+ stopAfter: candidate.stopAfter
1512
+ });
1513
+ priorities.set(candidate.eventName, candidate.priority);
1514
+ }
1515
+ return bindings;
1516
+ }
1517
+ function resolveMiniProgramEventBinding(attributes, eventName) {
1518
+ return collectMiniProgramEventBindings(attributes).get(eventName) ?? null;
1519
+ }
1520
+ //#endregion
1521
+ //#region src/view/selectorQueryScope.ts
1522
+ const MPCORE_COMPONENT_SCOPE_ID = "__mpcoreComponentScopeId";
1523
+ function isScopeCandidate(value) {
1524
+ return typeof value === "object" && value !== null || typeof value === "function";
1525
+ }
1526
+ function resolveScopeCandidates(scope) {
1527
+ const candidates = [];
1528
+ const visited = /* @__PURE__ */ new Set();
1529
+ const queue = [scope];
1530
+ while (queue.length > 0) {
1531
+ const candidate = queue.shift();
1532
+ if (!isScopeCandidate(candidate) || visited.has(candidate)) continue;
1533
+ visited.add(candidate);
1534
+ candidates.push(candidate);
1535
+ queue.push(candidate.$, candidate.$el, candidate.$state, candidate[WEVU_NATIVE_INSTANCE_KEY]);
1536
+ }
1537
+ return candidates;
1538
+ }
1539
+ function resolveSelectorQueryNativeScope(scope, page, components) {
1540
+ if (!scope) return;
1541
+ const nativeScopes = [page, ...components];
1542
+ const nativeScopeSet = new Set(nativeScopes);
1543
+ for (const candidate of resolveScopeCandidates(scope)) {
1544
+ if (nativeScopeSet.has(candidate)) return candidate;
1545
+ const scopeId = candidate[MPCORE_COMPONENT_SCOPE_ID];
1546
+ if (typeof scopeId !== "string" || !scopeId) continue;
1547
+ const currentScope = nativeScopes.find((nativeScope) => nativeScope[MPCORE_COMPONENT_SCOPE_ID] === scopeId);
1548
+ if (currentScope) return currentScope;
1549
+ }
1550
+ return null;
1551
+ }
1552
+ function setSelectorQueryScopeId(scope, scopeId) {
1553
+ Object.defineProperty(scope, MPCORE_COMPONENT_SCOPE_ID, {
1554
+ configurable: true,
1555
+ enumerable: false,
1556
+ value: scopeId,
1557
+ writable: false
1558
+ });
1559
+ }
1560
+ function resolveSelectorQueryScopeId(scope) {
1561
+ if (!scope) return null;
1562
+ for (const candidate of resolveScopeCandidates(scope)) {
1563
+ const scopeId = candidate[MPCORE_COMPONENT_SCOPE_ID];
1564
+ if (typeof scopeId === "string" && scopeId) return scopeId;
1565
+ }
1566
+ return null;
1567
+ }
1568
+ function resolveSelectorQueryScopeSnapshot(scope, snapshots) {
1569
+ if (!scope) return null;
1570
+ for (const candidate of resolveScopeCandidates(scope)) {
1571
+ const snapshot = snapshots.get(candidate);
1572
+ if (snapshot) return {
1573
+ nativeScope: candidate,
1574
+ snapshot
1575
+ };
1576
+ }
1577
+ return null;
1578
+ }
1579
+ //#endregion
1535
1580
  //#region src/browser/render/shared.ts
1536
1581
  const TEMPLATE_INTERPOLATION_RE$2 = /\{\{([^{}]+)\}\}/g;
1537
1582
  const DATASET_NAME_RE$4 = /-([a-z])/g;
@@ -1783,6 +1828,7 @@ function syncComponentProperties$1(instance, definition, nextProperties, binding
1783
1828
  if (hasComponentPropertyValueChanged(instance.properties[key], previousSnapshot, nextValue, bindingAffected)) {
1784
1829
  previousProperties[key] = instance.properties[key];
1785
1830
  instance.properties[key] = nextValue;
1831
+ instance.data[key] = nextValue;
1786
1832
  changedRootKeys.push(key);
1787
1833
  }
1788
1834
  instance.__propertySnapshots ??= {};
@@ -1852,7 +1898,9 @@ function createBrowserComponentInstance(componentScopeId, context, clonedNode, c
1852
1898
  }
1853
1899
  function renderBrowserComponentTemplate(context, componentEntry, renderNodeTree, componentScope, componentScopeId, seenComponentScopes) {
1854
1900
  const componentDocument = parseTemplateDocument$1(readTemplateSource$1(context.files, componentEntry.templatePath));
1855
- return renderNodeTree((componentDocument.children ?? [])[0] ?? componentDocument, componentScope, context, `${componentEntry.filePath.replace(JS_FILE_RE$1, "")}.json`, componentEntry.filePath, componentScopeId, seenComponentScopes);
1901
+ const componentRoot = (componentDocument.children ?? [])[0] ?? componentDocument;
1902
+ const templateRenderState = createTemplateRenderState(componentRoot);
1903
+ return renderNodeTree(componentRoot, componentScope, context, `${componentEntry.filePath.replace(JS_FILE_RE$1, "")}.json`, componentEntry.filePath, componentScopeId, seenComponentScopes, templateRenderState);
1856
1904
  }
1857
1905
  //#endregion
1858
1906
  //#region src/browser/render/index.ts
@@ -1873,17 +1921,18 @@ function expandNodeByFor$1(node, scope) {
1873
1921
  instanceSuffix: `:for-${index}`
1874
1922
  }));
1875
1923
  }
1876
- function renderNodeVariants$1(node, scope, context, ownerJsonPath, ownerFilePath, instancePath, seenComponentScopes) {
1877
- return expandNodeByFor$1(node, scope).map(({ node: expandedNode, scope: expandedScope, instanceSuffix }) => renderNodeTree$1(expandedNode, expandedScope, context, ownerJsonPath, ownerFilePath, `${instancePath}${instanceSuffix}`, seenComponentScopes));
1924
+ function renderNodeVariants$1(node, scope, context, ownerJsonPath, ownerFilePath, instancePath, seenComponentScopes, templateRenderState) {
1925
+ return expandNodeByFor$1(node, scope).map(({ node: expandedNode, scope: expandedScope, instanceSuffix }) => renderNodeTree$1(expandedNode, expandedScope, context, ownerJsonPath, ownerFilePath, `${instancePath}${instanceSuffix}`, seenComponentScopes, templateRenderState));
1878
1926
  }
1879
- function renderChildren$1(children, scope, context, ownerJsonPath, ownerFilePath, instancePath, seenComponentScopes) {
1927
+ function renderChildren$1(children, scope, context, ownerJsonPath, ownerFilePath, instancePath, seenComponentScopes, templateRenderState) {
1880
1928
  const renderedChildren = [];
1881
1929
  for (let index = 0; index < children.length; index += 1) {
1882
1930
  const child = children[index];
1883
1931
  if (!isTagNode$1(child)) {
1884
- renderedChildren.push(...renderNodeVariants$1(child, scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/node-${index}`, seenComponentScopes));
1932
+ renderedChildren.push(...renderNodeVariants$1(child, scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/node-${index}`, seenComponentScopes, templateRenderState));
1885
1933
  continue;
1886
1934
  }
1935
+ if (isTemplateDefinition(child)) continue;
1887
1936
  const hasConditional = child.attribs?.["wx:if"] != null;
1888
1937
  const isElseBranch = child.attribs?.["wx:elif"] != null || child.attribs?.["wx:else"] != null;
1889
1938
  if (hasConditional) {
@@ -1898,7 +1947,7 @@ function renderChildren$1(children, scope, context, ownerJsonPath, ownerFilePath
1898
1947
  if (!isTagNode$1(branch)) break;
1899
1948
  if (cursor !== index && !WX_ELSE_ATTRS$1.has(Object.keys(branch.attribs ?? {}).find((key) => key.startsWith("wx:")) ?? "")) break;
1900
1949
  if (branch.attribs?.["wx:else"] != null ? !branchMatched : !branchMatched && evaluateConditionalBranch$1(branch, scope)) {
1901
- renderedChildren.push(...renderNodeVariants$1(branch, scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/node-${cursor}`, seenComponentScopes));
1950
+ renderedChildren.push(...renderNodeVariants$1(branch, scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/node-${cursor}`, seenComponentScopes, templateRenderState));
1902
1951
  branchMatched = true;
1903
1952
  }
1904
1953
  let nextBranchIndex = cursor + 1;
@@ -1911,11 +1960,11 @@ function renderChildren$1(children, scope, context, ownerJsonPath, ownerFilePath
1911
1960
  continue;
1912
1961
  }
1913
1962
  if (isElseBranch) continue;
1914
- renderedChildren.push(...renderNodeVariants$1(child, scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/node-${index}`, seenComponentScopes));
1963
+ renderedChildren.push(...renderNodeVariants$1(child, scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/node-${index}`, seenComponentScopes, templateRenderState));
1915
1964
  }
1916
1965
  return renderedChildren;
1917
1966
  }
1918
- function collectComponentSlots$1(componentNode, scope, ownerJsonPath, ownerFilePath, instancePath) {
1967
+ function collectComponentSlots$1(componentNode, scope, ownerJsonPath, ownerFilePath, instancePath, templateRenderState) {
1919
1968
  const slots = /* @__PURE__ */ new Map();
1920
1969
  (componentNode.children ?? []).forEach((node, index) => {
1921
1970
  const slotName = isTagNode$1(node) && typeof node.attribs?.slot === "string" && node.attribs.slot.trim() ? node.attribs.slot.trim() : "default";
@@ -1925,7 +1974,8 @@ function collectComponentSlots$1(componentNode, scope, ownerJsonPath, ownerFileP
1925
1974
  node,
1926
1975
  ownerFilePath,
1927
1976
  ownerJsonPath,
1928
- scope
1977
+ scope,
1978
+ templateRenderState
1929
1979
  });
1930
1980
  slots.set(slotName, entries);
1931
1981
  });
@@ -1942,16 +1992,43 @@ function collectComponentSlots$1(componentNode, scope, ownerJsonPath, ownerFileP
1942
1992
  }
1943
1993
  return slots;
1944
1994
  }
1945
- function renderNodeTree$1(node, scope, context, ownerJsonPath, ownerFilePath, instancePath, seenComponentScopes) {
1995
+ function renderNodeTree$1(node, scope, context, ownerJsonPath, ownerFilePath, instancePath, seenComponentScopes, templateRenderState) {
1946
1996
  const clonedNode = cloneNode$1(node);
1947
1997
  if (!isTagNode$1(clonedNode)) {
1948
1998
  applyNodeBindings$1(clonedNode, scope);
1949
1999
  return clonedNode;
1950
2000
  }
2001
+ if (isTemplateDefinition(clonedNode)) return {
2002
+ type: "tag",
2003
+ name: "block",
2004
+ attribs: {},
2005
+ children: []
2006
+ };
2007
+ const templateName = resolveTemplateCall(clonedNode, scope.data);
2008
+ if (templateName) {
2009
+ const definition = templateRenderState.definitions.get(templateName);
2010
+ const templateScope = {
2011
+ ...scope,
2012
+ data: resolveTemplateData(clonedNode, scope.data)
2013
+ };
2014
+ const children = definition && !templateRenderState.stack.includes(templateName) ? renderChildren$1(definition.children ?? [], templateScope, context, ownerJsonPath, ownerFilePath, `${instancePath}/template-${templateName}`, seenComponentScopes, {
2015
+ definitions: templateRenderState.definitions,
2016
+ stack: [...templateRenderState.stack, templateName]
2017
+ }) : [];
2018
+ return {
2019
+ type: "tag",
2020
+ name: "block",
2021
+ attribs: {
2022
+ "data-sim-node": instancePath,
2023
+ "data-sim-scope": scope.getScopeId()
2024
+ },
2025
+ children
2026
+ };
2027
+ }
1951
2028
  if (clonedNode.name === "slot") {
1952
2029
  const slotName = clonedNode.attribs?.name?.trim() || "default";
1953
2030
  const projected = scope.slots?.get(slotName) ?? [];
1954
- const children = projected.length ? projected.flatMap((entry) => renderNodeVariants$1(entry.node, entry.scope, context, entry.ownerJsonPath, entry.ownerFilePath, entry.instancePath, seenComponentScopes)) : renderChildren$1(clonedNode.children ?? [], scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/slot-fallback-${slotName}`, seenComponentScopes);
2031
+ const children = projected.length ? projected.flatMap((entry) => renderNodeVariants$1(entry.node, entry.scope, context, entry.ownerJsonPath, entry.ownerFilePath, entry.instancePath, seenComponentScopes, entry.templateRenderState)) : renderChildren$1(clonedNode.children ?? [], scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/slot-fallback-${slotName}`, seenComponentScopes, templateRenderState);
1955
2032
  return {
1956
2033
  type: "tag",
1957
2034
  name: "block",
@@ -1969,7 +2046,7 @@ function renderNodeTree$1(node, scope, context, ownerJsonPath, ownerFilePath, in
1969
2046
  else syncComponentProperties$1(componentInstance, componentInstance.__definition__ ?? componentEntry.definition, nextProperties, bindingExpressions, context.changedPageKeys);
1970
2047
  seenComponentScopes.add(componentScopeId);
1971
2048
  const genericComponents = resolveComponentGenerics$1(context, clonedNode, ownerJsonPath, ownerFilePath, componentEntry.filePath);
1972
- const slots = collectComponentSlots$1(clonedNode, scope, ownerJsonPath, ownerFilePath, componentScopeId);
2049
+ const slots = collectComponentSlots$1(clonedNode, scope, ownerJsonPath, ownerFilePath, componentScopeId, templateRenderState);
1973
2050
  const componentScope = createComponentScope$1(clonedNode, scope, componentScopeId, componentInstance, genericComponents, slots);
1974
2051
  context.componentScopes.set(componentScopeId, componentScope);
1975
2052
  const renderedComponentRoot = renderBrowserComponentTemplate(context, componentEntry, renderNodeTree$1, componentScope, componentScopeId, seenComponentScopes);
@@ -1984,7 +2061,7 @@ function renderNodeTree$1(node, scope, context, ownerJsonPath, ownerFilePath, in
1984
2061
  return renderedComponentRoot;
1985
2062
  }
1986
2063
  applyNodeBindings$1(clonedNode, scope);
1987
- clonedNode.children = renderChildren$1(clonedNode.children ?? [], scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/${clonedNode.name}`, seenComponentScopes);
2064
+ clonedNode.children = renderChildren$1(clonedNode.children ?? [], scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/${clonedNode.name}`, seenComponentScopes, templateRenderState);
1988
2065
  return clonedNode;
1989
2066
  }
1990
2067
  function renderBrowserPageTree(context, page) {
@@ -2004,7 +2081,9 @@ function renderBrowserPageTree(context, page) {
2004
2081
  context.componentScopes.clear();
2005
2082
  context.componentScopes.set(pageScopeId, pageScope);
2006
2083
  const seenComponentScopes = /* @__PURE__ */ new Set();
2007
- const renderedRoot = renderNodeTree$1((document.children ?? [])[0] ?? document, pageScope, context, `${route}.json`, `${route}.js`, pageScopeId, seenComponentScopes);
2084
+ const root = (document.children ?? [])[0] ?? document;
2085
+ const templateRenderState = createTemplateRenderState(root);
2086
+ const renderedRoot = renderNodeTree$1(root, pageScope, context, `${route}.json`, `${route}.js`, pageScopeId, seenComponentScopes, templateRenderState);
2008
2087
  for (const [scopeId, instance] of [...context.componentCache.entries()]) if (!seenComponentScopes.has(scopeId)) {
2009
2088
  runComponentLifecycle(instance, "detached");
2010
2089
  context.componentCache.delete(scopeId);
@@ -2300,7 +2379,7 @@ function deriveMenuButtonBoundingClientRect(systemInfo) {
2300
2379
  }
2301
2380
  //#endregion
2302
2381
  //#region src/runtime/wxState.ts
2303
- const STORAGE_LIMIT_SIZE = 10 * 1024;
2382
+ const STORAGE_LIMIT_SIZE = 10240;
2304
2383
  const DEFAULT_MODAL_CANCEL_COLOR = "#000000";
2305
2384
  const DEFAULT_MODAL_CANCEL_TEXT = "取消";
2306
2385
  const DEFAULT_MODAL_CONFIRM_COLOR = "#576B95";
@@ -6443,6 +6522,7 @@ function syncComponentProperties(instance, definition, nextProperties, bindingEx
6443
6522
  if (hasComponentPropertyValueChanged(instance.properties[key], previousSnapshot, nextValue, bindingAffected)) {
6444
6523
  previousProperties[key] = instance.properties[key];
6445
6524
  instance.properties[key] = nextValue;
6525
+ instance.data[key] = nextValue;
6446
6526
  changedRootKeys.push(key);
6447
6527
  }
6448
6528
  instance.__propertySnapshots ??= {};
@@ -6511,7 +6591,9 @@ function createRuntimeComponentInstance(componentScopeId, context, clonedNode, c
6511
6591
  }
6512
6592
  function renderRuntimeComponentTemplate(context, componentEntry, renderNodeTree, componentScope, componentScopeId, seenComponentScopes) {
6513
6593
  const componentDocument = parseTemplateDocument(readTemplateSource(context.artifactSource, componentEntry.absoluteTemplatePath));
6514
- return renderNodeTree((componentDocument.children ?? [])[0] ?? componentDocument, componentScope, context, path.resolve(context.project.miniprogramRootPath, `${componentEntry.filePath.replace(JS_FILE_RE, "")}.json`), componentEntry.filePath, componentScopeId, seenComponentScopes);
6594
+ const componentRoot = (componentDocument.children ?? [])[0] ?? componentDocument;
6595
+ const templateRenderState = createTemplateRenderState(componentRoot);
6596
+ return renderNodeTree(componentRoot, componentScope, context, path.resolve(context.project.miniprogramRootPath, `${componentEntry.filePath.replace(JS_FILE_RE, "")}.json`), componentEntry.filePath, componentScopeId, seenComponentScopes, templateRenderState);
6515
6597
  }
6516
6598
  //#endregion
6517
6599
  //#region src/runtime/render/index.ts
@@ -6532,17 +6614,18 @@ function expandNodeByFor(node, scope) {
6532
6614
  instanceSuffix: `:for-${index}`
6533
6615
  }));
6534
6616
  }
6535
- function renderNodeVariants(node, scope, context, ownerJsonPath, ownerFilePath, instancePath, seenComponentScopes) {
6536
- return expandNodeByFor(node, scope).map(({ node: expandedNode, scope: expandedScope, instanceSuffix }) => renderNodeTree(expandedNode, expandedScope, context, ownerJsonPath, ownerFilePath, `${instancePath}${instanceSuffix}`, seenComponentScopes));
6617
+ function renderNodeVariants(node, scope, context, ownerJsonPath, ownerFilePath, instancePath, seenComponentScopes, templateRenderState) {
6618
+ return expandNodeByFor(node, scope).map(({ node: expandedNode, scope: expandedScope, instanceSuffix }) => renderNodeTree(expandedNode, expandedScope, context, ownerJsonPath, ownerFilePath, `${instancePath}${instanceSuffix}`, seenComponentScopes, templateRenderState));
6537
6619
  }
6538
- function renderChildren(children, scope, context, ownerJsonPath, ownerFilePath, instancePath, seenComponentScopes) {
6620
+ function renderChildren(children, scope, context, ownerJsonPath, ownerFilePath, instancePath, seenComponentScopes, templateRenderState) {
6539
6621
  const renderedChildren = [];
6540
6622
  for (let index = 0; index < children.length; index += 1) {
6541
6623
  const child = children[index];
6542
6624
  if (!isTagNode(child)) {
6543
- renderedChildren.push(...renderNodeVariants(child, scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/node-${index}`, seenComponentScopes));
6625
+ renderedChildren.push(...renderNodeVariants(child, scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/node-${index}`, seenComponentScopes, templateRenderState));
6544
6626
  continue;
6545
6627
  }
6628
+ if (isTemplateDefinition(child)) continue;
6546
6629
  const hasConditional = child.attribs?.["wx:if"] != null;
6547
6630
  const isElseBranch = child.attribs?.["wx:elif"] != null || child.attribs?.["wx:else"] != null;
6548
6631
  if (hasConditional) {
@@ -6557,7 +6640,7 @@ function renderChildren(children, scope, context, ownerJsonPath, ownerFilePath,
6557
6640
  if (!isTagNode(branch)) break;
6558
6641
  if (cursor !== index && !WX_ELSE_ATTRS.has(Object.keys(branch.attribs ?? {}).find((key) => key.startsWith("wx:")) ?? "")) break;
6559
6642
  if (branch.attribs?.["wx:else"] != null ? !branchMatched : !branchMatched && evaluateConditionalBranch(branch, scope)) {
6560
- renderedChildren.push(...renderNodeVariants(branch, scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/node-${cursor}`, seenComponentScopes));
6643
+ renderedChildren.push(...renderNodeVariants(branch, scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/node-${cursor}`, seenComponentScopes, templateRenderState));
6561
6644
  branchMatched = true;
6562
6645
  }
6563
6646
  let nextBranchIndex = cursor + 1;
@@ -6570,11 +6653,11 @@ function renderChildren(children, scope, context, ownerJsonPath, ownerFilePath,
6570
6653
  continue;
6571
6654
  }
6572
6655
  if (isElseBranch) continue;
6573
- renderedChildren.push(...renderNodeVariants(child, scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/node-${index}`, seenComponentScopes));
6656
+ renderedChildren.push(...renderNodeVariants(child, scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/node-${index}`, seenComponentScopes, templateRenderState));
6574
6657
  }
6575
6658
  return renderedChildren;
6576
6659
  }
6577
- function collectComponentSlots(componentNode, scope, ownerJsonPath, ownerFilePath, instancePath) {
6660
+ function collectComponentSlots(componentNode, scope, ownerJsonPath, ownerFilePath, instancePath, templateRenderState) {
6578
6661
  const slots = /* @__PURE__ */ new Map();
6579
6662
  (componentNode.children ?? []).forEach((node, index) => {
6580
6663
  const slotName = isTagNode(node) && typeof node.attribs?.slot === "string" && node.attribs.slot.trim() ? node.attribs.slot.trim() : "default";
@@ -6584,7 +6667,8 @@ function collectComponentSlots(componentNode, scope, ownerJsonPath, ownerFilePat
6584
6667
  node,
6585
6668
  ownerFilePath,
6586
6669
  ownerJsonPath,
6587
- scope
6670
+ scope,
6671
+ templateRenderState
6588
6672
  });
6589
6673
  slots.set(slotName, entries);
6590
6674
  });
@@ -6601,16 +6685,43 @@ function collectComponentSlots(componentNode, scope, ownerJsonPath, ownerFilePat
6601
6685
  }
6602
6686
  return slots;
6603
6687
  }
6604
- function renderNodeTree(node, scope, context, ownerJsonPath, ownerFilePath, instancePath, seenComponentScopes) {
6688
+ function renderNodeTree(node, scope, context, ownerJsonPath, ownerFilePath, instancePath, seenComponentScopes, templateRenderState) {
6605
6689
  const clonedNode = cloneNode(node);
6606
6690
  if (!isTagNode(clonedNode)) {
6607
6691
  applyNodeBindings(clonedNode, scope);
6608
6692
  return clonedNode;
6609
6693
  }
6694
+ if (isTemplateDefinition(clonedNode)) return {
6695
+ type: "tag",
6696
+ name: "block",
6697
+ attribs: {},
6698
+ children: []
6699
+ };
6700
+ const templateName = resolveTemplateCall(clonedNode, scope.data);
6701
+ if (templateName) {
6702
+ const definition = templateRenderState.definitions.get(templateName);
6703
+ const templateScope = {
6704
+ ...scope,
6705
+ data: resolveTemplateData(clonedNode, scope.data)
6706
+ };
6707
+ const children = definition && !templateRenderState.stack.includes(templateName) ? renderChildren(definition.children ?? [], templateScope, context, ownerJsonPath, ownerFilePath, `${instancePath}/template-${templateName}`, seenComponentScopes, {
6708
+ definitions: templateRenderState.definitions,
6709
+ stack: [...templateRenderState.stack, templateName]
6710
+ }) : [];
6711
+ return {
6712
+ type: "tag",
6713
+ name: "block",
6714
+ attribs: {
6715
+ "data-sim-node": instancePath,
6716
+ "data-sim-scope": scope.getScopeId()
6717
+ },
6718
+ children
6719
+ };
6720
+ }
6610
6721
  if (clonedNode.name === "slot") {
6611
6722
  const slotName = clonedNode.attribs?.name?.trim() || "default";
6612
6723
  const projected = scope.slots?.get(slotName) ?? [];
6613
- const children = projected.length ? projected.flatMap((entry) => renderNodeVariants(entry.node, entry.scope, context, entry.ownerJsonPath, entry.ownerFilePath, entry.instancePath, seenComponentScopes)) : renderChildren(clonedNode.children ?? [], scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/slot-fallback-${slotName}`, seenComponentScopes);
6724
+ const children = projected.length ? projected.flatMap((entry) => renderNodeVariants(entry.node, entry.scope, context, entry.ownerJsonPath, entry.ownerFilePath, entry.instancePath, seenComponentScopes, entry.templateRenderState)) : renderChildren(clonedNode.children ?? [], scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/slot-fallback-${slotName}`, seenComponentScopes, templateRenderState);
6614
6725
  return {
6615
6726
  type: "tag",
6616
6727
  name: "block",
@@ -6631,7 +6742,7 @@ function renderNodeTree(node, scope, context, ownerJsonPath, ownerFilePath, inst
6631
6742
  else syncComponentProperties(componentInstance, componentInstance.__definition__ ?? componentEntry.definition, nextProperties, bindingExpressions, context.changedPageKeys);
6632
6743
  seenComponentScopes.add(componentScopeId);
6633
6744
  const genericComponents = resolveComponentGenerics(context, clonedNode, ownerJsonPath, ownerFilePath, componentEntry.filePath);
6634
- const slots = collectComponentSlots(clonedNode, scope, ownerJsonPath, ownerFilePath, componentScopeId);
6745
+ const slots = collectComponentSlots(clonedNode, scope, ownerJsonPath, ownerFilePath, componentScopeId, templateRenderState);
6635
6746
  const componentScope = createComponentScope(clonedNode, scope, componentScopeId, componentInstance, genericComponents, slots);
6636
6747
  context.componentScopes.set(componentScopeId, componentScope);
6637
6748
  const renderedComponentRoot = renderRuntimeComponentTemplate(context, componentEntry, renderNodeTree, componentScope, componentScopeId, seenComponentScopes);
@@ -6649,7 +6760,7 @@ function renderNodeTree(node, scope, context, ownerJsonPath, ownerFilePath, inst
6649
6760
  }
6650
6761
  applyNodeBindings(clonedNode, scope);
6651
6762
  clonedNode.attribs["data-sim-node"] = instancePath;
6652
- clonedNode.children = renderChildren(clonedNode.children ?? [], scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/${clonedNode.name}`, seenComponentScopes);
6763
+ clonedNode.children = renderChildren(clonedNode.children ?? [], scope, context, ownerJsonPath, ownerFilePath, `${instancePath}/${clonedNode.name}`, seenComponentScopes, templateRenderState);
6653
6764
  return clonedNode;
6654
6765
  }
6655
6766
  function renderRuntimePageTree(context, page) {
@@ -6669,7 +6780,9 @@ function renderRuntimePageTree(context, page) {
6669
6780
  context.componentScopes.clear();
6670
6781
  context.componentScopes.set(pageScopeId, pageScope);
6671
6782
  const seenComponentScopes = /* @__PURE__ */ new Set();
6672
- const renderedRoot = renderNodeTree((document.children ?? [])[0] ?? document, pageScope, context, path.resolve(context.project.miniprogramRootPath, `${route}.json`), `${route}.js`, pageScopeId, seenComponentScopes);
6783
+ const root = (document.children ?? [])[0] ?? document;
6784
+ const templateRenderState = createTemplateRenderState(root);
6785
+ const renderedRoot = renderNodeTree(root, pageScope, context, path.resolve(context.project.miniprogramRootPath, `${route}.json`), `${route}.js`, pageScopeId, seenComponentScopes, templateRenderState);
6673
6786
  for (const [scopeId, instance] of [...context.componentCache.entries()]) if (!seenComponentScopes.has(scopeId)) {
6674
6787
  runComponentLifecycle(instance, "detached");
6675
6788
  context.componentCache.delete(scopeId);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mpcore/simulator",
3
3
  "type": "module",
4
- "version": "0.3.0",
4
+ "version": "0.3.2",
5
5
  "description": "Headless mini-program runtime and testing kernel for the weapp-vite ecosystem",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -41,7 +41,7 @@
41
41
  "@babel/parser": "^8.0.4",
42
42
  "htmlparser2": "^12.0.0",
43
43
  "pathe": "^2.0.3",
44
- "@weapp-core/constants": "0.1.15"
44
+ "@weapp-core/constants": "0.1.17"
45
45
  },
46
46
  "publishConfig": {
47
47
  "access": "public",