Skip to content

Commit 8055445

Browse files
authored
fix(compiler-sfc): register exported bindings in normal script when using script setup (#4601)
fix #4600
1 parent cab9541 commit 8055445

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default {
1111
1212
x()
1313
14-
return { x }
14+
return { n, x }
1515
}
1616
1717
}"
@@ -26,7 +26,7 @@ export default {
2626
2727
x()
2828
29-
return { x }
29+
return { n, x }
3030
}
3131
3232
}
@@ -66,7 +66,7 @@ function setup(__props, { expose }) {
6666
6767
x()
6868
69-
return { x }
69+
return { n, x }
7070
}
7171
7272
@@ -87,7 +87,7 @@ function setup(__props, { expose }) {
8787
8888
x()
8989
90-
return { x }
90+
return { n, x }
9191
}
9292
9393

packages/compiler-sfc/__tests__/compileScript.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,20 @@ describe('SFC analyze <script> bindings', () => {
12951295
expect(bindings!.__isScriptSetup).toBe(false)
12961296
})
12971297

1298+
it('recognizes exported vars', () => {
1299+
const { bindings } = compile(`
1300+
<script>
1301+
export const foo = 2
1302+
</script>
1303+
<script setup>
1304+
console.log(foo)
1305+
</script>
1306+
`)
1307+
expect(bindings).toStrictEqual({
1308+
foo: BindingTypes.SETUP_CONST
1309+
})
1310+
})
1311+
12981312
it('recognizes async setup return', () => {
12991313
const { bindings } = compile(`
13001314
<script>

packages/compiler-sfc/src/compileScript.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ export function compileScript(
702702
const start = node.start! + scriptStartOffset!
703703
const end = node.declaration.start! + scriptStartOffset!
704704
s.overwrite(start, end, `const ${defaultTempVar} = `)
705-
} else if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
705+
} else if (node.type === 'ExportNamedDeclaration') {
706706
const defaultSpecifier = node.specifiers.find(
707707
s => s.exported.type === 'Identifier' && s.exported.name === 'default'
708708
) as ExportSpecifier
@@ -735,6 +735,9 @@ export function compileScript(
735735
)
736736
}
737737
}
738+
if (node.declaration) {
739+
walkDeclaration(node.declaration, setupBindings, userImportAlias)
740+
}
738741
} else if (
739742
(node.type === 'VariableDeclaration' ||
740743
node.type === 'FunctionDeclaration' ||

0 commit comments

Comments
 (0)