@wevu/compiler 6.6.5 → 6.6.7

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
@@ -192,6 +192,20 @@ interface ScopedSlotComponentAsset {
192
192
  classStyleWxs?: boolean;
193
193
  inlineExpressions?: InlineExpressionAsset[];
194
194
  }
195
+ /**
196
+ * 内联表达式资源描述。
197
+ */
198
+ interface InlineExpressionIndexBindingAsset {
199
+ key: string;
200
+ binding: string;
201
+ }
202
+ /**
203
+ * 内联表达式作用域恢复器描述。
204
+ */
205
+ interface InlineExpressionScopeResolverAsset {
206
+ key: string;
207
+ expression: string;
208
+ }
195
209
  /**
196
210
  * 内联表达式资源描述。
197
211
  */
@@ -199,6 +213,8 @@ interface InlineExpressionAsset {
199
213
  id: string;
200
214
  expression: string;
201
215
  scopeKeys: string[];
216
+ indexBindings?: InlineExpressionIndexBindingAsset[];
217
+ scopeResolvers?: InlineExpressionScopeResolverAsset[];
202
218
  }
203
219
  /**
204
220
  * 模板编译结果。
package/dist/index.mjs CHANGED
@@ -2576,7 +2576,21 @@ function buildInlineMapExpression(inlineExpressions) {
2576
2576
  t.identifier("scope"),
2577
2577
  t.identifier("$event")
2578
2578
  ], exprAst);
2579
- const entryObj = t.objectExpression([t.objectProperty(t.identifier("keys"), keysExpr), t.objectProperty(t.identifier("fn"), fnExpr)]);
2579
+ const entryObjProps = [t.objectProperty(t.identifier("keys"), keysExpr), t.objectProperty(t.identifier("fn"), fnExpr)];
2580
+ if (entry.indexBindings?.length) {
2581
+ const indexKeysExpr = t.arrayExpression(entry.indexBindings.map((binding) => t.stringLiteral(binding.key)));
2582
+ entryObjProps.push(t.objectProperty(t.identifier("indexKeys"), indexKeysExpr));
2583
+ }
2584
+ if (entry.scopeResolvers?.length) {
2585
+ const resolverMap = new Map(entry.scopeResolvers.map((item) => [item.key, item.expression]));
2586
+ const resolverExpr = t.arrayExpression(entry.scopeKeys.map((key) => {
2587
+ const source = resolverMap.get(key);
2588
+ if (!source) return t.identifier("undefined");
2589
+ return parseBabelExpression(source) ?? t.identifier("undefined");
2590
+ }));
2591
+ entryObjProps.push(t.objectProperty(t.identifier("scopeResolvers"), resolverExpr));
2592
+ }
2593
+ const entryObj = t.objectExpression(entryObjProps);
2580
2594
  return t.objectProperty(t.stringLiteral(entry.id), entryObj);
2581
2595
  });
2582
2596
  return t.objectExpression(entries);
@@ -4397,6 +4411,7 @@ const INLINE_GLOBALS = new Set([
4397
4411
  "require",
4398
4412
  "arguments",
4399
4413
  "__weapp_vite",
4414
+ "__wevuUnref",
4400
4415
  "globalThis",
4401
4416
  "setTimeout",
4402
4417
  "clearTimeout",
@@ -4413,8 +4428,10 @@ const INLINE_GLOBALS = new Set([
4413
4428
  "ctx",
4414
4429
  "scope"
4415
4430
  ]);
4431
+ const IDENTIFIER_RE = /^[A-Z_$][\w$]*$/i;
4432
+ const SIMPLE_PATH_RE = /^[A-Z_$][\w$]*(?:\.[A-Z_$][\w$]*)*$/i;
4416
4433
  function createMemberAccess$1(target, prop) {
4417
- if (/^[A-Z_$][\w$]*$/i.test(prop)) return t.memberExpression(t.identifier(target), t.identifier(prop));
4434
+ if (IDENTIFIER_RE.test(prop)) return t.memberExpression(t.identifier(target), t.identifier(prop));
4418
4435
  return t.memberExpression(t.identifier(target), t.stringLiteral(prop), true);
4419
4436
  }
4420
4437
  function resolveSlotPropBinding(slotProps, name) {
@@ -4423,29 +4440,16 @@ function resolveSlotPropBinding(slotProps, name) {
4423
4440
  if (!prop) return "__wvSlotPropsData";
4424
4441
  return generateExpression(createMemberAccess$1("__wvSlotPropsData", prop));
4425
4442
  }
4426
- function registerInlineExpression(exp, context) {
4427
- const parsed = parseBabelExpressionFile(exp);
4428
- if (!parsed) return null;
4429
- const { ast, expression } = parsed;
4430
- const locals = collectScopedSlotLocals(context);
4431
- const slotProps = collectSlotPropMapping(context);
4432
- for (const name of Object.keys(slotProps)) locals.add(name);
4433
- const usedLocals = [];
4434
- const usedLocalSet = /* @__PURE__ */ new Set();
4435
- const markLocal = (name) => {
4436
- if (usedLocalSet.has(name)) return;
4437
- usedLocalSet.add(name);
4438
- usedLocals.push(name);
4439
- };
4443
+ function rewriteExpressionAst(ast, locals, options) {
4440
4444
  traverse(ast, {
4441
4445
  Identifier(path) {
4442
4446
  if (!path.isReferencedIdentifier()) return;
4443
4447
  const name = path.node.name;
4444
4448
  if (name === "$event") return;
4445
4449
  if (name === "ctx" || name === "scope") return;
4446
- if (path.scope.hasBinding(name)) return;
4450
+ if (path.scope.getBinding(name)) return;
4447
4451
  if (locals.has(name)) {
4448
- markLocal(name);
4452
+ options?.markLocal?.(name);
4449
4453
  path.replaceWith(createMemberAccess$1("scope", name));
4450
4454
  return;
4451
4455
  }
@@ -4456,19 +4460,85 @@ function registerInlineExpression(exp, context) {
4456
4460
  path.replaceWith(t.identifier("ctx"));
4457
4461
  }
4458
4462
  });
4459
- const updatedExpression = generateExpression(expression);
4463
+ }
4464
+ function buildInlineIndexBindings(context) {
4465
+ if (!context.forStack.length) return [];
4466
+ return context.forStack.map((forInfo, level) => ({
4467
+ key: `__wv_i${level}`,
4468
+ binding: forInfo.index?.trim() || "index"
4469
+ }));
4470
+ }
4471
+ function buildForItemResolverExpression(targetKey, context, slotProps, indexBindings) {
4472
+ let targetLevel = -1;
4473
+ for (let level = context.forStack.length - 1; level >= 0; level -= 1) if (context.forStack[level]?.item === targetKey) {
4474
+ targetLevel = level;
4475
+ break;
4476
+ }
4477
+ if (targetLevel < 0) return null;
4478
+ const listExp = context.forStack[targetLevel]?.listExp?.trim() ?? "";
4479
+ const indexBinding = indexBindings[targetLevel];
4480
+ if (!listExp || !indexBinding || !SIMPLE_PATH_RE.test(listExp)) return null;
4481
+ const root = listExp.split(".")[0];
4482
+ const localRoots = new Set(Object.keys(slotProps));
4483
+ for (let level = 0; level <= targetLevel; level += 1) {
4484
+ const item = context.forStack[level]?.item?.trim();
4485
+ const index = context.forStack[level]?.index?.trim();
4486
+ if (item) localRoots.add(item);
4487
+ if (index) localRoots.add(index);
4488
+ localRoots.add("index");
4489
+ }
4490
+ if (localRoots.has(root)) return null;
4491
+ return `({type:'for-item',path:${JSON.stringify(listExp)},indexKey:${JSON.stringify(indexBinding.key)}})`;
4492
+ }
4493
+ function buildScopeResolvers(usedLocals, context, slotProps, indexBindings) {
4494
+ const resolvers = [];
4495
+ for (const key of usedLocals) {
4496
+ const expression = buildForItemResolverExpression(key, context, slotProps, indexBindings);
4497
+ if (!expression) continue;
4498
+ resolvers.push({
4499
+ key,
4500
+ expression
4501
+ });
4502
+ }
4503
+ return resolvers;
4504
+ }
4505
+ function registerInlineExpression(exp, context) {
4506
+ const parsed = parseBabelExpressionFile(exp);
4507
+ if (!parsed) return null;
4508
+ const { ast } = parsed;
4509
+ const locals = collectScopedSlotLocals(context);
4510
+ const slotProps = collectSlotPropMapping(context);
4511
+ for (const name of Object.keys(slotProps)) locals.add(name);
4512
+ const usedLocals = [];
4513
+ const usedLocalSet = /* @__PURE__ */ new Set();
4514
+ const markLocal = (name) => {
4515
+ if (usedLocalSet.has(name)) return;
4516
+ usedLocalSet.add(name);
4517
+ usedLocals.push(name);
4518
+ };
4519
+ rewriteExpressionAst(ast, locals, { markLocal });
4520
+ const updatedStmt = ast.program.body[0];
4521
+ const updatedExpressionNode = updatedStmt && "expression" in updatedStmt ? updatedStmt.expression : null;
4522
+ const updatedExpression = updatedExpressionNode ? generateExpression(updatedExpressionNode) : exp;
4460
4523
  const scopeBindings = usedLocals.map((name) => {
4461
4524
  return resolveSlotPropBinding(slotProps, name) ?? name;
4462
4525
  });
4526
+ const indexBindings = buildInlineIndexBindings(context);
4527
+ const scopeResolvers = buildScopeResolvers(usedLocals, context, slotProps, indexBindings);
4463
4528
  const asset = {
4464
4529
  id: `__wv_inline_${context.inlineExpressionSeed++}`,
4465
4530
  expression: updatedExpression,
4466
4531
  scopeKeys: usedLocals
4467
4532
  };
4533
+ if (scopeResolvers.length) {
4534
+ asset.indexBindings = indexBindings;
4535
+ asset.scopeResolvers = scopeResolvers;
4536
+ }
4468
4537
  context.inlineExpressions.push(asset);
4469
4538
  return {
4470
4539
  id: asset.id,
4471
- scopeBindings
4540
+ scopeBindings,
4541
+ indexBindings: scopeResolvers.length ? indexBindings.map((binding) => binding.binding) : []
4472
4542
  };
4473
4543
  }
4474
4544
 
@@ -4891,6 +4961,11 @@ function buildInlineScopeAttrs(scopeBindings, context) {
4891
4961
  return `data-wv-s${index}="${renderMustache(binding.replace(/"/g, "&quot;"), context)}"`;
4892
4962
  });
4893
4963
  }
4964
+ function buildInlineIndexAttrs(indexBindings, context) {
4965
+ return indexBindings.map((binding, index) => {
4966
+ return `data-wv-i${index}="${renderMustache(binding.replace(/"/g, "&quot;"), context)}"`;
4967
+ });
4968
+ }
4894
4969
  function resolveEventPrefix(modifiers) {
4895
4970
  const hasCatch = modifiers.some((modifier) => modifier.content === "catch");
4896
4971
  const hasStop = modifiers.some((modifier) => modifier.content === "stop");
@@ -4916,9 +4991,11 @@ function transformOnDirective(node, context) {
4916
4991
  if (context.rewriteScopedSlot) {
4917
4992
  if (inlineExpression) {
4918
4993
  const scopeAttrs = buildInlineScopeAttrs(inlineExpression.scopeBindings, context);
4994
+ const indexAttrs = buildInlineIndexAttrs(inlineExpression.indexBindings, context);
4919
4995
  return [
4920
4996
  `data-wv-inline-id="${inlineExpression.id}"`,
4921
4997
  ...scopeAttrs,
4998
+ ...indexAttrs,
4922
4999
  `${bindAttr}="__weapp_vite_owner"`
4923
5000
  ].filter(Boolean).join(" ");
4924
5001
  }
@@ -4931,9 +5008,11 @@ function transformOnDirective(node, context) {
4931
5008
  const expValue = normalizeWxmlExpressionWithContext(rawExpValue, context);
4932
5009
  if (inlineExpression) {
4933
5010
  const scopeAttrs = buildInlineScopeAttrs(inlineExpression.scopeBindings, context);
5011
+ const indexAttrs = buildInlineIndexAttrs(inlineExpression.indexBindings, context);
4934
5012
  return [
4935
5013
  `data-wv-inline-id="${inlineExpression.id}"`,
4936
5014
  ...scopeAttrs,
5015
+ ...indexAttrs,
4937
5016
  `${bindAttr}="__weapp_vite_inline"`
4938
5017
  ].filter(Boolean).join(" ");
4939
5018
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wevu/compiler",
3
3
  "type": "module",
4
- "version": "6.6.5",
4
+ "version": "6.6.7",
5
5
  "description": "wevu 编译器基础包,面向小程序模板的编译与转换",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",