Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(compiler-sfc): register exported bindings in normal script when u…
…sing script setup (#4601)

fix #4600
  • Loading branch information
edison1105 committed Sep 16, 2021
1 parent cab9541 commit 8055445
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Expand Up @@ -11,7 +11,7 @@ export default {
x()
return { x }
return { n, x }
}
}"
Expand All @@ -26,7 +26,7 @@ export default {
x()
return { x }
return { n, x }
}
}
Expand Down Expand Up @@ -66,7 +66,7 @@ function setup(__props, { expose }) {
x()
return { x }
return { n, x }
}
Expand All @@ -87,7 +87,7 @@ function setup(__props, { expose }) {
x()
return { x }
return { n, x }
}
Expand Down
14 changes: 14 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Expand Up @@ -1295,6 +1295,20 @@ describe('SFC analyze <script> bindings', () => {
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', () => {
const { bindings } = compile(`
<script>
Expand Down
5 changes: 4 additions & 1 deletion packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -702,7 +702,7 @@ export function compileScript(
const start = node.start! + scriptStartOffset!
const end = node.declaration.start! + scriptStartOffset!
s.overwrite(start, end, `const ${defaultTempVar} = `)
} else if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
} else if (node.type === 'ExportNamedDeclaration') {
const defaultSpecifier = node.specifiers.find(
s => s.exported.type === 'Identifier' && s.exported.name === 'default'
) as ExportSpecifier
Expand Down Expand Up @@ -735,6 +735,9 @@ export function compileScript(
)
}
}
if (node.declaration) {
walkDeclaration(node.declaration, setupBindings, userImportAlias)
}
} else if (
(node.type === 'VariableDeclaration' ||
node.type === 'FunctionDeclaration' ||
Expand Down

0 comments on commit 8055445

Please sign in to comment.