@@ -129,6 +129,7 @@ export interface SFCScriptCompileOptions {
129
129
export interface ImportBinding {
130
130
isType : boolean
131
131
imported : string
132
+ local : string
132
133
source : string
133
134
isFromSetup : boolean
134
135
isUsedInTemplate : boolean
@@ -272,7 +273,6 @@ export function compileScript(
272
273
const bindingMetadata : BindingMetadata = { }
273
274
const helperImports : Set < string > = new Set ( )
274
275
const userImports : Record < string , ImportBinding > = Object . create ( null )
275
- const userImportAlias : Record < string , string > = Object . create ( null )
276
276
const scriptBindings : Record < string , BindingTypes > = Object . create ( null )
277
277
const setupBindings : Record < string , BindingTypes > = Object . create ( null )
278
278
@@ -362,10 +362,6 @@ export function compileScript(
362
362
isFromSetup : boolean ,
363
363
needTemplateUsageCheck : boolean
364
364
) {
365
- if ( source === 'vue' && imported ) {
366
- userImportAlias [ imported ] = local
367
- }
368
-
369
365
// template usage check is only needed in non-inline mode, so we can skip
370
366
// the work if inlineTemplate is true.
371
367
let isUsedInTemplate = needTemplateUsageCheck
@@ -382,6 +378,7 @@ export function compileScript(
382
378
userImports [ local ] = {
383
379
isType,
384
380
imported : imported || 'default' ,
381
+ local,
385
382
source,
386
383
isFromSetup,
387
384
isUsedInTemplate
@@ -990,7 +987,7 @@ export function compileScript(
990
987
}
991
988
}
992
989
if ( node . declaration ) {
993
- walkDeclaration ( node . declaration , scriptBindings , userImportAlias )
990
+ walkDeclaration ( node . declaration , scriptBindings , userImports )
994
991
}
995
992
} else if (
996
993
( node . type === 'VariableDeclaration' ||
@@ -999,7 +996,7 @@ export function compileScript(
999
996
node . type === 'TSEnumDeclaration' ) &&
1000
997
! node . declare
1001
998
) {
1002
- walkDeclaration ( node , scriptBindings , userImportAlias )
999
+ walkDeclaration ( node , scriptBindings , userImports )
1003
1000
}
1004
1001
}
1005
1002
@@ -1199,7 +1196,7 @@ export function compileScript(
1199
1196
node . type === 'ClassDeclaration' ) &&
1200
1197
! node . declare
1201
1198
) {
1202
- walkDeclaration ( node , setupBindings , userImportAlias )
1199
+ walkDeclaration ( node , setupBindings , userImports )
1203
1200
}
1204
1201
1205
1202
// walk statements & named exports / variable declarations for top level
@@ -1654,8 +1651,17 @@ function registerBinding(
1654
1651
function walkDeclaration (
1655
1652
node : Declaration ,
1656
1653
bindings : Record < string , BindingTypes > ,
1657
- userImportAlias : Record < string , string >
1654
+ userImports : Record < string , ImportBinding >
1658
1655
) {
1656
+ function getUserBinding ( name : string ) {
1657
+ const binding = Object . values ( userImports ) . find (
1658
+ binding => binding . source === 'vue' && binding . imported === name
1659
+ )
1660
+ if ( binding ) return binding . local
1661
+ else if ( ! userImports [ name ] ) return name
1662
+ return undefined
1663
+ }
1664
+
1659
1665
if ( node . type === 'VariableDeclaration' ) {
1660
1666
const isConst = node . kind === 'const'
1661
1667
// export const foo = ...
@@ -1669,7 +1675,7 @@ function walkDeclaration(
1669
1675
)
1670
1676
if ( id . type === 'Identifier' ) {
1671
1677
let bindingType
1672
- const userReactiveBinding = userImportAlias [ 'reactive' ] || 'reactive'
1678
+ const userReactiveBinding = getUserBinding ( 'reactive' )
1673
1679
if ( isCallOf ( init , userReactiveBinding ) ) {
1674
1680
// treat reactive() calls as let since it's meant to be mutable
1675
1681
bindingType = isConst
@@ -1685,7 +1691,7 @@ function walkDeclaration(
1685
1691
? BindingTypes . SETUP_REACTIVE_CONST
1686
1692
: BindingTypes . SETUP_CONST
1687
1693
} else if ( isConst ) {
1688
- if ( isCallOf ( init , userImportAlias [ 'ref' ] || 'ref' ) ) {
1694
+ if ( isCallOf ( init , getUserBinding ( 'ref' ) ) ) {
1689
1695
bindingType = BindingTypes . SETUP_REF
1690
1696
} else {
1691
1697
bindingType = BindingTypes . SETUP_MAYBE_REF
@@ -1982,10 +1988,11 @@ function genRuntimeEmits(emits: Set<string>) {
1982
1988
1983
1989
function isCallOf (
1984
1990
node : Node | null | undefined ,
1985
- test : string | ( ( id : string ) => boolean )
1991
+ test : string | ( ( id : string ) => boolean ) | null | undefined
1986
1992
) : node is CallExpression {
1987
1993
return ! ! (
1988
1994
node &&
1995
+ test &&
1989
1996
node . type === 'CallExpression' &&
1990
1997
node . callee . type === 'Identifier' &&
1991
1998
( typeof test === 'string'
@@ -1994,7 +2001,7 @@ function isCallOf(
1994
2001
)
1995
2002
}
1996
2003
1997
- function canNeverBeRef ( node : Node , userReactiveImport : string ) : boolean {
2004
+ function canNeverBeRef ( node : Node , userReactiveImport ? : string ) : boolean {
1998
2005
if ( isCallOf ( node , userReactiveImport ) ) {
1999
2006
return true
2000
2007
}
0 commit comments