@@ -6,32 +6,47 @@ import { generateStyleScopedClasses } from '../style/scopedClasses';
6
6
import { createTemplateCodegenContext , type TemplateCodegenContext } from '../template/context' ;
7
7
import { generateInterpolation } from '../template/interpolation' ;
8
8
import { generateStyleScopedClassReferences } from '../template/styleScopedClasses' ;
9
- import { endOfLine , newLine } from '../utils' ;
9
+ import { endOfLine , generateSfcBlockSection , newLine } from '../utils' ;
10
10
import { generateIntersectMerge , generateSpreadMerge } from '../utils/merge' ;
11
11
import type { ScriptCodegenContext } from './context' ;
12
12
import type { ScriptCodegenOptions } from './index' ;
13
13
14
14
export function * generateTemplate (
15
15
options : ScriptCodegenOptions ,
16
16
ctx : ScriptCodegenContext ,
17
- ) : Generator < Code , TemplateCodegenContext > {
17
+ ) : Generator < Code > {
18
18
ctx . generatedTemplate = true ;
19
19
20
20
const templateCodegenCtx = createTemplateCodegenContext ( {
21
21
scriptSetupBindingNames : new Set ( ) ,
22
22
} ) ;
23
- yield * generateTemplateCtx ( options ) ;
23
+ yield * generateBindings ( options , ctx ) ;
24
+ yield * generateTemplateCtx ( options , ctx ) ;
24
25
yield * generateTemplateElements ( ) ;
25
26
yield * generateTemplateComponents ( options ) ;
26
27
yield * generateTemplateDirectives ( options ) ;
27
28
yield * generateTemplateBody ( options , templateCodegenCtx ) ;
28
- return templateCodegenCtx ;
29
+
30
+ if ( options . sfc . script && options . scriptRanges ?. exportDefault ) {
31
+ yield `const __VLS_self = (await import('${ options . vueCompilerOptions . lib } ')).defineComponent(` ;
32
+ const { args } = options . scriptRanges . exportDefault ;
33
+ yield generateSfcBlockSection ( options . sfc . script , args . start , args . end , codeFeatures . all ) ;
34
+ yield `)${ endOfLine } ` ;
35
+ }
29
36
}
30
37
31
- function * generateTemplateCtx ( options : ScriptCodegenOptions ) : Generator < Code > {
32
- const exps = [ ] ;
38
+ function * generateTemplateCtx (
39
+ options : ScriptCodegenOptions ,
40
+ ctx : ScriptCodegenContext ,
41
+ ) : Generator < Code > {
42
+ const exps : Code [ ] = [ ] ;
33
43
34
- exps . push ( `{} as InstanceType<__VLS_PickNotAny<typeof __VLS_self, new () => {}>>` ) ;
44
+ if ( options . sfc . script && options . scriptRanges ?. exportDefault ) {
45
+ exps . push ( `{} as InstanceType<__VLS_PickNotAny<typeof __VLS_self, new () => {}>>` ) ;
46
+ }
47
+ else {
48
+ exps . push ( `{} as import('${ options . vueCompilerOptions . lib } ').ComponentPublicInstance` ) ;
49
+ }
35
50
36
51
if ( options . vueCompilerOptions . petiteVueExtensions . some ( ext => options . fileName . endsWith ( ext ) ) ) {
37
52
exps . push ( `globalThis` ) ;
@@ -40,6 +55,34 @@ function* generateTemplateCtx(options: ScriptCodegenOptions): Generator<Code> {
40
55
exps . push ( `{} as __VLS_StyleModules` ) ;
41
56
}
42
57
58
+ if ( ctx . generatedPropsType || options . scriptSetupRanges ?. defineProps ) {
59
+ yield `type __VLS_InternalProps = ` ;
60
+ const { defineProps } = options . scriptSetupRanges ?? { } ;
61
+ if ( defineProps ) {
62
+ yield `__VLS_SpreadMerge<__VLS_PublicProps, typeof ${ defineProps . name ?? `__VLS_props` } >` ;
63
+ }
64
+ else {
65
+ yield `__VLS_PublicProps` ;
66
+ }
67
+ yield endOfLine ;
68
+ exps . push ( `{} as __VLS_InternalProps` ) ;
69
+ exps . push ( `{} as { $props: __VLS_InternalProps }` ) ;
70
+ }
71
+
72
+ const emitTypes : Code [ ] = [ ] ;
73
+ if ( options . scriptSetupRanges ?. defineEmits ) {
74
+ emitTypes . push ( `typeof ${ options . scriptSetupRanges . defineEmits . name ?? `__VLS_emit` } ` ) ;
75
+ }
76
+ if ( options . scriptSetupRanges ?. defineModel . length ) {
77
+ emitTypes . push ( `typeof __VLS_modelEmit` ) ;
78
+ }
79
+ if ( emitTypes . length ) {
80
+ yield `type __VLS_ResolvedEmit = ${ emitTypes . join ( ' & ' ) } ${ endOfLine } ` ;
81
+ exps . push ( `{} as { $emit: __VLS_ResolvedEmit }` ) ;
82
+ }
83
+
84
+ exps . push ( `{} as import('${ options . vueCompilerOptions . lib } ').ShallowUnwrapRef<typeof __VLS_bindings>` ) ;
85
+
43
86
yield `const __VLS_ctx = ` ;
44
87
yield * generateSpreadMerge ( exps ) ;
45
88
yield endOfLine ;
@@ -65,14 +108,14 @@ function* generateTemplateComponents(options: ScriptCodegenOptions): Generator<C
65
108
types . push ( `typeof __VLS_componentsOption` ) ;
66
109
}
67
110
68
- yield `type __VLS_LocalComponents =` ;
111
+ yield `type __VLS_LocalComponents = ` ;
69
112
yield * generateIntersectMerge ( types ) ;
70
113
yield endOfLine ;
71
114
72
115
yield `let __VLS_components!: __VLS_LocalComponents & __VLS_GlobalComponents${ endOfLine } ` ;
73
116
}
74
117
75
- export function * generateTemplateDirectives ( options : ScriptCodegenOptions ) : Generator < Code > {
118
+ function * generateTemplateDirectives ( options : ScriptCodegenOptions ) : Generator < Code > {
76
119
const types : Code [ ] = [ `typeof __VLS_ctx` ] ;
77
120
78
121
if ( options . sfc . script && options . scriptRanges ?. exportDefault ?. directivesOption ) {
@@ -88,7 +131,7 @@ export function* generateTemplateDirectives(options: ScriptCodegenOptions): Gene
88
131
types . push ( `__VLS_ResolveDirectives<typeof __VLS_directivesOption>` ) ;
89
132
}
90
133
91
- yield `type __VLS_LocalDirectives =` ;
134
+ yield `type __VLS_LocalDirectives = ` ;
92
135
yield * generateIntersectMerge ( types ) ;
93
136
yield endOfLine ;
94
137
@@ -138,7 +181,39 @@ function* generateCssVars(options: ScriptCodegenOptions, ctx: TemplateCodegenCon
138
181
yield `// CSS variable injection end ${ newLine } ` ;
139
182
}
140
183
141
- export function getTemplateUsageVars ( options : ScriptCodegenOptions , ctx : ScriptCodegenContext ) {
184
+ function * generateBindings (
185
+ options : ScriptCodegenOptions ,
186
+ ctx : ScriptCodegenContext ,
187
+ ) : Generator < Code > {
188
+ yield `const __VLS_bindings = {${ newLine } ` ;
189
+ if ( options . sfc . scriptSetup && options . scriptSetupRanges ) {
190
+ const templateUsageVars = getTemplateUsageVars ( options , ctx ) ;
191
+ for (
192
+ const [ content , bindings ] of [
193
+ [ options . sfc . scriptSetup . content , options . scriptSetupRanges . bindings ] as const ,
194
+ options . sfc . script && options . scriptRanges
195
+ ? [ options . sfc . script . content , options . scriptRanges . bindings ] as const
196
+ : [ '' , [ ] ] as const ,
197
+ ]
198
+ ) {
199
+ for ( const { range } of bindings ) {
200
+ const varName = content . slice ( range . start , range . end ) ;
201
+ if ( ! templateUsageVars . has ( varName ) ) {
202
+ continue ;
203
+ }
204
+
205
+ const token = Symbol ( varName . length ) ;
206
+ yield [ '' , undefined , 0 , { __linkedToken : token } ] ;
207
+ yield `${ varName } : ${ varName } as typeof ` ;
208
+ yield [ '' , undefined , 0 , { __linkedToken : token } ] ;
209
+ yield `${ varName } ,${ newLine } ` ;
210
+ }
211
+ }
212
+ }
213
+ yield `}${ endOfLine } ` ;
214
+ }
215
+
216
+ function getTemplateUsageVars ( options : ScriptCodegenOptions , ctx : ScriptCodegenContext ) {
142
217
const usageVars = new Set < string > ( ) ;
143
218
const components = new Set ( options . sfc . template ?. ast ?. components ) ;
144
219
0 commit comments