1
+ import { camelize , capitalize } from '@vue/shared' ;
1
2
import * as path from 'path-browserify' ;
2
3
import type { Code } from '../../types' ;
3
4
import { codeFeatures } from '../codeFeatures' ;
@@ -6,7 +7,6 @@ import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
6
7
import { generateComponentSetupReturns , generateEmitsOption , generatePropsOption } from './component' ;
7
8
import type { ScriptCodegenContext } from './context' ;
8
9
import type { ScriptCodegenOptions } from './index' ;
9
- import { getTemplateUsageVars } from './template' ;
10
10
11
11
export function * generateComponentSelf (
12
12
options : ScriptCodegenOptions ,
@@ -21,27 +21,20 @@ export function* generateComponentSelf(
21
21
yield * generateComponentSetupReturns ( options . scriptSetupRanges ) ;
22
22
}
23
23
// 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 ;
44
32
}
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 } ` ;
45
38
}
46
39
yield `}${ endOfLine } ` ; // return {
47
40
yield `},${ newLine } ` ; // setup() {
0 commit comments