Skip to content

Commit

Permalink
refactor(language-core): reduce codegen size of template returns
Browse files Browse the repository at this point in the history
  • Loading branch information
KazariEX committed Feb 13, 2025
1 parent 2328e96 commit c4c0413
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 104 deletions.
10 changes: 5 additions & 5 deletions packages/language-core/lib/codegen/localTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ type __VLS_WithDefaults<P, D> = {
`__VLS_PrettifyLocal`,
() => `type __VLS_PrettifyLocal<T> = { [K in keyof T]: T[K]; } & {}${endOfLine}`
);
const WithTemplateSlots = defineHelper(
`__VLS_WithTemplateSlots`,
const WithSlots = defineHelper(
`__VLS_WithSlots`,
() => `
type __VLS_WithTemplateSlots<T, S> = T & {
type __VLS_WithSlots<T, S> = T & {
new(): {
${getSlotsPropertyName(vueCompilerOptions.target)}: S;
${vueCompilerOptions.jsxSlots ? `$props: ${PropsChildren.name}<S>;` : ''}
Expand Down Expand Up @@ -84,7 +84,7 @@ type __VLS_TypePropsToOption<T> = {
[PrettifyLocal.name]: PrettifyLocal,
[OmitKeepDiscriminatedUnion.name]: OmitKeepDiscriminatedUnion,
[WithDefaults.name]: WithDefaults,
[WithTemplateSlots.name]: WithTemplateSlots,
[WithSlots.name]: WithSlots,
[PropsChildren.name]: PropsChildren,
[TypePropsToOption.name]: TypePropsToOption,
[OmitIndexSignature.name]: OmitIndexSignature,
Expand All @@ -99,7 +99,7 @@ type __VLS_TypePropsToOption<T> = {
get PrettifyLocal() { return PrettifyLocal.name; },
get OmitKeepDiscriminatedUnion() { return OmitKeepDiscriminatedUnion.name; },
get WithDefaults() { return WithDefaults.name; },
get WithTemplateSlots() { return WithTemplateSlots.name; },
get WithSlots() { return WithSlots.name; },
get PropsChildren() { return PropsChildren.name; },
get TypePropsToOption() { return TypePropsToOption.name; },
get OmitIndexSignature() { return OmitIndexSignature.name; },
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/codegen/script/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function* generateComponent(
yield `__typeRefs: {} as __VLS_TemplateRefs,${newLine}`;
}
if (options.vueCompilerOptions.target >= 3.5 && options.templateCodegen?.singleRootElType) {
yield `__typeEl: {} as __VLS_TemplateEl,${newLine}`;
yield `__typeEl: {} as __VLS_RootEl,${newLine}`;
}
yield `})`;
}
Expand Down Expand Up @@ -154,7 +154,7 @@ export function* generatePropsOption(
});
}
if (inheritAttrs && options.templateCodegen?.inheritedAttrVars.size) {
let attrsType = `__VLS_TemplateAttrs`;
let attrsType = `Partial<__VLS_InheritedAttrs>`;
if (hasEmitsOption) {
attrsType = `Omit<${attrsType}, \`on\${string}\`>`;
}
Expand Down
40 changes: 18 additions & 22 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ export function* generateScriptSetup(
}

yield `return {} as {${newLine}`
+ ` props: ${ctx.localTypes.PrettifyLocal}<__VLS_OwnProps & __VLS_PublicProps & __VLS_TemplateAttrs> & __VLS_BuiltInPublicProps,${newLine}`
+ ` props: ${ctx.localTypes.PrettifyLocal}<__VLS_OwnProps & __VLS_PublicProps & Partial<__VLS_InheritedAttrs>> & __VLS_BuiltInPublicProps,${newLine}`
+ ` expose(exposed: import('${options.vueCompilerOptions.lib}').ShallowUnwrapRef<${scriptSetupRanges.defineExpose ? 'typeof __VLS_exposed' : '{}'}>): void,${newLine}`
+ ` attrs: any,${newLine}`
+ ` slots: __VLS_TemplateSlots,${newLine}`
+ ` slots: __VLS_Slots,${newLine}`
+ ` emit: ${emitTypes.length ? emitTypes.join(' & ') : `{}`},${newLine}`
+ `}${endOfLine}`;
yield `})(),${newLine}`; // __VLS_setup = (async () => {
Expand Down Expand Up @@ -106,8 +106,8 @@ function* generateSetupFunction(
scriptSetupRanges.withDefaults?.callExp ?? callExp,
typeArg,
name,
'__VLS_props',
'__VLS_Props'
`__VLS_props`,
`__VLS_Props`
));
}
if (scriptSetupRanges.defineEmits) {
Expand All @@ -118,25 +118,21 @@ function* generateSetupFunction(
callExp,
typeArg,
name,
'__VLS_emit',
'__VLS_Emit'
`__VLS_emit`,
`__VLS_Emit`
));
}
if (scriptSetupRanges.defineSlots) {
const { name, callExp, isObjectBindingPattern } = scriptSetupRanges.defineSlots;
if (isObjectBindingPattern) {
setupCodeModifies.push([
[`__VLS_slots;\nconst __VLS_slots = `],
callExp.start,
callExp.start,
]);
} else if (!name) {
setupCodeModifies.push([
[`const __VLS_slots = `],
callExp.start,
callExp.start
]);
}
const { name, statement, callExp, typeArg } = scriptSetupRanges.defineSlots;
setupCodeModifies.push(...generateDefineWithType(
scriptSetup,
statement,
callExp,
typeArg,
name,
`__VLS_slots`,
`__VLS_Slots`
));
}
if (scriptSetupRanges.defineExpose) {
const { callExp, arg, typeArg } = scriptSetupRanges.defineExpose;
Expand Down Expand Up @@ -302,7 +298,7 @@ function* generateSetupFunction(
yield* generateComponent(options, ctx, scriptSetup, scriptSetupRanges);
yield endOfLine;
yield `${syntax} `;
yield `{} as ${ctx.localTypes.WithTemplateSlots}<typeof __VLS_component, __VLS_TemplateSlots>${endOfLine}`;
yield `{} as ${ctx.localTypes.WithSlots}<typeof __VLS_component, __VLS_Slots>${endOfLine}`;
}
else {
yield `${syntax} `;
Expand Down Expand Up @@ -462,7 +458,7 @@ function* generateComponentProps(
yield ` & `;
}
ctx.generatedPropsType = true;
yield `${ctx.localTypes.PropsChildren}<typeof __VLS_slots>`;
yield `${ctx.localTypes.PropsChildren}<__VLS_Slots>`;
}
if (scriptSetupRanges.defineProp.length) {
if (ctx.generatedPropsType) {
Expand Down
13 changes: 4 additions & 9 deletions packages/language-core/lib/codegen/script/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,12 @@ function* generateTemplateBody(
else {
yield `// no template${newLine}`;
if (!options.scriptSetupRanges?.defineSlots) {
yield `const __VLS_slots = {}${endOfLine}`;
yield `type __VLS_Slots = {}${endOfLine}`;
}
yield `const __VLS_inheritedAttrs = {}${endOfLine}`;
yield `const __VLS_refs = {}${endOfLine}`;
yield `const __VLS_rootEl = {} as any${endOfLine}`;
yield `type __VLS_InheritedAttrs = {}${endOfLine}`;
yield `type __VLS_TemplateRefs = {}${endOfLine}`;
yield `type __VLS_RootEl = any${endOfLine}`;
}

yield `type __VLS_TemplateAttrs = Partial<typeof __VLS_inheritedAttrs>${endOfLine}`;
yield `type __VLS_TemplateSlots = typeof ${options.scriptSetupRanges?.defineSlots?.name ?? '__VLS_slots'}${endOfLine}`;
yield `type __VLS_TemplateRefs = typeof __VLS_refs${endOfLine}`;
yield `type __VLS_TemplateEl = typeof __VLS_rootEl${endOfLine}`;
}

function* generateStyleScopedClasses(
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export function* generateComponent(
if (options.selfComponentName && possibleOriginalNames.includes(options.selfComponentName)) {
yield `typeof __VLS_self & (new () => { `
+ getSlotsPropertyName(options.vueCompilerOptions.target)
+ `: typeof ${options.slotsAssignName ?? `__VLS_slots`} }), `;
+ `: __VLS_Slots }), `;
}
else {
yield `void, `;
Expand Down
30 changes: 16 additions & 14 deletions packages/language-core/lib/codegen/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type * as ts from 'typescript';
import type { Code, Sfc, VueCompilerOptions } from '../../types';
import { getSlotsPropertyName } from '../../utils/shared';
import { endOfLine, newLine, wrapWith } from '../utils';
import { generateStringLiteralKey } from '../utils/stringLiteralKey';
import { TemplateCodegenContext, createTemplateCodegenContext } from './context';
import { generateObjectProperty } from './objectProperty';
import { generateStyleScopedClassReferences } from './styleScopedClasses';
Expand Down Expand Up @@ -49,7 +48,7 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co
const speicalTypes = [
[slotsPropertyName, yield* generateSlots(options, ctx)],
['$attrs', yield* generateInheritedAttrs(options, ctx)],
['$refs', yield* generateRefs(ctx)],
['$refs', yield* generateTemplateRefs(options, ctx)],
['$el', yield* generateRootEl(ctx)]
];

Expand All @@ -67,9 +66,9 @@ function* generateSlots(
options: TemplateCodegenOptions,
ctx: TemplateCodegenContext
): Generator<Code> {
const name = getSlotsPropertyName(options.vueCompilerOptions.target);
if (!options.hasDefineSlots) {
yield `var __VLS_slots!: __VLS_PrettifyGlobal<__VLS_OmitStringIndex<typeof __VLS_ctx.${name}>`;
const name = getSlotsPropertyName(options.vueCompilerOptions.target);
yield `type __VLS_Slots = __VLS_PrettifyGlobal<__VLS_OmitStringIndex<typeof __VLS_ctx.${name}>`;
for (const { expVar, propsVar } of ctx.dynamicSlots) {
ctx.hasSlot = true;
yield `${newLine}& { [K in NonNullable<typeof ${expVar}>]?: (props: typeof ${propsVar}) => any }`;
Expand Down Expand Up @@ -99,14 +98,14 @@ function* generateSlots(
}
yield `>${endOfLine}`;
}
return `typeof ${options.slotsAssignName ?? `__VLS_slots`}`;
return `__VLS_Slots`;
}

function* generateInheritedAttrs(
options: TemplateCodegenOptions,
ctx: TemplateCodegenContext
): Generator<Code> {
yield 'let __VLS_inheritedAttrs!: {}';
yield `type __VLS_InheritedAttrs = {}`;
for (const varName of ctx.inheritedAttrVars) {
yield ` & typeof ${varName}`;
}
Expand All @@ -126,32 +125,35 @@ function* generateInheritedAttrs(
}
yield `]${endOfLine}`;
}
return `import('${options.vueCompilerOptions.lib}').ComponentPublicInstance['$attrs'] & Partial<typeof __VLS_inheritedAttrs>`;
return `import('${options.vueCompilerOptions.lib}').ComponentPublicInstance['$attrs'] & Partial<__VLS_InheritedAttrs>`;
}

function* generateRefs(
function* generateTemplateRefs(
options: TemplateCodegenOptions,
ctx: TemplateCodegenContext
): Generator<Code> {
yield `const __VLS_refs = {${newLine}`;
yield `type __VLS_TemplateRefs = {${newLine}`;
for (const [name, [varName, offset]] of ctx.templateRefs) {
yield* generateStringLiteralKey(
yield* generateObjectProperty(
options,
ctx,
name,
offset,
ctx.codeFeatures.navigationAndCompletion
);
yield `: ${varName},${newLine}`;
yield `: typeof ${varName},${newLine}`;
}
yield `}${endOfLine}`;
return `typeof __VLS_refs`;
return `__VLS_TemplateRefs`;
}

function* generateRootEl(
ctx: TemplateCodegenContext
): Generator<Code> {
yield `let __VLS_rootEl!: `;
yield `type __VLS_RootEl = `;
yield ctx.singleRootElType ?? `any`;
yield endOfLine;
return `typeof __VLS_rootEl`;
return `__VLS_RootEl`;
}

export function* forEachElementNode(node: CompilerDOM.RootNode | CompilerDOM.TemplateChildNode): Generator<CompilerDOM.ElementNode> {
Expand Down
8 changes: 1 addition & 7 deletions packages/language-core/lib/parsers/scriptSetupRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ type DefineEmits = CallExpressionRange & {

type DefineSlots = CallExpressionRange & {
name?: string;
isObjectBindingPattern?: boolean;
statement: TextRange;
};

Expand Down Expand Up @@ -352,12 +351,7 @@ export function parseScriptSetupRanges(
statement: getStatementRange(ts, parents, node, ast)
};
if (ts.isVariableDeclaration(parent)) {
if (ts.isIdentifier(parent.name)) {
defineSlots.name = _getNodeText(parent.name);
}
else {
defineSlots.isObjectBindingPattern = ts.isObjectBindingPattern(parent.name);
}
defineSlots.name = _getNodeText(parent.name);
}
}
else if (vueCompilerOptions.macros.defineExpose.includes(callText)) {
Expand Down
Loading

0 comments on commit c4c0413

Please sign in to comment.