@wevu/compiler 6.16.19 → 6.16.21

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
@@ -276,6 +276,11 @@ interface ForParseResult {
276
276
  */
277
277
  interface TemplateCompileOptions {
278
278
  platform?: MiniProgramPlatform;
279
+ /**
280
+ * Vue `<script setup>` props 解构重命名映射,key 为模板中使用的本地别名,value 为原始 prop 名。
281
+ */
282
+ propsAliases?: Record<string, string>;
283
+ propsDerivedKeys?: string[];
279
284
  htmlTagToWxml?: boolean | Record<string, string>;
280
285
  htmlTagToWxmlTagClass?: boolean;
281
286
  scopedSlotsCompiler?: ScopedSlotsCompilerMode;
@@ -705,6 +710,14 @@ interface TransformScriptOptions {
705
710
  * 模板中作为组件 prop 传递的函数候选路径。
706
711
  */
707
712
  functionPropPaths?: string[];
713
+ /**
714
+ * Vue `<script setup>` props 解构重命名映射,key 为模板中使用的本地别名,value 为原始 prop 名。
715
+ */
716
+ propsAliases?: Record<string, string>;
717
+ /**
718
+ * Vue `<script setup>` 中直接来自 props 的绑定名,用于运行时区分 props-derived binding 与普通 setup state。
719
+ */
720
+ propsDerivedKeys?: string[];
708
721
  /**
709
722
  * 对 `<script setup>` 类型声明生成的结构化 props(如 Array/Object)放宽小程序运行时类型约束,
710
723
  * 以避免小程序属性校验对复杂表达式/代理值产生误报。
package/dist/index.mjs CHANGED
@@ -13,7 +13,7 @@ import os from "node:os";
13
13
  import process from "node:process";
14
14
  import { collectFeatureFlagsFromCode, collectJsxImportedComponentsAndDefaultExportFromBabelAst, collectJsxTemplateTagsFromBabelExpression, getRenderPropertyFromComponentOptions, parseJsLikeWithEngine, resolveRenderExpressionFromComponentOptions, toStaticObjectKey, unwrapTypeScriptExpression } from "@weapp-vite/ast";
15
15
  import { LRUCache } from "lru-cache";
16
- import { WEVU_CLASS_STYLE_RUNTIME_FILE, WEVU_CLASS_STYLE_RUNTIME_MODULE, WEVU_EXPRESSION_ERROR_IDENTIFIER, WEVU_FUNCTION_PROP_PATHS_KEY, WEVU_INLINE_HANDLER, WEVU_INLINE_MAP_KEY, WEVU_IS_PAGE_KEY, WEVU_LAYOUT_HOSTS_KEY, WEVU_LAYOUT_HOST_ID_PREFIX, WEVU_LAYOUT_HOST_REF_PREFIX, WEVU_MODEL_HANDLER, WEVU_OWNER_HANDLER, WEVU_PROPS_KEY, WEVU_SLOT_NAMES_ATTR, WEVU_SLOT_NAMES_PROP, WEVU_SLOT_OWNER_ID_ATTR, WEVU_SLOT_OWNER_ID_KEY, WEVU_SLOT_OWNER_ID_PROP, WEVU_SLOT_OWNER_KEY, WEVU_SLOT_OWNER_PROXY_KEY, WEVU_SLOT_PROPS_ATTR, WEVU_SLOT_PROPS_DATA_KEY, WEVU_SLOT_PROPS_KEY, WEVU_SLOT_SCOPE_ATTR, WEVU_SLOT_SCOPE_KEY, WEVU_TEMPLATE_REFS_KEY } from "@weapp-core/constants";
16
+ import { WEVU_CLASS_STYLE_RUNTIME_FILE, WEVU_CLASS_STYLE_RUNTIME_MODULE, WEVU_EXPRESSION_ERROR_IDENTIFIER, WEVU_FUNCTION_PROP_PATHS_KEY, WEVU_INLINE_HANDLER, WEVU_INLINE_MAP_KEY, WEVU_IS_PAGE_KEY, WEVU_LAYOUT_HOSTS_KEY, WEVU_LAYOUT_HOST_ID_PREFIX, WEVU_LAYOUT_HOST_REF_PREFIX, WEVU_MODEL_HANDLER, WEVU_OWNER_HANDLER, WEVU_PROPS_ALIASES_KEY, WEVU_PROPS_DERIVED_KEYS_KEY, WEVU_PROPS_KEY, WEVU_SLOT_NAMES_ATTR, WEVU_SLOT_NAMES_PROP, WEVU_SLOT_OWNER_ID_ATTR, WEVU_SLOT_OWNER_ID_KEY, WEVU_SLOT_OWNER_ID_PROP, WEVU_SLOT_OWNER_KEY, WEVU_SLOT_OWNER_PROXY_KEY, WEVU_SLOT_PROPS_ATTR, WEVU_SLOT_PROPS_DATA_KEY, WEVU_SLOT_PROPS_KEY, WEVU_SLOT_SCOPE_ATTR, WEVU_SLOT_SCOPE_KEY, WEVU_TEMPLATE_REFS_KEY } from "@weapp-core/constants";
17
17
  import { compileScript, parse } from "vue/compiler-sfc";
18
18
  import { fileURLToPath } from "node:url";
19
19
  import { parse as parse$1 } from "@vue/compiler-dom";
@@ -2241,6 +2241,41 @@ function buildConsoleErrorGuard(message, errorId) {
2241
2241
  function buildRuntimeExpressionErrorGuard(binding, errorId) {
2242
2242
  return buildConsoleErrorGuard(`[wevu] 模板运行时表达式执行失败: ${binding.name} = ${binding.exp}`, errorId);
2243
2243
  }
2244
+ function shouldReportRuntimeExpressionError(binding) {
2245
+ return binding.type === "bind";
2246
+ }
2247
+ function createDataPropsFallbackExpression(fallback) {
2248
+ const propsObject = t.memberExpression(t.thisExpression(), t.identifier(WEVU_PROPS_KEY));
2249
+ const propsAccess = t.memberExpression(propsObject, t.identifier("data"));
2250
+ const hasPropsObject = t.binaryExpression("!=", propsObject, t.nullLiteral());
2251
+ const hasPropsValue = t.logicalExpression("&&", hasPropsObject, t.logicalExpression("||", t.binaryExpression("!==", propsAccess, t.identifier("undefined")), t.callExpression(t.memberExpression(t.memberExpression(t.memberExpression(t.identifier("Object"), t.identifier("prototype")), t.identifier("hasOwnProperty")), t.identifier("call")), [propsObject, t.stringLiteral("data")])));
2252
+ return t.conditionalExpression(hasPropsValue, propsAccess, fallback);
2253
+ }
2254
+ function createDataRuntimeAccess(helpers) {
2255
+ const unrefHelper = helpers.unref ? t.cloneNode(helpers.unref) : t.identifier("unref");
2256
+ return t.callExpression(unrefHelper, [createDataPropsFallbackExpression(t.memberExpression(t.thisExpression(), t.identifier("data")))]);
2257
+ }
2258
+ function rewriteDataAccessExpression(exp, helpers) {
2259
+ if (t.isIdentifier(exp) && exp.name === "data") return createDataRuntimeAccess(helpers);
2260
+ if (t.isMemberExpression(exp)) return t.memberExpression(rewriteDataAccessExpression(exp.object, helpers), t.cloneNode(exp.property), exp.computed);
2261
+ if (t.isObjectExpression(exp)) return t.objectExpression(exp.properties.map((property) => {
2262
+ if (!t.isObjectProperty(property) || !t.isExpression(property.value)) return t.cloneNode(property, true);
2263
+ return t.objectProperty(t.cloneNode(property.key), rewriteDataAccessExpression(property.value, helpers), property.computed, property.shorthand);
2264
+ }));
2265
+ if (t.isArrayExpression(exp)) return t.arrayExpression(exp.elements.map((element) => {
2266
+ if (!element || t.isSpreadElement(element)) return element ? t.cloneNode(element, true) : null;
2267
+ return rewriteDataAccessExpression(element, helpers);
2268
+ }));
2269
+ if (t.isBinaryExpression(exp)) return t.isPrivateName(exp.left) || t.isPrivateName(exp.right) ? t.cloneNode(exp, true) : t.binaryExpression(exp.operator, rewriteDataAccessExpression(exp.left, helpers), rewriteDataAccessExpression(exp.right, helpers));
2270
+ if (t.isLogicalExpression(exp)) return t.logicalExpression(exp.operator, rewriteDataAccessExpression(exp.left, helpers), rewriteDataAccessExpression(exp.right, helpers));
2271
+ if (t.isConditionalExpression(exp)) return t.conditionalExpression(rewriteDataAccessExpression(exp.test, helpers), rewriteDataAccessExpression(exp.consequent, helpers), rewriteDataAccessExpression(exp.alternate, helpers));
2272
+ if (t.isUnaryExpression(exp)) return t.unaryExpression(exp.operator, rewriteDataAccessExpression(exp.argument, helpers), exp.prefix);
2273
+ if (t.isCallExpression(exp)) return t.callExpression(rewriteDataAccessExpression(exp.callee, helpers), exp.arguments.map((arg) => {
2274
+ if (t.isSpreadElement(arg)) return t.cloneNode(arg, true);
2275
+ return rewriteDataAccessExpression(arg, helpers);
2276
+ }));
2277
+ return t.cloneNode(exp, true);
2278
+ }
2244
2279
  function buildNormalizedExpression(binding, helpers) {
2245
2280
  const errorId = t.identifier(WEVU_EXPRESSION_ERROR_IDENTIFIER);
2246
2281
  if (binding.type === "bind") {
@@ -2249,9 +2284,9 @@ function buildNormalizedExpression(binding, helpers) {
2249
2284
  }
2250
2285
  const normalizeHelper = binding.type === "class" ? helpers.normalizeClass : helpers.normalizeStyle;
2251
2286
  const errorFallback = binding.errorFallback ?? "";
2252
- const exp = binding.expAst ? t.cloneNode(binding.expAst, true) : t.stringLiteral("");
2287
+ const exp = binding.expAst ? rewriteDataAccessExpression(binding.expAst, helpers) : t.stringLiteral("");
2253
2288
  const normalizedCall = t.callExpression(t.cloneNode(normalizeHelper), [exp]);
2254
- 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)])), []);
2289
+ return t.callExpression(t.arrowFunctionExpression([], t.blockStatement([t.tryStatement(t.blockStatement([t.returnStatement(normalizedCall)]), t.catchClause(t.cloneNode(errorId), t.blockStatement([...shouldReportRuntimeExpressionError(binding) ? [buildRuntimeExpressionErrorGuard(binding, errorId)] : [], t.returnStatement(t.stringLiteral(errorFallback))])), null)])), []);
2255
2290
  }
2256
2291
  function buildArrayMapExpression(binding, forStack, level, listId, helpers) {
2257
2292
  const itemParam = createValidIdentifier(forStack[level].item, `__wv_item_${level}`);
@@ -2318,11 +2353,37 @@ function buildComputedFunctionBody(binding, helpers) {
2318
2353
  }
2319
2354
  //#endregion
2320
2355
  //#region src/plugins/vue/transform/classStyleComputed.ts
2321
- function buildClassStyleComputedEntries(bindings, helpers) {
2356
+ function createMemberAccess$2(target, prop) {
2357
+ if (t.isValidIdentifier(prop)) return t.memberExpression(target, t.identifier(prop));
2358
+ return t.memberExpression(target, t.stringLiteral(prop), true);
2359
+ }
2360
+ function applyPropsAliasesToExpression(expression, propsAliases) {
2361
+ if (!propsAliases || !Object.keys(propsAliases).length) return expression;
2362
+ const ast = t.file(t.program([t.expressionStatement(expression)]));
2363
+ traverse(ast, { Identifier(path) {
2364
+ if (!path.isReferencedIdentifier()) return;
2365
+ const propName = propsAliases[path.node.name];
2366
+ if (!propName || path.scope.hasBinding(path.node.name)) return;
2367
+ const replacement = createMemberAccess$2(t.memberExpression(t.thisExpression(), t.identifier("__wevuProps")), propName);
2368
+ const parent = path.parentPath;
2369
+ if (parent.isObjectProperty() && parent.node.shorthand && parent.node.key === path.node) {
2370
+ parent.node.shorthand = false;
2371
+ parent.node.value = replacement;
2372
+ return;
2373
+ }
2374
+ path.replaceWith(replacement);
2375
+ } });
2376
+ const statement = ast.program.body[0];
2377
+ return t.isExpressionStatement(statement) ? statement.expression : expression;
2378
+ }
2379
+ function buildClassStyleComputedEntries(bindings, helpers, propsAliases) {
2322
2380
  const entries = [];
2323
2381
  for (const binding of bindings) {
2324
2382
  const key = createStaticObjectKey(binding.name);
2325
- const body = buildComputedFunctionBody(binding, helpers);
2383
+ const body = buildComputedFunctionBody({
2384
+ ...binding,
2385
+ expAst: binding.expAst ? applyPropsAliasesToExpression(t.cloneNode(binding.expAst, true), propsAliases) : binding.expAst
2386
+ }, helpers);
2326
2387
  const fn = t.functionExpression(null, [], body);
2327
2388
  entries.push(t.objectProperty(key, fn));
2328
2389
  }
@@ -2347,14 +2408,15 @@ function buildClassStyleComputedCode(bindings, helpers) {
2347
2408
  }
2348
2409
  //#endregion
2349
2410
  //#region src/plugins/vue/transform/transformScript/rewrite/classStyle.ts
2350
- function injectClassStyleComputed(optionsObject, bindings, warn) {
2411
+ function injectClassStyleComputed(optionsObject, bindings, propsAliases, warn) {
2351
2412
  if (!bindings.length) return false;
2352
2413
  const warnHandler = resolveWarnHandler(warn);
2353
2414
  const entries = buildClassStyleComputedEntries(bindings, {
2354
2415
  normalizeClass: t.identifier("__wevuNormalizeClass"),
2355
2416
  normalizeStyle: t.identifier("__wevuNormalizeStyle"),
2356
- unref: t.identifier("__wevuUnref")
2357
- });
2417
+ unref: t.identifier("__wevuUnref"),
2418
+ resolvePropValue: t.identifier("__wevuResolvePropValue")
2419
+ }, propsAliases);
2358
2420
  if (!entries.length) return false;
2359
2421
  const computedProp = getObjectPropertyByKey$1(optionsObject, "computed");
2360
2422
  if (!computedProp) {
@@ -2376,6 +2438,7 @@ function ensureClassStyleRuntimeImports(program) {
2376
2438
  ensureRuntimeImport(program, "normalizeClass", "__wevuNormalizeClass");
2377
2439
  ensureRuntimeImport(program, "normalizeStyle", "__wevuNormalizeStyle");
2378
2440
  ensureRuntimeImport(program, "unref", "__wevuUnref");
2441
+ ensureRuntimeImport(program, "resolvePropValue", "__wevuResolvePropValue");
2379
2442
  }
2380
2443
  //#endregion
2381
2444
  //#region src/plugins/vue/transform/transformScript/rewrite/defaults.ts
@@ -2675,6 +2738,24 @@ function injectLayoutHosts(optionsObject, bindings, warn) {
2675
2738
  return false;
2676
2739
  }
2677
2740
  //#endregion
2741
+ //#region src/plugins/vue/transform/transformScript/rewrite/propsAliases.ts
2742
+ function injectPropsAliases(optionsObject, propsAliases) {
2743
+ const entries = Object.entries(propsAliases ?? {}).filter(([alias, propName]) => alias && propName).map(([alias, propName]) => {
2744
+ return t.objectProperty(createStaticObjectKey$1(alias), t.stringLiteral(propName));
2745
+ });
2746
+ if (!entries.length) return false;
2747
+ if (getObjectPropertyByKey$1(optionsObject, WEVU_PROPS_ALIASES_KEY)) return false;
2748
+ optionsObject.properties.push(t.objectProperty(createStaticObjectKey$1(WEVU_PROPS_ALIASES_KEY), t.objectExpression(entries)));
2749
+ return true;
2750
+ }
2751
+ function injectPropsDerivedKeys(optionsObject, propsDerivedKeys) {
2752
+ const keys = [...new Set(propsDerivedKeys ?? [])].filter(Boolean);
2753
+ if (!keys.length) return false;
2754
+ if (getObjectPropertyByKey$1(optionsObject, WEVU_PROPS_DERIVED_KEYS_KEY)) return false;
2755
+ optionsObject.properties.push(t.objectProperty(createStaticObjectKey$1(WEVU_PROPS_DERIVED_KEYS_KEY), t.arrayExpression(keys.map((key) => t.stringLiteral(key)))));
2756
+ return true;
2757
+ }
2758
+ //#endregion
2678
2759
  //#region src/plugins/vue/transform/transformScript/rewrite/setupInitialData.ts
2679
2760
  function unwrapExpression(node) {
2680
2761
  if (t.isTSAsExpression(node) || t.isTSSatisfiesExpression(node) || t.isTSNonNullExpression(node) || t.isTypeCastExpression(node) || t.isParenthesizedExpression(node)) return unwrapExpression(node.expression);
@@ -2936,10 +3017,14 @@ function rewriteDefaultExport(ast, state, options, enabledPageFeatures, serializ
2936
3017
  }) || transformed;
2937
3018
  if (componentOptionsObject) transformed = injectSetupInitialData(componentOptionsObject) || transformed;
2938
3019
  if (componentOptionsObject && options?.relaxStructuredTypeOnlyProps) transformed = relaxStructuredTypeOnlyProps(componentOptionsObject) || transformed;
3020
+ if (componentOptionsObject) {
3021
+ transformed = injectPropsAliases(componentOptionsObject, options?.propsAliases) || transformed;
3022
+ transformed = injectPropsDerivedKeys(componentOptionsObject, options?.propsDerivedKeys) || transformed;
3023
+ }
2939
3024
  const classStyleBindings = options?.classStyleBindings ?? [];
2940
3025
  if (classStyleBindings.length) if (componentOptionsObject) {
2941
3026
  ensureClassStyleRuntimeImports(ast.program);
2942
- transformed = injectClassStyleComputed(componentOptionsObject, classStyleBindings, warn) || transformed;
3027
+ transformed = injectClassStyleComputed(componentOptionsObject, classStyleBindings, options?.propsAliases, warn) || transformed;
2943
3028
  } else warn("无法自动注入 class/style 计算属性:组件选项不是对象字面量。");
2944
3029
  const templateRefs = options?.templateRefs ?? [];
2945
3030
  if (templateRefs.length) if (componentOptionsObject) transformed = injectTemplateRefs(componentOptionsObject, templateRefs, warn) || transformed;
@@ -5363,10 +5448,10 @@ function rewriteForAliasExpression(exp, context) {
5363
5448
  if (!path.isReferencedIdentifier()) return;
5364
5449
  const name = path.node.name;
5365
5450
  if (path.scope.hasBinding(name)) return;
5366
- if (!hasOwn(forAliases, name)) return;
5367
- const aliasExp = parseBabelExpression(forAliases[name]);
5368
- if (!aliasExp) return;
5369
- replaceIdentifierWithExpression(path, t.cloneNode(aliasExp, true));
5451
+ if (hasOwn(forAliases, name)) {
5452
+ const aliasExp = parseBabelExpression(forAliases[name]);
5453
+ if (aliasExp) replaceIdentifierWithExpression(path, t.cloneNode(aliasExp, true));
5454
+ }
5370
5455
  } });
5371
5456
  const stmt = ast.program.body[0];
5372
5457
  const updatedExpression = stmt && "expression" in stmt ? stmt.expression : null;
@@ -5683,6 +5768,7 @@ const JS_RUNTIME_GLOBALS = new Set([
5683
5768
  "arguments",
5684
5769
  "globalThis",
5685
5770
  "__wevuUnref",
5771
+ "__wevuResolvePropValue",
5686
5772
  "getApp",
5687
5773
  "getCurrentPages",
5688
5774
  ...getMiniProgramRuntimeGlobalKeys()
@@ -5714,11 +5800,35 @@ function createUnrefCall(exp) {
5714
5800
  function createHasOwnPropertyCall(target, key) {
5715
5801
  return t.callExpression(t.memberExpression(t.memberExpression(t.memberExpression(t.identifier("Object"), t.identifier("prototype")), t.identifier("hasOwnProperty")), t.identifier("call")), [target, t.stringLiteral(key)]);
5716
5802
  }
5717
- function createIdentifierAccessWithPropsFallback(name) {
5803
+ function createResolvePropValueCall(name, fallback, preferProps = false) {
5804
+ const args = [
5805
+ t.thisExpression(),
5806
+ t.stringLiteral(name),
5807
+ fallback
5808
+ ];
5809
+ if (preferProps) args.push(t.booleanLiteral(true));
5810
+ return t.callExpression(t.identifier("__wevuResolvePropValue"), args);
5811
+ }
5812
+ function createPropsKeyAccessFallback(name, fallback) {
5813
+ const propsObject = createThisMemberAccess(WEVU_PROPS_KEY);
5814
+ const propsAccess = createMemberAccess(propsObject, name);
5815
+ const hasPropsObject = t.binaryExpression("!=", propsObject, t.nullLiteral());
5816
+ const hasPropsValue = t.logicalExpression("&&", hasPropsObject, t.logicalExpression("||", t.binaryExpression("!==", propsAccess, t.identifier("undefined")), createHasOwnPropertyCall(propsObject, name)));
5817
+ return t.conditionalExpression(hasPropsValue, propsAccess, fallback);
5818
+ }
5819
+ function createIdentifierAccessWithPropsFallback(name, context, useRuntimePropHelper = false) {
5820
+ if (useRuntimePropHelper) {
5821
+ if (name === "props") {
5822
+ const propsObject = createThisMemberAccess(WEVU_PROPS_KEY);
5823
+ return t.conditionalExpression(t.binaryExpression("!=", propsObject, t.nullLiteral()), propsObject, createThisMemberAccess("props"));
5824
+ }
5825
+ return createResolvePropValueCall(name, createThisMemberAccess(name));
5826
+ }
5718
5827
  if (name === "props") {
5719
5828
  const propsObject = createThisMemberAccess(WEVU_PROPS_KEY);
5720
5829
  return t.conditionalExpression(t.binaryExpression("!=", propsObject, t.nullLiteral()), propsObject, createThisMemberAccess("props"));
5721
5830
  }
5831
+ if (name === "data") return createPropsKeyAccessFallback(name, createThisMemberAccess(name));
5722
5832
  const thisAccess = createThisMemberAccess(name);
5723
5833
  const propsAccess = createMemberAccess(createThisMemberAccess(WEVU_PROPS_KEY), name);
5724
5834
  const propsObject = createThisMemberAccess(WEVU_PROPS_KEY);
@@ -5730,10 +5840,14 @@ function createIdentifierAccessWithPropsFallback(name) {
5730
5840
  const hasStateObject = t.binaryExpression("!=", stateObject, t.nullLiteral());
5731
5841
  const hasStateKey = createHasOwnPropertyCall(stateObject, name);
5732
5842
  const hasThisMember = t.binaryExpression("in", t.stringLiteral(name), t.thisExpression());
5733
- const shouldUseStateAccess = t.logicalExpression("&&", hasStateObject, hasStateKey);
5734
- const shouldUsePropsAccess = t.logicalExpression("&&", hasUsablePropsValue, t.unaryExpression("!", hasThisMember));
5843
+ const isPropsDerivedKey = Boolean(context.propsDerivedKeys?.includes(name));
5844
+ const shouldUseStateAccess = isPropsDerivedKey ? t.booleanLiteral(false) : t.logicalExpression("&&", hasStateObject, hasStateKey);
5845
+ const shouldUsePropsAccess = t.logicalExpression("&&", hasUsablePropsValue, isPropsDerivedKey ? t.booleanLiteral(true) : t.unaryExpression("!", hasThisMember));
5735
5846
  return t.conditionalExpression(shouldUseStateAccess, thisAccess, t.conditionalExpression(shouldUsePropsAccess, propsAccess, thisAccess));
5736
5847
  }
5848
+ function createAliasedPropsAccess(name, propName) {
5849
+ return createResolvePropValueCall(propName, createThisMemberAccess(name), true);
5850
+ }
5737
5851
  function collectForAliasMapping(context) {
5738
5852
  const mapping = {};
5739
5853
  for (const forInfo of context.forStack) {
@@ -5782,14 +5896,19 @@ function normalizeJsExpressionWithContext(exp, context, options) {
5782
5896
  replacement = createUnrefCall(prop ? createMemberAccess(base, prop) : base);
5783
5897
  } else if (name === WEVU_SLOT_OWNER_KEY || name === WEVU_SLOT_PROPS_DATA_KEY || name === WEVU_SLOT_PROPS_KEY || name === WEVU_SLOT_SCOPE_KEY) replacement = createUnrefCall(createThisMemberAccess(name));
5784
5898
  else replacement = createUnrefCall(createMemberAccess(createThisMemberAccess(WEVU_SLOT_OWNER_PROXY_KEY), name));
5785
- else replacement = createUnrefCall(createIdentifierAccessWithPropsFallback(name));
5899
+ else {
5900
+ const propsAlias = context.propsAliases?.[name];
5901
+ replacement = createUnrefCall(propsAlias ? createAliasedPropsAccess(name, propsAlias) : createIdentifierAccessWithPropsFallback(name, context, options?.runtimePropAccess === "helper"));
5902
+ }
5786
5903
  const parent = path.parentPath;
5787
5904
  if (parent.isObjectProperty() && parent.node.shorthand && parent.node.key === path.node) {
5788
5905
  parent.node.shorthand = false;
5789
5906
  parent.node.value = replacement;
5907
+ path.skip();
5790
5908
  return;
5791
5909
  }
5792
5910
  path.replaceWith(replacement);
5911
+ path.skip();
5793
5912
  } });
5794
5913
  const stmt = ast.program.body[0];
5795
5914
  return (stmt && "expression" in stmt ? stmt.expression : null) || null;
@@ -5825,7 +5944,10 @@ function shouldFallbackToRuntimeBinding(exp) {
5825
5944
  * 将复杂表达式注册为 JS 运行时计算绑定,返回可用于模板 mustache 的绑定引用。
5826
5945
  */
5827
5946
  function registerRuntimeBindingExpression(exp, context, options) {
5828
- const expAst = normalizeJsExpressionWithContext(exp, context, options);
5947
+ const expAst = normalizeJsExpressionWithContext(exp, context, {
5948
+ ...options,
5949
+ runtimePropAccess: "helper"
5950
+ });
5829
5951
  if (!expAst) return null;
5830
5952
  const binding = {
5831
5953
  name: `__wv_bind_${context.classStyleBindings.filter((item) => item.type === "bind").length}`,
@@ -5936,7 +6058,10 @@ function renderClassAttribute(staticClass, dynamicClassExp, context) {
5936
6058
  }
5937
6059
  const jsParts = [];
5938
6060
  if (staticValue) jsParts.push(t.stringLiteral(staticValue));
5939
- const dynamicAst = normalizeJsExpressionWithContext(dynamicClassExp, context, { hint: "class 绑定" });
6061
+ const dynamicAst = normalizeJsExpressionWithContext(dynamicClassExp, context, {
6062
+ hint: "class 绑定",
6063
+ runtimePropAccess: "helper"
6064
+ });
5940
6065
  if (dynamicAst) jsParts.push(dynamicAst);
5941
6066
  const expAst = mergeJsExpressionParts(jsParts);
5942
6067
  const binding = createClassStyleBinding(context, "class", generateExpressionCode(expAst), expAst, staticValue ?? "");
@@ -5965,11 +6090,17 @@ function renderStyleAttribute(staticStyle, dynamicStyleExp, vShowExp, context) {
5965
6090
  const jsParts = [];
5966
6091
  if (staticValue) jsParts.push(t.stringLiteral(staticValue));
5967
6092
  if (dynamicStyleExp) {
5968
- const dynamicAst = normalizeJsExpressionWithContext(dynamicStyleExp, context, { hint: "style 绑定" });
6093
+ const dynamicAst = normalizeJsExpressionWithContext(dynamicStyleExp, context, {
6094
+ hint: "style 绑定",
6095
+ runtimePropAccess: "helper"
6096
+ });
5969
6097
  if (dynamicAst) jsParts.push(dynamicAst);
5970
6098
  }
5971
6099
  if (vShowExp) {
5972
- const showAst = normalizeJsExpressionWithContext(vShowExp, context, { hint: "v-show" });
6100
+ const showAst = normalizeJsExpressionWithContext(vShowExp, context, {
6101
+ hint: "v-show",
6102
+ runtimePropAccess: "helper"
6103
+ });
5973
6104
  if (showAst) jsParts.push(t.conditionalExpression(showAst, t.stringLiteral(""), t.stringLiteral("display: none")));
5974
6105
  }
5975
6106
  const expAst = mergeJsExpressionParts(jsParts);
@@ -6644,6 +6775,7 @@ function buildSlotDeclaration(name, propsExp, children, context, options) {
6644
6775
  props: propsExp ? parseSlotPropsExpression(propsExp, context) : {},
6645
6776
  children,
6646
6777
  implicitDefault: options?.implicitDefault,
6778
+ conditionKind: options?.conditionKind,
6647
6779
  condition: options?.condition
6648
6780
  };
6649
6781
  }
@@ -6737,6 +6869,9 @@ function isRenderableFallbackChild(child) {
6737
6869
  function renderSlotFallback(decl, context, transformNode) {
6738
6870
  const slotAttr = renderSlotNameAttribute(decl.name, context, "slot");
6739
6871
  const wrapCondition = (content) => {
6872
+ if (decl.conditionKind === "else") return context.platform.wrapElse(content);
6873
+ if (decl.conditionKind === "else-if" && decl.condition) return context.platform.wrapElseIf(decl.condition, content, (exp) => renderMustache(exp, context));
6874
+ if (decl.conditionKind === "if" && decl.condition) return context.platform.wrapIf(decl.condition, content, (exp) => renderMustache(exp, context));
6740
6875
  return decl.condition ? context.platform.wrapIf(decl.condition, content, (exp) => renderMustache(exp, context)) : content;
6741
6876
  };
6742
6877
  if (!slotAttr) {
@@ -6844,9 +6979,14 @@ function shouldAugmentPlainSlot(decl, context, ownerNode) {
6844
6979
  return hasDirectComponentSlotChild(decl.children, context);
6845
6980
  }
6846
6981
  function resolveTemplateSlotCondition(node, context) {
6847
- const ifDirective = node.props.find((prop) => prop.type === NodeTypes.DIRECTIVE && prop.name === "if" && prop.exp?.type === NodeTypes.SIMPLE_EXPRESSION);
6848
- const rawExp = ifDirective?.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? ifDirective.exp.content : "";
6849
- return rawExp ? normalizeWxmlExpressionWithContext(rawExp, context) : void 0;
6982
+ const directive = node.props.find((prop) => prop.type === NodeTypes.DIRECTIVE && (prop.name === "if" || prop.name === "else-if" || prop.name === "else") && (prop.name === "else" || prop.exp?.type === NodeTypes.SIMPLE_EXPRESSION));
6983
+ if (!directive) return {};
6984
+ if (directive.name === "else") return { conditionKind: "else" };
6985
+ const rawExp = directive.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? directive.exp.content : "";
6986
+ return {
6987
+ conditionKind: directive.name === "else-if" ? "else-if" : "if",
6988
+ condition: rawExp ? normalizeWxmlExpressionWithContext(rawExp, context) : void 0
6989
+ };
6850
6990
  }
6851
6991
  function pushSlotNamesAttr(attrs, slotNames, context) {
6852
6992
  if (!slotNames.length) return;
@@ -6905,7 +7045,9 @@ function transformComponentWithSlots(node, context, transformNode, options) {
6905
7045
  if (child.type === NodeTypes.ELEMENT && child.tag === "template") {
6906
7046
  const templateSlot = findSlotDirective(child);
6907
7047
  if (templateSlot) {
6908
- const declaration = buildSlotDeclaration(resolveSlotNameFromDirective(templateSlot), templateSlot.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? templateSlot.exp.content : void 0, child.children, context, { condition: resolveTemplateSlotCondition(child, context) });
7048
+ const slotName = resolveSlotNameFromDirective(templateSlot);
7049
+ const templateSlotCondition = resolveTemplateSlotCondition(child, context);
7050
+ const declaration = buildSlotDeclaration(slotName, templateSlot.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? templateSlot.exp.content : void 0, child.children, context, templateSlotCondition);
6909
7051
  slotDeclarations.push(declaration);
6910
7052
  renderItems.push({
6911
7053
  type: "declaration",
@@ -7000,7 +7142,9 @@ function transformComponentWithSlotsFallback(node, context, transformNode, optio
7000
7142
  if (child.type === NodeTypes.ELEMENT && child.tag === "template") {
7001
7143
  const templateSlot = findSlotDirective(child);
7002
7144
  if (templateSlot) {
7003
- const declaration = buildSlotDeclaration(resolveSlotNameFromDirective(templateSlot), templateSlot.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? templateSlot.exp.content : void 0, child.children, context, { condition: resolveTemplateSlotCondition(child, context) });
7145
+ const slotName = resolveSlotNameFromDirective(templateSlot);
7146
+ const templateSlotCondition = resolveTemplateSlotCondition(child, context);
7147
+ const declaration = buildSlotDeclaration(slotName, templateSlot.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? templateSlot.exp.content : void 0, child.children, context, templateSlotCondition);
7004
7148
  slotDeclarations.push(declaration);
7005
7149
  renderItems.push({
7006
7150
  type: "declaration",
@@ -7370,6 +7514,8 @@ function compileVueTemplateToWxml(template, filename, options) {
7370
7514
  filename,
7371
7515
  warnings,
7372
7516
  platform: options?.platform ?? getMiniProgramTemplatePlatform(),
7517
+ propsAliases: options?.propsAliases,
7518
+ propsDerivedKeys: options?.propsDerivedKeys,
7373
7519
  htmlTagToWxmlMap,
7374
7520
  htmlTagToWxmlTagClass: options?.htmlTagToWxmlTagClass ?? true,
7375
7521
  scopedSlotsCompiler: options?.scopedSlotsCompiler ?? "auto",
@@ -8228,18 +8374,91 @@ function composeSourceMaps(transformedMap, originalMap) {
8228
8374
  //#endregion
8229
8375
  //#region src/plugins/vue/transform/compileVueFile/script.ts
8230
8376
  const TYPE_ONLY_DEFINE_PROPS_RE = /\bdefineProps\s*</;
8231
- async function compileScriptPhase(descriptor, descriptorForCompile, filename, options, _autoUsingComponents, templateCompiled, isAppFile, componentSourceInfo) {
8377
+ function resolveScriptSetupPropsAliases(bindings) {
8378
+ const aliases = bindings?.__propsAliases;
8379
+ if (!aliases || typeof aliases !== "object") return;
8380
+ const resolved = {};
8381
+ for (const [alias, propName] of Object.entries(aliases)) if (typeof propName === "string" && propName.length > 0) resolved[alias] = propName;
8382
+ return Object.keys(resolved).length ? resolved : void 0;
8383
+ }
8384
+ function resolveScriptSetupPropsDerivedKeys(bindings) {
8385
+ const keys = /* @__PURE__ */ new Set();
8386
+ for (const [key, bindingType] of Object.entries(bindings ?? {})) {
8387
+ if (key.startsWith("__") || bindingType !== "props" && bindingType !== "props-aliased") continue;
8388
+ keys.add(key);
8389
+ }
8390
+ return keys.size ? [...keys] : void 0;
8391
+ }
8392
+ function collectScriptSetupReturnInfo(scriptCode) {
8393
+ const keys = /* @__PURE__ */ new Set();
8394
+ const propsObjectAliases = new Set(["__props"]);
8395
+ const destructuredPropsKeys = /* @__PURE__ */ new Set();
8396
+ try {
8397
+ traverse(parseJsLike(scriptCode), {
8398
+ VariableDeclarator(path) {
8399
+ const init = path.node.init;
8400
+ if (t.isIdentifier(path.node.id) && t.isIdentifier(init) && propsObjectAliases.has(init.name)) {
8401
+ propsObjectAliases.add(path.node.id.name);
8402
+ return;
8403
+ }
8404
+ if (!t.isObjectPattern(path.node.id) || !t.isIdentifier(init) || !propsObjectAliases.has(init.name)) return;
8405
+ for (const property of path.node.id.properties) {
8406
+ if (!t.isObjectProperty(property)) continue;
8407
+ if (t.isIdentifier(property.value)) destructuredPropsKeys.add(property.value.name);
8408
+ else if (t.isAssignmentPattern(property.value) && t.isIdentifier(property.value.left)) destructuredPropsKeys.add(property.value.left.name);
8409
+ }
8410
+ },
8411
+ ObjectProperty(path) {
8412
+ const objectPath = path.parentPath;
8413
+ if (!objectPath.isObjectExpression() || !objectPath.parentPath.isVariableDeclarator() || !t.isIdentifier(objectPath.parentPath.node.id, { name: "__returned__" })) return;
8414
+ const prop = path.node;
8415
+ if (prop.computed) return;
8416
+ if (t.isIdentifier(prop.key)) keys.add(prop.key.name);
8417
+ else if (t.isStringLiteral(prop.key)) keys.add(prop.key.value);
8418
+ }
8419
+ });
8420
+ } catch {
8421
+ return {
8422
+ returnedKeys: keys,
8423
+ destructuredPropsKeys
8424
+ };
8425
+ }
8426
+ return {
8427
+ returnedKeys: keys,
8428
+ destructuredPropsKeys
8429
+ };
8430
+ }
8431
+ function resolveEffectivePropsDerivedKeys(bindings, scriptCode) {
8432
+ const directKeys = resolveScriptSetupPropsDerivedKeys(bindings) ?? [];
8433
+ const { returnedKeys, destructuredPropsKeys } = collectScriptSetupReturnInfo(scriptCode);
8434
+ const aliases = resolveScriptSetupPropsAliases(bindings) ?? {};
8435
+ const propsKeys = new Set(Object.entries(bindings ?? {}).filter(([key, bindingType]) => key && !key.startsWith("__") && bindingType === "props").map(([key]) => key));
8436
+ const keys = /* @__PURE__ */ new Set();
8437
+ for (const key of directKeys) keys.add(key);
8438
+ for (const key of destructuredPropsKeys) keys.add(key);
8439
+ for (const [alias, propName] of Object.entries(aliases)) {
8440
+ if (!returnedKeys.has(alias)) keys.add(alias);
8441
+ if (propsKeys.has(propName) && !returnedKeys.has(propName)) keys.add(propName);
8442
+ }
8443
+ for (const key of propsKeys) if (!returnedKeys.has(key)) keys.add(key);
8444
+ return keys.size ? [...keys] : void 0;
8445
+ }
8446
+ async function compileScriptPhase(descriptor, descriptorForCompile, filename, options, _autoUsingComponents, templateCompiled, isAppFile, componentSourceInfo, precompiledScript) {
8232
8447
  const autoUsingComponentsMap = { ...componentSourceInfo?.autoUsingComponentsMap ?? {} };
8233
8448
  const autoComponentMeta = { ...componentSourceInfo?.autoComponentMeta ?? {} };
8234
8449
  const relaxStructuredTypeOnlyProps = Boolean(descriptor.scriptSetup?.content && TYPE_ONLY_DEFINE_PROPS_RE.test(descriptor.scriptSetup.content));
8235
8450
  let scriptCode;
8236
8451
  let scriptMap = null;
8452
+ let propsAliases = options?.template?.propsAliases;
8453
+ let propsDerivedKeys;
8237
8454
  if (descriptor.script || descriptor.scriptSetup) {
8238
- const scriptCompiled = compileScript(descriptorForCompile, {
8455
+ const scriptCompiled = precompiledScript ?? compileScript(descriptorForCompile, {
8239
8456
  id: filename,
8240
8457
  isProd: false
8241
8458
  });
8459
+ propsAliases ??= resolveScriptSetupPropsAliases(scriptCompiled.bindings);
8242
8460
  scriptCode = scriptCompiled.content;
8461
+ propsDerivedKeys = resolveEffectivePropsDerivedKeys(scriptCompiled.bindings, scriptCode);
8243
8462
  scriptMap = scriptCompiled.map && typeof scriptCompiled.map === "object" ? scriptCompiled.map : null;
8244
8463
  if (scriptCode.includes("defineAppJson") || scriptCode.includes("definePageJson") || scriptCode.includes("defineComponentJson")) scriptCode = stripJsonMacroCallsFromCode(scriptCode, filename);
8245
8464
  if (!isAppFile && !scriptCode.includes("export default")) scriptCode += "\nexport default {}";
@@ -8259,6 +8478,8 @@ async function compileScriptPhase(descriptor, descriptorForCompile, filename, op
8259
8478
  layoutHosts: templateCompiled?.layoutHosts,
8260
8479
  inlineExpressions: templateCompiled?.inlineExpressions,
8261
8480
  functionPropPaths: templateCompiled?.functionPropPaths,
8481
+ propsAliases,
8482
+ propsDerivedKeys,
8262
8483
  relaxStructuredTypeOnlyProps
8263
8484
  });
8264
8485
  return {
@@ -8343,10 +8564,22 @@ async function compileVueFile(source, filename, options) {
8343
8564
  autoUsingComponents,
8344
8565
  autoImportTags
8345
8566
  });
8567
+ const scriptCompiled = parsed.descriptor.script || parsed.descriptor.scriptSetup ? compileScript(parsed.descriptorForCompile, {
8568
+ id: filename,
8569
+ isProd: false
8570
+ }) : void 0;
8571
+ const propsAliases = scriptCompiled ? resolveScriptSetupPropsAliases(scriptCompiled.bindings) : void 0;
8572
+ const propsDerivedKeys = scriptCompiled ? resolveEffectivePropsDerivedKeys(scriptCompiled.bindings, scriptCompiled.content) : void 0;
8346
8573
  const baseTemplateOptions = parsed.isAppFile ? {
8347
8574
  ...options?.template,
8575
+ propsAliases,
8576
+ propsDerivedKeys,
8348
8577
  scopedSlotsRequireProps: true
8349
- } : options?.template;
8578
+ } : {
8579
+ ...options?.template,
8580
+ propsAliases,
8581
+ propsDerivedKeys
8582
+ };
8350
8583
  const templateOptions = componentSourceInfo.wevuComponentTags.size ? {
8351
8584
  ...baseTemplateOptions,
8352
8585
  wevuComponentTags: componentSourceInfo.wevuComponentTags
@@ -8355,7 +8588,7 @@ async function compileVueFile(source, filename, options) {
8355
8588
  wevuComponentTags: []
8356
8589
  };
8357
8590
  const templateCompiled = compileTemplatePhase(parsed.descriptor, filename, templateOptions, result);
8358
- const scriptPhase = await compileScriptPhase(parsed.descriptor, parsed.descriptorForCompile, filename, options, autoUsingComponents, templateCompiled, parsed.isAppFile, componentSourceInfo);
8591
+ const scriptPhase = await compileScriptPhase(parsed.descriptor, parsed.descriptorForCompile, filename, options, autoUsingComponents, templateCompiled, parsed.isAppFile, componentSourceInfo, scriptCompiled);
8359
8592
  result.script = scriptPhase.script;
8360
8593
  result.scriptMap = scriptPhase.scriptMap;
8361
8594
  compileStylePhase(parsed.descriptor, filename, result);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wevu/compiler",
3
3
  "type": "module",
4
- "version": "6.16.19",
4
+ "version": "6.16.21",
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.8",
53
+ "@weapp-core/constants": "0.1.9",
54
54
  "@weapp-core/shared": "3.0.4",
55
- "@weapp-vite/ast": "6.16.19",
55
+ "@weapp-vite/ast": "6.16.21",
56
56
  "rolldown-require": "2.0.17"
57
57
  },
58
58
  "publishConfig": {