@wevu/compiler 6.10.1 → 6.11.1
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 +196 -59
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -625,6 +625,11 @@ interface TransformScriptOptions {
|
|
|
625
625
|
* 内联表达式元数据(用于事件处理)
|
|
626
626
|
*/
|
|
627
627
|
inlineExpressions?: InlineExpressionAsset[];
|
|
628
|
+
/**
|
|
629
|
+
* 对 `<script setup>` 类型声明生成的结构化 props(如 Array/Object)放宽小程序运行时类型约束,
|
|
630
|
+
* 以避免小程序属性校验对复杂表达式/代理值产生误报。
|
|
631
|
+
*/
|
|
632
|
+
relaxStructuredTypeOnlyProps?: boolean;
|
|
628
633
|
}
|
|
629
634
|
//#endregion
|
|
630
635
|
//#region src/plugins/vue/transform/transformScript/index.d.ts
|
package/dist/index.mjs
CHANGED
|
@@ -16,6 +16,7 @@ import { LRUCache } from "lru-cache";
|
|
|
16
16
|
import { compileScript, parse } from "vue/compiler-sfc";
|
|
17
17
|
import { fileURLToPath } from "node:url";
|
|
18
18
|
import { parse as parse$1 } from "comment-json";
|
|
19
|
+
import vm from "node:vm";
|
|
19
20
|
//#region src/auto-import-components/builtin.auto.ts
|
|
20
21
|
const components = [
|
|
21
22
|
"wxs",
|
|
@@ -1901,6 +1902,39 @@ function createImportVisitors(program, state) {
|
|
|
1901
1902
|
}
|
|
1902
1903
|
} };
|
|
1903
1904
|
}
|
|
1905
|
+
function isPlainRecord(value) {
|
|
1906
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
1907
|
+
const proto = Object.getPrototypeOf(value);
|
|
1908
|
+
return proto === null || proto === Object.prototype;
|
|
1909
|
+
}
|
|
1910
|
+
function isStaticObjectKeyMatch(key, expected) {
|
|
1911
|
+
if (t.isIdentifier(key)) return key.name === expected;
|
|
1912
|
+
if (t.isStringLiteral(key)) return key.value === expected;
|
|
1913
|
+
return false;
|
|
1914
|
+
}
|
|
1915
|
+
function getObjectPropertyByKey$1(node, key) {
|
|
1916
|
+
for (const prop of node.properties) {
|
|
1917
|
+
if (!t.isObjectProperty(prop) || prop.computed) continue;
|
|
1918
|
+
if (isStaticObjectKeyMatch(prop.key, key)) return prop;
|
|
1919
|
+
}
|
|
1920
|
+
return null;
|
|
1921
|
+
}
|
|
1922
|
+
function createStaticObjectKey$1(key) {
|
|
1923
|
+
return t.isValidIdentifier(key) ? t.identifier(key) : t.stringLiteral(key);
|
|
1924
|
+
}
|
|
1925
|
+
//#endregion
|
|
1926
|
+
//#region src/plugins/vue/transform/transformScript/macros/pageMeta.ts
|
|
1927
|
+
/**
|
|
1928
|
+
* 移除页面元信息宏,避免其进入运行时脚本。
|
|
1929
|
+
*/
|
|
1930
|
+
function createPageMetaVisitors(state) {
|
|
1931
|
+
return { CallExpression(path) {
|
|
1932
|
+
if (!t.isIdentifier(path.node.callee, { name: "definePageMeta" })) return;
|
|
1933
|
+
if (!path.parentPath?.isExpressionStatement()) return;
|
|
1934
|
+
path.parentPath.remove();
|
|
1935
|
+
state.transformed = true;
|
|
1936
|
+
} };
|
|
1937
|
+
}
|
|
1904
1938
|
//#endregion
|
|
1905
1939
|
//#region src/plugins/vue/transform/transformScript/macros/setupExpose.ts
|
|
1906
1940
|
function createSetupExposeVisitors(state) {
|
|
@@ -2096,14 +2130,23 @@ function createStripTypesVisitors(state) {
|
|
|
2096
2130
|
//#endregion
|
|
2097
2131
|
//#region src/plugins/vue/transform/transformScript/macros/index.ts
|
|
2098
2132
|
function createMacroVisitors(state) {
|
|
2133
|
+
const setupExposeVisitors = createSetupExposeVisitors(state);
|
|
2134
|
+
const stripTypesVisitors = createStripTypesVisitors(state);
|
|
2135
|
+
const pageMetaVisitors = createPageMetaVisitors(state);
|
|
2099
2136
|
return {
|
|
2100
|
-
...
|
|
2101
|
-
...
|
|
2137
|
+
...setupExposeVisitors,
|
|
2138
|
+
...stripTypesVisitors,
|
|
2139
|
+
...pageMetaVisitors,
|
|
2140
|
+
CallExpression(path) {
|
|
2141
|
+
setupExposeVisitors.CallExpression?.(path);
|
|
2142
|
+
stripTypesVisitors.CallExpression?.(path);
|
|
2143
|
+
if (!path.removed) pageMetaVisitors.CallExpression?.(path);
|
|
2144
|
+
}
|
|
2102
2145
|
};
|
|
2103
2146
|
}
|
|
2104
2147
|
//#endregion
|
|
2105
2148
|
//#region src/plugins/vue/transform/classStyleComputedBuilders.ts
|
|
2106
|
-
function createStaticObjectKey
|
|
2149
|
+
function createStaticObjectKey(key) {
|
|
2107
2150
|
return t.isValidIdentifier(key) ? t.identifier(key) : t.stringLiteral(key);
|
|
2108
2151
|
}
|
|
2109
2152
|
function createValidIdentifier(name, fallback) {
|
|
@@ -2189,7 +2232,7 @@ function buildComputedFunctionBody(binding, helpers) {
|
|
|
2189
2232
|
function buildClassStyleComputedEntries(bindings, helpers) {
|
|
2190
2233
|
const entries = [];
|
|
2191
2234
|
for (const binding of bindings) {
|
|
2192
|
-
const key = createStaticObjectKey
|
|
2235
|
+
const key = createStaticObjectKey(binding.name);
|
|
2193
2236
|
const body = buildComputedFunctionBody(binding, helpers);
|
|
2194
2237
|
const fn = t.functionExpression(null, [], body);
|
|
2195
2238
|
entries.push(t.objectProperty(key, fn));
|
|
@@ -2214,28 +2257,6 @@ function buildClassStyleComputedCode(bindings, helpers) {
|
|
|
2214
2257
|
return code;
|
|
2215
2258
|
}
|
|
2216
2259
|
//#endregion
|
|
2217
|
-
//#region src/plugins/vue/transform/transformScript/utils.ts
|
|
2218
|
-
function isPlainRecord(value) {
|
|
2219
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
2220
|
-
const proto = Object.getPrototypeOf(value);
|
|
2221
|
-
return proto === null || proto === Object.prototype;
|
|
2222
|
-
}
|
|
2223
|
-
function isStaticObjectKeyMatch(key, expected) {
|
|
2224
|
-
if (t.isIdentifier(key)) return key.name === expected;
|
|
2225
|
-
if (t.isStringLiteral(key)) return key.value === expected;
|
|
2226
|
-
return false;
|
|
2227
|
-
}
|
|
2228
|
-
function getObjectPropertyByKey$1(node, key) {
|
|
2229
|
-
for (const prop of node.properties) {
|
|
2230
|
-
if (!t.isObjectProperty(prop) || prop.computed) continue;
|
|
2231
|
-
if (isStaticObjectKeyMatch(prop.key, key)) return prop;
|
|
2232
|
-
}
|
|
2233
|
-
return null;
|
|
2234
|
-
}
|
|
2235
|
-
function createStaticObjectKey(key) {
|
|
2236
|
-
return t.isValidIdentifier(key) ? t.identifier(key) : t.stringLiteral(key);
|
|
2237
|
-
}
|
|
2238
|
-
//#endregion
|
|
2239
2260
|
//#region src/plugins/vue/transform/transformScript/rewrite/classStyle.ts
|
|
2240
2261
|
function injectClassStyleComputed(optionsObject, bindings, warn) {
|
|
2241
2262
|
if (!bindings.length) return false;
|
|
@@ -2248,7 +2269,7 @@ function injectClassStyleComputed(optionsObject, bindings, warn) {
|
|
|
2248
2269
|
if (!entries.length) return false;
|
|
2249
2270
|
const computedProp = getObjectPropertyByKey$1(optionsObject, "computed");
|
|
2250
2271
|
if (!computedProp) {
|
|
2251
|
-
optionsObject.properties.splice(0, 0, t.objectProperty(createStaticObjectKey("computed"), t.objectExpression(entries)));
|
|
2272
|
+
optionsObject.properties.splice(0, 0, t.objectProperty(createStaticObjectKey$1("computed"), t.objectExpression(entries)));
|
|
2252
2273
|
return true;
|
|
2253
2274
|
}
|
|
2254
2275
|
if (t.isObjectExpression(computedProp.value)) {
|
|
@@ -2276,7 +2297,7 @@ function mergePlainDefaultsIntoObjectExpression(target, defaults) {
|
|
|
2276
2297
|
const injectedProps = [];
|
|
2277
2298
|
for (const [key, value] of entries) {
|
|
2278
2299
|
if (getObjectPropertyByKey$1(target, key)) continue;
|
|
2279
|
-
injectedProps.push(t.objectProperty(createStaticObjectKey(key), t.valueToNode(value)));
|
|
2300
|
+
injectedProps.push(t.objectProperty(createStaticObjectKey$1(key), t.valueToNode(value)));
|
|
2280
2301
|
}
|
|
2281
2302
|
if (!injectedProps.length) return false;
|
|
2282
2303
|
target.properties.splice(0, 0, ...injectedProps);
|
|
@@ -2287,12 +2308,12 @@ function mergeNestedDefaults(optionsObject, key, defaultsValue) {
|
|
|
2287
2308
|
if (!isPlainRecord(defaultsValue)) {
|
|
2288
2309
|
if (defaultsValue === void 0) return false;
|
|
2289
2310
|
if (getObjectPropertyByKey$1(optionsObject, key)) return false;
|
|
2290
|
-
optionsObject.properties.splice(0, 0, t.objectProperty(createStaticObjectKey(key), t.valueToNode(defaultsValue)));
|
|
2311
|
+
optionsObject.properties.splice(0, 0, t.objectProperty(createStaticObjectKey$1(key), t.valueToNode(defaultsValue)));
|
|
2291
2312
|
return true;
|
|
2292
2313
|
}
|
|
2293
2314
|
const existing = getObjectPropertyByKey$1(optionsObject, key);
|
|
2294
2315
|
if (!existing) {
|
|
2295
|
-
optionsObject.properties.splice(0, 0, t.objectProperty(createStaticObjectKey(key), t.valueToNode(defaultsValue)));
|
|
2316
|
+
optionsObject.properties.splice(0, 0, t.objectProperty(createStaticObjectKey$1(key), t.valueToNode(defaultsValue)));
|
|
2296
2317
|
return true;
|
|
2297
2318
|
}
|
|
2298
2319
|
if (t.isObjectExpression(existing.value)) return mergePlainDefaultsIntoObjectExpression(existing.value, defaultsValue);
|
|
@@ -2314,7 +2335,7 @@ function applyWevuDefaultsToOptionsObject(optionsObject, defaults) {
|
|
|
2314
2335
|
continue;
|
|
2315
2336
|
}
|
|
2316
2337
|
if (value === void 0 || getObjectPropertyByKey$1(optionsObject, key)) continue;
|
|
2317
|
-
optionsObject.properties.splice(0, 0, t.objectProperty(createStaticObjectKey(key), t.valueToNode(value)));
|
|
2338
|
+
optionsObject.properties.splice(0, 0, t.objectProperty(createStaticObjectKey$1(key), t.valueToNode(value)));
|
|
2318
2339
|
changed = true;
|
|
2319
2340
|
}
|
|
2320
2341
|
return changed;
|
|
@@ -2322,17 +2343,17 @@ function applyWevuDefaultsToOptionsObject(optionsObject, defaults) {
|
|
|
2322
2343
|
function ensureNestedOptionValue(optionsObject, key, nestedKey, value) {
|
|
2323
2344
|
const existing = getObjectPropertyByKey$1(optionsObject, key);
|
|
2324
2345
|
if (!existing) {
|
|
2325
|
-
const nested = t.objectExpression([t.objectProperty(createStaticObjectKey(nestedKey), t.valueToNode(value))]);
|
|
2326
|
-
optionsObject.properties.splice(0, 0, t.objectProperty(createStaticObjectKey(key), nested));
|
|
2346
|
+
const nested = t.objectExpression([t.objectProperty(createStaticObjectKey$1(nestedKey), t.valueToNode(value))]);
|
|
2347
|
+
optionsObject.properties.splice(0, 0, t.objectProperty(createStaticObjectKey$1(key), nested));
|
|
2327
2348
|
return true;
|
|
2328
2349
|
}
|
|
2329
2350
|
if (t.isObjectExpression(existing.value)) {
|
|
2330
2351
|
if (getObjectPropertyByKey$1(existing.value, nestedKey)) return false;
|
|
2331
|
-
existing.value.properties.splice(0, 0, t.objectProperty(createStaticObjectKey(nestedKey), t.valueToNode(value)));
|
|
2352
|
+
existing.value.properties.splice(0, 0, t.objectProperty(createStaticObjectKey$1(nestedKey), t.valueToNode(value)));
|
|
2332
2353
|
return true;
|
|
2333
2354
|
}
|
|
2334
2355
|
if (t.isIdentifier(existing.value) || t.isMemberExpression(existing.value)) {
|
|
2335
|
-
existing.value = t.objectExpression([t.objectProperty(createStaticObjectKey(nestedKey), t.valueToNode(value)), t.spreadElement(t.cloneNode(existing.value, true))]);
|
|
2356
|
+
existing.value = t.objectExpression([t.objectProperty(createStaticObjectKey$1(nestedKey), t.valueToNode(value)), t.spreadElement(t.cloneNode(existing.value, true))]);
|
|
2336
2357
|
return true;
|
|
2337
2358
|
}
|
|
2338
2359
|
return false;
|
|
@@ -2505,7 +2526,7 @@ function buildMethodsMergeFromSpreadSources(componentExpr, inlineMapExpr) {
|
|
|
2505
2526
|
return t.callExpression(t.memberExpression(t.identifier("Object"), t.identifier("assign")), [
|
|
2506
2527
|
t.objectExpression([]),
|
|
2507
2528
|
...spreadSources,
|
|
2508
|
-
t.objectExpression([t.objectProperty(createStaticObjectKey("__weapp_vite_inline_map"), inlineMapExpr)])
|
|
2529
|
+
t.objectExpression([t.objectProperty(createStaticObjectKey$1("__weapp_vite_inline_map"), inlineMapExpr)])
|
|
2509
2530
|
]);
|
|
2510
2531
|
}
|
|
2511
2532
|
function injectInlineExpressions(componentExpr, inlineExpressions) {
|
|
@@ -2515,16 +2536,16 @@ function injectInlineExpressions(componentExpr, inlineExpressions) {
|
|
|
2515
2536
|
if (!methodsProp) {
|
|
2516
2537
|
const mergedMethods = buildMethodsMergeFromSpreadSources(componentExpr, inlineMapExpr);
|
|
2517
2538
|
if (mergedMethods) {
|
|
2518
|
-
componentExpr.properties.push(t.objectProperty(createStaticObjectKey("methods"), mergedMethods));
|
|
2539
|
+
componentExpr.properties.push(t.objectProperty(createStaticObjectKey$1("methods"), mergedMethods));
|
|
2519
2540
|
return true;
|
|
2520
2541
|
}
|
|
2521
|
-
componentExpr.properties.push(t.objectProperty(createStaticObjectKey("methods"), t.objectExpression([t.objectProperty(createStaticObjectKey("__weapp_vite_inline_map"), inlineMapExpr)])));
|
|
2542
|
+
componentExpr.properties.push(t.objectProperty(createStaticObjectKey$1("methods"), t.objectExpression([t.objectProperty(createStaticObjectKey$1("__weapp_vite_inline_map"), inlineMapExpr)])));
|
|
2522
2543
|
return true;
|
|
2523
2544
|
}
|
|
2524
2545
|
if (!t.isObjectExpression(methodsProp.value)) return false;
|
|
2525
2546
|
const mapProp = getObjectPropertyByKey$1(methodsProp.value, "__weapp_vite_inline_map");
|
|
2526
2547
|
if (!mapProp) {
|
|
2527
|
-
methodsProp.value.properties.push(t.objectProperty(createStaticObjectKey("__weapp_vite_inline_map"), inlineMapExpr));
|
|
2548
|
+
methodsProp.value.properties.push(t.objectProperty(createStaticObjectKey$1("__weapp_vite_inline_map"), inlineMapExpr));
|
|
2528
2549
|
return true;
|
|
2529
2550
|
}
|
|
2530
2551
|
if (t.isObjectExpression(mapProp.value)) {
|
|
@@ -2631,10 +2652,10 @@ function injectSetupInitialData(componentOptionsObject) {
|
|
|
2631
2652
|
if (!sourceExpression) continue;
|
|
2632
2653
|
const seedExpression = extractSeedExpression(sourceExpression);
|
|
2633
2654
|
if (!seedExpression) continue;
|
|
2634
|
-
seedProperties.push(t.objectProperty(createStaticObjectKey(propertyName), seedExpression));
|
|
2655
|
+
seedProperties.push(t.objectProperty(createStaticObjectKey$1(propertyName), seedExpression));
|
|
2635
2656
|
}
|
|
2636
2657
|
if (!seedProperties.length) return false;
|
|
2637
|
-
componentOptionsObject.properties.unshift(t.objectMethod("method", createStaticObjectKey("data"), [], t.blockStatement([t.returnStatement(t.objectExpression(seedProperties))])));
|
|
2658
|
+
componentOptionsObject.properties.unshift(t.objectMethod("method", createStaticObjectKey$1("data"), [], t.blockStatement([t.returnStatement(t.objectExpression(seedProperties))])));
|
|
2638
2659
|
return true;
|
|
2639
2660
|
}
|
|
2640
2661
|
//#endregion
|
|
@@ -2654,7 +2675,7 @@ function injectTemplateRefs(optionsObject, bindings, warn) {
|
|
|
2654
2675
|
const warnHandler = resolveWarnHandler(warn);
|
|
2655
2676
|
const entries = bindings.map((binding) => buildTemplateRefEntry(binding));
|
|
2656
2677
|
const refsArray = t.arrayExpression(entries);
|
|
2657
|
-
const key = createStaticObjectKey("__wevuTemplateRefs");
|
|
2678
|
+
const key = createStaticObjectKey$1("__wevuTemplateRefs");
|
|
2658
2679
|
const existing = getObjectPropertyByKey$1(optionsObject, "__wevuTemplateRefs");
|
|
2659
2680
|
if (!existing) {
|
|
2660
2681
|
optionsObject.properties.push(t.objectProperty(key, refsArray));
|
|
@@ -2682,15 +2703,45 @@ function hasStaticProperty(target, keyName) {
|
|
|
2682
2703
|
}
|
|
2683
2704
|
return false;
|
|
2684
2705
|
}
|
|
2685
|
-
function isObjectAssignCall(node) {
|
|
2686
|
-
const callee = node.callee;
|
|
2687
|
-
return t.isMemberExpression(callee) && t.isIdentifier(callee.object, { name: "Object" }) && t.isIdentifier(callee.property, { name: "assign" });
|
|
2688
|
-
}
|
|
2689
2706
|
function unwrapTypeLikeExpression(node) {
|
|
2690
2707
|
if (t.isTSAsExpression(node) || t.isTSSatisfiesExpression(node) || t.isTSNonNullExpression(node) || t.isTypeCastExpression(node)) return unwrapTypeLikeExpression(node.expression);
|
|
2691
2708
|
if (t.isParenthesizedExpression(node)) return unwrapTypeLikeExpression(node.expression);
|
|
2692
2709
|
return node;
|
|
2693
2710
|
}
|
|
2711
|
+
function isStructuredRuntimeType(node) {
|
|
2712
|
+
return t.isIdentifier(node, { name: "Array" }) || t.isIdentifier(node, { name: "Object" });
|
|
2713
|
+
}
|
|
2714
|
+
function shouldRelaxStructuredPropType(node) {
|
|
2715
|
+
const normalized = unwrapTypeLikeExpression(node);
|
|
2716
|
+
if (isStructuredRuntimeType(normalized)) return true;
|
|
2717
|
+
if (!t.isArrayExpression(normalized) || normalized.elements.length === 0) return false;
|
|
2718
|
+
return normalized.elements.every((element) => {
|
|
2719
|
+
if (!element || t.isSpreadElement(element) || !t.isExpression(element)) return false;
|
|
2720
|
+
return isStructuredRuntimeType(unwrapTypeLikeExpression(element));
|
|
2721
|
+
});
|
|
2722
|
+
}
|
|
2723
|
+
function relaxStructuredTypeOnlyProps(componentOptionsObject) {
|
|
2724
|
+
const propsProp = getObjectPropertyByKey$1(componentOptionsObject, "props");
|
|
2725
|
+
if (!propsProp || !t.isExpression(propsProp.value)) return false;
|
|
2726
|
+
const propsValue = unwrapTypeLikeExpression(propsProp.value);
|
|
2727
|
+
if (!t.isObjectExpression(propsValue)) return false;
|
|
2728
|
+
let changed = false;
|
|
2729
|
+
for (const prop of propsValue.properties) {
|
|
2730
|
+
if (!t.isObjectProperty(prop) || prop.computed || !t.isExpression(prop.value)) continue;
|
|
2731
|
+
const definition = unwrapTypeLikeExpression(prop.value);
|
|
2732
|
+
if (!t.isObjectExpression(definition)) continue;
|
|
2733
|
+
const typeProp = getObjectPropertyByKey$1(definition, "type");
|
|
2734
|
+
if (!typeProp || !t.isExpression(typeProp.value)) continue;
|
|
2735
|
+
if (!shouldRelaxStructuredPropType(typeProp.value)) continue;
|
|
2736
|
+
typeProp.value = t.nullLiteral();
|
|
2737
|
+
changed = true;
|
|
2738
|
+
}
|
|
2739
|
+
return changed;
|
|
2740
|
+
}
|
|
2741
|
+
function isObjectAssignCall(node) {
|
|
2742
|
+
const callee = node.callee;
|
|
2743
|
+
return t.isMemberExpression(callee) && t.isIdentifier(callee.object, { name: "Object" }) && t.isIdentifier(callee.property, { name: "assign" });
|
|
2744
|
+
}
|
|
2694
2745
|
function resolveObjectExpressionFromProgram(program, name) {
|
|
2695
2746
|
for (let index = program.body.length - 1; index >= 0; index -= 1) {
|
|
2696
2747
|
const statement = program.body[index];
|
|
@@ -2757,6 +2808,7 @@ function rewriteDefaultExport(ast, state, options, enabledPageFeatures, serializ
|
|
|
2757
2808
|
options
|
|
2758
2809
|
}) || transformed;
|
|
2759
2810
|
if (componentOptionsObject) transformed = injectSetupInitialData(componentOptionsObject) || transformed;
|
|
2811
|
+
if (componentOptionsObject && options?.relaxStructuredTypeOnlyProps) transformed = relaxStructuredTypeOnlyProps(componentOptionsObject) || transformed;
|
|
2760
2812
|
const classStyleBindings = options?.classStyleBindings ?? [];
|
|
2761
2813
|
if (classStyleBindings.length) if (componentOptionsObject) {
|
|
2762
2814
|
ensureClassStyleRuntimeImports(ast.program);
|
|
@@ -6701,9 +6753,13 @@ function resolveScriptSetupExtension(lang) {
|
|
|
6701
6753
|
return "js";
|
|
6702
6754
|
}
|
|
6703
6755
|
const IDENTIFIER_LIKE_KEY_RE = /^[A-Z_$][\w$]*$/i;
|
|
6756
|
+
const OBJECT_TO_STRING = Object.prototype.toString;
|
|
6704
6757
|
function isIdentifierLikeKey(key) {
|
|
6705
6758
|
return IDENTIFIER_LIKE_KEY_RE.test(key);
|
|
6706
6759
|
}
|
|
6760
|
+
function getObjectTag(value) {
|
|
6761
|
+
return OBJECT_TO_STRING.call(value);
|
|
6762
|
+
}
|
|
6707
6763
|
const SERIALIZABLE_NATIVE_FUNCTIONS = new Map([
|
|
6708
6764
|
[String, "String"],
|
|
6709
6765
|
[Number, "Number"],
|
|
@@ -6747,7 +6803,29 @@ function normalizeFunctionSourceToExpression(source) {
|
|
|
6747
6803
|
if (converted && isParsableFunctionExpressionSource(converted)) return converted;
|
|
6748
6804
|
throw new Error("defineOptions 的参数中包含无法序列化的函数值。");
|
|
6749
6805
|
}
|
|
6750
|
-
function
|
|
6806
|
+
function parseExpressionFromSource(source) {
|
|
6807
|
+
const statement = parse$2(`(${source})`, BABEL_TS_MODULE_PARSER_OPTIONS).program.body[0];
|
|
6808
|
+
if (!statement || !t.isExpressionStatement(statement)) throw new Error("defineOptions 的函数源码解析失败。");
|
|
6809
|
+
return statement.expression;
|
|
6810
|
+
}
|
|
6811
|
+
function rewriteFunctionSourceWithScopeValues(source, scopeValues) {
|
|
6812
|
+
if (!Object.keys(scopeValues).length) return source;
|
|
6813
|
+
const wrappedAst = parse$2(`(${source})`, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
6814
|
+
traverse(wrappedAst, { Identifier(path) {
|
|
6815
|
+
if (!path.isReferencedIdentifier()) return;
|
|
6816
|
+
if (path.scope.hasBinding(path.node.name)) return;
|
|
6817
|
+
if (!Object.hasOwn(scopeValues, path.node.name)) return;
|
|
6818
|
+
try {
|
|
6819
|
+
const replacement = serializeStaticValueToExpression(scopeValues[path.node.name], /* @__PURE__ */ new WeakSet(), {});
|
|
6820
|
+
path.replaceWith(parseExpressionFromSource(replacement));
|
|
6821
|
+
} catch {}
|
|
6822
|
+
} });
|
|
6823
|
+
const statement = wrappedAst.program.body[0];
|
|
6824
|
+
if (!statement || !t.isExpressionStatement(statement)) throw new Error("defineOptions 的函数源码解析失败。");
|
|
6825
|
+
const expression = statement.expression;
|
|
6826
|
+
return generate(expression).code;
|
|
6827
|
+
}
|
|
6828
|
+
function serializeStaticValueToExpression(value, seen = /* @__PURE__ */ new WeakSet(), scopeValues = {}) {
|
|
6751
6829
|
if (value === null) return "null";
|
|
6752
6830
|
if (value === void 0) return "undefined";
|
|
6753
6831
|
const valueType = typeof value;
|
|
@@ -6769,11 +6847,11 @@ function serializeStaticValueToExpression(value, seen = /* @__PURE__ */ new Weak
|
|
|
6769
6847
|
if (nativeLiteral) return nativeLiteral;
|
|
6770
6848
|
throw new Error("defineOptions 的参数中不支持原生函数值。");
|
|
6771
6849
|
}
|
|
6772
|
-
return `(${normalizeFunctionSourceToExpression(source)})`;
|
|
6850
|
+
return `(${normalizeFunctionSourceToExpression(rewriteFunctionSourceWithScopeValues(normalizeFunctionSourceToExpression(source), scopeValues))})`;
|
|
6773
6851
|
}
|
|
6774
|
-
if (value
|
|
6775
|
-
if (value
|
|
6776
|
-
if (Array.isArray(value)) return `[${value.map((item) => serializeStaticValueToExpression(item, seen)).join(", ")}]`;
|
|
6852
|
+
if (getObjectTag(value) === "[object Date]") return `new Date(${JSON.stringify(value.toISOString())})`;
|
|
6853
|
+
if (getObjectTag(value) === "[object RegExp]") return value.toString();
|
|
6854
|
+
if (Array.isArray(value)) return `[${value.map((item) => serializeStaticValueToExpression(item, seen, scopeValues)).join(", ")}]`;
|
|
6777
6855
|
if (value && typeof value === "object") {
|
|
6778
6856
|
if (seen.has(value)) throw new Error("defineOptions 的参数中不支持循环引用。");
|
|
6779
6857
|
seen.add(value);
|
|
@@ -6781,7 +6859,7 @@ function serializeStaticValueToExpression(value, seen = /* @__PURE__ */ new Weak
|
|
|
6781
6859
|
if (proto !== Object.prototype && proto !== null) throw new Error("defineOptions 的参数仅支持普通对象。");
|
|
6782
6860
|
const objectValue = value;
|
|
6783
6861
|
return `{ ${Object.keys(objectValue).map((key) => {
|
|
6784
|
-
return `${isIdentifierLikeKey(key) ? key : JSON.stringify(key)}: ${serializeStaticValueToExpression(objectValue[key], seen)}`;
|
|
6862
|
+
return `${isIdentifierLikeKey(key) ? key : JSON.stringify(key)}: ${serializeStaticValueToExpression(objectValue[key], seen, scopeValues)}`;
|
|
6785
6863
|
}).join(", ")} }`;
|
|
6786
6864
|
}
|
|
6787
6865
|
throw new Error(`defineOptions 的参数中包含不支持的值类型:${valueType}`);
|
|
@@ -6804,6 +6882,49 @@ function collectDefineOptionsStatements(content, filename) {
|
|
|
6804
6882
|
} });
|
|
6805
6883
|
return { statements };
|
|
6806
6884
|
}
|
|
6885
|
+
function evaluateStaticExpression(source) {
|
|
6886
|
+
return vm.runInNewContext(`(${source})`, {});
|
|
6887
|
+
}
|
|
6888
|
+
function collectStaticTopLevelScopeValues(content) {
|
|
6889
|
+
const ast = parse$2(content, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
6890
|
+
const scopeValues = {};
|
|
6891
|
+
for (const statement of ast.program.body) {
|
|
6892
|
+
if (!t.isVariableDeclaration(statement) || statement.kind !== "const") continue;
|
|
6893
|
+
for (const declarator of statement.declarations) {
|
|
6894
|
+
if (!t.isIdentifier(declarator.id) || !declarator.init) continue;
|
|
6895
|
+
try {
|
|
6896
|
+
scopeValues[declarator.id.name] = evaluateStaticExpression(declarator.init.end != null && declarator.init.start != null ? content.slice(declarator.init.start, declarator.init.end) : "");
|
|
6897
|
+
} catch {}
|
|
6898
|
+
}
|
|
6899
|
+
}
|
|
6900
|
+
return scopeValues;
|
|
6901
|
+
}
|
|
6902
|
+
function collectKeptBindingNames(keptStatementPaths) {
|
|
6903
|
+
const names = /* @__PURE__ */ new Set();
|
|
6904
|
+
for (const statementPath of keptStatementPaths) {
|
|
6905
|
+
if (statementPath.isImportDeclaration()) {
|
|
6906
|
+
if (statementPath.node.importKind === "type") continue;
|
|
6907
|
+
for (const specifier of statementPath.get("specifiers")) if (specifier.isImportSpecifier() || specifier.isImportDefaultSpecifier() || specifier.isImportNamespaceSpecifier()) {
|
|
6908
|
+
if (specifier.isImportSpecifier() && specifier.node.importKind === "type") continue;
|
|
6909
|
+
const local = specifier.node.local;
|
|
6910
|
+
if (t.isIdentifier(local)) names.add(local.name);
|
|
6911
|
+
}
|
|
6912
|
+
continue;
|
|
6913
|
+
}
|
|
6914
|
+
if (statementPath.isVariableDeclaration()) {
|
|
6915
|
+
for (const declarator of statementPath.get("declarations")) {
|
|
6916
|
+
const id = declarator.get("id");
|
|
6917
|
+
if (id.isIdentifier()) names.add(id.node.name);
|
|
6918
|
+
}
|
|
6919
|
+
continue;
|
|
6920
|
+
}
|
|
6921
|
+
if (statementPath.isFunctionDeclaration() || statementPath.isClassDeclaration()) {
|
|
6922
|
+
const id = statementPath.node.id;
|
|
6923
|
+
if (t.isIdentifier(id)) names.add(id.name);
|
|
6924
|
+
}
|
|
6925
|
+
}
|
|
6926
|
+
return [...names];
|
|
6927
|
+
}
|
|
6807
6928
|
async function evaluateDefineOptionsValues(params) {
|
|
6808
6929
|
const { content, filename, lang, statements } = params;
|
|
6809
6930
|
const ms = new MagicString(content);
|
|
@@ -6812,11 +6933,13 @@ async function evaluateDefineOptionsValues(params) {
|
|
|
6812
6933
|
const programPath = statements[0]?.statementPath.parentPath;
|
|
6813
6934
|
if (!programPath) return {
|
|
6814
6935
|
values: [],
|
|
6815
|
-
dependencies: []
|
|
6936
|
+
dependencies: [],
|
|
6937
|
+
scopeValues: {}
|
|
6816
6938
|
};
|
|
6817
6939
|
const bodyPaths = programPath?.get("body") ?? [];
|
|
6818
6940
|
const macroStatementPaths = statements.map((item) => item.statementPath);
|
|
6819
6941
|
const { keptStatementPaths } = collectKeptStatementPaths(programPath, macroStatementPaths);
|
|
6942
|
+
const keptBindingNames = collectKeptBindingNames(keptStatementPaths);
|
|
6820
6943
|
for (const statementPath of keptStatementPaths) {
|
|
6821
6944
|
if (!statementPath.isImportDeclaration()) continue;
|
|
6822
6945
|
const sourceNode = statementPath.node.source;
|
|
@@ -6837,10 +6960,15 @@ async function evaluateDefineOptionsValues(params) {
|
|
|
6837
6960
|
if (typeof start === "number" && typeof end === "number") ms.remove(start, end);
|
|
6838
6961
|
}
|
|
6839
6962
|
const extension = resolveScriptSetupExtension(lang);
|
|
6840
|
-
const
|
|
6963
|
+
const header = `
|
|
6841
6964
|
const __weapp_define_options_values = []
|
|
6965
|
+
const __weapp_define_scope_values = {}
|
|
6842
6966
|
const __weapp_defineOptions = (value) => (__weapp_define_options_values.push(value), value)
|
|
6843
|
-
`.trimStart()
|
|
6967
|
+
`.trimStart();
|
|
6968
|
+
const scopeFooter = keptBindingNames.map((name) => `__weapp_define_scope_values[${JSON.stringify(name)}] = ${name}`).join("\n");
|
|
6969
|
+
const evalSource = `${header + ms.toString() + (scopeFooter ? `\n${scopeFooter}\n` : "\n")}export const __weapp_define_scope = __weapp_define_scope_values\n
|
|
6970
|
+
export const __weapp_define_options = __weapp_define_options_values
|
|
6971
|
+
`;
|
|
6844
6972
|
return await withTempDirLock(tempDir, async () => {
|
|
6845
6973
|
await fs.ensureDir(tempDir);
|
|
6846
6974
|
const basename = path.basename(filename, path.extname(filename));
|
|
@@ -6852,10 +6980,11 @@ const __weapp_defineOptions = (value) => (__weapp_define_options_values.push(val
|
|
|
6852
6980
|
filepath: tempFile,
|
|
6853
6981
|
cwd: dir
|
|
6854
6982
|
});
|
|
6855
|
-
const exported = mod?.
|
|
6983
|
+
const exported = mod?.__weapp_define_options ?? mod;
|
|
6856
6984
|
return {
|
|
6857
6985
|
values: Array.isArray(exported) ? exported : [exported],
|
|
6858
|
-
dependencies
|
|
6986
|
+
dependencies,
|
|
6987
|
+
scopeValues: mod?.__weapp_define_scope ?? {}
|
|
6859
6988
|
};
|
|
6860
6989
|
} finally {
|
|
6861
6990
|
try {
|
|
@@ -6885,6 +7014,7 @@ async function inlineScriptSetupDefineOptionsArgs(content, filename, lang) {
|
|
|
6885
7014
|
};
|
|
6886
7015
|
let values = [];
|
|
6887
7016
|
let dependencies = [];
|
|
7017
|
+
let scopeValues = collectStaticTopLevelScopeValues(content);
|
|
6888
7018
|
try {
|
|
6889
7019
|
const evaluated = await evaluateDefineOptionsValues({
|
|
6890
7020
|
content,
|
|
@@ -6894,6 +7024,10 @@ async function inlineScriptSetupDefineOptionsArgs(content, filename, lang) {
|
|
|
6894
7024
|
});
|
|
6895
7025
|
values = evaluated.values;
|
|
6896
7026
|
dependencies = evaluated.dependencies;
|
|
7027
|
+
scopeValues = {
|
|
7028
|
+
...scopeValues,
|
|
7029
|
+
...evaluated.scopeValues
|
|
7030
|
+
};
|
|
6897
7031
|
} catch (error) {
|
|
6898
7032
|
if (shouldFallbackToRawDefineOptions(error)) return {
|
|
6899
7033
|
code: content,
|
|
@@ -6905,7 +7039,7 @@ async function inlineScriptSetupDefineOptionsArgs(content, filename, lang) {
|
|
|
6905
7039
|
for (let index = 0; index < statements.length; index += 1) {
|
|
6906
7040
|
const argNode = statements[index].argPath?.node;
|
|
6907
7041
|
if (!argNode || typeof argNode.start !== "number" || typeof argNode.end !== "number") continue;
|
|
6908
|
-
const literal = serializeStaticValueToExpression(await resolveDefineOptionsValue(values[index]));
|
|
7042
|
+
const literal = serializeStaticValueToExpression(await resolveDefineOptionsValue(values[index]), /* @__PURE__ */ new WeakSet(), scopeValues);
|
|
6909
7043
|
ms.overwrite(argNode.start, argNode.end, literal);
|
|
6910
7044
|
}
|
|
6911
7045
|
return {
|
|
@@ -7063,6 +7197,7 @@ async function parseVueFile(source, filename, options) {
|
|
|
7063
7197
|
}
|
|
7064
7198
|
//#endregion
|
|
7065
7199
|
//#region src/plugins/vue/transform/compileVueFile/script.ts
|
|
7200
|
+
const TYPE_ONLY_DEFINE_PROPS_RE = /\bdefineProps\s*</;
|
|
7066
7201
|
function collectTemplateComponentNames(template, filename, warn) {
|
|
7067
7202
|
const warnHandler = resolveWarnHandler(warn);
|
|
7068
7203
|
return collectVueTemplateTags(template, {
|
|
@@ -7075,6 +7210,7 @@ function collectTemplateComponentNames(template, filename, warn) {
|
|
|
7075
7210
|
async function compileScriptPhase(descriptor, descriptorForCompile, filename, options, autoUsingComponents, templateCompiled, isAppFile) {
|
|
7076
7211
|
const autoUsingComponentsMap = {};
|
|
7077
7212
|
const autoComponentMeta = {};
|
|
7213
|
+
const relaxStructuredTypeOnlyProps = Boolean(descriptor.scriptSetup?.content && TYPE_ONLY_DEFINE_PROPS_RE.test(descriptor.scriptSetup.content));
|
|
7078
7214
|
if (autoUsingComponents && descriptor.scriptSetup && descriptor.template) {
|
|
7079
7215
|
const templateComponentNames = collectTemplateComponentNames(descriptor.template.content, filename, autoUsingComponents.warn ?? options?.warn);
|
|
7080
7216
|
if (templateComponentNames.size) try {
|
|
@@ -7142,7 +7278,8 @@ async function compileScriptPhase(descriptor, descriptorForCompile, filename, op
|
|
|
7142
7278
|
classStyleRuntime: templateCompiled?.classStyleRuntime,
|
|
7143
7279
|
classStyleBindings: templateCompiled?.classStyleBindings,
|
|
7144
7280
|
templateRefs: templateCompiled?.templateRefs,
|
|
7145
|
-
inlineExpressions: templateCompiled?.inlineExpressions
|
|
7281
|
+
inlineExpressions: templateCompiled?.inlineExpressions,
|
|
7282
|
+
relaxStructuredTypeOnlyProps
|
|
7146
7283
|
}).code,
|
|
7147
7284
|
autoUsingComponentsMap,
|
|
7148
7285
|
autoComponentMeta
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wevu/compiler",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "6.
|
|
4
|
+
"version": "6.11.1",
|
|
5
5
|
"description": "wevu 编译器基础包,面向小程序模板的编译与转换",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"pathe": "^2.0.3",
|
|
51
51
|
"vue": "^3.5.30",
|
|
52
52
|
"@weapp-core/shared": "3.0.2",
|
|
53
|
-
"@weapp-vite/ast": "6.
|
|
53
|
+
"@weapp-vite/ast": "6.11.1",
|
|
54
54
|
"rolldown-require": "2.0.8"
|
|
55
55
|
},
|
|
56
56
|
"publishConfig": {
|