@wevu/compiler 6.16.20 → 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 +5 -0
- package/dist/index.mjs +143 -27
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -280,6 +280,7 @@ interface TemplateCompileOptions {
|
|
|
280
280
|
* Vue `<script setup>` props 解构重命名映射,key 为模板中使用的本地别名,value 为原始 prop 名。
|
|
281
281
|
*/
|
|
282
282
|
propsAliases?: Record<string, string>;
|
|
283
|
+
propsDerivedKeys?: string[];
|
|
283
284
|
htmlTagToWxml?: boolean | Record<string, string>;
|
|
284
285
|
htmlTagToWxmlTagClass?: boolean;
|
|
285
286
|
scopedSlotsCompiler?: ScopedSlotsCompilerMode;
|
|
@@ -713,6 +714,10 @@ interface TransformScriptOptions {
|
|
|
713
714
|
* Vue `<script setup>` props 解构重命名映射,key 为模板中使用的本地别名,value 为原始 prop 名。
|
|
714
715
|
*/
|
|
715
716
|
propsAliases?: Record<string, string>;
|
|
717
|
+
/**
|
|
718
|
+
* Vue `<script setup>` 中直接来自 props 的绑定名,用于运行时区分 props-derived binding 与普通 setup state。
|
|
719
|
+
*/
|
|
720
|
+
propsDerivedKeys?: string[];
|
|
716
721
|
/**
|
|
717
722
|
* 对 `<script setup>` 类型声明生成的结构化 props(如 Array/Object)放宽小程序运行时类型约束,
|
|
718
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,9 @@ 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
|
+
}
|
|
2244
2247
|
function createDataPropsFallbackExpression(fallback) {
|
|
2245
2248
|
const propsObject = t.memberExpression(t.thisExpression(), t.identifier(WEVU_PROPS_KEY));
|
|
2246
2249
|
const propsAccess = t.memberExpression(propsObject, t.identifier("data"));
|
|
@@ -2283,7 +2286,7 @@ function buildNormalizedExpression(binding, helpers) {
|
|
|
2283
2286
|
const errorFallback = binding.errorFallback ?? "";
|
|
2284
2287
|
const exp = binding.expAst ? rewriteDataAccessExpression(binding.expAst, helpers) : t.stringLiteral("");
|
|
2285
2288
|
const normalizedCall = t.callExpression(t.cloneNode(normalizeHelper), [exp]);
|
|
2286
|
-
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)])), []);
|
|
2287
2290
|
}
|
|
2288
2291
|
function buildArrayMapExpression(binding, forStack, level, listId, helpers) {
|
|
2289
2292
|
const itemParam = createValidIdentifier(forStack[level].item, `__wv_item_${level}`);
|
|
@@ -2361,7 +2364,7 @@ function applyPropsAliasesToExpression(expression, propsAliases) {
|
|
|
2361
2364
|
if (!path.isReferencedIdentifier()) return;
|
|
2362
2365
|
const propName = propsAliases[path.node.name];
|
|
2363
2366
|
if (!propName || path.scope.hasBinding(path.node.name)) return;
|
|
2364
|
-
const replacement = createMemberAccess$2(t.identifier("
|
|
2367
|
+
const replacement = createMemberAccess$2(t.memberExpression(t.thisExpression(), t.identifier("__wevuProps")), propName);
|
|
2365
2368
|
const parent = path.parentPath;
|
|
2366
2369
|
if (parent.isObjectProperty() && parent.node.shorthand && parent.node.key === path.node) {
|
|
2367
2370
|
parent.node.shorthand = false;
|
|
@@ -2411,7 +2414,8 @@ function injectClassStyleComputed(optionsObject, bindings, propsAliases, warn) {
|
|
|
2411
2414
|
const entries = buildClassStyleComputedEntries(bindings, {
|
|
2412
2415
|
normalizeClass: t.identifier("__wevuNormalizeClass"),
|
|
2413
2416
|
normalizeStyle: t.identifier("__wevuNormalizeStyle"),
|
|
2414
|
-
unref: t.identifier("__wevuUnref")
|
|
2417
|
+
unref: t.identifier("__wevuUnref"),
|
|
2418
|
+
resolvePropValue: t.identifier("__wevuResolvePropValue")
|
|
2415
2419
|
}, propsAliases);
|
|
2416
2420
|
if (!entries.length) return false;
|
|
2417
2421
|
const computedProp = getObjectPropertyByKey$1(optionsObject, "computed");
|
|
@@ -2434,6 +2438,7 @@ function ensureClassStyleRuntimeImports(program) {
|
|
|
2434
2438
|
ensureRuntimeImport(program, "normalizeClass", "__wevuNormalizeClass");
|
|
2435
2439
|
ensureRuntimeImport(program, "normalizeStyle", "__wevuNormalizeStyle");
|
|
2436
2440
|
ensureRuntimeImport(program, "unref", "__wevuUnref");
|
|
2441
|
+
ensureRuntimeImport(program, "resolvePropValue", "__wevuResolvePropValue");
|
|
2437
2442
|
}
|
|
2438
2443
|
//#endregion
|
|
2439
2444
|
//#region src/plugins/vue/transform/transformScript/rewrite/defaults.ts
|
|
@@ -2733,6 +2738,24 @@ function injectLayoutHosts(optionsObject, bindings, warn) {
|
|
|
2733
2738
|
return false;
|
|
2734
2739
|
}
|
|
2735
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
|
|
2736
2759
|
//#region src/plugins/vue/transform/transformScript/rewrite/setupInitialData.ts
|
|
2737
2760
|
function unwrapExpression(node) {
|
|
2738
2761
|
if (t.isTSAsExpression(node) || t.isTSSatisfiesExpression(node) || t.isTSNonNullExpression(node) || t.isTypeCastExpression(node) || t.isParenthesizedExpression(node)) return unwrapExpression(node.expression);
|
|
@@ -2994,6 +3017,10 @@ function rewriteDefaultExport(ast, state, options, enabledPageFeatures, serializ
|
|
|
2994
3017
|
}) || transformed;
|
|
2995
3018
|
if (componentOptionsObject) transformed = injectSetupInitialData(componentOptionsObject) || transformed;
|
|
2996
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
|
+
}
|
|
2997
3024
|
const classStyleBindings = options?.classStyleBindings ?? [];
|
|
2998
3025
|
if (classStyleBindings.length) if (componentOptionsObject) {
|
|
2999
3026
|
ensureClassStyleRuntimeImports(ast.program);
|
|
@@ -5413,8 +5440,7 @@ function rewriteScopedSlotExpression(exp, context) {
|
|
|
5413
5440
|
function rewriteForAliasExpression(exp, context) {
|
|
5414
5441
|
const normalized = normalizeWxmlExpression(exp);
|
|
5415
5442
|
const forAliases = collectForAliasMapping$2(context);
|
|
5416
|
-
|
|
5417
|
-
if (!Object.keys(forAliases).length && !Object.keys(propsAliases).length) return normalized;
|
|
5443
|
+
if (!Object.keys(forAliases).length) return normalized;
|
|
5418
5444
|
const parsed = parseBabelExpressionFile(normalized);
|
|
5419
5445
|
if (!parsed) return normalized;
|
|
5420
5446
|
const { ast } = parsed;
|
|
@@ -5425,10 +5451,7 @@ function rewriteForAliasExpression(exp, context) {
|
|
|
5425
5451
|
if (hasOwn(forAliases, name)) {
|
|
5426
5452
|
const aliasExp = parseBabelExpression(forAliases[name]);
|
|
5427
5453
|
if (aliasExp) replaceIdentifierWithExpression(path, t.cloneNode(aliasExp, true));
|
|
5428
|
-
return;
|
|
5429
5454
|
}
|
|
5430
|
-
const propName = propsAliases[name];
|
|
5431
|
-
if (propName && t.isValidIdentifier(propName)) replaceIdentifierWithExpression(path, t.identifier(propName));
|
|
5432
5455
|
} });
|
|
5433
5456
|
const stmt = ast.program.body[0];
|
|
5434
5457
|
const updatedExpression = stmt && "expression" in stmt ? stmt.expression : null;
|
|
@@ -5745,6 +5768,7 @@ const JS_RUNTIME_GLOBALS = new Set([
|
|
|
5745
5768
|
"arguments",
|
|
5746
5769
|
"globalThis",
|
|
5747
5770
|
"__wevuUnref",
|
|
5771
|
+
"__wevuResolvePropValue",
|
|
5748
5772
|
"getApp",
|
|
5749
5773
|
"getCurrentPages",
|
|
5750
5774
|
...getMiniProgramRuntimeGlobalKeys()
|
|
@@ -5776,6 +5800,15 @@ function createUnrefCall(exp) {
|
|
|
5776
5800
|
function createHasOwnPropertyCall(target, key) {
|
|
5777
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)]);
|
|
5778
5802
|
}
|
|
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
|
+
}
|
|
5779
5812
|
function createPropsKeyAccessFallback(name, fallback) {
|
|
5780
5813
|
const propsObject = createThisMemberAccess(WEVU_PROPS_KEY);
|
|
5781
5814
|
const propsAccess = createMemberAccess(propsObject, name);
|
|
@@ -5783,7 +5816,14 @@ function createPropsKeyAccessFallback(name, fallback) {
|
|
|
5783
5816
|
const hasPropsValue = t.logicalExpression("&&", hasPropsObject, t.logicalExpression("||", t.binaryExpression("!==", propsAccess, t.identifier("undefined")), createHasOwnPropertyCall(propsObject, name)));
|
|
5784
5817
|
return t.conditionalExpression(hasPropsValue, propsAccess, fallback);
|
|
5785
5818
|
}
|
|
5786
|
-
function createIdentifierAccessWithPropsFallback(name) {
|
|
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
|
+
}
|
|
5787
5827
|
if (name === "props") {
|
|
5788
5828
|
const propsObject = createThisMemberAccess(WEVU_PROPS_KEY);
|
|
5789
5829
|
return t.conditionalExpression(t.binaryExpression("!=", propsObject, t.nullLiteral()), propsObject, createThisMemberAccess("props"));
|
|
@@ -5800,20 +5840,13 @@ function createIdentifierAccessWithPropsFallback(name) {
|
|
|
5800
5840
|
const hasStateObject = t.binaryExpression("!=", stateObject, t.nullLiteral());
|
|
5801
5841
|
const hasStateKey = createHasOwnPropertyCall(stateObject, name);
|
|
5802
5842
|
const hasThisMember = t.binaryExpression("in", t.stringLiteral(name), t.thisExpression());
|
|
5803
|
-
const
|
|
5804
|
-
const
|
|
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));
|
|
5805
5846
|
return t.conditionalExpression(shouldUseStateAccess, thisAccess, t.conditionalExpression(shouldUsePropsAccess, propsAccess, thisAccess));
|
|
5806
5847
|
}
|
|
5807
5848
|
function createAliasedPropsAccess(name, propName) {
|
|
5808
|
-
|
|
5809
|
-
const propsAccess = createMemberAccess(propsObject, propName);
|
|
5810
|
-
const hasPropsObject = t.binaryExpression("!=", propsObject, t.nullLiteral());
|
|
5811
|
-
const hasDefinedPropsValue = t.binaryExpression("!==", propsAccess, t.identifier("undefined"));
|
|
5812
|
-
const hasPropsKey = createHasOwnPropertyCall(propsObject, propName);
|
|
5813
|
-
const hasUsablePropsValue = t.logicalExpression("&&", hasPropsObject, t.logicalExpression("||", hasDefinedPropsValue, hasPropsKey));
|
|
5814
|
-
const thisAccess = createThisMemberAccess(name);
|
|
5815
|
-
const hasThisMember = t.binaryExpression("in", t.stringLiteral(name), t.thisExpression());
|
|
5816
|
-
return t.conditionalExpression(t.logicalExpression("&&", hasUsablePropsValue, t.unaryExpression("!", hasThisMember)), propsAccess, thisAccess);
|
|
5849
|
+
return createResolvePropValueCall(propName, createThisMemberAccess(name), true);
|
|
5817
5850
|
}
|
|
5818
5851
|
function collectForAliasMapping(context) {
|
|
5819
5852
|
const mapping = {};
|
|
@@ -5865,15 +5898,17 @@ function normalizeJsExpressionWithContext(exp, context, options) {
|
|
|
5865
5898
|
else replacement = createUnrefCall(createMemberAccess(createThisMemberAccess(WEVU_SLOT_OWNER_PROXY_KEY), name));
|
|
5866
5899
|
else {
|
|
5867
5900
|
const propsAlias = context.propsAliases?.[name];
|
|
5868
|
-
replacement = createUnrefCall(propsAlias ? createAliasedPropsAccess(name, propsAlias) : createIdentifierAccessWithPropsFallback(name));
|
|
5901
|
+
replacement = createUnrefCall(propsAlias ? createAliasedPropsAccess(name, propsAlias) : createIdentifierAccessWithPropsFallback(name, context, options?.runtimePropAccess === "helper"));
|
|
5869
5902
|
}
|
|
5870
5903
|
const parent = path.parentPath;
|
|
5871
5904
|
if (parent.isObjectProperty() && parent.node.shorthand && parent.node.key === path.node) {
|
|
5872
5905
|
parent.node.shorthand = false;
|
|
5873
5906
|
parent.node.value = replacement;
|
|
5907
|
+
path.skip();
|
|
5874
5908
|
return;
|
|
5875
5909
|
}
|
|
5876
5910
|
path.replaceWith(replacement);
|
|
5911
|
+
path.skip();
|
|
5877
5912
|
} });
|
|
5878
5913
|
const stmt = ast.program.body[0];
|
|
5879
5914
|
return (stmt && "expression" in stmt ? stmt.expression : null) || null;
|
|
@@ -5909,7 +5944,10 @@ function shouldFallbackToRuntimeBinding(exp) {
|
|
|
5909
5944
|
* 将复杂表达式注册为 JS 运行时计算绑定,返回可用于模板 mustache 的绑定引用。
|
|
5910
5945
|
*/
|
|
5911
5946
|
function registerRuntimeBindingExpression(exp, context, options) {
|
|
5912
|
-
const expAst = normalizeJsExpressionWithContext(exp, context,
|
|
5947
|
+
const expAst = normalizeJsExpressionWithContext(exp, context, {
|
|
5948
|
+
...options,
|
|
5949
|
+
runtimePropAccess: "helper"
|
|
5950
|
+
});
|
|
5913
5951
|
if (!expAst) return null;
|
|
5914
5952
|
const binding = {
|
|
5915
5953
|
name: `__wv_bind_${context.classStyleBindings.filter((item) => item.type === "bind").length}`,
|
|
@@ -6020,7 +6058,10 @@ function renderClassAttribute(staticClass, dynamicClassExp, context) {
|
|
|
6020
6058
|
}
|
|
6021
6059
|
const jsParts = [];
|
|
6022
6060
|
if (staticValue) jsParts.push(t.stringLiteral(staticValue));
|
|
6023
|
-
const dynamicAst = normalizeJsExpressionWithContext(dynamicClassExp, context, {
|
|
6061
|
+
const dynamicAst = normalizeJsExpressionWithContext(dynamicClassExp, context, {
|
|
6062
|
+
hint: "class 绑定",
|
|
6063
|
+
runtimePropAccess: "helper"
|
|
6064
|
+
});
|
|
6024
6065
|
if (dynamicAst) jsParts.push(dynamicAst);
|
|
6025
6066
|
const expAst = mergeJsExpressionParts(jsParts);
|
|
6026
6067
|
const binding = createClassStyleBinding(context, "class", generateExpressionCode(expAst), expAst, staticValue ?? "");
|
|
@@ -6049,11 +6090,17 @@ function renderStyleAttribute(staticStyle, dynamicStyleExp, vShowExp, context) {
|
|
|
6049
6090
|
const jsParts = [];
|
|
6050
6091
|
if (staticValue) jsParts.push(t.stringLiteral(staticValue));
|
|
6051
6092
|
if (dynamicStyleExp) {
|
|
6052
|
-
const dynamicAst = normalizeJsExpressionWithContext(dynamicStyleExp, context, {
|
|
6093
|
+
const dynamicAst = normalizeJsExpressionWithContext(dynamicStyleExp, context, {
|
|
6094
|
+
hint: "style 绑定",
|
|
6095
|
+
runtimePropAccess: "helper"
|
|
6096
|
+
});
|
|
6053
6097
|
if (dynamicAst) jsParts.push(dynamicAst);
|
|
6054
6098
|
}
|
|
6055
6099
|
if (vShowExp) {
|
|
6056
|
-
const showAst = normalizeJsExpressionWithContext(vShowExp, context, {
|
|
6100
|
+
const showAst = normalizeJsExpressionWithContext(vShowExp, context, {
|
|
6101
|
+
hint: "v-show",
|
|
6102
|
+
runtimePropAccess: "helper"
|
|
6103
|
+
});
|
|
6057
6104
|
if (showAst) jsParts.push(t.conditionalExpression(showAst, t.stringLiteral(""), t.stringLiteral("display: none")));
|
|
6058
6105
|
}
|
|
6059
6106
|
const expAst = mergeJsExpressionParts(jsParts);
|
|
@@ -7468,6 +7515,7 @@ function compileVueTemplateToWxml(template, filename, options) {
|
|
|
7468
7515
|
warnings,
|
|
7469
7516
|
platform: options?.platform ?? getMiniProgramTemplatePlatform(),
|
|
7470
7517
|
propsAliases: options?.propsAliases,
|
|
7518
|
+
propsDerivedKeys: options?.propsDerivedKeys,
|
|
7471
7519
|
htmlTagToWxmlMap,
|
|
7472
7520
|
htmlTagToWxmlTagClass: options?.htmlTagToWxmlTagClass ?? true,
|
|
7473
7521
|
scopedSlotsCompiler: options?.scopedSlotsCompiler ?? "auto",
|
|
@@ -8333,6 +8381,68 @@ function resolveScriptSetupPropsAliases(bindings) {
|
|
|
8333
8381
|
for (const [alias, propName] of Object.entries(aliases)) if (typeof propName === "string" && propName.length > 0) resolved[alias] = propName;
|
|
8334
8382
|
return Object.keys(resolved).length ? resolved : void 0;
|
|
8335
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
|
+
}
|
|
8336
8446
|
async function compileScriptPhase(descriptor, descriptorForCompile, filename, options, _autoUsingComponents, templateCompiled, isAppFile, componentSourceInfo, precompiledScript) {
|
|
8337
8447
|
const autoUsingComponentsMap = { ...componentSourceInfo?.autoUsingComponentsMap ?? {} };
|
|
8338
8448
|
const autoComponentMeta = { ...componentSourceInfo?.autoComponentMeta ?? {} };
|
|
@@ -8340,6 +8450,7 @@ async function compileScriptPhase(descriptor, descriptorForCompile, filename, op
|
|
|
8340
8450
|
let scriptCode;
|
|
8341
8451
|
let scriptMap = null;
|
|
8342
8452
|
let propsAliases = options?.template?.propsAliases;
|
|
8453
|
+
let propsDerivedKeys;
|
|
8343
8454
|
if (descriptor.script || descriptor.scriptSetup) {
|
|
8344
8455
|
const scriptCompiled = precompiledScript ?? compileScript(descriptorForCompile, {
|
|
8345
8456
|
id: filename,
|
|
@@ -8347,6 +8458,7 @@ async function compileScriptPhase(descriptor, descriptorForCompile, filename, op
|
|
|
8347
8458
|
});
|
|
8348
8459
|
propsAliases ??= resolveScriptSetupPropsAliases(scriptCompiled.bindings);
|
|
8349
8460
|
scriptCode = scriptCompiled.content;
|
|
8461
|
+
propsDerivedKeys = resolveEffectivePropsDerivedKeys(scriptCompiled.bindings, scriptCode);
|
|
8350
8462
|
scriptMap = scriptCompiled.map && typeof scriptCompiled.map === "object" ? scriptCompiled.map : null;
|
|
8351
8463
|
if (scriptCode.includes("defineAppJson") || scriptCode.includes("definePageJson") || scriptCode.includes("defineComponentJson")) scriptCode = stripJsonMacroCallsFromCode(scriptCode, filename);
|
|
8352
8464
|
if (!isAppFile && !scriptCode.includes("export default")) scriptCode += "\nexport default {}";
|
|
@@ -8367,6 +8479,7 @@ async function compileScriptPhase(descriptor, descriptorForCompile, filename, op
|
|
|
8367
8479
|
inlineExpressions: templateCompiled?.inlineExpressions,
|
|
8368
8480
|
functionPropPaths: templateCompiled?.functionPropPaths,
|
|
8369
8481
|
propsAliases,
|
|
8482
|
+
propsDerivedKeys,
|
|
8370
8483
|
relaxStructuredTypeOnlyProps
|
|
8371
8484
|
});
|
|
8372
8485
|
return {
|
|
@@ -8456,13 +8569,16 @@ async function compileVueFile(source, filename, options) {
|
|
|
8456
8569
|
isProd: false
|
|
8457
8570
|
}) : void 0;
|
|
8458
8571
|
const propsAliases = scriptCompiled ? resolveScriptSetupPropsAliases(scriptCompiled.bindings) : void 0;
|
|
8572
|
+
const propsDerivedKeys = scriptCompiled ? resolveEffectivePropsDerivedKeys(scriptCompiled.bindings, scriptCompiled.content) : void 0;
|
|
8459
8573
|
const baseTemplateOptions = parsed.isAppFile ? {
|
|
8460
8574
|
...options?.template,
|
|
8461
8575
|
propsAliases,
|
|
8576
|
+
propsDerivedKeys,
|
|
8462
8577
|
scopedSlotsRequireProps: true
|
|
8463
8578
|
} : {
|
|
8464
8579
|
...options?.template,
|
|
8465
|
-
propsAliases
|
|
8580
|
+
propsAliases,
|
|
8581
|
+
propsDerivedKeys
|
|
8466
8582
|
};
|
|
8467
8583
|
const templateOptions = componentSourceInfo.wevuComponentTags.size ? {
|
|
8468
8584
|
...baseTemplateOptions,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wevu/compiler",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "6.16.
|
|
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.
|
|
53
|
+
"@weapp-core/constants": "0.1.9",
|
|
54
54
|
"@weapp-core/shared": "3.0.4",
|
|
55
|
-
"@weapp-vite/ast": "6.16.
|
|
55
|
+
"@weapp-vite/ast": "6.16.21",
|
|
56
56
|
"rolldown-require": "2.0.17"
|
|
57
57
|
},
|
|
58
58
|
"publishConfig": {
|