@wevu/compiler 6.15.17 → 6.16.0
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 +11 -4
- package/dist/index.mjs +279 -90
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -283,6 +283,7 @@ interface TemplateCompileOptions {
|
|
|
283
283
|
mustacheInterpolation?: MustacheInterpolationMode;
|
|
284
284
|
wxsExtension?: string;
|
|
285
285
|
classStyleWxsSrc?: string;
|
|
286
|
+
wevuComponentTags?: Iterable<string>;
|
|
286
287
|
}
|
|
287
288
|
/**
|
|
288
289
|
* 作用域插槽编译模式。
|
|
@@ -384,6 +385,12 @@ declare function getMiniProgramTemplatePlatform(platform?: MpPlatform): MiniProg
|
|
|
384
385
|
declare function compileVueTemplateToWxml(template: string, filename: string, options?: TemplateCompileOptions): TemplateCompileResult;
|
|
385
386
|
//#endregion
|
|
386
387
|
//#region src/plugins/vue/transform/compileVueFile/types.d.ts
|
|
388
|
+
interface ResolvedUsingComponentInfo {
|
|
389
|
+
from?: string;
|
|
390
|
+
resolvedId?: string;
|
|
391
|
+
sourceType?: 'wevu-sfc' | 'native';
|
|
392
|
+
}
|
|
393
|
+
type ResolvedUsingComponentPath = string | ResolvedUsingComponentInfo;
|
|
387
394
|
/**
|
|
388
395
|
* Vue 单文件组件转换结果。
|
|
389
396
|
*/
|
|
@@ -415,7 +422,7 @@ interface AutoUsingComponentsOptions {
|
|
|
415
422
|
localName: string;
|
|
416
423
|
importedName?: string;
|
|
417
424
|
kind: 'default' | 'named';
|
|
418
|
-
}) => Promise<
|
|
425
|
+
}) => Promise<ResolvedUsingComponentPath | undefined>;
|
|
419
426
|
warn?: (message: string) => void;
|
|
420
427
|
}
|
|
421
428
|
/**
|
|
@@ -423,10 +430,10 @@ interface AutoUsingComponentsOptions {
|
|
|
423
430
|
*/
|
|
424
431
|
interface AutoImportTagsOptions {
|
|
425
432
|
enabled?: boolean;
|
|
426
|
-
resolveUsingComponent?: (tag: string, importerFilename: string) => Promise<{
|
|
433
|
+
resolveUsingComponent?: (tag: string, importerFilename: string) => Promise<({
|
|
427
434
|
name: string;
|
|
428
435
|
from: string;
|
|
429
|
-
} | undefined>;
|
|
436
|
+
} & ResolvedUsingComponentInfo) | undefined>;
|
|
430
437
|
warn?: (message: string) => void;
|
|
431
438
|
}
|
|
432
439
|
/**
|
|
@@ -850,4 +857,4 @@ interface CollectVueTemplateTagsOptions {
|
|
|
850
857
|
*/
|
|
851
858
|
declare function collectVueTemplateTags(template: string, options: CollectVueTemplateTagsOptions): Set<string>;
|
|
852
859
|
//#endregion
|
|
853
|
-
export { type AstEngineName, type AutoImportTagsOptions, type AutoUsingComponentsOptions, CLASS_STYLE_WXS_FILE, CLASS_STYLE_WXS_MODULE, type ClassStyleBinding, type ClassStyleRuntime, type CollectVueTemplateTagsOptions, type CompileVueFileOptions, type ForParseResult, type InlineExpressionAsset, type JsLikeLang, type JsonConfig, type JsonMergeContext, type JsonMergeStage, type JsonMergeStrategy, type MiniProgramPlatform, type ModuleResolver, type MpPlatform, RESERVED_VUE_COMPONENT_TAGS, type ReadAndParseSfcOptions, type ResolveSfcBlockSrcOptions, type ScopedSlotComponentAsset, type StyleCompileOptions, type StyleCompileResult, type TemplateCompileOptions, type TemplateCompileResult, type TemplateRefBinding, type TransformResult, type TransformScriptOptions, VUE_COMPONENT_TAG_RE, type VueTransformResult, WE_VU_MODULE_ID, WE_VU_PAGE_HOOK_TO_FEATURE, WE_VU_RUNTIME_APIS, type WevuDefaults, type WevuPageFeatureFlag, type WevuPageHookName, WevuRuntimeApiName, alipayPlatform, buildClassStyleComputedCode, buildClassStyleWxsTag, builtinComponentsSet, clearFileCaches, collectTargetOptionsObjectsFromCode, collectVueTemplateTags, collectWevuFeaturesFromCodeReachableImports, collectWevuPageFeatureFlags, collectWevuPageFeatureFlagsFromCode, compileConfigBlocks, compileJsxFile, compileVueFile as compileSfc, compileVueFile, compileVueStyleToWxss as compileStyle, compileVueStyleToWxss, compileVueTemplateToWxml as compileTemplate, compileVueTemplateToWxml, createJsonMerger, createPageEntryMatcher, defaultMiniProgramPlatform, defaultMiniProgramTemplatePlatform, defaultPlatform, evaluateJsLikeConfig, extractJsonMacroFromScriptSetup, generateScopedId, getClassStyleWxsSource, getDefaultMiniProgramPlatform, getDefaultMiniProgramTemplatePlatform, getMiniProgramTemplatePlatform, getSfcCheckMtime, injectWevuPageFeatureFlagsIntoOptionsObject, injectWevuPageFeaturesInJs, injectWevuPageFeaturesInJsWithResolver, invalidateFileCache, isAutoImportCandidateTag, isBuiltinComponent, isInvalidate, isJsonLikeLang, loadCache, mergeJsonWithStrategy, mtimeCache, normalizeConfigLang, pathExists, preprocessScriptSetupSrc, preprocessScriptSrc, readAndParseSfc, readFile, resolveClassStyleWxsLocation, resolveJsLikeLang, resolveSfcBlockSrc, restoreScriptSetupSrc, restoreScriptSrc, stripJsonMacroCallsFromCode, swanPlatform, transformScript, transformScript as transformSfcScript, ttPlatform, wechatPlatform };
|
|
860
|
+
export { type AstEngineName, type AutoImportTagsOptions, type AutoUsingComponentsOptions, CLASS_STYLE_WXS_FILE, CLASS_STYLE_WXS_MODULE, type ClassStyleBinding, type ClassStyleRuntime, type CollectVueTemplateTagsOptions, type CompileVueFileOptions, type ForParseResult, type InlineExpressionAsset, type JsLikeLang, type JsonConfig, type JsonMergeContext, type JsonMergeStage, type JsonMergeStrategy, type MiniProgramPlatform, type ModuleResolver, type MpPlatform, RESERVED_VUE_COMPONENT_TAGS, type ReadAndParseSfcOptions, type ResolveSfcBlockSrcOptions, type ResolvedUsingComponentInfo, type ResolvedUsingComponentPath, type ScopedSlotComponentAsset, type StyleCompileOptions, type StyleCompileResult, type TemplateCompileOptions, type TemplateCompileResult, type TemplateRefBinding, type TransformResult, type TransformScriptOptions, VUE_COMPONENT_TAG_RE, type VueTransformResult, WE_VU_MODULE_ID, WE_VU_PAGE_HOOK_TO_FEATURE, WE_VU_RUNTIME_APIS, type WevuDefaults, type WevuPageFeatureFlag, type WevuPageHookName, WevuRuntimeApiName, alipayPlatform, buildClassStyleComputedCode, buildClassStyleWxsTag, builtinComponentsSet, clearFileCaches, collectTargetOptionsObjectsFromCode, collectVueTemplateTags, collectWevuFeaturesFromCodeReachableImports, collectWevuPageFeatureFlags, collectWevuPageFeatureFlagsFromCode, compileConfigBlocks, compileJsxFile, compileVueFile as compileSfc, compileVueFile, compileVueStyleToWxss as compileStyle, compileVueStyleToWxss, compileVueTemplateToWxml as compileTemplate, compileVueTemplateToWxml, createJsonMerger, createPageEntryMatcher, defaultMiniProgramPlatform, defaultMiniProgramTemplatePlatform, defaultPlatform, evaluateJsLikeConfig, extractJsonMacroFromScriptSetup, generateScopedId, getClassStyleWxsSource, getDefaultMiniProgramPlatform, getDefaultMiniProgramTemplatePlatform, getMiniProgramTemplatePlatform, getSfcCheckMtime, injectWevuPageFeatureFlagsIntoOptionsObject, injectWevuPageFeaturesInJs, injectWevuPageFeaturesInJsWithResolver, invalidateFileCache, isAutoImportCandidateTag, isBuiltinComponent, isInvalidate, isJsonLikeLang, loadCache, mergeJsonWithStrategy, mtimeCache, normalizeConfigLang, pathExists, preprocessScriptSetupSrc, preprocessScriptSrc, readAndParseSfc, readFile, resolveClassStyleWxsLocation, resolveJsLikeLang, resolveSfcBlockSrc, restoreScriptSetupSrc, restoreScriptSrc, stripJsonMacroCallsFromCode, swanPlatform, transformScript, transformScript as transformSfcScript, ttPlatform, wechatPlatform };
|
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_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_OWNER_ATTR, WEVU_SLOT_OWNER_ID_ATTR, WEVU_SLOT_OWNER_ID_KEY, WEVU_SLOT_OWNER_ID_PROP, WEVU_SLOT_OWNER_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_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_ATTR, WEVU_SLOT_OWNER_ID_ATTR, WEVU_SLOT_OWNER_ID_KEY, WEVU_SLOT_OWNER_ID_PROP, WEVU_SLOT_OWNER_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";
|
|
@@ -1767,6 +1767,7 @@ function injectTemplateComponentMeta(ast, templateComponentMeta) {
|
|
|
1767
1767
|
* 1. 移除从 'vue' 导入 defineComponent
|
|
1768
1768
|
* 2. 移除 __name 属性
|
|
1769
1769
|
* 3. 移除空参数的 __expose() 调用(保留 defineExpose 生成的 __expose({...}))
|
|
1770
|
+
* 4. 移除运行时未使用的 __isScriptSetup 标识
|
|
1770
1771
|
*/
|
|
1771
1772
|
function vueSfcTransformPlugin() {
|
|
1772
1773
|
return {
|
|
@@ -1794,6 +1795,10 @@ function vueSfcTransformPlugin() {
|
|
|
1794
1795
|
path.node.properties = filtered;
|
|
1795
1796
|
},
|
|
1796
1797
|
CallExpression(path) {
|
|
1798
|
+
if (t.isMemberExpression(path.node.callee) && t.isIdentifier(path.node.callee.object, { name: "Object" }) && t.isIdentifier(path.node.callee.property, { name: "defineProperty" }) && t.isIdentifier(path.node.arguments[0], { name: "__returned__" }) && t.isStringLiteral(path.node.arguments[1], { value: "__isScriptSetup" }) && path.parentPath?.isExpressionStatement()) {
|
|
1799
|
+
path.parentPath.remove();
|
|
1800
|
+
return;
|
|
1801
|
+
}
|
|
1797
1802
|
if (t.isIdentifier(path.node.callee, { name: "__expose" }) && path.parentPath?.isExpressionStatement() && path.node.arguments.length === 0) path.parentPath.remove();
|
|
1798
1803
|
}
|
|
1799
1804
|
}
|
|
@@ -3901,6 +3906,10 @@ function compileJsxTemplateAndCollectComponents(source, filename, options) {
|
|
|
3901
3906
|
//#region src/plugins/jsx/compileJsxFile.ts
|
|
3902
3907
|
const LEADING_DOT_RE = /^\./;
|
|
3903
3908
|
const SETUP_CALL_RE$1 = /\bsetup\s*\(/;
|
|
3909
|
+
function normalizeUsingComponentFrom(value) {
|
|
3910
|
+
if (!value) return;
|
|
3911
|
+
return typeof value === "string" ? value : value.from;
|
|
3912
|
+
}
|
|
3904
3913
|
/**
|
|
3905
3914
|
* 编译 JSX/TSX 文件,输出 wevu 脚本与 WXML 模板。
|
|
3906
3915
|
*/
|
|
@@ -3928,11 +3937,11 @@ async function compileJsxFile(source, filename, options) {
|
|
|
3928
3937
|
const autoUsingComponentsMap = {};
|
|
3929
3938
|
if (options?.autoUsingComponents?.resolveUsingComponentPath && autoComponentContext.templateTags.size > 0) for (const imported of autoComponentContext.importedComponents) {
|
|
3930
3939
|
if (!autoComponentContext.templateTags.has(imported.localName)) continue;
|
|
3931
|
-
let resolved = await options.autoUsingComponents.resolveUsingComponentPath(imported.importSource, filename, {
|
|
3940
|
+
let resolved = normalizeUsingComponentFrom(await options.autoUsingComponents.resolveUsingComponentPath(imported.importSource, filename, {
|
|
3932
3941
|
localName: imported.localName,
|
|
3933
3942
|
importedName: imported.importedName,
|
|
3934
3943
|
kind: imported.kind
|
|
3935
|
-
});
|
|
3944
|
+
}));
|
|
3936
3945
|
if (!resolved && imported.importSource.startsWith("/")) resolved = removeExtensionDeep(imported.importSource);
|
|
3937
3946
|
if (!resolved) continue;
|
|
3938
3947
|
autoUsingComponentsMap[imported.localName] = resolved;
|
|
@@ -6270,6 +6279,7 @@ function collectSlotBindingExpression(node, context) {
|
|
|
6270
6279
|
}
|
|
6271
6280
|
//#endregion
|
|
6272
6281
|
//#region src/plugins/vue/compiler/template/elements/tag-slot.ts
|
|
6282
|
+
const SLOT_PRESENCE_SCAN_LIMIT = 32;
|
|
6273
6283
|
function renderSlotNameAttribute(info, context, attrName) {
|
|
6274
6284
|
if (info.type === "static" && info.value !== "default") return `${attrName}="${info.value}"`;
|
|
6275
6285
|
if (info.type === "dynamic") return `${attrName}="${renderMustache(normalizeWxmlExpressionWithContext(info.exp, context), context)}"`;
|
|
@@ -6319,11 +6329,23 @@ function stringifySlotName(info, context) {
|
|
|
6319
6329
|
if (info.type === "static") return info.value === "default" ? "'default'" : `'${info.value}'`;
|
|
6320
6330
|
return normalizeWxmlExpressionWithContext(info.exp, context);
|
|
6321
6331
|
}
|
|
6322
|
-
function
|
|
6332
|
+
function resolveSlotStaticName(info) {
|
|
6333
|
+
if (info.type === "default") return "default";
|
|
6334
|
+
if (info.type === "static") return info.value || "default";
|
|
6335
|
+
}
|
|
6336
|
+
function createSlotPresenceExpression(info) {
|
|
6337
|
+
const slotName = resolveSlotStaticName(info);
|
|
6338
|
+
if (!slotName) return;
|
|
6339
|
+
const slotLiteral = `'${slotName.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
|
|
6340
|
+
return `${WEVU_SLOT_NAMES_PROP}&&(${Array.from({ length: SLOT_PRESENCE_SCAN_LIMIT }, (_, index) => `${WEVU_SLOT_NAMES_PROP}[${index}]==${slotLiteral}`).join("||")})`;
|
|
6341
|
+
}
|
|
6342
|
+
function buildSlotDeclaration(name, propsExp, children, context, options) {
|
|
6323
6343
|
return {
|
|
6324
6344
|
name,
|
|
6325
6345
|
props: propsExp ? parseSlotPropsExpression(propsExp, context) : {},
|
|
6326
|
-
children
|
|
6346
|
+
children,
|
|
6347
|
+
implicitDefault: options?.implicitDefault,
|
|
6348
|
+
condition: options?.condition
|
|
6327
6349
|
};
|
|
6328
6350
|
}
|
|
6329
6351
|
function createScopedSlotComponent(context, slotKey, props, children, transformNode) {
|
|
@@ -6382,16 +6404,19 @@ function renderSlotFallback(decl, context, transformNode) {
|
|
|
6382
6404
|
const rawContent = rawRenderedChildren.map((item) => item.code).join("");
|
|
6383
6405
|
if (!rawContent) return "";
|
|
6384
6406
|
const slotAttr = renderSlotNameAttribute(decl.name, context, "slot");
|
|
6385
|
-
|
|
6386
|
-
|
|
6407
|
+
const wrapCondition = (content) => {
|
|
6408
|
+
return decl.condition ? context.platform.wrapIf(decl.condition, content, (exp) => renderMustache(exp, context)) : content;
|
|
6409
|
+
};
|
|
6410
|
+
if (!slotAttr) return wrapCondition(rawContent);
|
|
6411
|
+
if (!context.slotSingleRootNoWrapper) return wrapCondition(`<view ${slotAttr}>${rawContent}</view>`);
|
|
6387
6412
|
const renderedChildren = rawRenderedChildren.filter((item) => item.code.trim().length > 0);
|
|
6388
6413
|
if (!renderedChildren.length) return "";
|
|
6389
6414
|
const content = renderedChildren.map((item) => item.code).join("");
|
|
6390
6415
|
if (renderedChildren.length === 1) {
|
|
6391
6416
|
const projected = injectAttributeIntoOpeningTag(renderedChildren[0].code, slotAttr);
|
|
6392
|
-
if (projected) return projected;
|
|
6417
|
+
if (projected) return wrapCondition(projected);
|
|
6393
6418
|
}
|
|
6394
|
-
return `<view ${slotAttr}>${content}</view
|
|
6419
|
+
return wrapCondition(`<view ${slotAttr}>${content}</view>`);
|
|
6395
6420
|
}
|
|
6396
6421
|
function transformSlotElement(node, context, transformNode) {
|
|
6397
6422
|
if (isScopedSlotsDisabled(context)) return transformSlotElementPlain(node, context, transformNode);
|
|
@@ -6407,14 +6432,24 @@ function transformSlotElement(node, context, transformNode) {
|
|
|
6407
6432
|
const nameAttr = renderSlotNameAttribute(slotNameInfo, context, "name");
|
|
6408
6433
|
if (nameAttr) slotAttrs.push(nameAttr);
|
|
6409
6434
|
const slotAttrString = slotAttrs.length ? ` ${slotAttrs.join(" ")}` : "";
|
|
6410
|
-
|
|
6411
|
-
if (
|
|
6435
|
+
let slotTag = `<slot${slotAttrString} />`;
|
|
6436
|
+
if (fallbackContent) {
|
|
6437
|
+
const slotPresentExp = createSlotPresenceExpression(slotNameInfo);
|
|
6438
|
+
if (!slotPropsExp && slotPresentExp) slotTag = `${context.platform.wrapIf(slotPresentExp, slotTag, (exp) => renderMustache(exp, context))}${context.platform.wrapElse(fallbackContent)}`;
|
|
6439
|
+
else slotTag = `<slot${slotAttrString}>${fallbackContent}</slot>`;
|
|
6440
|
+
}
|
|
6441
|
+
if (!slotPropsExp && (context.scopedSlotsRequireProps || slotNameInfo.type !== "default")) return slotTag;
|
|
6412
6442
|
const genericKey = `scoped-slots-${resolveSlotKey(context, slotNameInfo)}`;
|
|
6413
6443
|
context.componentGenerics[genericKey] = true;
|
|
6414
6444
|
slotPropsExp = slotPropsExp ?? "[]";
|
|
6415
|
-
const scopedAttrs = [
|
|
6445
|
+
const scopedAttrs = [
|
|
6446
|
+
`${context.platform.directives.ifAttr}="${renderMustache(WEVU_SLOT_OWNER_ID_PROP, context)}"`,
|
|
6447
|
+
`${WEVU_SLOT_OWNER_ATTR}="${renderMustache(WEVU_SLOT_OWNER_ID_PROP, context)}"`,
|
|
6448
|
+
`${WEVU_SLOT_PROPS_ATTR}="${renderMustache(slotPropsExp, context)}"`
|
|
6449
|
+
];
|
|
6416
6450
|
if (context.slotMultipleInstance) scopedAttrs.push(`${WEVU_SLOT_SCOPE_ATTR}="${renderMustache(WEVU_SLOT_SCOPE_KEY, context)}"`);
|
|
6417
|
-
|
|
6451
|
+
const scopedTag = `<${genericKey}${scopedAttrs.length ? ` ${scopedAttrs.join(" ")}` : ""} />`;
|
|
6452
|
+
return `${slotTag}${scopedTag}`;
|
|
6418
6453
|
}
|
|
6419
6454
|
function transformSlotElementPlain(node, context, transformNode) {
|
|
6420
6455
|
const slotNameInfo = resolveSlotNameFromSlotElement(node);
|
|
@@ -6431,6 +6466,49 @@ function transformSlotElementPlain(node, context, transformNode) {
|
|
|
6431
6466
|
}
|
|
6432
6467
|
//#endregion
|
|
6433
6468
|
//#region src/plugins/vue/compiler/template/elements/tag-component.ts
|
|
6469
|
+
function hasLegacySlotAttribute(children) {
|
|
6470
|
+
return children.some((child) => {
|
|
6471
|
+
if (child.type !== NodeTypes.ELEMENT) return false;
|
|
6472
|
+
return child.props.some((prop) => prop.type === NodeTypes.ATTRIBUTE && prop.name === "slot");
|
|
6473
|
+
});
|
|
6474
|
+
}
|
|
6475
|
+
function isRenderableSlotChild(child) {
|
|
6476
|
+
if (child.type === NodeTypes.COMMENT) return false;
|
|
6477
|
+
if (child.type === NodeTypes.TEXT) return child.content.trim().length > 0;
|
|
6478
|
+
return true;
|
|
6479
|
+
}
|
|
6480
|
+
function hasDirectComponentSlotChild(children, context) {
|
|
6481
|
+
return children.some((child) => {
|
|
6482
|
+
if (child.type !== NodeTypes.ELEMENT) return false;
|
|
6483
|
+
if (child.tag === "component") return true;
|
|
6484
|
+
if (child.tag === "template") return false;
|
|
6485
|
+
if (isBuiltinTag(resolveTemplateTagName(child.tag, context))) return false;
|
|
6486
|
+
return /^[A-Z]/.test(child.tag);
|
|
6487
|
+
});
|
|
6488
|
+
}
|
|
6489
|
+
function resolveTemplateSlotCondition(node, context) {
|
|
6490
|
+
const ifDirective = node.props.find((prop) => prop.type === NodeTypes.DIRECTIVE && prop.name === "if" && prop.exp?.type === NodeTypes.SIMPLE_EXPRESSION);
|
|
6491
|
+
const rawExp = ifDirective?.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? ifDirective.exp.content : "";
|
|
6492
|
+
return rawExp ? normalizeWxmlExpressionWithContext(rawExp, context) : void 0;
|
|
6493
|
+
}
|
|
6494
|
+
function pushSlotNamesAttr(attrs, slotNames, context) {
|
|
6495
|
+
if (!slotNames.length) return;
|
|
6496
|
+
const seen = /* @__PURE__ */ new Set();
|
|
6497
|
+
const entries = [];
|
|
6498
|
+
for (const item of slotNames) {
|
|
6499
|
+
const dedupeKey = `${item.name}:${item.condition ?? ""}`;
|
|
6500
|
+
if (seen.has(dedupeKey)) continue;
|
|
6501
|
+
seen.add(dedupeKey);
|
|
6502
|
+
entries.push(item.condition ? `((${item.condition}) ? ${item.name} : '')` : item.name);
|
|
6503
|
+
}
|
|
6504
|
+
attrs.push(`${WEVU_SLOT_NAMES_ATTR}="${renderMustache(`[${entries.join(",")}]`, context)}"`);
|
|
6505
|
+
}
|
|
6506
|
+
function shouldExposePlainSlotPresence(node) {
|
|
6507
|
+
return node.tag === "component";
|
|
6508
|
+
}
|
|
6509
|
+
function isWevuComponentTag(node, context) {
|
|
6510
|
+
return context.wevuComponentTags ? context.wevuComponentTags.has(node.tag) : /^[A-Z]/.test(node.tag);
|
|
6511
|
+
}
|
|
6434
6512
|
function transformComponentWithSlots(node, context, transformNode, options) {
|
|
6435
6513
|
if (isScopedSlotsDisabled(context)) return transformComponentWithSlotsFallback(node, context, transformNode, options);
|
|
6436
6514
|
const extraAttrs = options?.extraAttrs ?? [];
|
|
@@ -6442,18 +6520,20 @@ function transformComponentWithSlots(node, context, transformNode, options) {
|
|
|
6442
6520
|
const templateSlot = findSlotDirective(child);
|
|
6443
6521
|
if (templateSlot) {
|
|
6444
6522
|
const slotName = resolveSlotNameFromDirective(templateSlot);
|
|
6445
|
-
slotDeclarations.push(buildSlotDeclaration(slotName, templateSlot.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? templateSlot.exp.content : void 0, child.children, context));
|
|
6523
|
+
slotDeclarations.push(buildSlotDeclaration(slotName, templateSlot.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? templateSlot.exp.content : void 0, child.children, context, { condition: resolveTemplateSlotCondition(child, context) }));
|
|
6446
6524
|
continue;
|
|
6447
6525
|
}
|
|
6448
6526
|
}
|
|
6449
6527
|
nonTemplateChildren.push(child);
|
|
6450
6528
|
}
|
|
6529
|
+
const defaultSlotChildren = nonTemplateChildren.filter(isRenderableSlotChild);
|
|
6451
6530
|
if (slotDirective) {
|
|
6452
6531
|
if (slotDeclarations.length) context.warnings.push("组件上的 v-slot 与 <template v-slot> 不能同时使用;仅使用组件上的 v-slot。");
|
|
6453
6532
|
slotDeclarations.length = 0;
|
|
6454
6533
|
slotDeclarations.push(buildSlotDeclaration(resolveSlotNameFromDirective(slotDirective), slotDirective.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? slotDirective.exp.content : void 0, node.children, context));
|
|
6455
|
-
} else if (slotDeclarations.length &&
|
|
6456
|
-
else slotDeclarations.push(buildSlotDeclaration({ type: "default" }, void 0,
|
|
6534
|
+
} else if (slotDeclarations.length && defaultSlotChildren.length) if (slotDeclarations.some((decl) => decl.name.type === "default" || decl.name.type === "static" && decl.name.value === "default")) context.warnings.push("存在显式的 v-slot:default,默认插槽内容将被忽略。");
|
|
6535
|
+
else slotDeclarations.push(buildSlotDeclaration({ type: "default" }, void 0, defaultSlotChildren, context, { implicitDefault: true }));
|
|
6536
|
+
else if (!slotDeclarations.length && defaultSlotChildren.length && !context.scopedSlotsRequireProps && !hasLegacySlotAttribute(defaultSlotChildren)) slotDeclarations.push(buildSlotDeclaration({ type: "default" }, void 0, defaultSlotChildren, context, { implicitDefault: true }));
|
|
6457
6537
|
if (!slotDeclarations.length) {
|
|
6458
6538
|
const { attrs, vTextExp } = collectElementAttributes(node, context, {
|
|
6459
6539
|
skipSlotDirective: true,
|
|
@@ -6462,25 +6542,30 @@ function transformComponentWithSlots(node, context, transformNode, options) {
|
|
|
6462
6542
|
});
|
|
6463
6543
|
let children = node.children.map((child) => transformNode(child, context)).join("");
|
|
6464
6544
|
if (vTextExp !== void 0) children = renderMustache(vTextExp, context);
|
|
6545
|
+
if (children && defaultSlotChildren.length && !hasLegacySlotAttribute(defaultSlotChildren) && isWevuComponentTag(node, context)) pushSlotNamesAttr(attrs, [{ name: "'default'" }], context);
|
|
6465
6546
|
const attrString = attrs.length ? ` ${attrs.join(" ")}` : "";
|
|
6466
6547
|
const { tag } = node;
|
|
6467
6548
|
return children ? `<${tag}${attrString}>${children}</${tag}>` : `<${tag}${attrString} />`;
|
|
6468
6549
|
}
|
|
6469
6550
|
const scopedSlotDeclarations = [];
|
|
6470
6551
|
const plainSlotDeclarations = [];
|
|
6471
|
-
for (const decl of slotDeclarations)
|
|
6472
|
-
|
|
6473
|
-
if (!context.scopedSlotsRequireProps || hasSlotProps) scopedSlotDeclarations.push(decl);
|
|
6474
|
-
else plainSlotDeclarations.push(decl);
|
|
6475
|
-
}
|
|
6552
|
+
for (const decl of slotDeclarations) if (Object.keys(decl.props).length > 0 || !context.scopedSlotsRequireProps && decl.implicitDefault && hasDirectComponentSlotChild(decl.children, context)) scopedSlotDeclarations.push(decl);
|
|
6553
|
+
else plainSlotDeclarations.push(decl);
|
|
6476
6554
|
const slotNames = [];
|
|
6477
6555
|
const slotGenericAttrs = [];
|
|
6478
6556
|
for (const decl of scopedSlotDeclarations) {
|
|
6479
6557
|
const slotKey = resolveSlotKey(context, decl.name);
|
|
6480
6558
|
const { componentName } = createScopedSlotComponent(context, slotKey, decl.props, decl.children, transformNode);
|
|
6481
|
-
slotNames.push(
|
|
6559
|
+
slotNames.push({
|
|
6560
|
+
name: stringifySlotName(decl.name, context),
|
|
6561
|
+
condition: decl.condition
|
|
6562
|
+
});
|
|
6482
6563
|
slotGenericAttrs.push(`generic:scoped-slots-${slotKey}="${componentName}"`);
|
|
6483
6564
|
}
|
|
6565
|
+
if (shouldExposePlainSlotPresence(node) || isWevuComponentTag(node, context)) for (const decl of plainSlotDeclarations) slotNames.push({
|
|
6566
|
+
name: stringifySlotName(decl.name, context),
|
|
6567
|
+
condition: decl.condition
|
|
6568
|
+
});
|
|
6484
6569
|
const { attrs } = collectElementAttributes(node, context, {
|
|
6485
6570
|
skipSlotDirective: true,
|
|
6486
6571
|
forInfo: options?.forInfo,
|
|
@@ -6491,7 +6576,7 @@ function transformComponentWithSlots(node, context, transformNode, options) {
|
|
|
6491
6576
|
...attrs,
|
|
6492
6577
|
...slotGenericAttrs
|
|
6493
6578
|
];
|
|
6494
|
-
|
|
6579
|
+
pushSlotNamesAttr(mergedAttrs, slotNames, context);
|
|
6495
6580
|
if (scopedSlotDeclarations.length) {
|
|
6496
6581
|
const scopePropsExp = buildScopePropsExpression(context);
|
|
6497
6582
|
if (scopePropsExp) mergedAttrs.push(`${WEVU_SLOT_SCOPE_ATTR}="${renderMustache(scopePropsExp, context)}"`);
|
|
@@ -6512,18 +6597,19 @@ function transformComponentWithSlotsFallback(node, context, transformNode, optio
|
|
|
6512
6597
|
const templateSlot = findSlotDirective(child);
|
|
6513
6598
|
if (templateSlot) {
|
|
6514
6599
|
const slotName = resolveSlotNameFromDirective(templateSlot);
|
|
6515
|
-
slotDeclarations.push(buildSlotDeclaration(slotName, templateSlot.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? templateSlot.exp.content : void 0, child.children, context));
|
|
6600
|
+
slotDeclarations.push(buildSlotDeclaration(slotName, templateSlot.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? templateSlot.exp.content : void 0, child.children, context, { condition: resolveTemplateSlotCondition(child, context) }));
|
|
6516
6601
|
continue;
|
|
6517
6602
|
}
|
|
6518
6603
|
}
|
|
6519
6604
|
nonTemplateChildren.push(child);
|
|
6520
6605
|
}
|
|
6606
|
+
const defaultSlotChildren = nonTemplateChildren.filter(isRenderableSlotChild);
|
|
6521
6607
|
if (slotDirective) {
|
|
6522
6608
|
if (slotDeclarations.length) context.warnings.push("组件上的 v-slot 与 <template v-slot> 不能同时使用;仅使用组件上的 v-slot。");
|
|
6523
6609
|
slotDeclarations.length = 0;
|
|
6524
6610
|
slotDeclarations.push(buildSlotDeclaration(resolveSlotNameFromDirective(slotDirective), slotDirective.exp?.type === NodeTypes.SIMPLE_EXPRESSION ? slotDirective.exp.content : void 0, node.children, context));
|
|
6525
|
-
} else if (slotDeclarations.length &&
|
|
6526
|
-
else slotDeclarations.push(buildSlotDeclaration({ type: "default" }, void 0,
|
|
6611
|
+
} else if (slotDeclarations.length && defaultSlotChildren.length) if (slotDeclarations.some((decl) => decl.name.type === "default" || decl.name.type === "static" && decl.name.value === "default")) context.warnings.push("存在显式的 v-slot:default,默认插槽内容将被忽略。");
|
|
6612
|
+
else slotDeclarations.push(buildSlotDeclaration({ type: "default" }, void 0, defaultSlotChildren, context));
|
|
6527
6613
|
if (!slotDeclarations.length) {
|
|
6528
6614
|
const { attrs, vTextExp } = collectElementAttributes(node, context, {
|
|
6529
6615
|
skipSlotDirective: true,
|
|
@@ -6532,6 +6618,7 @@ function transformComponentWithSlotsFallback(node, context, transformNode, optio
|
|
|
6532
6618
|
});
|
|
6533
6619
|
let children = node.children.map((child) => transformNode(child, context)).join("");
|
|
6534
6620
|
if (vTextExp !== void 0) children = renderMustache(vTextExp, context);
|
|
6621
|
+
if (children && defaultSlotChildren.length && !hasLegacySlotAttribute(defaultSlotChildren) && isWevuComponentTag(node, context)) pushSlotNamesAttr(attrs, [{ name: "'default'" }], context);
|
|
6535
6622
|
const attrString = attrs.length ? ` ${attrs.join(" ")}` : "";
|
|
6536
6623
|
const { tag } = node;
|
|
6537
6624
|
return children ? `<${tag}${attrString}>${children}</${tag}>` : `<${tag}${attrString} />`;
|
|
@@ -6544,6 +6631,10 @@ function transformComponentWithSlotsFallback(node, context, transformNode, optio
|
|
|
6544
6631
|
isComponent: true
|
|
6545
6632
|
});
|
|
6546
6633
|
const mergedAttrs = [...extraAttrs, ...attrs];
|
|
6634
|
+
if (shouldExposePlainSlotPresence(node) || isWevuComponentTag(node, context)) pushSlotNamesAttr(mergedAttrs, slotDeclarations.map((decl) => ({
|
|
6635
|
+
name: stringifySlotName(decl.name, context),
|
|
6636
|
+
condition: decl.condition
|
|
6637
|
+
})), context);
|
|
6547
6638
|
const attrString = mergedAttrs.length ? ` ${mergedAttrs.join(" ")}` : "";
|
|
6548
6639
|
const { tag } = node;
|
|
6549
6640
|
return renderedSlots ? `<${tag}${attrString}>${renderedSlots}</${tag}>` : `<${tag}${attrString} />`;
|
|
@@ -6569,7 +6660,8 @@ function transformComponentElement(node, context, transformNode) {
|
|
|
6569
6660
|
const attrs = [];
|
|
6570
6661
|
const slotDirective = findSlotDirective(node);
|
|
6571
6662
|
const templateSlotChildren = node.children.filter((child) => child.type === NodeTypes.ELEMENT && child.tag === "template" && findSlotDirective(child));
|
|
6572
|
-
|
|
6663
|
+
const shouldUseAugmentedDefaultSlot = node.children.length > 0 && !context.scopedSlotsRequireProps;
|
|
6664
|
+
if (slotDirective || templateSlotChildren.length > 0 || shouldUseAugmentedDefaultSlot) return transformComponentWithSlots({
|
|
6573
6665
|
...node,
|
|
6574
6666
|
props: otherProps
|
|
6575
6667
|
}, context, transformNode, { extraAttrs: [`data-is="${renderMustache(componentVar, context)}"`] });
|
|
@@ -6591,7 +6683,9 @@ function transformNormalElement(node, context, transformNode) {
|
|
|
6591
6683
|
const tag = resolveTemplateTagName(node.tag, context);
|
|
6592
6684
|
const slotDirective = findSlotDirective(node);
|
|
6593
6685
|
const templateSlotChildren = node.children.filter((child) => child.type === NodeTypes.ELEMENT && child.tag === "template" && findSlotDirective(child));
|
|
6594
|
-
|
|
6686
|
+
const shouldUseAugmentedDefaultSlot = node.children.length > 0 && !context.scopedSlotsRequireProps && !isBuiltinTag(tag);
|
|
6687
|
+
const shouldUseSlotPresenceMetadata = node.children.length > 0 && (context.wevuComponentTags ? context.wevuComponentTags.has(node.tag) : /^[A-Z]/.test(node.tag));
|
|
6688
|
+
if (slotDirective || templateSlotChildren.length > 0 || shouldUseAugmentedDefaultSlot || shouldUseSlotPresenceMetadata) return transformComponentWithSlots(node, context, transformNode);
|
|
6595
6689
|
const { attrs, vTextExp } = collectElementAttributes(node, context, { resolvedTag: tag });
|
|
6596
6690
|
let children = "";
|
|
6597
6691
|
if (node.children.length > 0) children = node.children.map((child) => transformNode(child, context)).join("");
|
|
@@ -6875,7 +6969,8 @@ function compileVueTemplateToWxml(template, filename, options) {
|
|
|
6875
6969
|
layoutHosts: [],
|
|
6876
6970
|
layoutHostIndexSeed: 0,
|
|
6877
6971
|
inlineExpressions: [],
|
|
6878
|
-
inlineExpressionSeed: 0
|
|
6972
|
+
inlineExpressionSeed: 0,
|
|
6973
|
+
wevuComponentTags: options?.wevuComponentTags ? new Set(options.wevuComponentTags) : void 0
|
|
6879
6974
|
};
|
|
6880
6975
|
let wxml = ast.children.map((child) => transformNode(child, context)).join("");
|
|
6881
6976
|
if (context.classStyleWxs) wxml = `${buildClassStyleWxsTag(context.classStyleWxsExtension || "wxs", context.classStyleWxsSrc)}\n${wxml}`;
|
|
@@ -6903,6 +6998,141 @@ function compileVueTemplateToWxml(template, filename, options) {
|
|
|
6903
6998
|
}
|
|
6904
6999
|
}
|
|
6905
7000
|
//#endregion
|
|
7001
|
+
//#region src/plugins/vue/transform/compileVueFile/componentSources.ts
|
|
7002
|
+
function normalizeResolvedUsingComponent(result) {
|
|
7003
|
+
if (!result) return;
|
|
7004
|
+
if (typeof result === "string") return { from: result };
|
|
7005
|
+
return result;
|
|
7006
|
+
}
|
|
7007
|
+
function isWevuSfcComponent(result) {
|
|
7008
|
+
return result?.sourceType === "wevu-sfc" || Boolean(result?.resolvedId?.endsWith(".vue")) || Boolean(result?.from?.endsWith(".vue"));
|
|
7009
|
+
}
|
|
7010
|
+
function isVueSfcSource(source) {
|
|
7011
|
+
return source.endsWith(".vue");
|
|
7012
|
+
}
|
|
7013
|
+
function pascalToKebab(name) {
|
|
7014
|
+
return name.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1-$2").toLowerCase();
|
|
7015
|
+
}
|
|
7016
|
+
function collectTemplateComponentNames(template, filename, warn) {
|
|
7017
|
+
const warnHandler = resolveWarnHandler(warn);
|
|
7018
|
+
const tags = collectVueTemplateTags(template, {
|
|
7019
|
+
filename,
|
|
7020
|
+
warnLabel: "自动 usingComponents",
|
|
7021
|
+
warn: (message) => warnHandler(message),
|
|
7022
|
+
shouldCollect: isAutoImportCandidateTag
|
|
7023
|
+
});
|
|
7024
|
+
for (const tag of [...tags]) if (tag.includes("-")) {
|
|
7025
|
+
const pascalName = tag.split("-").filter(Boolean).map((segment) => `${segment.charAt(0).toUpperCase()}${segment.slice(1)}`).join("");
|
|
7026
|
+
if (pascalName) tags.add(pascalName);
|
|
7027
|
+
}
|
|
7028
|
+
return tags;
|
|
7029
|
+
}
|
|
7030
|
+
async function collectScriptSetupUsingComponents(options) {
|
|
7031
|
+
const { descriptor, descriptorForCompile, filename, compileOptions, autoUsingComponents, result } = options;
|
|
7032
|
+
if (!descriptor.scriptSetup || !descriptor.template) return;
|
|
7033
|
+
const templateComponentNames = collectTemplateComponentNames(descriptor.template.content, filename, autoUsingComponents?.warn ?? compileOptions?.warn);
|
|
7034
|
+
if (!templateComponentNames.size) return;
|
|
7035
|
+
try {
|
|
7036
|
+
const setupAst = parse$3(descriptorForCompile.scriptSetup.content, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
7037
|
+
const pending = [];
|
|
7038
|
+
traverse(setupAst, { ImportDeclaration(path) {
|
|
7039
|
+
if (path.node.importKind === "type") return;
|
|
7040
|
+
if (!t.isStringLiteral(path.node.source)) return;
|
|
7041
|
+
const importSource = path.node.source.value;
|
|
7042
|
+
for (const specifier of path.node.specifiers) {
|
|
7043
|
+
if ("importKind" in specifier && specifier.importKind === "type") continue;
|
|
7044
|
+
if (!("local" in specifier) || !t.isIdentifier(specifier.local)) continue;
|
|
7045
|
+
const localName = specifier.local.name;
|
|
7046
|
+
if (!templateComponentNames.has(localName)) continue;
|
|
7047
|
+
if (t.isImportDefaultSpecifier(specifier)) pending.push({
|
|
7048
|
+
localName,
|
|
7049
|
+
importSource,
|
|
7050
|
+
importedName: "default",
|
|
7051
|
+
kind: "default"
|
|
7052
|
+
});
|
|
7053
|
+
else if (t.isImportSpecifier(specifier)) {
|
|
7054
|
+
const importedName = t.isIdentifier(specifier.imported) ? specifier.imported.name : t.isStringLiteral(specifier.imported) ? specifier.imported.value : void 0;
|
|
7055
|
+
pending.push({
|
|
7056
|
+
localName,
|
|
7057
|
+
importSource,
|
|
7058
|
+
importedName,
|
|
7059
|
+
kind: "named"
|
|
7060
|
+
});
|
|
7061
|
+
}
|
|
7062
|
+
}
|
|
7063
|
+
} });
|
|
7064
|
+
for (const { localName, importSource, importedName, kind } of pending) {
|
|
7065
|
+
let resolved = autoUsingComponents?.resolveUsingComponentPath ? normalizeResolvedUsingComponent(await autoUsingComponents.resolveUsingComponentPath(importSource, filename, {
|
|
7066
|
+
localName,
|
|
7067
|
+
importedName,
|
|
7068
|
+
kind
|
|
7069
|
+
})) : void 0;
|
|
7070
|
+
if (!resolved?.from && importSource.startsWith("/")) resolved = {
|
|
7071
|
+
from: removeExtensionDeep(importSource),
|
|
7072
|
+
resolvedId: importSource
|
|
7073
|
+
};
|
|
7074
|
+
if (resolved?.from) {
|
|
7075
|
+
result.autoUsingComponentsMap[localName] = resolved.from;
|
|
7076
|
+
result.autoComponentMeta[localName] = resolved.from;
|
|
7077
|
+
}
|
|
7078
|
+
if (isVueSfcSource(importSource) || isWevuSfcComponent(resolved)) {
|
|
7079
|
+
result.wevuComponentTags.add(localName);
|
|
7080
|
+
result.wevuComponentTags.add(pascalToKebab(localName));
|
|
7081
|
+
}
|
|
7082
|
+
}
|
|
7083
|
+
} catch (error) {
|
|
7084
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
7085
|
+
autoUsingComponents?.warn?.(`[Vue 编译] 解析 ${filename} 的 <script setup> 导入失败:${message}`);
|
|
7086
|
+
}
|
|
7087
|
+
}
|
|
7088
|
+
async function collectAutoImportWevuComponents(options) {
|
|
7089
|
+
const { descriptor, filename, autoImportTags, warn, result } = options;
|
|
7090
|
+
if (!autoImportTags || !descriptor.template) return;
|
|
7091
|
+
const warnHandler = resolveWarnHandler(autoImportTags.warn ?? warn);
|
|
7092
|
+
const tags = collectVueTemplateTags(descriptor.template.content, {
|
|
7093
|
+
filename,
|
|
7094
|
+
warnLabel: "自动导入标签",
|
|
7095
|
+
warn: (message) => warnHandler(message),
|
|
7096
|
+
shouldCollect: isAutoImportCandidateTag
|
|
7097
|
+
});
|
|
7098
|
+
for (const tag of tags) {
|
|
7099
|
+
let resolved;
|
|
7100
|
+
try {
|
|
7101
|
+
resolved = await autoImportTags.resolveUsingComponent(tag, filename);
|
|
7102
|
+
} catch {
|
|
7103
|
+
resolved = void 0;
|
|
7104
|
+
}
|
|
7105
|
+
if (!resolved?.from) continue;
|
|
7106
|
+
if (isWevuSfcComponent(resolved)) {
|
|
7107
|
+
result.wevuComponentTags.add(tag);
|
|
7108
|
+
if (resolved.name) result.wevuComponentTags.add(resolved.name);
|
|
7109
|
+
}
|
|
7110
|
+
}
|
|
7111
|
+
}
|
|
7112
|
+
async function collectComponentSourceInfo(options) {
|
|
7113
|
+
const result = {
|
|
7114
|
+
autoUsingComponentsMap: {},
|
|
7115
|
+
autoComponentMeta: {},
|
|
7116
|
+
wevuComponentTags: /* @__PURE__ */ new Set()
|
|
7117
|
+
};
|
|
7118
|
+
await collectScriptSetupUsingComponents({
|
|
7119
|
+
descriptor: options.descriptor,
|
|
7120
|
+
descriptorForCompile: options.descriptorForCompile,
|
|
7121
|
+
filename: options.filename,
|
|
7122
|
+
compileOptions: options.compileOptions,
|
|
7123
|
+
autoUsingComponents: options.autoUsingComponents,
|
|
7124
|
+
result
|
|
7125
|
+
});
|
|
7126
|
+
await collectAutoImportWevuComponents({
|
|
7127
|
+
descriptor: options.descriptor,
|
|
7128
|
+
filename: options.filename,
|
|
7129
|
+
autoImportTags: options.autoImportTags,
|
|
7130
|
+
warn: options.compileOptions?.warn,
|
|
7131
|
+
result
|
|
7132
|
+
});
|
|
7133
|
+
return result;
|
|
7134
|
+
}
|
|
7135
|
+
//#endregion
|
|
6906
7136
|
//#region src/plugins/vue/transform/config.ts
|
|
6907
7137
|
/**
|
|
6908
7138
|
* 规范化配置块语言。
|
|
@@ -7567,66 +7797,10 @@ function composeSourceMaps(transformedMap, originalMap) {
|
|
|
7567
7797
|
//#endregion
|
|
7568
7798
|
//#region src/plugins/vue/transform/compileVueFile/script.ts
|
|
7569
7799
|
const TYPE_ONLY_DEFINE_PROPS_RE = /\bdefineProps\s*</;
|
|
7570
|
-
function
|
|
7571
|
-
const
|
|
7572
|
-
|
|
7573
|
-
filename,
|
|
7574
|
-
warnLabel: "自动 usingComponents",
|
|
7575
|
-
warn: (message) => warnHandler(message),
|
|
7576
|
-
shouldCollect: (tag) => VUE_COMPONENT_TAG_RE.test(tag)
|
|
7577
|
-
});
|
|
7578
|
-
}
|
|
7579
|
-
async function compileScriptPhase(descriptor, descriptorForCompile, filename, options, autoUsingComponents, templateCompiled, isAppFile) {
|
|
7580
|
-
const autoUsingComponentsMap = {};
|
|
7581
|
-
const autoComponentMeta = {};
|
|
7800
|
+
async function compileScriptPhase(descriptor, descriptorForCompile, filename, options, _autoUsingComponents, templateCompiled, isAppFile, componentSourceInfo) {
|
|
7801
|
+
const autoUsingComponentsMap = { ...componentSourceInfo?.autoUsingComponentsMap ?? {} };
|
|
7802
|
+
const autoComponentMeta = { ...componentSourceInfo?.autoComponentMeta ?? {} };
|
|
7582
7803
|
const relaxStructuredTypeOnlyProps = Boolean(descriptor.scriptSetup?.content && TYPE_ONLY_DEFINE_PROPS_RE.test(descriptor.scriptSetup.content));
|
|
7583
|
-
if (autoUsingComponents && descriptor.scriptSetup && descriptor.template) {
|
|
7584
|
-
const templateComponentNames = collectTemplateComponentNames(descriptor.template.content, filename, autoUsingComponents.warn ?? options?.warn);
|
|
7585
|
-
if (templateComponentNames.size) try {
|
|
7586
|
-
const setupAst = parse$3(descriptorForCompile.scriptSetup.content, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
7587
|
-
const pending = [];
|
|
7588
|
-
traverse(setupAst, { ImportDeclaration(path) {
|
|
7589
|
-
if (path.node.importKind === "type") return;
|
|
7590
|
-
if (!t.isStringLiteral(path.node.source)) return;
|
|
7591
|
-
const importSource = path.node.source.value;
|
|
7592
|
-
for (const specifier of path.node.specifiers) {
|
|
7593
|
-
if ("importKind" in specifier && specifier.importKind === "type") continue;
|
|
7594
|
-
if (!("local" in specifier) || !t.isIdentifier(specifier.local)) continue;
|
|
7595
|
-
const localName = specifier.local.name;
|
|
7596
|
-
if (!templateComponentNames.has(localName)) continue;
|
|
7597
|
-
if (t.isImportDefaultSpecifier(specifier)) pending.push({
|
|
7598
|
-
localName,
|
|
7599
|
-
importSource,
|
|
7600
|
-
importedName: "default",
|
|
7601
|
-
kind: "default"
|
|
7602
|
-
});
|
|
7603
|
-
else if (t.isImportSpecifier(specifier)) {
|
|
7604
|
-
const importedName = t.isIdentifier(specifier.imported) ? specifier.imported.name : t.isStringLiteral(specifier.imported) ? specifier.imported.value : void 0;
|
|
7605
|
-
pending.push({
|
|
7606
|
-
localName,
|
|
7607
|
-
importSource,
|
|
7608
|
-
importedName,
|
|
7609
|
-
kind: "named"
|
|
7610
|
-
});
|
|
7611
|
-
}
|
|
7612
|
-
}
|
|
7613
|
-
} });
|
|
7614
|
-
for (const { localName, importSource, importedName, kind } of pending) {
|
|
7615
|
-
let resolved = await autoUsingComponents.resolveUsingComponentPath(importSource, filename, {
|
|
7616
|
-
localName,
|
|
7617
|
-
importedName,
|
|
7618
|
-
kind
|
|
7619
|
-
});
|
|
7620
|
-
if (!resolved && importSource.startsWith("/")) resolved = removeExtensionDeep(importSource);
|
|
7621
|
-
if (!resolved) continue;
|
|
7622
|
-
autoUsingComponentsMap[localName] = resolved;
|
|
7623
|
-
autoComponentMeta[localName] = resolved;
|
|
7624
|
-
}
|
|
7625
|
-
} catch (error) {
|
|
7626
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
7627
|
-
autoUsingComponents.warn?.(`[Vue 编译] 解析 ${filename} 的 <script setup> 导入失败:${message}`);
|
|
7628
|
-
}
|
|
7629
|
-
}
|
|
7630
7804
|
let scriptCode;
|
|
7631
7805
|
let scriptMap = null;
|
|
7632
7806
|
if (descriptor.script || descriptor.scriptSetup) {
|
|
@@ -7728,8 +7902,23 @@ async function compileVueFile(source, filename, options) {
|
|
|
7728
7902
|
} };
|
|
7729
7903
|
const autoUsingComponents = options?.autoUsingComponents?.enabled && parsed.descriptor.scriptSetup && parsed.descriptor.template && options.autoUsingComponents.resolveUsingComponentPath ? options.autoUsingComponents : void 0;
|
|
7730
7904
|
const autoImportTags = options?.autoImportTags?.enabled && parsed.descriptor.template && options.autoImportTags.resolveUsingComponent ? options.autoImportTags : void 0;
|
|
7731
|
-
const
|
|
7732
|
-
|
|
7905
|
+
const componentSourceInfo = await collectComponentSourceInfo({
|
|
7906
|
+
descriptor: parsed.descriptor,
|
|
7907
|
+
descriptorForCompile: parsed.descriptorForCompile,
|
|
7908
|
+
filename,
|
|
7909
|
+
compileOptions: options,
|
|
7910
|
+
autoUsingComponents,
|
|
7911
|
+
autoImportTags
|
|
7912
|
+
});
|
|
7913
|
+
const templateOptions = componentSourceInfo.wevuComponentTags.size ? {
|
|
7914
|
+
...options?.template,
|
|
7915
|
+
wevuComponentTags: componentSourceInfo.wevuComponentTags
|
|
7916
|
+
} : {
|
|
7917
|
+
...options?.template,
|
|
7918
|
+
wevuComponentTags: []
|
|
7919
|
+
};
|
|
7920
|
+
const templateCompiled = compileTemplatePhase(parsed.descriptor, filename, templateOptions, result);
|
|
7921
|
+
const scriptPhase = await compileScriptPhase(parsed.descriptor, parsed.descriptorForCompile, filename, options, autoUsingComponents, templateCompiled, parsed.isAppFile, componentSourceInfo);
|
|
7733
7922
|
result.script = scriptPhase.script;
|
|
7734
7923
|
result.scriptMap = scriptPhase.scriptMap;
|
|
7735
7924
|
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.
|
|
4
|
+
"version": "6.16.0",
|
|
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.33",
|
|
53
|
-
"@weapp-core/constants": "^0.1.
|
|
53
|
+
"@weapp-core/constants": "^0.1.4",
|
|
54
54
|
"@weapp-core/shared": "3.0.4",
|
|
55
|
-
"@weapp-vite/ast": "6.
|
|
55
|
+
"@weapp-vite/ast": "6.16.0",
|
|
56
56
|
"rolldown-require": "2.0.15"
|
|
57
57
|
},
|
|
58
58
|
"publishConfig": {
|