@wevu/compiler 6.16.18 → 6.16.20
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 +18 -0
- package/dist/index.mjs +181 -33
- package/package.json +4 -4
package/dist/index.d.mts
CHANGED
|
@@ -276,6 +276,10 @@ 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>;
|
|
279
283
|
htmlTagToWxml?: boolean | Record<string, string>;
|
|
280
284
|
htmlTagToWxmlTagClass?: boolean;
|
|
281
285
|
scopedSlotsCompiler?: ScopedSlotsCompilerMode;
|
|
@@ -449,6 +453,10 @@ interface CompileVueFileOptions {
|
|
|
449
453
|
astEngine?: AstEngineName$1;
|
|
450
454
|
isPage?: boolean;
|
|
451
455
|
isApp?: boolean;
|
|
456
|
+
/**
|
|
457
|
+
* 是否压缩生成的 wevu 脚本输出。
|
|
458
|
+
*/
|
|
459
|
+
minify?: boolean;
|
|
452
460
|
warn?: (message: string) => void;
|
|
453
461
|
autoUsingComponents?: AutoUsingComponentsOptions;
|
|
454
462
|
autoImportTags?: AutoImportTagsOptions;
|
|
@@ -669,6 +677,10 @@ interface TransformScriptOptions {
|
|
|
669
677
|
* wevu 默认值(仅用于 app.vue 注入)
|
|
670
678
|
*/
|
|
671
679
|
wevuDefaults?: WevuDefaults;
|
|
680
|
+
/**
|
|
681
|
+
* 是否压缩生成的脚本代码。
|
|
682
|
+
*/
|
|
683
|
+
minify?: boolean;
|
|
672
684
|
/**
|
|
673
685
|
* 编译期警告回调
|
|
674
686
|
*/
|
|
@@ -697,6 +709,10 @@ interface TransformScriptOptions {
|
|
|
697
709
|
* 模板中作为组件 prop 传递的函数候选路径。
|
|
698
710
|
*/
|
|
699
711
|
functionPropPaths?: string[];
|
|
712
|
+
/**
|
|
713
|
+
* Vue `<script setup>` props 解构重命名映射,key 为模板中使用的本地别名,value 为原始 prop 名。
|
|
714
|
+
*/
|
|
715
|
+
propsAliases?: Record<string, string>;
|
|
700
716
|
/**
|
|
701
717
|
* 对 `<script setup>` 类型声明生成的结构化 props(如 Array/Object)放宽小程序运行时类型约束,
|
|
702
718
|
* 以避免小程序属性校验对复杂表达式/代理值产生误报。
|
|
@@ -804,6 +820,7 @@ declare function injectWevuPageFeatureFlagsIntoOptionsObject(optionsObject: t.Ob
|
|
|
804
820
|
*/
|
|
805
821
|
declare function injectWevuPageFeaturesInJs(source: string, options?: {
|
|
806
822
|
astEngine?: AstEngineName$1;
|
|
823
|
+
minify?: boolean;
|
|
807
824
|
}): {
|
|
808
825
|
code: string;
|
|
809
826
|
transformed: boolean;
|
|
@@ -816,6 +833,7 @@ declare function injectWevuPageFeaturesInJsWithResolver(source: string, options:
|
|
|
816
833
|
id: string;
|
|
817
834
|
resolver: ModuleResolver;
|
|
818
835
|
astEngine?: AstEngineName$1;
|
|
836
|
+
minify?: boolean;
|
|
819
837
|
}): Promise<{
|
|
820
838
|
code: string;
|
|
821
839
|
transformed: boolean;
|
package/dist/index.mjs
CHANGED
|
@@ -1615,7 +1615,9 @@ function injectWevuPageFeaturesInJs(source, options) {
|
|
|
1615
1615
|
transformed: false
|
|
1616
1616
|
};
|
|
1617
1617
|
const generated = generate(ast, {
|
|
1618
|
-
|
|
1618
|
+
compact: options?.minify === true,
|
|
1619
|
+
minified: options?.minify === true,
|
|
1620
|
+
retainLines: options?.minify !== true,
|
|
1619
1621
|
sourceMaps: true,
|
|
1620
1622
|
sourceFileName: "inline.js"
|
|
1621
1623
|
}, source);
|
|
@@ -1661,7 +1663,9 @@ async function injectWevuPageFeaturesInJsWithResolver(source, options) {
|
|
|
1661
1663
|
transformed: false
|
|
1662
1664
|
};
|
|
1663
1665
|
const generated = generate(ast, {
|
|
1664
|
-
|
|
1666
|
+
compact: options?.minify === true,
|
|
1667
|
+
minified: options?.minify === true,
|
|
1668
|
+
retainLines: options?.minify !== true,
|
|
1665
1669
|
sourceMaps: true,
|
|
1666
1670
|
sourceFileName: "inline.js"
|
|
1667
1671
|
}, source);
|
|
@@ -2237,6 +2241,38 @@ function buildConsoleErrorGuard(message, errorId) {
|
|
|
2237
2241
|
function buildRuntimeExpressionErrorGuard(binding, errorId) {
|
|
2238
2242
|
return buildConsoleErrorGuard(`[wevu] 模板运行时表达式执行失败: ${binding.name} = ${binding.exp}`, errorId);
|
|
2239
2243
|
}
|
|
2244
|
+
function createDataPropsFallbackExpression(fallback) {
|
|
2245
|
+
const propsObject = t.memberExpression(t.thisExpression(), t.identifier(WEVU_PROPS_KEY));
|
|
2246
|
+
const propsAccess = t.memberExpression(propsObject, t.identifier("data"));
|
|
2247
|
+
const hasPropsObject = t.binaryExpression("!=", propsObject, t.nullLiteral());
|
|
2248
|
+
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")])));
|
|
2249
|
+
return t.conditionalExpression(hasPropsValue, propsAccess, fallback);
|
|
2250
|
+
}
|
|
2251
|
+
function createDataRuntimeAccess(helpers) {
|
|
2252
|
+
const unrefHelper = helpers.unref ? t.cloneNode(helpers.unref) : t.identifier("unref");
|
|
2253
|
+
return t.callExpression(unrefHelper, [createDataPropsFallbackExpression(t.memberExpression(t.thisExpression(), t.identifier("data")))]);
|
|
2254
|
+
}
|
|
2255
|
+
function rewriteDataAccessExpression(exp, helpers) {
|
|
2256
|
+
if (t.isIdentifier(exp) && exp.name === "data") return createDataRuntimeAccess(helpers);
|
|
2257
|
+
if (t.isMemberExpression(exp)) return t.memberExpression(rewriteDataAccessExpression(exp.object, helpers), t.cloneNode(exp.property), exp.computed);
|
|
2258
|
+
if (t.isObjectExpression(exp)) return t.objectExpression(exp.properties.map((property) => {
|
|
2259
|
+
if (!t.isObjectProperty(property) || !t.isExpression(property.value)) return t.cloneNode(property, true);
|
|
2260
|
+
return t.objectProperty(t.cloneNode(property.key), rewriteDataAccessExpression(property.value, helpers), property.computed, property.shorthand);
|
|
2261
|
+
}));
|
|
2262
|
+
if (t.isArrayExpression(exp)) return t.arrayExpression(exp.elements.map((element) => {
|
|
2263
|
+
if (!element || t.isSpreadElement(element)) return element ? t.cloneNode(element, true) : null;
|
|
2264
|
+
return rewriteDataAccessExpression(element, helpers);
|
|
2265
|
+
}));
|
|
2266
|
+
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));
|
|
2267
|
+
if (t.isLogicalExpression(exp)) return t.logicalExpression(exp.operator, rewriteDataAccessExpression(exp.left, helpers), rewriteDataAccessExpression(exp.right, helpers));
|
|
2268
|
+
if (t.isConditionalExpression(exp)) return t.conditionalExpression(rewriteDataAccessExpression(exp.test, helpers), rewriteDataAccessExpression(exp.consequent, helpers), rewriteDataAccessExpression(exp.alternate, helpers));
|
|
2269
|
+
if (t.isUnaryExpression(exp)) return t.unaryExpression(exp.operator, rewriteDataAccessExpression(exp.argument, helpers), exp.prefix);
|
|
2270
|
+
if (t.isCallExpression(exp)) return t.callExpression(rewriteDataAccessExpression(exp.callee, helpers), exp.arguments.map((arg) => {
|
|
2271
|
+
if (t.isSpreadElement(arg)) return t.cloneNode(arg, true);
|
|
2272
|
+
return rewriteDataAccessExpression(arg, helpers);
|
|
2273
|
+
}));
|
|
2274
|
+
return t.cloneNode(exp, true);
|
|
2275
|
+
}
|
|
2240
2276
|
function buildNormalizedExpression(binding, helpers) {
|
|
2241
2277
|
const errorId = t.identifier(WEVU_EXPRESSION_ERROR_IDENTIFIER);
|
|
2242
2278
|
if (binding.type === "bind") {
|
|
@@ -2245,7 +2281,7 @@ function buildNormalizedExpression(binding, helpers) {
|
|
|
2245
2281
|
}
|
|
2246
2282
|
const normalizeHelper = binding.type === "class" ? helpers.normalizeClass : helpers.normalizeStyle;
|
|
2247
2283
|
const errorFallback = binding.errorFallback ?? "";
|
|
2248
|
-
const exp = binding.expAst ?
|
|
2284
|
+
const exp = binding.expAst ? rewriteDataAccessExpression(binding.expAst, helpers) : t.stringLiteral("");
|
|
2249
2285
|
const normalizedCall = t.callExpression(t.cloneNode(normalizeHelper), [exp]);
|
|
2250
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)])), []);
|
|
2251
2287
|
}
|
|
@@ -2314,11 +2350,37 @@ function buildComputedFunctionBody(binding, helpers) {
|
|
|
2314
2350
|
}
|
|
2315
2351
|
//#endregion
|
|
2316
2352
|
//#region src/plugins/vue/transform/classStyleComputed.ts
|
|
2317
|
-
function
|
|
2353
|
+
function createMemberAccess$2(target, prop) {
|
|
2354
|
+
if (t.isValidIdentifier(prop)) return t.memberExpression(target, t.identifier(prop));
|
|
2355
|
+
return t.memberExpression(target, t.stringLiteral(prop), true);
|
|
2356
|
+
}
|
|
2357
|
+
function applyPropsAliasesToExpression(expression, propsAliases) {
|
|
2358
|
+
if (!propsAliases || !Object.keys(propsAliases).length) return expression;
|
|
2359
|
+
const ast = t.file(t.program([t.expressionStatement(expression)]));
|
|
2360
|
+
traverse(ast, { Identifier(path) {
|
|
2361
|
+
if (!path.isReferencedIdentifier()) return;
|
|
2362
|
+
const propName = propsAliases[path.node.name];
|
|
2363
|
+
if (!propName || path.scope.hasBinding(path.node.name)) return;
|
|
2364
|
+
const replacement = createMemberAccess$2(t.identifier("props"), propName);
|
|
2365
|
+
const parent = path.parentPath;
|
|
2366
|
+
if (parent.isObjectProperty() && parent.node.shorthand && parent.node.key === path.node) {
|
|
2367
|
+
parent.node.shorthand = false;
|
|
2368
|
+
parent.node.value = replacement;
|
|
2369
|
+
return;
|
|
2370
|
+
}
|
|
2371
|
+
path.replaceWith(replacement);
|
|
2372
|
+
} });
|
|
2373
|
+
const statement = ast.program.body[0];
|
|
2374
|
+
return t.isExpressionStatement(statement) ? statement.expression : expression;
|
|
2375
|
+
}
|
|
2376
|
+
function buildClassStyleComputedEntries(bindings, helpers, propsAliases) {
|
|
2318
2377
|
const entries = [];
|
|
2319
2378
|
for (const binding of bindings) {
|
|
2320
2379
|
const key = createStaticObjectKey(binding.name);
|
|
2321
|
-
const body = buildComputedFunctionBody(
|
|
2380
|
+
const body = buildComputedFunctionBody({
|
|
2381
|
+
...binding,
|
|
2382
|
+
expAst: binding.expAst ? applyPropsAliasesToExpression(t.cloneNode(binding.expAst, true), propsAliases) : binding.expAst
|
|
2383
|
+
}, helpers);
|
|
2322
2384
|
const fn = t.functionExpression(null, [], body);
|
|
2323
2385
|
entries.push(t.objectProperty(key, fn));
|
|
2324
2386
|
}
|
|
@@ -2343,14 +2405,14 @@ function buildClassStyleComputedCode(bindings, helpers) {
|
|
|
2343
2405
|
}
|
|
2344
2406
|
//#endregion
|
|
2345
2407
|
//#region src/plugins/vue/transform/transformScript/rewrite/classStyle.ts
|
|
2346
|
-
function injectClassStyleComputed(optionsObject, bindings, warn) {
|
|
2408
|
+
function injectClassStyleComputed(optionsObject, bindings, propsAliases, warn) {
|
|
2347
2409
|
if (!bindings.length) return false;
|
|
2348
2410
|
const warnHandler = resolveWarnHandler(warn);
|
|
2349
2411
|
const entries = buildClassStyleComputedEntries(bindings, {
|
|
2350
2412
|
normalizeClass: t.identifier("__wevuNormalizeClass"),
|
|
2351
2413
|
normalizeStyle: t.identifier("__wevuNormalizeStyle"),
|
|
2352
2414
|
unref: t.identifier("__wevuUnref")
|
|
2353
|
-
});
|
|
2415
|
+
}, propsAliases);
|
|
2354
2416
|
if (!entries.length) return false;
|
|
2355
2417
|
const computedProp = getObjectPropertyByKey$1(optionsObject, "computed");
|
|
2356
2418
|
if (!computedProp) {
|
|
@@ -2935,7 +2997,7 @@ function rewriteDefaultExport(ast, state, options, enabledPageFeatures, serializ
|
|
|
2935
2997
|
const classStyleBindings = options?.classStyleBindings ?? [];
|
|
2936
2998
|
if (classStyleBindings.length) if (componentOptionsObject) {
|
|
2937
2999
|
ensureClassStyleRuntimeImports(ast.program);
|
|
2938
|
-
transformed = injectClassStyleComputed(componentOptionsObject, classStyleBindings, warn) || transformed;
|
|
3000
|
+
transformed = injectClassStyleComputed(componentOptionsObject, classStyleBindings, options?.propsAliases, warn) || transformed;
|
|
2939
3001
|
} else warn("无法自动注入 class/style 计算属性:组件选项不是对象字面量。");
|
|
2940
3002
|
const templateRefs = options?.templateRefs ?? [];
|
|
2941
3003
|
if (templateRefs.length) if (componentOptionsObject) transformed = injectTemplateRefs(componentOptionsObject, templateRefs, warn) || transformed;
|
|
@@ -2998,7 +3060,9 @@ function transformScript(source, options) {
|
|
|
2998
3060
|
transformed: false
|
|
2999
3061
|
};
|
|
3000
3062
|
const generated = generate(ast, {
|
|
3001
|
-
|
|
3063
|
+
compact: options?.minify === true,
|
|
3064
|
+
minified: options?.minify === true,
|
|
3065
|
+
retainLines: options?.minify !== true,
|
|
3002
3066
|
sourceMaps: true,
|
|
3003
3067
|
sourceFileName: "inline.ts"
|
|
3004
3068
|
}, source);
|
|
@@ -3295,26 +3359,49 @@ function stripRenderOptionFromScript(source, filename, warn) {
|
|
|
3295
3359
|
}
|
|
3296
3360
|
//#endregion
|
|
3297
3361
|
//#region src/plugins/vue/compiler/template/format.ts
|
|
3298
|
-
const TOKEN_RE = /<[^>]+>/g;
|
|
3299
3362
|
function tokenizeWxml(source) {
|
|
3300
3363
|
const tokens = [];
|
|
3301
3364
|
let index = 0;
|
|
3302
|
-
|
|
3303
|
-
const start =
|
|
3365
|
+
while (index < source.length) {
|
|
3366
|
+
const start = source.indexOf("<", index);
|
|
3367
|
+
if (start < 0) {
|
|
3368
|
+
tokens.push({
|
|
3369
|
+
type: "text",
|
|
3370
|
+
value: source.slice(index)
|
|
3371
|
+
});
|
|
3372
|
+
break;
|
|
3373
|
+
}
|
|
3304
3374
|
if (start > index) tokens.push({
|
|
3305
3375
|
type: "text",
|
|
3306
3376
|
value: source.slice(index, start)
|
|
3307
3377
|
});
|
|
3378
|
+
let quote;
|
|
3379
|
+
let end = start + 1;
|
|
3380
|
+
for (; end < source.length; end++) {
|
|
3381
|
+
const char = source[end];
|
|
3382
|
+
if (quote) {
|
|
3383
|
+
if (char === quote) quote = void 0;
|
|
3384
|
+
continue;
|
|
3385
|
+
}
|
|
3386
|
+
if (char === "\"" || char === "'") {
|
|
3387
|
+
quote = char;
|
|
3388
|
+
continue;
|
|
3389
|
+
}
|
|
3390
|
+
if (char === ">") break;
|
|
3391
|
+
}
|
|
3392
|
+
if (end >= source.length) {
|
|
3393
|
+
tokens.push({
|
|
3394
|
+
type: "text",
|
|
3395
|
+
value: source.slice(start)
|
|
3396
|
+
});
|
|
3397
|
+
break;
|
|
3398
|
+
}
|
|
3308
3399
|
tokens.push({
|
|
3309
3400
|
type: "tag",
|
|
3310
|
-
value:
|
|
3401
|
+
value: source.slice(start, end + 1)
|
|
3311
3402
|
});
|
|
3312
|
-
index =
|
|
3403
|
+
index = end + 1;
|
|
3313
3404
|
}
|
|
3314
|
-
if (index < source.length) tokens.push({
|
|
3315
|
-
type: "text",
|
|
3316
|
-
value: source.slice(index)
|
|
3317
|
-
});
|
|
3318
3405
|
return tokens.filter((token) => token.value.length > 0);
|
|
3319
3406
|
}
|
|
3320
3407
|
function isClosingTag(value) {
|
|
@@ -4110,6 +4197,7 @@ async function compileJsxFile(source, filename, options) {
|
|
|
4110
4197
|
skipComponentTransform: options?.isApp,
|
|
4111
4198
|
isApp: options?.isApp,
|
|
4112
4199
|
isPage: options?.isPage,
|
|
4200
|
+
minify: options?.minify,
|
|
4113
4201
|
warn: options?.warn,
|
|
4114
4202
|
wevuDefaults: options?.wevuDefaults,
|
|
4115
4203
|
inlineExpressions
|
|
@@ -5325,7 +5413,8 @@ function rewriteScopedSlotExpression(exp, context) {
|
|
|
5325
5413
|
function rewriteForAliasExpression(exp, context) {
|
|
5326
5414
|
const normalized = normalizeWxmlExpression(exp);
|
|
5327
5415
|
const forAliases = collectForAliasMapping$2(context);
|
|
5328
|
-
|
|
5416
|
+
const propsAliases = context.propsAliases ?? {};
|
|
5417
|
+
if (!Object.keys(forAliases).length && !Object.keys(propsAliases).length) return normalized;
|
|
5329
5418
|
const parsed = parseBabelExpressionFile(normalized);
|
|
5330
5419
|
if (!parsed) return normalized;
|
|
5331
5420
|
const { ast } = parsed;
|
|
@@ -5333,10 +5422,13 @@ function rewriteForAliasExpression(exp, context) {
|
|
|
5333
5422
|
if (!path.isReferencedIdentifier()) return;
|
|
5334
5423
|
const name = path.node.name;
|
|
5335
5424
|
if (path.scope.hasBinding(name)) return;
|
|
5336
|
-
if (
|
|
5337
|
-
|
|
5338
|
-
|
|
5339
|
-
|
|
5425
|
+
if (hasOwn(forAliases, name)) {
|
|
5426
|
+
const aliasExp = parseBabelExpression(forAliases[name]);
|
|
5427
|
+
if (aliasExp) replaceIdentifierWithExpression(path, t.cloneNode(aliasExp, true));
|
|
5428
|
+
return;
|
|
5429
|
+
}
|
|
5430
|
+
const propName = propsAliases[name];
|
|
5431
|
+
if (propName && t.isValidIdentifier(propName)) replaceIdentifierWithExpression(path, t.identifier(propName));
|
|
5340
5432
|
} });
|
|
5341
5433
|
const stmt = ast.program.body[0];
|
|
5342
5434
|
const updatedExpression = stmt && "expression" in stmt ? stmt.expression : null;
|
|
@@ -5684,11 +5776,19 @@ function createUnrefCall(exp) {
|
|
|
5684
5776
|
function createHasOwnPropertyCall(target, key) {
|
|
5685
5777
|
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)]);
|
|
5686
5778
|
}
|
|
5779
|
+
function createPropsKeyAccessFallback(name, fallback) {
|
|
5780
|
+
const propsObject = createThisMemberAccess(WEVU_PROPS_KEY);
|
|
5781
|
+
const propsAccess = createMemberAccess(propsObject, name);
|
|
5782
|
+
const hasPropsObject = t.binaryExpression("!=", propsObject, t.nullLiteral());
|
|
5783
|
+
const hasPropsValue = t.logicalExpression("&&", hasPropsObject, t.logicalExpression("||", t.binaryExpression("!==", propsAccess, t.identifier("undefined")), createHasOwnPropertyCall(propsObject, name)));
|
|
5784
|
+
return t.conditionalExpression(hasPropsValue, propsAccess, fallback);
|
|
5785
|
+
}
|
|
5687
5786
|
function createIdentifierAccessWithPropsFallback(name) {
|
|
5688
5787
|
if (name === "props") {
|
|
5689
5788
|
const propsObject = createThisMemberAccess(WEVU_PROPS_KEY);
|
|
5690
5789
|
return t.conditionalExpression(t.binaryExpression("!=", propsObject, t.nullLiteral()), propsObject, createThisMemberAccess("props"));
|
|
5691
5790
|
}
|
|
5791
|
+
if (name === "data") return createPropsKeyAccessFallback(name, createThisMemberAccess(name));
|
|
5692
5792
|
const thisAccess = createThisMemberAccess(name);
|
|
5693
5793
|
const propsAccess = createMemberAccess(createThisMemberAccess(WEVU_PROPS_KEY), name);
|
|
5694
5794
|
const propsObject = createThisMemberAccess(WEVU_PROPS_KEY);
|
|
@@ -5704,6 +5804,17 @@ function createIdentifierAccessWithPropsFallback(name) {
|
|
|
5704
5804
|
const shouldUsePropsAccess = t.logicalExpression("&&", hasUsablePropsValue, t.unaryExpression("!", hasThisMember));
|
|
5705
5805
|
return t.conditionalExpression(shouldUseStateAccess, thisAccess, t.conditionalExpression(shouldUsePropsAccess, propsAccess, thisAccess));
|
|
5706
5806
|
}
|
|
5807
|
+
function createAliasedPropsAccess(name, propName) {
|
|
5808
|
+
const propsObject = createThisMemberAccess(WEVU_PROPS_KEY);
|
|
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);
|
|
5817
|
+
}
|
|
5707
5818
|
function collectForAliasMapping(context) {
|
|
5708
5819
|
const mapping = {};
|
|
5709
5820
|
for (const forInfo of context.forStack) {
|
|
@@ -5752,7 +5863,10 @@ function normalizeJsExpressionWithContext(exp, context, options) {
|
|
|
5752
5863
|
replacement = createUnrefCall(prop ? createMemberAccess(base, prop) : base);
|
|
5753
5864
|
} 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));
|
|
5754
5865
|
else replacement = createUnrefCall(createMemberAccess(createThisMemberAccess(WEVU_SLOT_OWNER_PROXY_KEY), name));
|
|
5755
|
-
else
|
|
5866
|
+
else {
|
|
5867
|
+
const propsAlias = context.propsAliases?.[name];
|
|
5868
|
+
replacement = createUnrefCall(propsAlias ? createAliasedPropsAccess(name, propsAlias) : createIdentifierAccessWithPropsFallback(name));
|
|
5869
|
+
}
|
|
5756
5870
|
const parent = path.parentPath;
|
|
5757
5871
|
if (parent.isObjectProperty() && parent.node.shorthand && parent.node.key === path.node) {
|
|
5758
5872
|
parent.node.shorthand = false;
|
|
@@ -6614,6 +6728,7 @@ function buildSlotDeclaration(name, propsExp, children, context, options) {
|
|
|
6614
6728
|
props: propsExp ? parseSlotPropsExpression(propsExp, context) : {},
|
|
6615
6729
|
children,
|
|
6616
6730
|
implicitDefault: options?.implicitDefault,
|
|
6731
|
+
conditionKind: options?.conditionKind,
|
|
6617
6732
|
condition: options?.condition
|
|
6618
6733
|
};
|
|
6619
6734
|
}
|
|
@@ -6707,6 +6822,9 @@ function isRenderableFallbackChild(child) {
|
|
|
6707
6822
|
function renderSlotFallback(decl, context, transformNode) {
|
|
6708
6823
|
const slotAttr = renderSlotNameAttribute(decl.name, context, "slot");
|
|
6709
6824
|
const wrapCondition = (content) => {
|
|
6825
|
+
if (decl.conditionKind === "else") return context.platform.wrapElse(content);
|
|
6826
|
+
if (decl.conditionKind === "else-if" && decl.condition) return context.platform.wrapElseIf(decl.condition, content, (exp) => renderMustache(exp, context));
|
|
6827
|
+
if (decl.conditionKind === "if" && decl.condition) return context.platform.wrapIf(decl.condition, content, (exp) => renderMustache(exp, context));
|
|
6710
6828
|
return decl.condition ? context.platform.wrapIf(decl.condition, content, (exp) => renderMustache(exp, context)) : content;
|
|
6711
6829
|
};
|
|
6712
6830
|
if (!slotAttr) {
|
|
@@ -6814,9 +6932,14 @@ function shouldAugmentPlainSlot(decl, context, ownerNode) {
|
|
|
6814
6932
|
return hasDirectComponentSlotChild(decl.children, context);
|
|
6815
6933
|
}
|
|
6816
6934
|
function resolveTemplateSlotCondition(node, context) {
|
|
6817
|
-
const
|
|
6818
|
-
|
|
6819
|
-
|
|
6935
|
+
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));
|
|
6936
|
+
if (!directive) return {};
|
|
6937
|
+
if (directive.name === "else") return { conditionKind: "else" };
|
|
6938
|
+
const rawExp = directive.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? directive.exp.content : "";
|
|
6939
|
+
return {
|
|
6940
|
+
conditionKind: directive.name === "else-if" ? "else-if" : "if",
|
|
6941
|
+
condition: rawExp ? normalizeWxmlExpressionWithContext(rawExp, context) : void 0
|
|
6942
|
+
};
|
|
6820
6943
|
}
|
|
6821
6944
|
function pushSlotNamesAttr(attrs, slotNames, context) {
|
|
6822
6945
|
if (!slotNames.length) return;
|
|
@@ -6875,7 +6998,9 @@ function transformComponentWithSlots(node, context, transformNode, options) {
|
|
|
6875
6998
|
if (child.type === NodeTypes.ELEMENT && child.tag === "template") {
|
|
6876
6999
|
const templateSlot = findSlotDirective(child);
|
|
6877
7000
|
if (templateSlot) {
|
|
6878
|
-
const
|
|
7001
|
+
const slotName = resolveSlotNameFromDirective(templateSlot);
|
|
7002
|
+
const templateSlotCondition = resolveTemplateSlotCondition(child, context);
|
|
7003
|
+
const declaration = buildSlotDeclaration(slotName, templateSlot.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? templateSlot.exp.content : void 0, child.children, context, templateSlotCondition);
|
|
6879
7004
|
slotDeclarations.push(declaration);
|
|
6880
7005
|
renderItems.push({
|
|
6881
7006
|
type: "declaration",
|
|
@@ -6970,7 +7095,9 @@ function transformComponentWithSlotsFallback(node, context, transformNode, optio
|
|
|
6970
7095
|
if (child.type === NodeTypes.ELEMENT && child.tag === "template") {
|
|
6971
7096
|
const templateSlot = findSlotDirective(child);
|
|
6972
7097
|
if (templateSlot) {
|
|
6973
|
-
const
|
|
7098
|
+
const slotName = resolveSlotNameFromDirective(templateSlot);
|
|
7099
|
+
const templateSlotCondition = resolveTemplateSlotCondition(child, context);
|
|
7100
|
+
const declaration = buildSlotDeclaration(slotName, templateSlot.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? templateSlot.exp.content : void 0, child.children, context, templateSlotCondition);
|
|
6974
7101
|
slotDeclarations.push(declaration);
|
|
6975
7102
|
renderItems.push({
|
|
6976
7103
|
type: "declaration",
|
|
@@ -7340,6 +7467,7 @@ function compileVueTemplateToWxml(template, filename, options) {
|
|
|
7340
7467
|
filename,
|
|
7341
7468
|
warnings,
|
|
7342
7469
|
platform: options?.platform ?? getMiniProgramTemplatePlatform(),
|
|
7470
|
+
propsAliases: options?.propsAliases,
|
|
7343
7471
|
htmlTagToWxmlMap,
|
|
7344
7472
|
htmlTagToWxmlTagClass: options?.htmlTagToWxmlTagClass ?? true,
|
|
7345
7473
|
scopedSlotsCompiler: options?.scopedSlotsCompiler ?? "auto",
|
|
@@ -8198,17 +8326,26 @@ function composeSourceMaps(transformedMap, originalMap) {
|
|
|
8198
8326
|
//#endregion
|
|
8199
8327
|
//#region src/plugins/vue/transform/compileVueFile/script.ts
|
|
8200
8328
|
const TYPE_ONLY_DEFINE_PROPS_RE = /\bdefineProps\s*</;
|
|
8201
|
-
|
|
8329
|
+
function resolveScriptSetupPropsAliases(bindings) {
|
|
8330
|
+
const aliases = bindings?.__propsAliases;
|
|
8331
|
+
if (!aliases || typeof aliases !== "object") return;
|
|
8332
|
+
const resolved = {};
|
|
8333
|
+
for (const [alias, propName] of Object.entries(aliases)) if (typeof propName === "string" && propName.length > 0) resolved[alias] = propName;
|
|
8334
|
+
return Object.keys(resolved).length ? resolved : void 0;
|
|
8335
|
+
}
|
|
8336
|
+
async function compileScriptPhase(descriptor, descriptorForCompile, filename, options, _autoUsingComponents, templateCompiled, isAppFile, componentSourceInfo, precompiledScript) {
|
|
8202
8337
|
const autoUsingComponentsMap = { ...componentSourceInfo?.autoUsingComponentsMap ?? {} };
|
|
8203
8338
|
const autoComponentMeta = { ...componentSourceInfo?.autoComponentMeta ?? {} };
|
|
8204
8339
|
const relaxStructuredTypeOnlyProps = Boolean(descriptor.scriptSetup?.content && TYPE_ONLY_DEFINE_PROPS_RE.test(descriptor.scriptSetup.content));
|
|
8205
8340
|
let scriptCode;
|
|
8206
8341
|
let scriptMap = null;
|
|
8342
|
+
let propsAliases = options?.template?.propsAliases;
|
|
8207
8343
|
if (descriptor.script || descriptor.scriptSetup) {
|
|
8208
|
-
const scriptCompiled = compileScript(descriptorForCompile, {
|
|
8344
|
+
const scriptCompiled = precompiledScript ?? compileScript(descriptorForCompile, {
|
|
8209
8345
|
id: filename,
|
|
8210
8346
|
isProd: false
|
|
8211
8347
|
});
|
|
8348
|
+
propsAliases ??= resolveScriptSetupPropsAliases(scriptCompiled.bindings);
|
|
8212
8349
|
scriptCode = scriptCompiled.content;
|
|
8213
8350
|
scriptMap = scriptCompiled.map && typeof scriptCompiled.map === "object" ? scriptCompiled.map : null;
|
|
8214
8351
|
if (scriptCode.includes("defineAppJson") || scriptCode.includes("definePageJson") || scriptCode.includes("defineComponentJson")) scriptCode = stripJsonMacroCallsFromCode(scriptCode, filename);
|
|
@@ -8219,6 +8356,7 @@ async function compileScriptPhase(descriptor, descriptorForCompile, filename, op
|
|
|
8219
8356
|
skipComponentTransform: isAppFile,
|
|
8220
8357
|
isApp: isAppFile,
|
|
8221
8358
|
isPage: options?.isPage === true,
|
|
8359
|
+
minify: options?.minify,
|
|
8222
8360
|
warn: options?.warn,
|
|
8223
8361
|
templateComponentMeta: Object.keys(autoComponentMeta).length ? autoComponentMeta : void 0,
|
|
8224
8362
|
wevuDefaults: options?.wevuDefaults,
|
|
@@ -8228,6 +8366,7 @@ async function compileScriptPhase(descriptor, descriptorForCompile, filename, op
|
|
|
8228
8366
|
layoutHosts: templateCompiled?.layoutHosts,
|
|
8229
8367
|
inlineExpressions: templateCompiled?.inlineExpressions,
|
|
8230
8368
|
functionPropPaths: templateCompiled?.functionPropPaths,
|
|
8369
|
+
propsAliases,
|
|
8231
8370
|
relaxStructuredTypeOnlyProps
|
|
8232
8371
|
});
|
|
8233
8372
|
return {
|
|
@@ -8312,10 +8451,19 @@ async function compileVueFile(source, filename, options) {
|
|
|
8312
8451
|
autoUsingComponents,
|
|
8313
8452
|
autoImportTags
|
|
8314
8453
|
});
|
|
8454
|
+
const scriptCompiled = parsed.descriptor.script || parsed.descriptor.scriptSetup ? compileScript(parsed.descriptorForCompile, {
|
|
8455
|
+
id: filename,
|
|
8456
|
+
isProd: false
|
|
8457
|
+
}) : void 0;
|
|
8458
|
+
const propsAliases = scriptCompiled ? resolveScriptSetupPropsAliases(scriptCompiled.bindings) : void 0;
|
|
8315
8459
|
const baseTemplateOptions = parsed.isAppFile ? {
|
|
8316
8460
|
...options?.template,
|
|
8461
|
+
propsAliases,
|
|
8317
8462
|
scopedSlotsRequireProps: true
|
|
8318
|
-
} :
|
|
8463
|
+
} : {
|
|
8464
|
+
...options?.template,
|
|
8465
|
+
propsAliases
|
|
8466
|
+
};
|
|
8319
8467
|
const templateOptions = componentSourceInfo.wevuComponentTags.size ? {
|
|
8320
8468
|
...baseTemplateOptions,
|
|
8321
8469
|
wevuComponentTags: componentSourceInfo.wevuComponentTags
|
|
@@ -8324,7 +8472,7 @@ async function compileVueFile(source, filename, options) {
|
|
|
8324
8472
|
wevuComponentTags: []
|
|
8325
8473
|
};
|
|
8326
8474
|
const templateCompiled = compileTemplatePhase(parsed.descriptor, filename, templateOptions, result);
|
|
8327
|
-
const scriptPhase = await compileScriptPhase(parsed.descriptor, parsed.descriptorForCompile, filename, options, autoUsingComponents, templateCompiled, parsed.isAppFile, componentSourceInfo);
|
|
8475
|
+
const scriptPhase = await compileScriptPhase(parsed.descriptor, parsed.descriptorForCompile, filename, options, autoUsingComponents, templateCompiled, parsed.isAppFile, componentSourceInfo, scriptCompiled);
|
|
8328
8476
|
result.script = scriptPhase.script;
|
|
8329
8477
|
result.scriptMap = scriptPhase.scriptMap;
|
|
8330
8478
|
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.
|
|
4
|
+
"version": "6.16.20",
|
|
5
5
|
"description": "wevu 编译器基础包,面向小程序模板的编译与转换",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -45,15 +45,15 @@
|
|
|
45
45
|
"@vue/compiler-core": "^3.5.34",
|
|
46
46
|
"@vue/compiler-dom": "^3.5.34",
|
|
47
47
|
"comment-json": "^5.0.0",
|
|
48
|
-
"lru-cache": "^11.
|
|
48
|
+
"lru-cache": "^11.5.0",
|
|
49
49
|
"magic-string": "^0.30.21",
|
|
50
50
|
"merge": "^2.1.1",
|
|
51
51
|
"pathe": "^2.0.3",
|
|
52
52
|
"vue": "^3.5.34",
|
|
53
53
|
"@weapp-core/constants": "0.1.8",
|
|
54
54
|
"@weapp-core/shared": "3.0.4",
|
|
55
|
-
"@weapp-vite/ast": "6.16.
|
|
56
|
-
"rolldown-require": "2.0.
|
|
55
|
+
"@weapp-vite/ast": "6.16.20",
|
|
56
|
+
"rolldown-require": "2.0.17"
|
|
57
57
|
},
|
|
58
58
|
"publishConfig": {
|
|
59
59
|
"access": "public",
|