Skip to content

Commit ff550ce

Browse files
committed
fix vue-tsc speed
1 parent 4a98355 commit ff550ce

File tree

6 files changed

+27
-46
lines changed

6 files changed

+27
-46
lines changed

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

-7
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,6 @@ type __VLS_NormalizeEmits<T> = __VLS_PrettifyGlobal<
6969
>
7070
>;
7171
type __VLS_PrettifyGlobal<T> = { [K in keyof T]: T[K]; } & {};
72-
type __VLS_PickRefsExpose<T> = T extends object
73-
? { [K in keyof T]: (T[K] extends any[]
74-
? Parameters<T[K][0]['expose']>[0][]
75-
: T[K] extends { expose?: (exposed: infer E) => void }
76-
? E
77-
: T[K]) | null }
78-
: never;
7972
8073
declare function __VLS_getVForSourceType(source: number): [number, number, number][];
8174
declare function __VLS_getVForSourceType(source: string): [string, number, number][];

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export function* generateTemplateCtx(
2626
if (options.sfc.styles.some(style => style.module)) {
2727
exps.push(`{} as __VLS_StyleModules`);
2828
}
29-
exps.push(`{ $refs: __VLS_refs }`);
3029

3130
yield `const __VLS_ctx = `;
3231
if (exps.length === 1) {
@@ -156,14 +155,14 @@ function* generateTemplateBody(
156155
yield `// no template${newLine}`;
157156
if (!options.scriptSetupRanges?.slots.define) {
158157
yield `const __VLS_slots = {}${endOfLine}`;
159-
yield `const __VLS_refs = {}${endOfLine}`;
158+
yield `const $refs = {}${endOfLine}`;
160159
yield `const __VLS_inheritedAttrs = {}${endOfLine}`;
161160
}
162161
}
163162

164163
yield `const __VLS_templateResult = {`;
165164
yield `slots: ${options.scriptSetupRanges?.slots.name ?? '__VLS_slots'},${newLine}`;
166-
yield `refs: __VLS_refs,${newLine}`;
165+
yield `refs: $refs,${newLine}`;
167166
yield `attrs: {} as Partial<typeof __VLS_inheritedAttrs>,${newLine}`;
168167
yield `}${endOfLine}`;
169168
}

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

+14-24
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,19 @@ export function* generateComponent(
225225

226226
const refName = yield* generateVScope(options, ctx, node, props);
227227
if (refName) {
228+
const varName = ctx.getInternalVariable();
229+
options.templateRefNames.set(refName, varName);
228230
ctx.usedComponentCtxVars.add(var_defineComponentCtx);
231+
232+
yield `// @ts-ignore${newLine}`;
233+
if (node.codegenNode?.type === CompilerDOM.NodeTypes.VNODE_CALL
234+
&& node.codegenNode.props?.type === CompilerDOM.NodeTypes.JS_OBJECT_EXPRESSION
235+
&& node.codegenNode.props.properties.find(({ key }) => key.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && key.content === 'ref_for')
236+
) {
237+
yield `var ${varName} = [{} as Parameters<typeof ${var_defineComponentCtx}['expose']>[0]]${endOfLine}`;
238+
} else {
239+
yield `var ${varName} = {} as Parameters<typeof ${var_defineComponentCtx}['expose']>[0]${endOfLine}`;
240+
}
229241
}
230242

231243
const usedComponentEventsVar = yield* generateElementEvents(options, ctx, node, var_functionalComponent, var_componentInstance, var_componentEmit, var_componentEvents);
@@ -257,19 +269,6 @@ export function* generateComponent(
257269

258270
if (ctx.usedComponentCtxVars.has(var_defineComponentCtx)) {
259271
yield `const ${var_defineComponentCtx} = __VLS_pickFunctionalComponentCtx(${var_originalComponent}, ${var_componentInstance})${endOfLine}`;
260-
if (refName) {
261-
yield `// @ts-ignore${newLine}`;
262-
if (node.codegenNode?.type === CompilerDOM.NodeTypes.VNODE_CALL
263-
&& node.codegenNode.props?.type === CompilerDOM.NodeTypes.JS_OBJECT_EXPRESSION
264-
&& node.codegenNode.props.properties.find(({ key }) => key.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && key.content === 'ref_for')
265-
) {
266-
yield `(${refName} ??= []).push(${var_defineComponentCtx})`;
267-
} else {
268-
yield `${refName} = ${var_defineComponentCtx}`;
269-
}
270-
271-
yield endOfLine;
272-
}
273272
}
274273
}
275274

@@ -335,14 +334,7 @@ export function* generateElement(
335334

336335
const refName = yield* generateVScope(options, ctx, node, node.props);
337336
if (refName) {
338-
yield `// @ts-ignore${newLine}`;
339-
yield `${refName} = __VLS_intrinsicElements`;
340-
yield* generatePropertyAccess(
341-
options,
342-
ctx,
343-
node.tag
344-
);
345-
yield endOfLine;
337+
options.templateRefNames.set(refName, `__VLS_intrinsicElements['${node.tag}']`);
346338
}
347339

348340
const slotDir = node.props.find(p => p.type === CompilerDOM.NodeTypes.DIRECTIVE && p.name === 'slot') as CompilerDOM.DirectiveNode;
@@ -574,9 +566,7 @@ function* generateReferencesForElements(
574566
ctx.accessExternalVariable(content, startOffset);
575567
}
576568

577-
const refName = CompilerDOM.toValidAssetId(prop.value.content, '_VLS_refs' as any);
578-
options.templateRefNames.set(prop.value.content, refName);
579-
return refName;
569+
return prop.value.content;
580570
}
581571
}
582572
}

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co
3232
if (options.propsAssignName) {
3333
ctx.addLocalVariable(options.propsAssignName);
3434
}
35+
ctx.addLocalVariable('$refs');
3536

3637
yield* generatePreResolveComponents();
3738

@@ -56,14 +57,12 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co
5657
return ctx;
5758

5859
function* generateRefs(): Generator<Code> {
59-
for (const [, validId] of options.templateRefNames) {
60-
yield `let ${validId}${newLine}`;
60+
yield `const __VLS_refs = {${newLine}`;
61+
for (const [name, varName] of options.templateRefNames) {
62+
yield `'${name}': ${varName}!,${newLine}`;
6163
}
62-
yield `declare const __VLS_refs: __VLS_PickRefsExpose<{${newLine}`;
63-
for (const [name, validId] of options.templateRefNames) {
64-
yield `'${name}': NonNullable<typeof ${validId}>,${newLine}`;
65-
}
66-
yield `}>${endOfLine}`;
64+
yield `}${endOfLine}`;
65+
yield `declare var $refs: typeof __VLS_refs${endOfLine}`;
6766
}
6867

6968
function* generateSlotsType(): Generator<Code> {

packages/tsc/tests/typecheck.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ describe(`vue-tsc`, () => {
99
getTscOutput('stable')
1010
).toMatchInlineSnapshot(`
1111
[
12-
"test-workspace/tsc/failureFixtures/directives/main.vue(4,6): error TS2339: Property 'notExist' does not exist on type '{ $refs: {}; $: ComponentInternalInstance; $data: {}; $props: Partial<{}> & Omit<{} & VNodeProps & AllowedComponentProps & ComponentCustomProps & Readonly<...>, never>; ... 10 more ...; exist: typeof exist; }'.",
13-
"test-workspace/tsc/failureFixtures/directives/main.vue(9,6): error TS2339: Property 'notExist' does not exist on type '{ $refs: {}; $: ComponentInternalInstance; $data: {}; $props: Partial<{}> & Omit<{} & VNodeProps & AllowedComponentProps & ComponentCustomProps & Readonly<...>, never>; ... 10 more ...; exist: typeof exist; }'.",
12+
"test-workspace/tsc/failureFixtures/directives/main.vue(4,6): error TS2339: Property 'notExist' does not exist on type 'CreateComponentPublicInstance<Readonly<ExtractPropTypes<{}>>, { exist: typeof exist; }, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, ... 12 more ..., {}>'.",
13+
"test-workspace/tsc/failureFixtures/directives/main.vue(9,6): error TS2339: Property 'notExist' does not exist on type 'CreateComponentPublicInstance<Readonly<ExtractPropTypes<{}>>, { exist: typeof exist; }, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, ... 12 more ..., {}>'.",
1414
"test-workspace/tsc/failureFixtures/directives/main.vue(12,2): error TS2578: Unused '@ts-expect-error' directive.",
1515
]
1616
`);
@@ -21,8 +21,8 @@ describe(`vue-tsc`, () => {
2121
getTscOutput('next')
2222
).toMatchInlineSnapshot(`
2323
[
24-
"test-workspace/tsc/failureFixtures/directives/main.vue(4,6): error TS2339: Property 'notExist' does not exist on type '{ $refs: {}; $: ComponentInternalInstance; $data: {}; $props: Partial<{}> & Omit<{} & VNodeProps & AllowedComponentProps & ComponentCustomProps & Readonly<...>, never>; ... 10 more ...; exist: typeof exist; }'.",
25-
"test-workspace/tsc/failureFixtures/directives/main.vue(9,6): error TS2339: Property 'notExist' does not exist on type '{ $refs: {}; $: ComponentInternalInstance; $data: {}; $props: Partial<{}> & Omit<{} & VNodeProps & AllowedComponentProps & ComponentCustomProps & Readonly<...>, never>; ... 10 more ...; exist: typeof exist; }'.",
24+
"test-workspace/tsc/failureFixtures/directives/main.vue(4,6): error TS2339: Property 'notExist' does not exist on type 'CreateComponentPublicInstance<Readonly<ExtractPropTypes<{}>>, { exist: typeof exist; }, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, ... 12 more ..., {}>'.",
25+
"test-workspace/tsc/failureFixtures/directives/main.vue(9,6): error TS2339: Property 'notExist' does not exist on type 'CreateComponentPublicInstance<Readonly<ExtractPropTypes<{}>>, { exist: typeof exist; }, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, ... 12 more ..., {}>'.",
2626
"test-workspace/tsc/failureFixtures/directives/main.vue(12,2): error TS2578: Unused '@ts-expect-error' directive.",
2727
"test-workspace/tsc/passedFixtures/#3373/tsconfig.json(4,3): error TS5102: Option 'importsNotUsedAsValues' has been removed. Please remove it from your configuration.
2828
Use 'verbatimModuleSyntax' instead.",

test-workspace/tsc/passedFixtures/vue3.5/templateRef/main.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import { exactType } from '../../shared';
66
<template>
77
<TemplateRef ref="templateRef" />
88

9-
{{ exactType($refs.templateRef?.$refs.generic?.foo, {} as 1 | undefined) }}
9+
{{ exactType($refs.templateRef.$refs.generic.foo, {} as 1) }}
1010
</template>

0 commit comments

Comments
 (0)