@@ -5,7 +5,6 @@ import { setTemplateRefIdent } from './generators/templateRef'
55import {
66 buildCodeFragment ,
77 codeFragmentToString ,
8- genCall ,
98 INDENT_END ,
109 INDENT_START ,
1110 NEWLINE ,
@@ -20,7 +19,9 @@ import type {
2019export type CodegenOptions = Omit <
2120 BaseCodegenOptions ,
2221 'optimizeImports' | 'inline' | 'bindingMetadata' | 'prefixIdentifiers'
23- >
22+ > & {
23+ templates ?: string [ ]
24+ }
2425
2526export class CodegenContext {
2627 options : Required < CodegenOptions >
@@ -84,16 +85,20 @@ export class CodegenContext {
8485 ssr : false ,
8586 isTS : false ,
8687 inSSR : false ,
88+ templates : [ ] ,
8789 expressionPlugins : [ ] ,
8890 }
8991 this . options = extend ( defaultOptions , options )
9092 this . block = ir . block
9193 }
9294}
9395
94- export interface VaporCodegenResult extends BaseCodegenResult {
96+ export interface VaporCodegenResult
97+ extends Omit < BaseCodegenResult , 'preamble' > {
9598 ast : RootIRNode
9699 helpers : Set < string >
100+ templates : string [ ]
101+ delegates : Set < string >
97102}
98103
99104// IR -> JS codegen
@@ -115,37 +120,19 @@ export function generate(
115120 push ( ...genBlockContent ( ir . block , context , true ) )
116121 push ( INDENT_END , NEWLINE )
117122
118- const delegates = genDelegates ( context )
119- const templates = genTemplates ( ir . template , ir . rootTemplateIndex , context )
120- const imports = genHelperImports ( context )
121- const preamble = imports + templates + delegates
123+ if ( context . delegates . size ) {
124+ context . helper ( 'delegateEvents' )
125+ }
126+ const templates = genTemplates ( ir . templates , ir . rootTemplateIndex , context )
122127
123128 const [ code , map ] = codeFragmentToString ( frag , context )
124129
125130 return {
126131 code,
127132 ast : ir ,
128- preamble,
129133 map : map && map . toJSON ( ) ,
130134 helpers,
135+ templates,
136+ delegates : context . delegates ,
131137 }
132138}
133-
134- function genDelegates ( { delegates, helper } : CodegenContext ) {
135- return delegates . size
136- ? `${ genCall (
137- helper ( 'delegateEvents' ) ,
138- ...Array . from ( delegates ) . map ( ( v ) => `"${ v } "` ) ,
139- ) . join ( '' ) } \n`
140- : ''
141- }
142-
143- function genHelperImports ( { helpers, options } : CodegenContext ) {
144- let imports = ''
145- if ( helpers . size ) {
146- imports += `import { ${ [ ...helpers ]
147- . map ( ( h ) => `${ h } as _${ h } ` )
148- . join ( ', ' ) } } from '${ options . runtimeModuleName } ';\n`
149- }
150- return imports
151- }
0 commit comments