Skip to content

Commit

Permalink
fix(compile-script):two script blocks complier err
Browse files Browse the repository at this point in the history
  • Loading branch information
facexl committed Apr 24, 2022
1 parent 4a3237a commit 444860d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
21 changes: 20 additions & 1 deletion packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,26 @@ defineExpose({ foo: 123 })
// should replace callee
expect(content).toMatch(/\bexpose\(\{ foo: 123 \}\)/)
})

test('between two blocks should have "\n"', () => {
const { content } = compile(`
<script setup>
import foo from './foo'
</script>
// <script>
// const bar = 1;
// </script>
`)
expect(content).not.toMatch('// import foo')
})
test('between two blocks should have "\n"', () => {
const { content } = compile(`
<script setup>
import foo from './foo'
</script>
<script>const bar = 1</script>
`)
expect(content).not.toMatch(`const bar = 1import foo from './foo'`)
})
describe('<script> and <script setup> co-usage', () => {
test('script first', () => {
const { content } = compile(`
Expand Down
6 changes: 4 additions & 2 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,10 @@ export function compileScript(
// <script> after <script setup>
// we need to move the block up so that `const __default__` is
// declared before being used in the actual component definition
// between two blocks should have '\n'
if (scriptStartOffset! > startOffset) {
s.move(scriptStartOffset!, scriptEndOffset!, 0)
s.overwrite(scriptStartOffset!,scriptEndOffset!,s.slice(scriptStartOffset!, scriptEndOffset!)+'\n')
s.move(scriptStartOffset!, scriptEndOffset!, 0)
}
}

Expand Down Expand Up @@ -1445,7 +1447,7 @@ export function compileScript(
)
}

s.trim()
s.trim('\s[^\n]')

return {
...scriptSetup,
Expand Down

0 comments on commit 444860d

Please sign in to comment.