1+ import { camelize , capitalize } from '@vue/shared' ;
12import * as path from 'path-browserify' ;
23import type { Code } from '../../types' ;
34import { codeFeatures } from '../codeFeatures' ;
@@ -6,7 +7,6 @@ import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
67import { generateComponentSetupReturns , generateEmitsOption , generatePropsOption } from './component' ;
78import type { ScriptCodegenContext } from './context' ;
89import type { ScriptCodegenOptions } from './index' ;
9- import { getTemplateUsageVars } from './template' ;
1010
1111export function * generateComponentSelf (
1212 options : ScriptCodegenOptions ,
@@ -21,27 +21,20 @@ export function* generateComponentSelf(
2121 yield * generateComponentSetupReturns ( options . scriptSetupRanges ) ;
2222 }
2323 // bindings
24- const templateUsageVars = getTemplateUsageVars ( options , ctx ) ;
25- for (
26- const [ content , bindings ] of [
27- [ options . sfc . scriptSetup . content , options . scriptSetupRanges . bindings ] as const ,
28- options . sfc . script && options . scriptRanges
29- ? [ options . sfc . script . content , options . scriptRanges . bindings ] as const
30- : [ '' , [ ] ] as const ,
31- ]
32- ) {
33- for ( const { range } of bindings ) {
34- const varName = content . slice ( range . start , range . end ) ;
35- if ( ! templateUsageVars . has ( varName ) && ! templateCodegenCtx . accessExternalVariables . has ( varName ) ) {
36- continue ;
37- }
38-
39- const token = Symbol ( varName . length ) ;
40- yield [ '' , undefined , 0 , { __linkedToken : token } ] ;
41- yield `${ varName } : ${ varName } as typeof ` ;
42- yield [ '' , undefined , 0 , { __linkedToken : token } ] ;
43- yield `${ varName } ,${ newLine } ` ;
24+ const templateUsageVars = new Set ( [
25+ ...options . sfc . template ?. ast ?. components . flatMap ( c => [ camelize ( c ) , capitalize ( camelize ( c ) ) ] ) ?? [ ] ,
26+ ...options . templateCodegen ?. accessExternalVariables . keys ( ) ?? [ ] ,
27+ ...templateCodegenCtx . accessExternalVariables . keys ( ) ,
28+ ] ) ;
29+ for ( const varName of ctx . bindingNames ) {
30+ if ( ! templateUsageVars . has ( varName ) ) {
31+ continue ;
4432 }
33+ const token = Symbol ( varName . length ) ;
34+ yield [ '' , undefined , 0 , { __linkedToken : token } ] ;
35+ yield `${ varName } : ${ varName } as typeof ` ;
36+ yield [ '' , undefined , 0 , { __linkedToken : token } ] ;
37+ yield `${ varName } ,${ newLine } ` ;
4538 }
4639 yield `}${ endOfLine } ` ; // return {
4740 yield `},${ newLine } ` ; // setup() {
0 commit comments