@wevu/compiler 0.1.1 → 0.1.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
@@ -249,7 +249,7 @@ type ClassStyleRuntime = 'wxs' | 'js';
249
249
  */
250
250
  interface ClassStyleBinding {
251
251
  name: string;
252
- type: 'class' | 'style';
252
+ type: 'class' | 'style' | 'bind';
253
253
  exp: string;
254
254
  expAst?: Expression;
255
255
  forStack: ForParseResult[];
package/dist/index.mjs CHANGED
@@ -2212,6 +2212,10 @@ function createValidIdentifier(name, fallback) {
2212
2212
  return t.identifier(fallback);
2213
2213
  }
2214
2214
  function buildNormalizedExpression(binding, helpers) {
2215
+ if (binding.type === "bind") {
2216
+ const exp = binding.expAst ? t.cloneNode(binding.expAst, true) : t.identifier("undefined");
2217
+ return t.callExpression(t.arrowFunctionExpression([], t.blockStatement([t.tryStatement(t.blockStatement([t.returnStatement(exp)]), t.catchClause(t.identifier("__wv_expr_err"), t.blockStatement([t.returnStatement(t.identifier("undefined"))])), null)])), []);
2218
+ }
2215
2219
  const normalizeHelper = binding.type === "class" ? helpers.normalizeClass : helpers.normalizeStyle;
2216
2220
  const exp = binding.expAst ? t.cloneNode(binding.expAst, true) : t.stringLiteral("");
2217
2221
  const normalizedCall = t.callExpression(t.cloneNode(normalizeHelper), [exp]);
@@ -2691,7 +2695,7 @@ function normalizeJsxText(value) {
2691
2695
  function printExpression(exp) {
2692
2696
  return generate(exp).code;
2693
2697
  }
2694
- function unwrapTsExpression$1(exp) {
2698
+ function unwrapTsExpression$2(exp) {
2695
2699
  let current = exp;
2696
2700
  while (t.isTSAsExpression(current) || t.isTSTypeAssertion(current) || t.isTSNonNullExpression(current) || t.isParenthesizedExpression(current) || t.isTSInstantiationExpression(current)) {
2697
2701
  if (t.isTSAsExpression(current) || t.isTSTypeAssertion(current) || t.isTSNonNullExpression(current)) {
@@ -2707,7 +2711,7 @@ function unwrapTsExpression$1(exp) {
2707
2711
  return current;
2708
2712
  }
2709
2713
  function normalizeInterpolationExpression(exp) {
2710
- return normalizeWxmlExpression(printExpression(unwrapTsExpression$1(exp)));
2714
+ return normalizeWxmlExpression(printExpression(unwrapTsExpression$2(exp)));
2711
2715
  }
2712
2716
  function pushScope$1(context, names) {
2713
2717
  for (const name of names) {
@@ -2771,20 +2775,20 @@ function toJsxTagName(name, context) {
2771
2775
  }
2772
2776
  function resolveRenderableExpression(node) {
2773
2777
  if (t.isObjectMethod(node)) {
2774
- for (const statement of node.body.body) if (t.isReturnStatement(statement) && statement.argument) return unwrapTsExpression$1(statement.argument);
2778
+ for (const statement of node.body.body) if (t.isReturnStatement(statement) && statement.argument) return unwrapTsExpression$2(statement.argument);
2775
2779
  return null;
2776
2780
  }
2777
2781
  if (!node.value) return null;
2778
2782
  const value = node.value;
2779
2783
  if (t.isArrowFunctionExpression(value)) {
2780
2784
  if (t.isBlockStatement(value.body)) {
2781
- for (const statement of value.body.body) if (t.isReturnStatement(statement) && statement.argument) return unwrapTsExpression$1(statement.argument);
2785
+ for (const statement of value.body.body) if (t.isReturnStatement(statement) && statement.argument) return unwrapTsExpression$2(statement.argument);
2782
2786
  return null;
2783
2787
  }
2784
- return unwrapTsExpression$1(value.body);
2788
+ return unwrapTsExpression$2(value.body);
2785
2789
  }
2786
2790
  if (t.isFunctionExpression(value)) {
2787
- for (const statement of value.body.body) if (t.isReturnStatement(statement) && statement.argument) return unwrapTsExpression$1(statement.argument);
2791
+ for (const statement of value.body.body) if (t.isReturnStatement(statement) && statement.argument) return unwrapTsExpression$2(statement.argument);
2788
2792
  }
2789
2793
  return null;
2790
2794
  }
@@ -2913,7 +2917,7 @@ function readJsxAttributeExpression(value) {
2913
2917
  if (t.isStringLiteral(value)) return value;
2914
2918
  if (!t.isJSXExpressionContainer(value)) return null;
2915
2919
  if (t.isJSXEmptyExpression(value.expression)) return null;
2916
- return unwrapTsExpression$1(value.expression);
2920
+ return unwrapTsExpression$2(value.expression);
2917
2921
  }
2918
2922
  function extractJsxKeyExpression(node) {
2919
2923
  for (const attr of node.openingElement.attributes) {
@@ -2991,10 +2995,10 @@ function compileMapExpression(exp, context) {
2991
2995
  let bodyExp = null;
2992
2996
  if (t.isBlockStatement(callback.body)) {
2993
2997
  for (const statement of callback.body.body) if (t.isReturnStatement(statement) && statement.argument) {
2994
- bodyExp = unwrapTsExpression$1(statement.argument);
2998
+ bodyExp = unwrapTsExpression$2(statement.argument);
2995
2999
  break;
2996
3000
  }
2997
- } else bodyExp = unwrapTsExpression$1(callback.body);
3001
+ } else bodyExp = unwrapTsExpression$2(callback.body);
2998
3002
  const body = bodyExp ? compileRenderableExpression(bodyExp, context) : "";
2999
3003
  popScope$1(context, addedScope.length);
3000
3004
  if (!body) return "";
@@ -3027,7 +3031,7 @@ function compileLogicalExpression(exp, context) {
3027
3031
  return `{{${normalizeInterpolationExpression(exp)}}}`;
3028
3032
  }
3029
3033
  function compileRenderableExpression(exp, context) {
3030
- const node = unwrapTsExpression$1(exp);
3034
+ const node = unwrapTsExpression$2(exp);
3031
3035
  if (t.isJSXElement(node)) return compileJsxElement(node, context);
3032
3036
  if (t.isJSXFragment(node)) return compileJsxFragment(node, context);
3033
3037
  if (t.isConditionalExpression(node)) return compileConditionalExpression(node, context);
@@ -4516,7 +4520,7 @@ function toWxmlStringLiteral(value) {
4516
4520
  function cloneForStack(context) {
4517
4521
  return context.forStack.map((info) => ({ ...info }));
4518
4522
  }
4519
- function buildForIndexAccess(context) {
4523
+ function buildForIndexAccess$1(context) {
4520
4524
  if (!context.forStack.length) return "";
4521
4525
  return context.forStack.map((info) => `[${info.index ?? "index"}]`).join("");
4522
4526
  }
@@ -4529,15 +4533,15 @@ function mergeJsExpressionParts(parts) {
4529
4533
  if (parts.length === 1) return parts[0];
4530
4534
  return t.arrayExpression(parts);
4531
4535
  }
4532
- function unwrapTsExpression(node) {
4533
- if (t.isTSAsExpression(node) || t.isTSNonNullExpression(node) || t.isTSTypeAssertion(node)) return unwrapTsExpression(node.expression);
4536
+ function unwrapTsExpression$1(node) {
4537
+ if (t.isTSAsExpression(node) || t.isTSNonNullExpression(node) || t.isTSTypeAssertion(node)) return unwrapTsExpression$1(node.expression);
4534
4538
  return node;
4535
4539
  }
4536
4540
  function shouldPreferJsClassStyleRuntime(exp) {
4537
4541
  const ast = parseBabelExpression(exp);
4538
4542
  if (!ast) return true;
4539
4543
  const visit = (node) => {
4540
- const current = unwrapTsExpression(node);
4544
+ const current = unwrapTsExpression$1(node);
4541
4545
  if (t.isIdentifier(current) || t.isMemberExpression(current) || t.isOptionalMemberExpression(current) || t.isCallExpression(current) || t.isOptionalCallExpression(current) || t.isThisExpression(current) || t.isSuper(current) || t.isAwaitExpression(current) || t.isYieldExpression(current) || t.isNewExpression(current)) return true;
4542
4546
  if (t.isStringLiteral(current) || t.isNumericLiteral(current) || t.isBooleanLiteral(current) || t.isNullLiteral(current) || t.isBigIntLiteral(current) || t.isRegExpLiteral(current)) return false;
4543
4547
  if (t.isTemplateLiteral(current)) return current.expressions.some((exp) => visit(exp));
@@ -4560,9 +4564,9 @@ function shouldPreferJsClassStyleRuntime(exp) {
4560
4564
  return visit(ast);
4561
4565
  }
4562
4566
  function createClassStyleBinding(context, type, exp, expAst) {
4563
- const index = context.classStyleBindings.length;
4567
+ const sameTypeCount = context.classStyleBindings.filter((binding) => binding.type === type).length;
4564
4568
  return {
4565
- name: type === "class" ? `__wv_cls_${index}` : `__wv_style_${index}`,
4569
+ name: type === "class" ? `__wv_cls_${sameTypeCount}` : type === "style" ? `__wv_style_${sameTypeCount}` : `__wv_bind_${sameTypeCount}`,
4566
4570
  type,
4567
4571
  exp,
4568
4572
  expAst,
@@ -4588,7 +4592,7 @@ function renderClassAttribute(staticClass, dynamicClassExp, context) {
4588
4592
  const expAst = mergeJsExpressionParts(jsParts);
4589
4593
  const binding = createClassStyleBinding(context, "class", generateExpressionCode(expAst), expAst);
4590
4594
  context.classStyleBindings.push(binding);
4591
- const indexAccess = buildForIndexAccess(context);
4595
+ const indexAccess = buildForIndexAccess$1(context);
4592
4596
  return `class="{{${binding.name}${indexAccess}}}"`;
4593
4597
  }
4594
4598
  function renderStyleAttribute(staticStyle, dynamicStyleExp, vShowExp, context) {
@@ -4622,7 +4626,7 @@ function renderStyleAttribute(staticStyle, dynamicStyleExp, vShowExp, context) {
4622
4626
  const expAst = mergeJsExpressionParts(jsParts);
4623
4627
  const binding = createClassStyleBinding(context, "style", generateExpressionCode(expAst), expAst);
4624
4628
  context.classStyleBindings.push(binding);
4625
- const indexAccess = buildForIndexAccess(context);
4629
+ const indexAccess = buildForIndexAccess$1(context);
4626
4630
  return `style="{{${binding.name}${indexAccess}}}"`;
4627
4631
  }
4628
4632
  function transformAttribute(node, _context) {
@@ -4634,6 +4638,33 @@ function transformAttribute(node, _context) {
4634
4638
 
4635
4639
  //#endregion
4636
4640
  //#region src/plugins/vue/compiler/template/directives/bind.ts
4641
+ function unwrapTsExpression(node) {
4642
+ if (node.type === "TSAsExpression" || node.type === "TSNonNullExpression" || node.type === "TSTypeAssertion") return unwrapTsExpression(node.expression);
4643
+ return node;
4644
+ }
4645
+ function isTopLevelObjectLiteral(exp) {
4646
+ const parsed = parseBabelExpression(exp);
4647
+ if (!parsed) return false;
4648
+ return unwrapTsExpression(parsed).type === "ObjectExpression";
4649
+ }
4650
+ function buildForIndexAccess(context) {
4651
+ if (!context.forStack.length) return "";
4652
+ return context.forStack.map((info) => `[${info.index ?? "index"}]`).join("");
4653
+ }
4654
+ function createBindRuntimeAttr(argValue, rawExpValue, context) {
4655
+ const expAst = normalizeJsExpressionWithContext(rawExpValue, context, { hint: `:${argValue} 绑定` });
4656
+ if (!expAst) return null;
4657
+ const binding = {
4658
+ name: `__wv_bind_${context.classStyleBindings.filter((item) => item.type === "bind").length}`,
4659
+ type: "bind",
4660
+ exp: rawExpValue,
4661
+ expAst,
4662
+ forStack: context.forStack.map((info) => ({ ...info }))
4663
+ };
4664
+ context.classStyleBindings.push(binding);
4665
+ const indexAccess = buildForIndexAccess(context);
4666
+ return `${argValue}="{{${binding.name}${indexAccess}}}"`;
4667
+ }
4637
4668
  const isSimpleIdentifier = (value) => /^[A-Z_$][\w$]*$/i.test(value);
4638
4669
  const isSimpleMemberPath = (value) => /^[A-Z_$][\w$]*(?:\.[A-Z_$][\w$]*)*$/i.test(value);
4639
4670
  function transformBindDirective(node, context, forInfo) {
@@ -4641,8 +4672,10 @@ function transformBindDirective(node, context, forInfo) {
4641
4672
  if (!arg) return null;
4642
4673
  const argValue = arg.type === NodeTypes.SIMPLE_EXPRESSION ? arg.content : "";
4643
4674
  if (!exp) return null;
4644
- const expValue = normalizeWxmlExpressionWithContext(exp.type === NodeTypes.SIMPLE_EXPRESSION ? exp.content : "", context);
4675
+ const rawExpValue = exp.type === NodeTypes.SIMPLE_EXPRESSION ? exp.content : "";
4676
+ if (!rawExpValue) return null;
4645
4677
  if (argValue === "key") {
4678
+ const expValue = normalizeWxmlExpressionWithContext(rawExpValue, context);
4646
4679
  const trimmed = expValue.trim();
4647
4680
  const warnKeyFallback = (reason) => {
4648
4681
  if (!forInfo) return;
@@ -4666,7 +4699,8 @@ function transformBindDirective(node, context, forInfo) {
4666
4699
  }
4667
4700
  return context.platform.keyAttr(expValue);
4668
4701
  }
4669
- return `${argValue}="{{${expValue}}}"`;
4702
+ if (isTopLevelObjectLiteral(rawExpValue)) return createBindRuntimeAttr(argValue, rawExpValue, context);
4703
+ return `${argValue}="{{${normalizeWxmlExpressionWithContext(rawExpValue, context)}}}"`;
4670
4704
  }
4671
4705
 
4672
4706
  //#endregion
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wevu/compiler",
3
3
  "type": "module",
4
- "version": "0.1.1",
4
+ "version": "0.1.2",
5
5
  "description": "wevu 编译器基础包,面向小程序模板的编译与转换",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -53,8 +53,8 @@
53
53
  "merge": "^2.1.1",
54
54
  "pathe": "^2.0.3",
55
55
  "vue": "^3.5.28",
56
- "rolldown-require": "2.0.6",
57
- "@weapp-core/shared": "3.0.1"
56
+ "@weapp-core/shared": "3.0.1",
57
+ "rolldown-require": "2.0.6"
58
58
  },
59
59
  "publishConfig": {
60
60
  "access": "public",