@@ -284,11 +284,9 @@ export function compileScript(
284
284
userImportAlias [ imported ] = local
285
285
}
286
286
287
- // template usage check is only needed in non-inline mode, so we can skip
288
- // the work if inlineTemplate is true.
289
287
let isUsedInTemplate = true
290
- if ( isTS && sfc . template && ! sfc . template . src && ! sfc . template . lang ) {
291
- isUsedInTemplate = isImportUsed ( local , sfc )
288
+ if ( sfc . template && ! sfc . template . src && ! sfc . template . lang ) {
289
+ isUsedInTemplate = isImportUsed ( local , sfc , isTS )
292
290
}
293
291
294
292
userImports [ local ] = {
@@ -1782,7 +1780,7 @@ function getObjectOrArrayExpressionKeys(value: Node): string[] {
1782
1780
1783
1781
const templateUsageCheckCache = new LRU < string , string > ( 512 )
1784
1782
1785
- function resolveTemplateUsageCheckString ( sfc : SFCDescriptor ) {
1783
+ function resolveTemplateUsageCheckString ( sfc : SFCDescriptor , isTS : boolean ) {
1786
1784
const { content } = sfc . template !
1787
1785
const cached = templateUsageCheckCache . get ( content )
1788
1786
if ( cached ) {
@@ -1809,15 +1807,15 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
1809
1807
code += `,v${ capitalize ( camelize ( baseName ) ) } `
1810
1808
}
1811
1809
if ( value ) {
1812
- code += `,${ processExp ( value , baseName ) } `
1810
+ code += `,${ processExp ( value , isTS , baseName ) } `
1813
1811
}
1814
1812
}
1815
1813
}
1816
1814
} ,
1817
1815
chars ( text ) {
1818
1816
const res = parseText ( text )
1819
1817
if ( res ) {
1820
- code += `,${ processExp ( res . expression ) } `
1818
+ code += `,${ processExp ( res . expression , isTS ) } `
1821
1819
}
1822
1820
}
1823
1821
} )
@@ -1829,8 +1827,8 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
1829
1827
1830
1828
const forAliasRE = / ( [ \s \S ] * ?) \s + (?: i n | o f ) \s + ( [ \s \S ] * ) /
1831
1829
1832
- function processExp ( exp : string , dir ?: string ) : string {
1833
- if ( / a s \s + \w | < .* > | : / . test ( exp ) ) {
1830
+ function processExp ( exp : string , isTS : boolean , dir ?: string ) : string {
1831
+ if ( isTS && / a s \s + \w | < .* > | : / . test ( exp ) ) {
1834
1832
if ( dir === 'slot' ) {
1835
1833
exp = `(${ exp } )=>{}`
1836
1834
} else if ( dir === 'on' ) {
@@ -1839,7 +1837,7 @@ function processExp(exp: string, dir?: string): string {
1839
1837
const inMatch = exp . match ( forAliasRE )
1840
1838
if ( inMatch ) {
1841
1839
const [ , LHS , RHS ] = inMatch
1842
- return processExp ( `(${ LHS } )=>{}` ) + processExp ( RHS )
1840
+ return processExp ( `(${ LHS } )=>{}` , true ) + processExp ( RHS , true )
1843
1841
}
1844
1842
}
1845
1843
let ret = ''
@@ -1867,36 +1865,38 @@ function stripTemplateString(str: string): string {
1867
1865
return ''
1868
1866
}
1869
1867
1870
- function isImportUsed ( local : string , sfc : SFCDescriptor ) : boolean {
1868
+ function isImportUsed (
1869
+ local : string ,
1870
+ sfc : SFCDescriptor ,
1871
+ isTS : boolean
1872
+ ) : boolean {
1871
1873
return new RegExp (
1872
1874
// #4274 escape $ since it's a special char in regex
1873
1875
// (and is the only regex special char that is valid in identifiers)
1874
1876
`[^\\w$_]${ local . replace ( / \$ / g, '\\$' ) } [^\\w$_]`
1875
- ) . test ( resolveTemplateUsageCheckString ( sfc ) )
1877
+ ) . test ( resolveTemplateUsageCheckString ( sfc , isTS ) )
1876
1878
}
1877
1879
1878
1880
/**
1879
1881
* Note: this comparison assumes the prev/next script are already identical,
1880
- * and only checks the special case where <script setup lang="ts" > unused import
1882
+ * and only checks the special case where <script setup> unused import
1881
1883
* pruning result changes due to template changes.
1882
1884
*/
1883
1885
export function hmrShouldReload (
1884
1886
prevImports : Record < string , ImportBinding > ,
1885
1887
next : SFCDescriptor
1886
1888
) : boolean {
1887
- if (
1888
- ! next . scriptSetup ||
1889
- ( next . scriptSetup . lang !== 'ts' && next . scriptSetup . lang !== 'tsx' )
1890
- ) {
1889
+ if ( ! next . scriptSetup ) {
1891
1890
return false
1892
1891
}
1893
1892
1893
+ const isTS = next . scriptSetup . lang === 'ts' || next . scriptSetup . lang === 'tsx'
1894
1894
// for each previous import, check if its used status remain the same based on
1895
1895
// the next descriptor's template
1896
1896
for ( const key in prevImports ) {
1897
1897
// if an import was previous unused, but now is used, we need to force
1898
1898
// reload so that the script now includes that import.
1899
- if ( ! prevImports [ key ] . isUsedInTemplate && isImportUsed ( key , next ) ) {
1899
+ if ( ! prevImports [ key ] . isUsedInTemplate && isImportUsed ( key , next , isTS ) ) {
1900
1900
return true
1901
1901
}
1902
1902
}
0 commit comments