Skip to content

Commit c4c0413

Browse files
committed
refactor(language-core): reduce codegen size of template returns
1 parent 2328e96 commit c4c0413

File tree

10 files changed

+63
-104
lines changed

10 files changed

+63
-104
lines changed

packages/language-core/lib/codegen/localTypes.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ type __VLS_WithDefaults<P, D> = {
2828
`__VLS_PrettifyLocal`,
2929
() => `type __VLS_PrettifyLocal<T> = { [K in keyof T]: T[K]; } & {}${endOfLine}`
3030
);
31-
const WithTemplateSlots = defineHelper(
32-
`__VLS_WithTemplateSlots`,
31+
const WithSlots = defineHelper(
32+
`__VLS_WithSlots`,
3333
() => `
34-
type __VLS_WithTemplateSlots<T, S> = T & {
34+
type __VLS_WithSlots<T, S> = T & {
3535
new(): {
3636
${getSlotsPropertyName(vueCompilerOptions.target)}: S;
3737
${vueCompilerOptions.jsxSlots ? `$props: ${PropsChildren.name}<S>;` : ''}
@@ -84,7 +84,7 @@ type __VLS_TypePropsToOption<T> = {
8484
[PrettifyLocal.name]: PrettifyLocal,
8585
[OmitKeepDiscriminatedUnion.name]: OmitKeepDiscriminatedUnion,
8686
[WithDefaults.name]: WithDefaults,
87-
[WithTemplateSlots.name]: WithTemplateSlots,
87+
[WithSlots.name]: WithSlots,
8888
[PropsChildren.name]: PropsChildren,
8989
[TypePropsToOption.name]: TypePropsToOption,
9090
[OmitIndexSignature.name]: OmitIndexSignature,
@@ -99,7 +99,7 @@ type __VLS_TypePropsToOption<T> = {
9999
get PrettifyLocal() { return PrettifyLocal.name; },
100100
get OmitKeepDiscriminatedUnion() { return OmitKeepDiscriminatedUnion.name; },
101101
get WithDefaults() { return WithDefaults.name; },
102-
get WithTemplateSlots() { return WithTemplateSlots.name; },
102+
get WithSlots() { return WithSlots.name; },
103103
get PropsChildren() { return PropsChildren.name; },
104104
get TypePropsToOption() { return TypePropsToOption.name; },
105105
get OmitIndexSignature() { return OmitIndexSignature.name; },

packages/language-core/lib/codegen/script/component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function* generateComponent(
4444
yield `__typeRefs: {} as __VLS_TemplateRefs,${newLine}`;
4545
}
4646
if (options.vueCompilerOptions.target >= 3.5 && options.templateCodegen?.singleRootElType) {
47-
yield `__typeEl: {} as __VLS_TemplateEl,${newLine}`;
47+
yield `__typeEl: {} as __VLS_RootEl,${newLine}`;
4848
}
4949
yield `})`;
5050
}
@@ -154,7 +154,7 @@ export function* generatePropsOption(
154154
});
155155
}
156156
if (inheritAttrs && options.templateCodegen?.inheritedAttrVars.size) {
157-
let attrsType = `__VLS_TemplateAttrs`;
157+
let attrsType = `Partial<__VLS_InheritedAttrs>`;
158158
if (hasEmitsOption) {
159159
attrsType = `Omit<${attrsType}, \`on\${string}\`>`;
160160
}

packages/language-core/lib/codegen/script/scriptSetup.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ export function* generateScriptSetup(
6565
}
6666

6767
yield `return {} as {${newLine}`
68-
+ ` props: ${ctx.localTypes.PrettifyLocal}<__VLS_OwnProps & __VLS_PublicProps & __VLS_TemplateAttrs> & __VLS_BuiltInPublicProps,${newLine}`
68+
+ ` props: ${ctx.localTypes.PrettifyLocal}<__VLS_OwnProps & __VLS_PublicProps & Partial<__VLS_InheritedAttrs>> & __VLS_BuiltInPublicProps,${newLine}`
6969
+ ` expose(exposed: import('${options.vueCompilerOptions.lib}').ShallowUnwrapRef<${scriptSetupRanges.defineExpose ? 'typeof __VLS_exposed' : '{}'}>): void,${newLine}`
7070
+ ` attrs: any,${newLine}`
71-
+ ` slots: __VLS_TemplateSlots,${newLine}`
71+
+ ` slots: __VLS_Slots,${newLine}`
7272
+ ` emit: ${emitTypes.length ? emitTypes.join(' & ') : `{}`},${newLine}`
7373
+ `}${endOfLine}`;
7474
yield `})(),${newLine}`; // __VLS_setup = (async () => {
@@ -106,8 +106,8 @@ function* generateSetupFunction(
106106
scriptSetupRanges.withDefaults?.callExp ?? callExp,
107107
typeArg,
108108
name,
109-
'__VLS_props',
110-
'__VLS_Props'
109+
`__VLS_props`,
110+
`__VLS_Props`
111111
));
112112
}
113113
if (scriptSetupRanges.defineEmits) {
@@ -118,25 +118,21 @@ function* generateSetupFunction(
118118
callExp,
119119
typeArg,
120120
name,
121-
'__VLS_emit',
122-
'__VLS_Emit'
121+
`__VLS_emit`,
122+
`__VLS_Emit`
123123
));
124124
}
125125
if (scriptSetupRanges.defineSlots) {
126-
const { name, callExp, isObjectBindingPattern } = scriptSetupRanges.defineSlots;
127-
if (isObjectBindingPattern) {
128-
setupCodeModifies.push([
129-
[`__VLS_slots;\nconst __VLS_slots = `],
130-
callExp.start,
131-
callExp.start,
132-
]);
133-
} else if (!name) {
134-
setupCodeModifies.push([
135-
[`const __VLS_slots = `],
136-
callExp.start,
137-
callExp.start
138-
]);
139-
}
126+
const { name, statement, callExp, typeArg } = scriptSetupRanges.defineSlots;
127+
setupCodeModifies.push(...generateDefineWithType(
128+
scriptSetup,
129+
statement,
130+
callExp,
131+
typeArg,
132+
name,
133+
`__VLS_slots`,
134+
`__VLS_Slots`
135+
));
140136
}
141137
if (scriptSetupRanges.defineExpose) {
142138
const { callExp, arg, typeArg } = scriptSetupRanges.defineExpose;
@@ -302,7 +298,7 @@ function* generateSetupFunction(
302298
yield* generateComponent(options, ctx, scriptSetup, scriptSetupRanges);
303299
yield endOfLine;
304300
yield `${syntax} `;
305-
yield `{} as ${ctx.localTypes.WithTemplateSlots}<typeof __VLS_component, __VLS_TemplateSlots>${endOfLine}`;
301+
yield `{} as ${ctx.localTypes.WithSlots}<typeof __VLS_component, __VLS_Slots>${endOfLine}`;
306302
}
307303
else {
308304
yield `${syntax} `;
@@ -462,7 +458,7 @@ function* generateComponentProps(
462458
yield ` & `;
463459
}
464460
ctx.generatedPropsType = true;
465-
yield `${ctx.localTypes.PropsChildren}<typeof __VLS_slots>`;
461+
yield `${ctx.localTypes.PropsChildren}<__VLS_Slots>`;
466462
}
467463
if (scriptSetupRanges.defineProp.length) {
468464
if (ctx.generatedPropsType) {

packages/language-core/lib/codegen/script/template.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,12 @@ function* generateTemplateBody(
124124
else {
125125
yield `// no template${newLine}`;
126126
if (!options.scriptSetupRanges?.defineSlots) {
127-
yield `const __VLS_slots = {}${endOfLine}`;
127+
yield `type __VLS_Slots = {}${endOfLine}`;
128128
}
129-
yield `const __VLS_inheritedAttrs = {}${endOfLine}`;
130-
yield `const __VLS_refs = {}${endOfLine}`;
131-
yield `const __VLS_rootEl = {} as any${endOfLine}`;
129+
yield `type __VLS_InheritedAttrs = {}${endOfLine}`;
130+
yield `type __VLS_TemplateRefs = {}${endOfLine}`;
131+
yield `type __VLS_RootEl = any${endOfLine}`;
132132
}
133-
134-
yield `type __VLS_TemplateAttrs = Partial<typeof __VLS_inheritedAttrs>${endOfLine}`;
135-
yield `type __VLS_TemplateSlots = typeof ${options.scriptSetupRanges?.defineSlots?.name ?? '__VLS_slots'}${endOfLine}`;
136-
yield `type __VLS_TemplateRefs = typeof __VLS_refs${endOfLine}`;
137-
yield `type __VLS_TemplateEl = typeof __VLS_rootEl${endOfLine}`;
138133
}
139134

140135
function* generateStyleScopedClasses(

packages/language-core/lib/codegen/template/element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export function* generateComponent(
152152
if (options.selfComponentName && possibleOriginalNames.includes(options.selfComponentName)) {
153153
yield `typeof __VLS_self & (new () => { `
154154
+ getSlotsPropertyName(options.vueCompilerOptions.target)
155-
+ `: typeof ${options.slotsAssignName ?? `__VLS_slots`} }), `;
155+
+ `: __VLS_Slots }), `;
156156
}
157157
else {
158158
yield `void, `;

packages/language-core/lib/codegen/template/index.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type * as ts from 'typescript';
33
import type { Code, Sfc, VueCompilerOptions } from '../../types';
44
import { getSlotsPropertyName } from '../../utils/shared';
55
import { endOfLine, newLine, wrapWith } from '../utils';
6-
import { generateStringLiteralKey } from '../utils/stringLiteralKey';
76
import { TemplateCodegenContext, createTemplateCodegenContext } from './context';
87
import { generateObjectProperty } from './objectProperty';
98
import { generateStyleScopedClassReferences } from './styleScopedClasses';
@@ -49,7 +48,7 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co
4948
const speicalTypes = [
5049
[slotsPropertyName, yield* generateSlots(options, ctx)],
5150
['$attrs', yield* generateInheritedAttrs(options, ctx)],
52-
['$refs', yield* generateRefs(ctx)],
51+
['$refs', yield* generateTemplateRefs(options, ctx)],
5352
['$el', yield* generateRootEl(ctx)]
5453
];
5554

@@ -67,9 +66,9 @@ function* generateSlots(
6766
options: TemplateCodegenOptions,
6867
ctx: TemplateCodegenContext
6968
): Generator<Code> {
70-
const name = getSlotsPropertyName(options.vueCompilerOptions.target);
7169
if (!options.hasDefineSlots) {
72-
yield `var __VLS_slots!: __VLS_PrettifyGlobal<__VLS_OmitStringIndex<typeof __VLS_ctx.${name}>`;
70+
const name = getSlotsPropertyName(options.vueCompilerOptions.target);
71+
yield `type __VLS_Slots = __VLS_PrettifyGlobal<__VLS_OmitStringIndex<typeof __VLS_ctx.${name}>`;
7372
for (const { expVar, propsVar } of ctx.dynamicSlots) {
7473
ctx.hasSlot = true;
7574
yield `${newLine}& { [K in NonNullable<typeof ${expVar}>]?: (props: typeof ${propsVar}) => any }`;
@@ -99,14 +98,14 @@ function* generateSlots(
9998
}
10099
yield `>${endOfLine}`;
101100
}
102-
return `typeof ${options.slotsAssignName ?? `__VLS_slots`}`;
101+
return `__VLS_Slots`;
103102
}
104103

105104
function* generateInheritedAttrs(
106105
options: TemplateCodegenOptions,
107106
ctx: TemplateCodegenContext
108107
): Generator<Code> {
109-
yield 'let __VLS_inheritedAttrs!: {}';
108+
yield `type __VLS_InheritedAttrs = {}`;
110109
for (const varName of ctx.inheritedAttrVars) {
111110
yield ` & typeof ${varName}`;
112111
}
@@ -126,32 +125,35 @@ function* generateInheritedAttrs(
126125
}
127126
yield `]${endOfLine}`;
128127
}
129-
return `import('${options.vueCompilerOptions.lib}').ComponentPublicInstance['$attrs'] & Partial<typeof __VLS_inheritedAttrs>`;
128+
return `import('${options.vueCompilerOptions.lib}').ComponentPublicInstance['$attrs'] & Partial<__VLS_InheritedAttrs>`;
130129
}
131130

132-
function* generateRefs(
131+
function* generateTemplateRefs(
132+
options: TemplateCodegenOptions,
133133
ctx: TemplateCodegenContext
134134
): Generator<Code> {
135-
yield `const __VLS_refs = {${newLine}`;
135+
yield `type __VLS_TemplateRefs = {${newLine}`;
136136
for (const [name, [varName, offset]] of ctx.templateRefs) {
137-
yield* generateStringLiteralKey(
137+
yield* generateObjectProperty(
138+
options,
139+
ctx,
138140
name,
139141
offset,
140142
ctx.codeFeatures.navigationAndCompletion
141143
);
142-
yield `: ${varName},${newLine}`;
144+
yield `: typeof ${varName},${newLine}`;
143145
}
144146
yield `}${endOfLine}`;
145-
return `typeof __VLS_refs`;
147+
return `__VLS_TemplateRefs`;
146148
}
147149

148150
function* generateRootEl(
149151
ctx: TemplateCodegenContext
150152
): Generator<Code> {
151-
yield `let __VLS_rootEl!: `;
153+
yield `type __VLS_RootEl = `;
152154
yield ctx.singleRootElType ?? `any`;
153155
yield endOfLine;
154-
return `typeof __VLS_rootEl`;
156+
return `__VLS_RootEl`;
155157
}
156158

157159
export function* forEachElementNode(node: CompilerDOM.RootNode | CompilerDOM.TemplateChildNode): Generator<CompilerDOM.ElementNode> {

packages/language-core/lib/parsers/scriptSetupRanges.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ type DefineEmits = CallExpressionRange & {
4444

4545
type DefineSlots = CallExpressionRange & {
4646
name?: string;
47-
isObjectBindingPattern?: boolean;
4847
statement: TextRange;
4948
};
5049

@@ -352,12 +351,7 @@ export function parseScriptSetupRanges(
352351
statement: getStatementRange(ts, parents, node, ast)
353352
};
354353
if (ts.isVariableDeclaration(parent)) {
355-
if (ts.isIdentifier(parent.name)) {
356-
defineSlots.name = _getNodeText(parent.name);
357-
}
358-
else {
359-
defineSlots.isObjectBindingPattern = ts.isObjectBindingPattern(parent.name);
360-
}
354+
defineSlots.name = _getNodeText(parent.name);
361355
}
362356
}
363357
else if (vueCompilerOptions.macros.defineExpose.includes(callText)) {

0 commit comments

Comments
 (0)