fix(compiler-sfc): fix <script> and <script setup> co-usage ordering edge case (#4419)

Fix: #4395
Fix: #4376
This commit is contained in:
klwf
2021-08-24 07:02:54 +08:00
committed by GitHub
parent a46b0a9a96
commit 98263821f8
3 changed files with 60 additions and 9 deletions

View File

@@ -1150,15 +1150,27 @@ export function compileScript(
// wrap setup code with function.
// export the content of <script setup> as a named export, `setup`.
// this allows `import { setup } from '*.vue'` for testing purposes.
s.prependLeft(
startOffset,
`\nexport default ${helper(
`defineComponent`
)}({${def}${runtimeOptions}\n ${
hasAwait ? `async ` : ``
}setup(${args}) {\n${exposeCall}`
)
s.appendRight(endOffset, `})`)
if (defaultExport) {
s.prependLeft(
startOffset,
`\n${hasAwait ? `async ` : ``}function setup(${args}) {\n`
)
s.append(
`\nexport default ${helper(
`defineComponent`
)}({${def}${runtimeOptions}\n setup})`
)
} else {
s.prependLeft(
startOffset,
`\nexport default ${helper(
`defineComponent`
)}({${def}${runtimeOptions}\n ${
hasAwait ? `async ` : ``
}setup(${args}) {\n${exposeCall}`
)
s.appendRight(endOffset, `})`)
}
} else {
if (defaultExport) {
// can't rely on spread operator in non ts mode