fix(compiler-sfc): register exported bindings in normal script when using script setup (#4601)

fix #4600
This commit is contained in:
edison 2021-09-17 05:22:29 +08:00 committed by GitHub
parent cab9541ffd
commit 8055445b68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 5 deletions

View File

@ -11,7 +11,7 @@ export default {
x() x()
return { x } return { n, x }
} }
}" }"
@ -26,7 +26,7 @@ export default {
x() x()
return { x } return { n, x }
} }
} }
@ -66,7 +66,7 @@ function setup(__props, { expose }) {
x() x()
return { x } return { n, x }
} }
@ -87,7 +87,7 @@ function setup(__props, { expose }) {
x() x()
return { x } return { n, x }
} }

View File

@ -1295,6 +1295,20 @@ describe('SFC analyze <script> bindings', () => {
expect(bindings!.__isScriptSetup).toBe(false) expect(bindings!.__isScriptSetup).toBe(false)
}) })
it('recognizes exported vars', () => {
const { bindings } = compile(`
<script>
export const foo = 2
</script>
<script setup>
console.log(foo)
</script>
`)
expect(bindings).toStrictEqual({
foo: BindingTypes.SETUP_CONST
})
})
it('recognizes async setup return', () => { it('recognizes async setup return', () => {
const { bindings } = compile(` const { bindings } = compile(`
<script> <script>

View File

@ -702,7 +702,7 @@ export function compileScript(
const start = node.start! + scriptStartOffset! const start = node.start! + scriptStartOffset!
const end = node.declaration.start! + scriptStartOffset! const end = node.declaration.start! + scriptStartOffset!
s.overwrite(start, end, `const ${defaultTempVar} = `) s.overwrite(start, end, `const ${defaultTempVar} = `)
} else if (node.type === 'ExportNamedDeclaration' && node.specifiers) { } else if (node.type === 'ExportNamedDeclaration') {
const defaultSpecifier = node.specifiers.find( const defaultSpecifier = node.specifiers.find(
s => s.exported.type === 'Identifier' && s.exported.name === 'default' s => s.exported.type === 'Identifier' && s.exported.name === 'default'
) as ExportSpecifier ) as ExportSpecifier
@ -735,6 +735,9 @@ export function compileScript(
) )
} }
} }
if (node.declaration) {
walkDeclaration(node.declaration, setupBindings, userImportAlias)
}
} else if ( } else if (
(node.type === 'VariableDeclaration' || (node.type === 'VariableDeclaration' ||
node.type === 'FunctionDeclaration' || node.type === 'FunctionDeclaration' ||