@@ -4,114 +4,99 @@ import { getSlotsPropertyName, hyphenateTag } from '../../utils/shared';
44import { endOfLine , newLine } from '../common' ;
55import { TemplateCodegenContext , createTemplateCodegenContext } from '../template/context' ;
66import { forEachInterpolationSegment } from '../template/interpolation' ;
7+ import { generateStyleScopedClasses } from '../template/styleScopedClasses' ;
78import type { ScriptCodegenContext } from './context' ;
89import { codeFeatures , type ScriptCodegenOptions } from './index' ;
910import { generateInternalComponent } from './internalComponent' ;
10- import { generateStyleScopedClasses } from '../template/styleScopedClasses' ;
11-
12- export function * generateTemplate (
13- options : ScriptCodegenOptions ,
14- ctx : ScriptCodegenContext ,
15- isClassComponent : boolean
16- ) : Generator < Code > {
17- ctx . generatedTemplate = true ;
1811
19- if ( ! options . vueCompilerOptions . skipTemplateCodegen ) {
20- if ( isClassComponent ) {
21- yield `__VLS_template = (() => {${ newLine } ` ;
22- }
23- else {
24- yield `const __VLS_template = (() => {${ newLine } ` ;
25- }
26- const templateCodegenCtx = createTemplateCodegenContext ( { scriptSetupBindingNames : new Set ( ) , edited : options . edited } ) ;
27- yield `const __VLS_template_return = () => {${ newLine } ` ;
28- yield * generateCtx ( options , isClassComponent ) ;
29- yield * generateTemplateContext ( options , templateCodegenCtx ) ;
30- yield * generateExportOptions ( options ) ;
31- yield * generateConstNameOption ( options ) ;
32- yield `}${ endOfLine } ` ;
33- yield * generateInternalComponent ( options , ctx , templateCodegenCtx ) ;
34- yield `return __VLS_template_return${ endOfLine } ` ;
35- yield `})()${ endOfLine } ` ;
12+ export function * generateTemplateCtx ( options : ScriptCodegenOptions , isClassComponent : boolean ) : Generator < Code > {
13+ const types = [ ] ;
14+ if ( isClassComponent ) {
15+ types . push ( `typeof this` ) ;
3616 }
3717 else {
38- yield `function __VLS_template() {${ newLine } ` ;
39- const templateUsageVars = [ ...getTemplateUsageVars ( options , ctx ) ] ;
40- yield `// @ts-ignore${ newLine } ` ;
41- yield `[${ templateUsageVars . join ( ', ' ) } ]${ newLine } ` ;
42- yield `return { slots: {}, refs: {}, attrs: {} }${ endOfLine } ` ;
43- yield `}${ newLine } ` ;
18+ types . push ( `InstanceType<__VLS_PickNotAny<typeof __VLS_internalComponent, new () => {}>>` ) ;
4419 }
20+ if ( options . vueCompilerOptions . petiteVueExtensions . some ( ext => options . fileBaseName . endsWith ( ext ) ) ) {
21+ types . push ( `typeof globalThis` ) ;
22+ }
23+ if ( options . sfc . styles . some ( style => style . module ) ) {
24+ types . push ( `__VLS_StyleModules` ) ;
25+ }
26+ yield `let __VLS_ctx!: ${ types . join ( ' & ' ) } ${ endOfLine } ` ;
4527}
4628
47- function * generateExportOptions ( options : ScriptCodegenOptions ) : Generator < Code > {
48- yield newLine ;
49- yield `const __VLS_componentsOption = ` ;
29+ export function * generateTemplateComponents ( options : ScriptCodegenOptions ) : Generator < Code > {
30+ const exps : Code [ ] = [ ] ;
31+
5032 if ( options . sfc . script && options . scriptRanges ?. exportDefault ?. componentsOption ) {
51- const componentsOption = options . scriptRanges . exportDefault . componentsOption ;
52- yield [
33+ const { componentsOption } = options . scriptRanges . exportDefault ;
34+ exps . push ( [
5335 options . sfc . script . content . substring ( componentsOption . start , componentsOption . end ) ,
5436 'script' ,
5537 componentsOption . start ,
5638 codeFeatures . navigation ,
57- ] ;
58- }
59- else {
60- yield `{}` ;
39+ ] ) ;
6140 }
62- yield endOfLine ;
63- }
6441
65- function * generateConstNameOption ( options : ScriptCodegenOptions ) : Generator < Code > {
42+ let nameType : Code | undefined ;
6643 if ( options . sfc . script && options . scriptRanges ?. exportDefault ?. nameOption ) {
67- const nameOption = options . scriptRanges . exportDefault . nameOption ;
68- yield `const __VLS_name = ` ;
69- yield `${ options . sfc . script . content . substring ( nameOption . start , nameOption . end ) } as const` ;
70- yield endOfLine ;
44+ const { nameOption } = options . scriptRanges . exportDefault ;
45+ nameType = options . sfc . script . content . substring ( nameOption . start , nameOption . end ) ;
7146 }
7247 else if ( options . sfc . scriptSetup ) {
7348 yield `let __VLS_name!: '${ options . scriptSetupRanges ?. options . name ?? options . fileBaseName . substring ( 0 , options . fileBaseName . lastIndexOf ( '.' ) ) } '${ endOfLine } ` ;
49+ nameType = 'typeof __VLS_name' ;
7450 }
75- else {
76- yield `const __VLS_name = undefined${ endOfLine } ` ;
51+ if ( nameType ) {
52+ exps . push ( `{} as {
53+ [K in ${ nameType } ]: typeof __VLS_internalComponent
54+ & (new () => {
55+ ${ getSlotsPropertyName ( options . vueCompilerOptions . target ) } : typeof ${ options . scriptSetupRanges ?. slots ?. name ?? '__VLS_slots' }
56+ })
57+ }` ) ;
58+ }
59+
60+ exps . push ( `{} as NonNullable<typeof __VLS_internalComponent extends { components: infer C } ? C : {}>` ) ;
61+ exps . push ( `{} as __VLS_GlobalComponents` ) ;
62+ exps . push ( `{} as typeof __VLS_ctx` ) ;
63+
64+ yield `const __VLS_components = {${ newLine } ` ;
65+ for ( const type of exps ) {
66+ yield `...` ;
67+ yield type ;
68+ yield `,${ newLine } ` ;
7769 }
70+ yield `}${ endOfLine } ` ;
7871}
7972
80- function * generateCtx (
73+ export function * generateTemplate (
8174 options : ScriptCodegenOptions ,
75+ ctx : ScriptCodegenContext ,
8276 isClassComponent : boolean
8377) : Generator < Code > {
84- yield `let __VLS_ctx!: ` ;
85- if ( options . vueCompilerOptions . petiteVueExtensions . some ( ext => options . fileBaseName . endsWith ( ext ) ) ) {
86- yield `typeof globalThis & ` ;
87- }
88- if ( ! isClassComponent ) {
89- yield `InstanceType<__VLS_PickNotAny<typeof __VLS_internalComponent, new () => {}>>` ;
78+ ctx . generatedTemplate = true ;
79+
80+ if ( ! options . vueCompilerOptions . skipTemplateCodegen ) {
81+ const templateCodegenCtx = createTemplateCodegenContext ( { scriptSetupBindingNames : new Set ( ) , edited : options . edited } ) ;
82+ yield * generateTemplateCtx ( options , isClassComponent ) ;
83+ yield * generateTemplateComponents ( options ) ;
84+ yield * generateTemplateBody ( options , templateCodegenCtx ) ;
85+ yield * generateInternalComponent ( options , ctx , templateCodegenCtx ) ;
9086 }
9187 else {
92- yield `typeof this` ;
93- }
94- /* CSS Module */
95- if ( options . sfc . styles . some ( style => style . module ) ) {
96- yield ` & __VLS_StyleModules` ;
88+ const templateUsageVars = [ ...getTemplateUsageVars ( options , ctx ) ] ;
89+ yield `// @ts-ignore${ newLine } ` ;
90+ yield `[${ templateUsageVars . join ( ', ' ) } ]${ newLine } ` ;
91+ yield `const __VLS_templateResult { slots: {}, refs: {}, attrs: {} }${ endOfLine } ` ;
9792 }
98- yield endOfLine ;
9993}
10094
101- function * generateTemplateContext (
95+ function * generateTemplateBody (
10296 options : ScriptCodegenOptions ,
10397 templateCodegenCtx : TemplateCodegenContext
10498) : Generator < Code > {
105- /* Components */
106- yield `/* Components */${ newLine } ` ;
107- yield `let __VLS_otherComponents!: NonNullable<typeof __VLS_internalComponent extends { components: infer C } ? C : {}> & typeof __VLS_componentsOption${ endOfLine } ` ;
108- yield `let __VLS_own!: __VLS_SelfComponent<typeof __VLS_name, typeof __VLS_internalComponent & (new () => { ${ getSlotsPropertyName ( options . vueCompilerOptions . target ) } : typeof ${ options . scriptSetupRanges ?. slots ?. name ?? '__VLS_slots' } })>${ endOfLine } ` ;
109- yield `let __VLS_localComponents!: typeof __VLS_otherComponents & Omit<typeof __VLS_own, keyof typeof __VLS_otherComponents>${ endOfLine } ` ;
110- yield `let __VLS_components!: typeof __VLS_localComponents & __VLS_GlobalComponents & typeof __VLS_ctx${ endOfLine } ` ; // for html completion, TS references...
111-
112- /* Style Scoped */
11399 const firstClasses = new Set < string > ( ) ;
114- yield `/* Style Scoped */${ newLine } ` ;
115100 yield `let __VLS_styleScopedClasses!: {}` ;
116101 for ( let i = 0 ; i < options . sfc . styles . length ; i ++ ) {
117102 const style = options . sfc . styles [ i ] ;
@@ -155,7 +140,7 @@ function* generateTemplateContext(
155140 }
156141 }
157142
158- yield `return { ${ newLine } ` ;
143+ yield `const __VLS_templateResult = { ` ;
159144 yield `slots: ${ options . scriptSetupRanges ?. slots . name ?? '__VLS_slots' } ,${ newLine } ` ;
160145 yield `refs: __VLS_refs as __VLS_PickRefsExpose<typeof __VLS_refs>,${ newLine } ` ;
161146 yield `attrs: {} as Partial<typeof __VLS_inheritedAttrs>,${ newLine } ` ;
0 commit comments