@@ -4,114 +4,99 @@ import { getSlotsPropertyName, hyphenateTag } from '../../utils/shared';
4
4
import { endOfLine , newLine } from '../common' ;
5
5
import { TemplateCodegenContext , createTemplateCodegenContext } from '../template/context' ;
6
6
import { forEachInterpolationSegment } from '../template/interpolation' ;
7
+ import { generateStyleScopedClasses } from '../template/styleScopedClasses' ;
7
8
import type { ScriptCodegenContext } from './context' ;
8
9
import { codeFeatures , type ScriptCodegenOptions } from './index' ;
9
10
import { 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 ;
18
11
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` ) ;
36
16
}
37
17
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 () => {}>>` ) ;
44
19
}
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 } ` ;
45
27
}
46
28
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
+
50
32
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 ( [
53
35
options . sfc . script . content . substring ( componentsOption . start , componentsOption . end ) ,
54
36
'script' ,
55
37
componentsOption . start ,
56
38
codeFeatures . navigation ,
57
- ] ;
58
- }
59
- else {
60
- yield `{}` ;
39
+ ] ) ;
61
40
}
62
- yield endOfLine ;
63
- }
64
41
65
- function * generateConstNameOption ( options : ScriptCodegenOptions ) : Generator < Code > {
42
+ let nameType : Code | undefined ;
66
43
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 ) ;
71
46
}
72
47
else if ( options . sfc . scriptSetup ) {
73
48
yield `let __VLS_name!: '${ options . scriptSetupRanges ?. options . name ?? options . fileBaseName . substring ( 0 , options . fileBaseName . lastIndexOf ( '.' ) ) } '${ endOfLine } ` ;
49
+ nameType = 'typeof __VLS_name' ;
74
50
}
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 } ` ;
77
69
}
70
+ yield `}${ endOfLine } ` ;
78
71
}
79
72
80
- function * generateCtx (
73
+ export function * generateTemplate (
81
74
options : ScriptCodegenOptions ,
75
+ ctx : ScriptCodegenContext ,
82
76
isClassComponent : boolean
83
77
) : 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 ) ;
90
86
}
91
87
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 } ` ;
97
92
}
98
- yield endOfLine ;
99
93
}
100
94
101
- function * generateTemplateContext (
95
+ function * generateTemplateBody (
102
96
options : ScriptCodegenOptions ,
103
97
templateCodegenCtx : TemplateCodegenContext
104
98
) : 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 */
113
99
const firstClasses = new Set < string > ( ) ;
114
- yield `/* Style Scoped */${ newLine } ` ;
115
100
yield `let __VLS_styleScopedClasses!: {}` ;
116
101
for ( let i = 0 ; i < options . sfc . styles . length ; i ++ ) {
117
102
const style = options . sfc . styles [ i ] ;
@@ -155,7 +140,7 @@ function* generateTemplateContext(
155
140
}
156
141
}
157
142
158
- yield `return { ${ newLine } ` ;
143
+ yield `const __VLS_templateResult = { ` ;
159
144
yield `slots: ${ options . scriptSetupRanges ?. slots . name ?? '__VLS_slots' } ,${ newLine } ` ;
160
145
yield `refs: __VLS_refs as __VLS_PickRefsExpose<typeof __VLS_refs>,${ newLine } ` ;
161
146
yield `attrs: {} as Partial<typeof __VLS_inheritedAttrs>,${ newLine } ` ;
0 commit comments