fix(compiler-sfc): <script> after <script setup> the script content not end with \\n

This commit is contained in:
liulinboyi 2022-04-24 20:39:18 +08:00 committed by Evan You
parent 242914d938
commit 3b7b107120
3 changed files with 166 additions and 136 deletions

View File

@ -43,6 +43,21 @@ return { a }
})" })"
`; `;
exports[`SFC compile <script setup> <script> after <script setup> the script content not end with \`\\n\` 1`] = `
"const n = 1
import { x } from './x'
export default {
setup(__props, { expose }) {
expose();
return { n, x }
}
}"
`;
exports[`SFC compile <script setup> <script> and <script setup> co-usage script first 1`] = ` exports[`SFC compile <script setup> <script> and <script setup> co-usage script first 1`] = `
"import { x } from './x' "import { x } from './x'

View File

@ -168,6 +168,16 @@ defineExpose({ foo: 123 })
expect(content).toMatch(/\bexpose\(\{ foo: 123 \}\)/) expect(content).toMatch(/\bexpose\(\{ foo: 123 \}\)/)
}) })
test('<script> after <script setup> the script content not end with `\\n`',() => {
const { content } = compile(`
<script setup>
import { x } from './x'
</script>
<script>const n = 1</script>
`)
assertCode(content)
})
describe('<script> and <script setup> co-usage', () => { describe('<script> and <script setup> co-usage', () => {
test('script first', () => { test('script first', () => {
const { content } = compile(` const { content } = compile(`

View File

@ -943,6 +943,11 @@ export function compileScript(
// declared before being used in the actual component definition // declared before being used in the actual component definition
if (scriptStartOffset! > startOffset) { if (scriptStartOffset! > startOffset) {
s.move(scriptStartOffset!, scriptEndOffset!, 0) s.move(scriptStartOffset!, scriptEndOffset!, 0)
const content = s.slice(scriptStartOffset!, scriptEndOffset!)
// when the script content not end with `\n` we add `\n` for the script content
if(!/(\n *$)/.test(content)) {
s.overwrite(scriptStartOffset!,scriptEndOffset!,`${content}\n`)
}
} }
} }