diff --git a/packages/compiler-core/src/babelUtils.ts b/packages/compiler-core/src/babelUtils.ts
index 5ddbc1ea412..52175ef48b1 100644
--- a/packages/compiler-core/src/babelUtils.ts
+++ b/packages/compiler-core/src/babelUtils.ts
@@ -6,7 +6,10 @@ import type {
Function,
ObjectProperty,
BlockStatement,
- Program
+ Program,
+ ImportDefaultSpecifier,
+ ImportNamespaceSpecifier,
+ ImportSpecifier
} from '@babel/types'
import { walk } from 'estree-walker'
@@ -243,6 +246,17 @@ export const isStaticProperty = (node: Node): node is ObjectProperty =>
export const isStaticPropertyKey = (node: Node, parent: Node) =>
isStaticProperty(parent) && parent.key === node
+export function getImportedName(
+ specifier: ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier
+) {
+ if (specifier.type === 'ImportSpecifier')
+ return specifier.imported.type === 'Identifier'
+ ? specifier.imported.name
+ : specifier.imported.value
+ else if (specifier.type === 'ImportNamespaceSpecifier') return '*'
+ return 'default'
+}
+
/**
* Copied from https://github.com/babel/babel/blob/main/packages/babel-types/src/validators/isReferenced.ts
* To avoid runtime dependency on @babel/types (which includes process references)
diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
index 6220f860f16..b7a13e56851 100644
--- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
+++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
@@ -1027,6 +1027,20 @@ return { ref }
}"
`;
+exports[`SFC compile
+
+ `)
+ assertCode(content)
+ expect(bindings).toStrictEqual({
+ foo: BindingTypes.SETUP_MAYBE_REF
+ })
+ })
})
// in dev mode, declared bindings are returned as an object from setup()
diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts
index c9fe242b25f..e2e129924e3 100644
--- a/packages/compiler-sfc/src/compileScript.ts
+++ b/packages/compiler-sfc/src/compileScript.ts
@@ -10,6 +10,7 @@ import {
SimpleExpressionNode,
isFunctionType,
walkIdentifiers,
+ getImportedName,
unwrapTSNode
} from '@vue/compiler-dom'
import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse'
@@ -380,7 +381,7 @@ export function compileScript(
function registerUserImport(
source: string,
local: string,
- imported: string | false,
+ imported: string,
isType: boolean,
isFromSetup: boolean,
needTemplateUsageCheck: boolean
@@ -400,7 +401,7 @@ export function compileScript(
userImports[local] = {
isType,
- imported: imported || 'default',
+ imported,
local,
source,
isFromSetup,
@@ -1002,10 +1003,7 @@ export function compileScript(
if (node.type === 'ImportDeclaration') {
// record imports for dedupe
for (const specifier of node.specifiers) {
- const imported =
- specifier.type === 'ImportSpecifier' &&
- specifier.imported.type === 'Identifier' &&
- specifier.imported.name
+ const imported = getImportedName(specifier)
registerUserImport(
node.source.value,
specifier.local.name,
@@ -1047,13 +1045,7 @@ export function compileScript(
for (let i = 0; i < node.specifiers.length; i++) {
const specifier = node.specifiers[i]
const local = specifier.local.name
- let imported =
- specifier.type === 'ImportSpecifier' &&
- specifier.imported.type === 'Identifier' &&
- specifier.imported.name
- if (specifier.type === 'ImportNamespaceSpecifier') {
- imported = '*'
- }
+ const imported = getImportedName(specifier)
const source = node.source.value
const existing = userImports[local]
if (