@@ -12,7 +12,12 @@ import {
12
12
walkIdentifiers
13
13
} from '@vue/compiler-dom'
14
14
import { DEFAULT_FILENAME , SFCDescriptor , SFCScriptBlock } from './parse'
15
- import { parse as _parse , ParserOptions , ParserPlugin } from '@babel/parser'
15
+ import {
16
+ parse as _parse ,
17
+ parseExpression ,
18
+ ParserOptions ,
19
+ ParserPlugin
20
+ } from '@babel/parser'
16
21
import { camelize , capitalize , generateCodeFrame , makeMap } from '@vue/shared'
17
22
import {
18
23
Node ,
@@ -348,14 +353,23 @@ export function compileScript(
348
353
local : string ,
349
354
imported : string | false ,
350
355
isType : boolean ,
351
- isFromSetup : boolean
356
+ isFromSetup : boolean ,
357
+ needTemplateUsageCheck : boolean
352
358
) {
353
359
if ( source === 'vue' && imported ) {
354
360
userImportAlias [ imported ] = local
355
361
}
356
362
357
- let isUsedInTemplate = true
358
- if ( isTS && sfc . template && ! sfc . template . src && ! sfc . template . lang ) {
363
+ // template usage check is only needed in non-inline mode, so we can skip
364
+ // the work if inlineTemplate is true.
365
+ let isUsedInTemplate = needTemplateUsageCheck
366
+ if (
367
+ needTemplateUsageCheck &&
368
+ isTS &&
369
+ sfc . template &&
370
+ ! sfc . template . src &&
371
+ ! sfc . template . lang
372
+ ) {
359
373
isUsedInTemplate = isImportUsed ( local , sfc )
360
374
}
361
375
@@ -813,7 +827,8 @@ export function compileScript(
813
827
node . importKind === 'type' ||
814
828
( specifier . type === 'ImportSpecifier' &&
815
829
specifier . importKind === 'type' ) ,
816
- false
830
+ false ,
831
+ ! options . inlineTemplate
817
832
)
818
833
}
819
834
} else if ( node . type === 'ExportDefaultDeclaration' ) {
@@ -1027,7 +1042,8 @@ export function compileScript(
1027
1042
node . importKind === 'type' ||
1028
1043
( specifier . type === 'ImportSpecifier' &&
1029
1044
specifier . importKind === 'type' ) ,
1030
- true
1045
+ true ,
1046
+ ! options . inlineTemplate
1031
1047
)
1032
1048
}
1033
1049
}
@@ -2051,14 +2067,14 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
2051
2067
code += `,v${ capitalize ( camelize ( prop . name ) ) } `
2052
2068
}
2053
2069
if ( prop . exp ) {
2054
- code += `,${ stripStrings (
2070
+ code += `,${ processExp (
2055
2071
( prop . exp as SimpleExpressionNode ) . content
2056
2072
) } `
2057
2073
}
2058
2074
}
2059
2075
}
2060
2076
} else if ( node . type === NodeTypes . INTERPOLATION ) {
2061
- code += `,${ stripStrings (
2077
+ code += `,${ processExp (
2062
2078
( node . content as SimpleExpressionNode ) . content
2063
2079
) } `
2064
2080
}
@@ -2071,6 +2087,19 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
2071
2087
return code
2072
2088
}
2073
2089
2090
+ function processExp ( exp : string ) {
2091
+ if ( / a s \w | < .* > / . test ( exp ) ) {
2092
+ let ret = ''
2093
+ // has potential type cast or generic arguments that uses types
2094
+ const ast = parseExpression ( exp , { plugins : [ 'typescript' ] } )
2095
+ walkIdentifiers ( ast , node => {
2096
+ ret += `,` + node . name
2097
+ } )
2098
+ return ret
2099
+ }
2100
+ return stripStrings ( exp )
2101
+ }
2102
+
2074
2103
function stripStrings ( exp : string ) {
2075
2104
return exp
2076
2105
. replace ( / ' [ ^ ' ] * ' | " [ ^ " ] * " / g, '' )
0 commit comments