@wevu/compiler 6.16.16 → 6.16.18

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
@@ -205,6 +205,7 @@ interface MiniProgramPlatform {
205
205
  }
206
206
  //#endregion
207
207
  //#region src/plugins/vue/compiler/template/types.d.ts
208
+ type FunctionPropNameMatcher = string | RegExp;
208
209
  /**
209
210
  * 作用域插槽组件资源描述。
210
211
  */
@@ -287,6 +288,7 @@ interface TemplateCompileOptions {
287
288
  formatWxml?: boolean;
288
289
  wxsExtension?: string;
289
290
  classStyleWxsSrc?: string;
291
+ functionPropNames?: Iterable<FunctionPropNameMatcher>;
290
292
  wevuComponentTags?: Iterable<string>;
291
293
  }
292
294
  /**
package/dist/index.mjs CHANGED
@@ -2231,16 +2231,23 @@ function createValidIdentifier(name, fallback) {
2231
2231
  if (name && t.isValidIdentifier(name)) return t.identifier(name);
2232
2232
  return t.identifier(fallback);
2233
2233
  }
2234
+ function buildConsoleErrorGuard(message, errorId) {
2235
+ return t.ifStatement(t.logicalExpression("&&", t.binaryExpression("!==", t.unaryExpression("typeof", t.identifier("console")), t.stringLiteral("undefined")), t.binaryExpression("===", t.unaryExpression("typeof", t.memberExpression(t.identifier("console"), t.identifier("error"))), t.stringLiteral("function"))), t.blockStatement([t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("console"), t.identifier("error")), [t.stringLiteral(message), t.cloneNode(errorId)]))]));
2236
+ }
2237
+ function buildRuntimeExpressionErrorGuard(binding, errorId) {
2238
+ return buildConsoleErrorGuard(`[wevu] 模板运行时表达式执行失败: ${binding.name} = ${binding.exp}`, errorId);
2239
+ }
2234
2240
  function buildNormalizedExpression(binding, helpers) {
2241
+ const errorId = t.identifier(WEVU_EXPRESSION_ERROR_IDENTIFIER);
2235
2242
  if (binding.type === "bind") {
2236
2243
  const exp = binding.expAst ? t.cloneNode(binding.expAst, true) : t.identifier("undefined");
2237
- return t.callExpression(t.arrowFunctionExpression([], t.blockStatement([t.tryStatement(t.blockStatement([t.returnStatement(exp)]), t.catchClause(t.identifier(WEVU_EXPRESSION_ERROR_IDENTIFIER), t.blockStatement([t.returnStatement(t.identifier("undefined"))])), null)])), []);
2244
+ return t.callExpression(t.arrowFunctionExpression([], t.blockStatement([t.tryStatement(t.blockStatement([t.returnStatement(exp)]), t.catchClause(t.cloneNode(errorId), t.blockStatement([buildRuntimeExpressionErrorGuard(binding, errorId), t.returnStatement(t.identifier("undefined"))])), null)])), []);
2238
2245
  }
2239
2246
  const normalizeHelper = binding.type === "class" ? helpers.normalizeClass : helpers.normalizeStyle;
2240
2247
  const errorFallback = binding.errorFallback ?? "";
2241
2248
  const exp = binding.expAst ? t.cloneNode(binding.expAst, true) : t.stringLiteral("");
2242
2249
  const normalizedCall = t.callExpression(t.cloneNode(normalizeHelper), [exp]);
2243
- return t.callExpression(t.arrowFunctionExpression([], t.blockStatement([t.tryStatement(t.blockStatement([t.returnStatement(normalizedCall)]), t.catchClause(t.identifier(WEVU_EXPRESSION_ERROR_IDENTIFIER), t.blockStatement([t.returnStatement(t.stringLiteral(errorFallback))])), null)])), []);
2250
+ return t.callExpression(t.arrowFunctionExpression([], t.blockStatement([t.tryStatement(t.blockStatement([t.returnStatement(normalizedCall)]), t.catchClause(t.cloneNode(errorId), t.blockStatement([buildRuntimeExpressionErrorGuard(binding, errorId), t.returnStatement(t.stringLiteral(errorFallback))])), null)])), []);
2244
2251
  }
2245
2252
  function buildArrayMapExpression(binding, forStack, level, listId, helpers) {
2246
2253
  const itemParam = createValidIdentifier(forStack[level].item, `__wv_item_${level}`);
@@ -2288,7 +2295,7 @@ function buildForExpression(binding, forStack, level, helpers) {
2288
2295
  const unrefHelper = helpers.unref ? t.cloneNode(helpers.unref) : t.identifier("unref");
2289
2296
  const listUnrefExp = t.callExpression(unrefHelper, [listExp]);
2290
2297
  const listDecl = t.variableDeclaration("let", [t.variableDeclarator(listId, t.arrayExpression([]))]);
2291
- const listSafeAssign = t.tryStatement(t.blockStatement([t.expressionStatement(t.assignmentExpression("=", listId, listUnrefExp))]), t.catchClause(t.identifier(`__wv_err_${level}`), t.blockStatement([t.expressionStatement(t.assignmentExpression("=", listId, t.arrayExpression([])))])), null);
2298
+ const listSafeAssign = t.tryStatement(t.blockStatement([t.expressionStatement(t.assignmentExpression("=", listId, listUnrefExp))]), t.catchClause(t.identifier(`__wv_err_${level}`), t.blockStatement([buildConsoleErrorGuard(`[wevu] 模板 v-for 数据源表达式执行失败: ${info.listExp}`, t.identifier(`__wv_err_${level}`)), t.expressionStatement(t.assignmentExpression("=", listId, t.arrayExpression([])))])), null);
2292
2299
  const arrayCheck = t.callExpression(t.memberExpression(t.identifier("Array"), t.identifier("isArray")), [listId]);
2293
2300
  const arrayMap = buildArrayMapExpression(binding, forStack, level, listId, helpers);
2294
2301
  const objectCheck = t.logicalExpression("&&", t.binaryExpression("!=", listId, t.nullLiteral()), t.binaryExpression("===", t.unaryExpression("typeof", listId), t.stringLiteral("object")));
@@ -6005,6 +6012,23 @@ function isComputedMemberExpression(rawExpValue) {
6005
6012
  const normalized = unwrapTsExpression(parsed);
6006
6013
  return normalized.type === "MemberExpression" && normalized.computed;
6007
6014
  }
6015
+ function shouldUseRuntimeFunctionPropBinding(argValue, context) {
6016
+ return context.functionPropNames.some((matcher) => {
6017
+ if (typeof matcher === "string") return matcher === argValue;
6018
+ matcher.lastIndex = 0;
6019
+ return matcher.test(argValue);
6020
+ });
6021
+ }
6022
+ function shouldWrapStaticMemberFunctionProp(argValue, path, context) {
6023
+ return path.includes(".") && shouldUseRuntimeFunctionPropBinding(argValue, context);
6024
+ }
6025
+ function createFunctionPropRuntimeAttr(argValue, rawExpValue, context) {
6026
+ const bindingRef = registerRuntimeBindingExpression(rawExpValue, context, { hint: `:${argValue} 函数 prop 绑定` });
6027
+ if (!bindingRef) return null;
6028
+ const bindingName = bindingRef.split("[")[0] || bindingRef;
6029
+ context.functionPropPaths.add(bindingName);
6030
+ return `${argValue}="${renderMustache(bindingRef, context)}"`;
6031
+ }
6008
6032
  function transformBindDirective(node, context, forInfo, options) {
6009
6033
  const { arg } = node;
6010
6034
  if (!arg) return null;
@@ -6013,16 +6037,12 @@ function transformBindDirective(node, context, forInfo, options) {
6013
6037
  if (!rawExpValue) return null;
6014
6038
  if (options?.isComponent && !COMPONENT_NON_PROP_BINDINGS.has(argValue)) {
6015
6039
  const path = collectFunctionPropPath(rawExpValue);
6016
- if (path) if (path.includes(".")) {
6017
- const bindingRef = registerRuntimeBindingExpression(rawExpValue, context, { hint: `:${argValue} 函数 prop 绑定` });
6018
- if (bindingRef) {
6019
- const bindingName = bindingRef.split("[")[0] || bindingRef;
6020
- context.functionPropPaths.add(bindingName);
6021
- return `${argValue}="${renderMustache(bindingRef, context)}"`;
6022
- }
6040
+ if (path) if (shouldWrapStaticMemberFunctionProp(argValue, path, context)) {
6041
+ const runtimeAttr = createFunctionPropRuntimeAttr(argValue, rawExpValue, context);
6042
+ if (runtimeAttr) return runtimeAttr;
6023
6043
  } else context.functionPropPaths.add(path);
6024
- else if (isComputedMemberExpression(rawExpValue)) {
6025
- const runtimeAttr = createBindRuntimeAttr(argValue, rawExpValue, context);
6044
+ else if (shouldUseRuntimeFunctionPropBinding(argValue, context) && isComputedMemberExpression(rawExpValue)) {
6045
+ const runtimeAttr = createFunctionPropRuntimeAttr(argValue, rawExpValue, context);
6026
6046
  if (runtimeAttr) return runtimeAttr;
6027
6047
  }
6028
6048
  }
@@ -6623,7 +6643,8 @@ function createScopedSlotComponent(context, slotKey, props, children, transformN
6623
6643
  forIndexSeed: 0,
6624
6644
  inlineExpressions: [],
6625
6645
  inlineExpressionSeed: 0,
6626
- functionPropPaths: /* @__PURE__ */ new Set()
6646
+ functionPropPaths: /* @__PURE__ */ new Set(),
6647
+ functionPropNames: context.functionPropNames
6627
6648
  };
6628
6649
  let template = withSlotProps(scopedContext, {
6629
6650
  ...collectScopePropMapping(context),
@@ -7347,6 +7368,7 @@ function compileVueTemplateToWxml(template, filename, options) {
7347
7368
  inlineExpressions: [],
7348
7369
  inlineExpressionSeed: 0,
7349
7370
  functionPropPaths: /* @__PURE__ */ new Set(),
7371
+ functionPropNames: Array.from(options?.functionPropNames ?? []),
7350
7372
  wevuComponentTags: options?.wevuComponentTags ? new Set(options.wevuComponentTags) : void 0
7351
7373
  };
7352
7374
  let wxml = ast.children.map((child) => transformNode(child, context)).join("");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wevu/compiler",
3
3
  "type": "module",
4
- "version": "6.16.16",
4
+ "version": "6.16.18",
5
5
  "description": "wevu 编译器基础包,面向小程序模板的编译与转换",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -50,9 +50,9 @@
50
50
  "merge": "^2.1.1",
51
51
  "pathe": "^2.0.3",
52
52
  "vue": "^3.5.34",
53
- "@weapp-core/constants": "^0.1.7",
53
+ "@weapp-core/constants": "0.1.8",
54
54
  "@weapp-core/shared": "3.0.4",
55
- "@weapp-vite/ast": "6.16.16",
55
+ "@weapp-vite/ast": "6.16.18",
56
56
  "rolldown-require": "2.0.16"
57
57
  },
58
58
  "publishConfig": {