Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sfc): recognizes exported vars in normal script #4601

Merged
merged 2 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,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
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,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 @@ -704,6 +704,9 @@ export function compileScript(
)
}
}
if (node.declaration) {
walkDeclaration(node.declaration, setupBindings, userImportAlias)
}
} else if (
(node.type === 'VariableDeclaration' ||
node.type === 'FunctionDeclaration' ||
Expand Down