Sfoglia il codice sorgente

wip: fix compileScript script/script-setup co-usage

Evan You 5 anni fa
parent
commit
08879cc3b5

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

@@ -1,5 +1,38 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
+exports[`SFC compile <script setup> <script> and <script setup> co-usage script first 1`] = `
+"import { x } from './x'
+      
+      export const n = 1
+      
+export default {
+  expose: [],
+  setup(__props) {
+
+      x()
+      
+return { x }
+}
+
+}"
+`;
+
+exports[`SFC compile <script setup> <script> and <script setup> co-usage script setup first 1`] = `
+"import { x } from './x'
+      
+export default {
+  expose: [],
+  setup(__props) {
+
+      x()
+      
+return { x }
+}
+
+}
+      export const n = 1"
+`;
+
 exports[`SFC compile <script setup> defineOptions() 1`] = `
 "export default {
   expose: [],

+ 28 - 0
packages/compiler-sfc/__tests__/compileScript.spec.ts

@@ -53,6 +53,34 @@ const bar = 1
   emit: ['a', 'b'],`)
   })
 
+  describe('<script> and <script setup> co-usage', () => {
+    test('script first', () => {
+      const { content } = compile(`
+      <script>
+      export const n = 1
+      </script>
+      <script setup>
+      import { x } from './x'
+      x()
+      </script>
+      `)
+      assertCode(content)
+    })
+
+    test('script setup first', () => {
+      const { content } = compile(`
+      <script setup>
+      import { x } from './x'
+      x()
+      </script>
+      <script>
+      export const n = 1
+      </script>
+      `)
+      assertCode(content)
+    })
+  })
+
   describe('imports', () => {
     test('should hoist and expose imports', () => {
       assertCode(

+ 2 - 2
packages/compiler-sfc/src/compileScript.ts

@@ -921,7 +921,7 @@ export function compileScript(
         hasAwait ? `async ` : ``
       }setup(${args}) {\n`
     )
-    s.append(`})`)
+    s.appendRight(endOffset, `})`)
   } else {
     if (defaultExport) {
       // can't rely on spread operator in non ts mode
@@ -939,7 +939,7 @@ export function compileScript(
         `\nexport default {${runtimeOptions}\n  ` +
           `${hasAwait ? `async ` : ``}setup(${args}) {\n`
       )
-      s.append(`}`)
+      s.appendRight(endOffset, `}`)
     }
   }